From: Daniel Karbach Date: Sun, 14 Oct 2012 13:31:29 +0000 (+0200) Subject: parse scripts X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=3a30342daecdb9ee050ab20fcb50819a599d6343;p=l2e.git parse scripts --- diff --git a/src/loader/Interpreter.cpp b/src/loader/Interpreter.cpp index e8d4ec9..8b9b56a 100644 --- a/src/loader/Interpreter.cpp +++ b/src/loader/Interpreter.cpp @@ -142,6 +142,8 @@ void Interpreter::ReadLiteral(int typeId, int id, char *object, const Literal &l std::memcpy(object, literal.GetString().c_str(), literal.GetString().size()); object[literal.GetString().size()] = '\0'; break; + case Literal::SCRIPT: + throw Error("script compiler not implemented"); case Literal::STRING: std::memcpy(object, literal.GetString().c_str(), literal.GetString().size()); object[literal.GetString().size()] = '\0'; @@ -151,7 +153,6 @@ void Interpreter::ReadLiteral(int typeId, int id, char *object, const Literal &l break; case Literal::OBJECT: throw Error("illogical branch: read literal object as non-object literal"); - break; } } @@ -213,6 +214,8 @@ void *Interpreter::GetObject(int typeId, const Value &v) { values[typeId].push_back(str); } break; + case Literal::SCRIPT: + throw Error("script compiler not implemented"); case Literal::STRING: { typeId = TypeDescription::GetTypeId("String"); diff --git a/src/loader/ParsedSource.cpp b/src/loader/ParsedSource.cpp index e1a1d0a..6b2fc80 100644 --- a/src/loader/ParsedSource.cpp +++ b/src/loader/ParsedSource.cpp @@ -279,6 +279,16 @@ Literal::Literal(const string &typeName, PropertyList *properties) } +Literal::Literal(const vector &s) +: props(0) +, script(s) +, i1(0), i2(0) +, i3(0), i4(0) +, b(false) +, type(SCRIPT) { + +} + Literal::~Literal() { switch (type) { case ARRAY_VALUES: @@ -294,6 +304,11 @@ Literal::~Literal() { case OBJECT: delete props; break; + case SCRIPT: + for (vector::const_iterator i(script.begin()), end(script.end()); i != end; ++i) { + delete *i; + } + break; default: break; } @@ -400,6 +415,14 @@ const PropertyList *Literal::GetProperties() const { } } +const vector &Literal::GetScript() const { + if (type == SCRIPT) { + return script; + } else { + throw runtime_error("tried to access script of non-script literal"); + } +} + Value::~Value() { if (isLiteral) { @@ -423,6 +446,61 @@ const std::string &Value::GetIdentifier() const { } } + +ScriptToken::ScriptToken(const string &s, Type t) +: literal(0) +, str(s) +, type(t) { + if (type == LITERAL) { + throw runtime_error("cannot create script literal without literal"); + } +} + +ScriptToken::ScriptToken(Literal *l) +: literal(l) +, str() +, type(LITERAL) { + if (!literal) { + throw runtime_error("cannot create script literal without literal"); + } +} + +ScriptToken::~ScriptToken() { + delete literal; +} + +const string &ScriptToken::RegisterName() const { + if (type == REGISTER) { + return str; + } else { + throw runtime_error("access to register name of non-register script token"); + } +} + +const string &ScriptToken::CommandName() const { + if (type == COMMAND) { + return str; + } else { + throw runtime_error("access to command name of non-command script token"); + } +} + +const string &ScriptToken::GetIdentifier() const { + if (type == IDENTIFIER) { + return str; + } else { + throw runtime_error("access to identifier of non-identifier script token"); + } +} + +const Literal *ScriptToken::GetLiteral() const { + if (type == LITERAL) { + return literal; + } else { + throw runtime_error("access to literal value of non-literal script token"); + } +} + } @@ -477,6 +555,9 @@ ostream &operator <<(ostream &out, const loader::Literal &l) { case loader::Literal::OBJECT: out << "object of type " << l.GetTypeName(); break; + case loader::Literal::SCRIPT: + out << "script"; + break; } return out; } diff --git a/src/loader/ParsedSource.h b/src/loader/ParsedSource.h index 64baaaa..9ba5297 100644 --- a/src/loader/ParsedSource.h +++ b/src/loader/ParsedSource.h @@ -18,6 +18,38 @@ namespace loader { +class ScriptToken { + +public: + enum Type { + COMMAND, + REGISTER, + IDENTIFIER, + LITERAL, + }; + + ScriptToken(const std::string &, Type); + explicit ScriptToken(Literal *); + ~ScriptToken(); +private: + ScriptToken(const ScriptToken &); + ScriptToken &operator =(const ScriptToken &); + +public: + Type GetType() const { return type; } + const std::string &RegisterName() const; + const std::string &CommandName() const; + const std::string &GetIdentifier() const; + const Literal *GetLiteral() const; + +private: + Literal *literal; + std::string str; + Type type; + +}; + + class Literal { public: @@ -30,7 +62,8 @@ public: PATH, STRING, VECTOR, - OBJECT + OBJECT, + SCRIPT, }; public: @@ -43,6 +76,7 @@ public: Literal(const std::string &); Literal(int x, int y); Literal(const std::string &typeName, PropertyList *properties); + explicit Literal(const std::vector &); ~Literal(); private: Literal(const Literal &); @@ -67,12 +101,14 @@ public: int GetY() const; const std::string &GetTypeName() const; const PropertyList *GetProperties() const; + const std::vector &GetScript() const; private: PropertyList *props; std::string typeName, str; std::vector values; std::vector propertyLists; + std::vector script; int i1, i2, i3, i4; bool b; Type type; diff --git a/src/loader/Parser.cpp b/src/loader/Parser.cpp index 183accb..52585c4 100644 --- a/src/loader/Parser.cpp +++ b/src/loader/Parser.cpp @@ -112,6 +112,7 @@ bool Parser::BeginningOfLiteral(const Tokenizer::Token &t) const { case Tokenizer::Token::BRACKET_OPEN: case Tokenizer::Token::PARENTHESIS_OPEN: case Tokenizer::Token::NUMBER: + case Tokenizer::Token::SCRIPT_BEGIN: case Tokenizer::Token::STRING: case Tokenizer::Token::KEYWORD_FALSE: case Tokenizer::Token::KEYWORD_TRUE: @@ -142,6 +143,10 @@ bool Parser::BeginOfPropertyList(const Tokenizer::Token &t) const { return t.type == Tokenizer::Token::ANGLE_BRACKET_OPEN; } +bool Parser::BeginningOfScriptLiteral(const Tokenizer::Token &t) const { + return t.type == Tokenizer::Token::SCRIPT_BEGIN; +} + Definition *Parser::ParseDefinition() { string typeName(ParseTypeName()); string identifier(ParseIdentifier()); @@ -218,7 +223,10 @@ Literal *Parser::ParseLiteral() { if (t.type == Tokenizer::Token::TYPE_NAME) { PropertyList *props(ParsePropertyList()); return new Literal(t.str, props); - } else if (BeginningOfLiteral(t)) { + } else if (BeginningOfScriptLiteral(t)) { + tok.Putback(t); + return ParseScript(); + } else if (BeginningOfPrimitiveLiteral(t)) { switch (t.type) { case Tokenizer::Token::CHEVRON_OPEN: tok.Putback(t); @@ -340,6 +348,51 @@ Literal *Parser::ParseVector() { return new Literal(x.number, y.number); } +Literal *Parser::ParseScript() { + std::string msg("error parsing script"); + Tokenizer::Token t(GetToken()); + AssertTokenType(t.type, Tokenizer::Token::SCRIPT_BEGIN, msg); + + vector script; + try { + while (t.type != Tokenizer::Token::SCRIPT_END) { + if (BeginningOfPrimitiveLiteral(t)) { + tok.Putback(t); + script.push_back(new ScriptToken(ParseLiteral())); + } else { + switch (t.type) { + case Tokenizer::Token::COMMAND: { + Tokenizer::Token t2(GetToken()); + AssertTokenType(t.type, Tokenizer::Token::IDENTIFIER, msg); + script.push_back(new ScriptToken(t2.str, ScriptToken::COMMAND)); + break; + } + case Tokenizer::Token::IDENTIFIER: { + script.push_back(new ScriptToken(t.str, ScriptToken::IDENTIFIER)); + break; + } + case Tokenizer::Token::REGISTER: { + Tokenizer::Token t2(GetToken()); + AssertTokenType(t.type, Tokenizer::Token::IDENTIFIER, msg); + script.push_back(new ScriptToken(t2.str, ScriptToken::REGISTER)); + break; + } + default: + throw Error(file, tok.Line(), string("unexpected token in script: ") + TokenTypeToString(t.type)); + } + } + t = GetToken(); + } + } catch (...) { + for (vector::const_iterator i(script.begin()), end(script.end()); i != end; ++i) { + delete *i; + } + throw; + } + return new Literal(script); +} + + void Parser::AssertTokenType(Tokenizer::Token::Type actual, Tokenizer::Token::Type expected) { if (expected != actual) { throw Error(file, tok.Line(), string("unexpected token ") + TokenTypeToString(actual) + ", expected " + TokenTypeToString(expected)); diff --git a/src/loader/Parser.h b/src/loader/Parser.h index 392e9b7..963c377 100644 --- a/src/loader/Parser.h +++ b/src/loader/Parser.h @@ -62,6 +62,7 @@ private: Literal *ParseArray(); Literal *ParseColor(); Literal *ParseVector(); + Literal *ParseScript(); private: void AssertTokenType(Tokenizer::Token::Type actual, Tokenizer::Token::Type expected); @@ -69,6 +70,7 @@ private: bool BeginningOfLiteral(const Tokenizer::Token &) const; bool BeginningOfPrimitiveLiteral(const Tokenizer::Token &) const; bool BeginOfPropertyList(const Tokenizer::Token &) const; + bool BeginningOfScriptLiteral(const Tokenizer::Token &) const; private: std::string file; diff --git a/src/loader/fwd.h b/src/loader/fwd.h index 2f90849..b8b8458 100644 --- a/src/loader/fwd.h +++ b/src/loader/fwd.h @@ -20,6 +20,7 @@ class PagedAllocator; class ParsedSource; class Parser; class PropertyList; +class ScriptToken; class Tokenizer; class TypeDescription; class Value;