Added a way to parse from a string

This commit is contained in:
Paul Ferrand 2020-03-27 22:53:29 +01:00
parent 51b992617d
commit 847af7b731
4 changed files with 52 additions and 1 deletions

View file

@ -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<StringViewReader>(sfzView));
processTopLevel();
flushCurrentHeader();
if (_listener)
_listener->onParseEnd();
}
void Parser::includeNewFile(const fs::path& path)
{
fs::path fullPath =

View file

@ -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);

View file

@ -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

View file

@ -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"