X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Floader%2FParser.cpp;h=aa770ff12a55fa1137a1a76511a3246a9d6a73e9;hb=622b633b3c284b3b44c8cbb71528d20b97fe8d59;hp=d6abcc39561e4b70f8fce0ced7ce880580701ad0;hpb=322be5954c0f8df6a9e3aaeb2456783a814d52c3;p=l2e.git diff --git a/src/loader/Parser.cpp b/src/loader/Parser.cpp index d6abcc3..aa770ff 100644 --- a/src/loader/Parser.cpp +++ b/src/loader/Parser.cpp @@ -7,6 +7,8 @@ #include "Parser.h" +#include "utility.h" + #include #include @@ -17,9 +19,10 @@ using std::vector; namespace loader { -Parser::Parser(const char *file, ParsedSource &product) +Parser::Parser(const string &file, ParsedSource &product) : file(file) -, in(file) +, dirname(Dirname(file)) +, in(this->file.c_str()) , tok(in) , product(product) { if (!in) { @@ -74,7 +77,7 @@ void Parser::ParseExportDirective() { void Parser::ParseIncludeDirective() { Tokenizer::Token t(GetToken()); AssertTokenType(t.type, Tokenizer::Token::STRING); - Parser sub(t.str.c_str(), product); // TODO: resolve path name + Parser sub(CatPath(dirname, t.str), product); sub.Parse(); } @@ -82,28 +85,30 @@ Declaration *Parser::ProbeDefinition() { string typeName(ParseTypeName()); string identifier(ParseIdentifier()); - Tokenizer::Token t(GetToken()); - tok.Putback(t); - if (BeginOfPropertyList(t)) { - auto_ptr propertyList(ParsePropertyList()); - auto_ptr dfn(new Definition(typeName, identifier)); - dfn->SetValue(propertyList.release()); - product.AddDefinition(dfn.get()); - return dfn.release(); - } else if (BeginningOfLiteral(t)) { - auto_ptr literal(ParseLiteral()); - auto_ptr dfn(new Definition(typeName, identifier)); - dfn->SetValue(literal.release()); - product.AddDefinition(dfn.get()); - return dfn.release(); - } else { - return new Declaration(typeName, identifier); + if (tok.HasMore()) { + Tokenizer::Token t(GetToken()); + tok.Putback(t); + if (BeginOfPropertyList(t)) { + auto_ptr propertyList(ParsePropertyList()); + auto_ptr dfn(new Definition(typeName, identifier)); + dfn->SetValue(propertyList.release()); + product.AddDefinition(dfn.get()); + return dfn.release(); + } else if (BeginningOfPrimitiveLiteral(t)) { + auto_ptr literal(ParseLiteral()); + auto_ptr dfn(new Definition(typeName, identifier)); + dfn->SetValue(literal.release()); + product.AddDefinition(dfn.get()); + return dfn.release(); + } } + return new Declaration(typeName, identifier); } bool Parser::BeginningOfLiteral(const Tokenizer::Token &t) const { switch (t.type) { case Tokenizer::Token::CHEVRON_OPEN: + case Tokenizer::Token::COLON: case Tokenizer::Token::BRACKET_OPEN: case Tokenizer::Token::PARENTHESIS_OPEN: case Tokenizer::Token::NUMBER: @@ -117,6 +122,22 @@ bool Parser::BeginningOfLiteral(const Tokenizer::Token &t) const { } } +bool Parser::BeginningOfPrimitiveLiteral(const Tokenizer::Token &t) const { + switch (t.type) { + case Tokenizer::Token::CHEVRON_OPEN: + case Tokenizer::Token::COLON: + case Tokenizer::Token::BRACKET_OPEN: + case Tokenizer::Token::PARENTHESIS_OPEN: + case Tokenizer::Token::NUMBER: + case Tokenizer::Token::STRING: + case Tokenizer::Token::KEYWORD_FALSE: + case Tokenizer::Token::KEYWORD_TRUE: + return true; + default: + return false; + } +} + bool Parser::BeginOfPropertyList(const Tokenizer::Token &t) const { return t.type == Tokenizer::Token::ANGLE_BRACKET_OPEN; } @@ -202,6 +223,10 @@ Literal *Parser::ParseLiteral() { case Tokenizer::Token::CHEVRON_OPEN: tok.Putback(t); return ParseVector(); + case Tokenizer::Token::COLON: + t = GetToken(); + AssertTokenType(t.type, Tokenizer::Token::STRING); + return new Literal(dirname, t.str); case Tokenizer::Token::BRACKET_OPEN: tok.Putback(t); return ParseArray(); @@ -279,7 +304,7 @@ Literal *Parser::ParseColor() { AssertTokenType(blue.type, Tokenizer::Token::NUMBER, "error parsing blue component of color"); t = GetToken(); - if (t.type == Tokenizer::Token::BRACKET_CLOSE) { + if (t.type == Tokenizer::Token::PARENTHESIS_CLOSE) { return new Literal(red.number, green.number, blue.number); } else if (t.type != Tokenizer::Token::COMMA) { Tokenizer::Token alpha(GetToken());