diff --git a/src/sfizz/parser/Parser.cpp b/src/sfizz/parser/Parser.cpp index 8007e40c..db286465 100644 --- a/src/sfizz/parser/Parser.cpp +++ b/src/sfizz/parser/Parser.cpp @@ -23,13 +23,18 @@ void Parser::addDefinition(absl::string_view id, absl::string_view value) _definitions[id] = std::string(value); } -void Parser::parseFile(const fs::path& path) +void Parser::reset() { _pathsIncluded.clear(); _currentHeader.reset(); _currentOpcodes.clear(); _errorCount = 0; _warningCount = 0; +} + +void Parser::parseFile(const fs::path& path) +{ + reset(); if (_listener) _listener->onParseBegin(); @@ -42,6 +47,21 @@ void Parser::parseFile(const fs::path& path) _listener->onParseEnd(); } +void Parser::parseString(absl::string_view sfzView) +{ + reset(); + + if (_listener) + _listener->onParseBegin(); + + _included.push_back(absl::make_unique(sfzView)); + processTopLevel(); + flushCurrentHeader(); + + if (_listener) + _listener->onParseEnd(); +} + void Parser::includeNewFile(const fs::path& path) { fs::path fullPath = diff --git a/src/sfizz/parser/Parser.h b/src/sfizz/parser/Parser.h index cdcb50cd..17f88709 100644 --- a/src/sfizz/parser/Parser.h +++ b/src/sfizz/parser/Parser.h @@ -29,6 +29,7 @@ public: void addDefinition(absl::string_view id, absl::string_view value); void parseFile(const fs::path& path); + void parseString(absl::string_view sfzView); void setRecursiveIncludeGuardEnabled(bool en) { _recursiveIncludeGuardEnabled = en; } void setMaximumIncludeDepth(size_t depth) { _maxIncludeDepth = depth; } @@ -66,6 +67,7 @@ private: void processDirective(); void processHeader(); void processOpcode(); + void reset(); // errors and warnings void emitError(const SourceRange& range, const std::string& message); diff --git a/src/sfizz/parser/ParserPrivate.cpp b/src/sfizz/parser/ParserPrivate.cpp index 0db8423d..27d1d153 100644 --- a/src/sfizz/parser/ParserPrivate.cpp +++ b/src/sfizz/parser/ParserPrivate.cpp @@ -139,4 +139,18 @@ int FileReader::getNextStreamByte() return _fileStream.get(); } +StringViewReader::StringViewReader(absl::string_view sfzView) + : Reader({}), _sfzView(sfzView) +{ + +} + +int StringViewReader::getNextStreamByte() +{ + if (position < _sfzView.length()) + return _sfzView[position++]; + + return kEof; +} + } // namespace sfz diff --git a/src/sfizz/parser/ParserPrivate.h b/src/sfizz/parser/ParserPrivate.h index a0c2fecd..520f87a6 100644 --- a/src/sfizz/parser/ParserPrivate.h +++ b/src/sfizz/parser/ParserPrivate.h @@ -119,6 +119,21 @@ private: fs::ifstream _fileStream; }; +/** + * @brief String-view-based version of Reader. + */ +class StringViewReader : public Reader { +public: + explicit StringViewReader(absl::string_view sfzView); + +protected: + int getNextStreamByte() override; + +private: + absl::string_view _sfzView; + size_t position { 0 }; +}; + } // namespace sfz #include "ParserPrivate.hpp"