X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fio%2Ftoken.cpp;h=07a5e981cd889d01d5920797ee0a4d63dd0ff604;hb=fb0b9b41ccd5b4738dc2ecd5a9db49aa0f291b8f;hp=76a9c2f355e42a54f26f758b3bd76466160fc5aa;hpb=f417749fb09718cde2faad77e8430cf175c68374;p=blank.git diff --git a/src/io/token.cpp b/src/io/token.cpp index 76a9c2f..07a5e98 100644 --- a/src/io/token.cpp +++ b/src/io/token.cpp @@ -1,15 +1,72 @@ +#include "Token.hpp" #include "Tokenizer.hpp" #include "TokenStreamReader.hpp" #include #include +#include +#include #include +#include using namespace std; namespace blank { +ostream &operator <<(ostream &out, Token::Type t) { + switch (t) { + case Token::ANGLE_BRACKET_OPEN: + return out << "ANGLE_BRACKET_OPEN"; + case Token::ANGLE_BRACKET_CLOSE: + return out << "ANGLE_BRACKET_CLOSE"; + case Token::CHEVRON_OPEN: + return out << "CHEVRON_OPEN"; + case Token::CHEVRON_CLOSE: + return out << "CHEVRON_CLOSE"; + case Token::BRACKET_OPEN: + return out << "BRACKET_OPEN"; + case Token::BRACKET_CLOSE: + return out << "BRACKET_CLOSE"; + case Token::PARENTHESIS_OPEN: + return out << "PARENTHESIS_OPEN"; + case Token::PARENTHESIS_CLOSE: + return out << "PARENTHESIS_CLOSE"; + case Token::COLON: + return out << "COLON"; + case Token::SEMICOLON: + return out << "SEMICOLON"; + case Token::COMMA: + return out << "COMMA"; + case Token::EQUALS: + return out << "EQUALS"; + case Token::NUMBER: + return out << "NUMBER"; + case Token::STRING: + return out << "STRING"; + case Token::IDENTIFIER: + return out << "IDENTIFIER"; + case Token::COMMENT: + return out << "COMMENT"; + default: + return out << "UNKNOWN"; + } +} + +ostream &operator <<(ostream &out, const Token &t) { + out << t.type; + switch (t.type) { + case Token::UNKNOWN: + case Token::NUMBER: + case Token::STRING: + case Token::IDENTIFIER: + case Token::COMMENT: + return out << '(' << t.value << ')'; + default: + return out; + } +} + Tokenizer::Tokenizer(istream &in) : in(in) , current() { @@ -173,7 +230,7 @@ void Tokenizer::ReadIdentifier() { istream::char_type c; while (in.get(c)) { - if (isalnum(c) || c == '_') { + if (isalnum(c) || c == '_' || c == '.') { current.value += c; } else { in.putback(c); @@ -194,27 +251,35 @@ bool TokenStreamReader::HasMore() { if (cached) { return true; } - while (in.HasMore()) { - if (in.Next().type != Token::COMMENT) { - cached = true; - return true; - } - } - return false; + SkipComments(); + return cached; } const Token &TokenStreamReader::Next() { + SkipComments(); + cached = false; + return in.Current(); +} + +void TokenStreamReader::SkipComments() { if (cached) { - cached = false; - return in.Current(); - } else { - return in.Next(); + if (in.Current().type == Token::COMMENT) { + cached = false; + } else { + return; + } + } + while (in.HasMore()) { + if (in.Next().type != Token::COMMENT) { + cached = true; + return; + } } } const Token &TokenStreamReader::Peek() { if (!cached) { - in.Next(); + Next(); cached = true; } return in.Current(); @@ -223,7 +288,9 @@ const Token &TokenStreamReader::Peek() { void TokenStreamReader::Assert(Token::Type t) { if (GetType() != t) { - throw runtime_error("unexpected token in input stream"); + stringstream s; + s << "unexpected token in input stream: expected " << t << ", but got " << in.Current(); + throw runtime_error(s.str()); } } @@ -330,6 +397,18 @@ void TokenStreamReader::ReadVec(glm::ivec4 &v) { Skip(Token::BRACKET_CLOSE); } +void TokenStreamReader::ReadQuat(glm::quat &q) { + Skip(Token::BRACKET_OPEN); + ReadNumber(q.w); + Skip(Token::COMMA); + ReadNumber(q.x); + Skip(Token::COMMA); + ReadNumber(q.y); + Skip(Token::COMMA); + ReadNumber(q.z); + Skip(Token::BRACKET_CLOSE); +} + bool TokenStreamReader::GetBool() { Next(); @@ -343,10 +422,14 @@ bool TokenStreamReader::GetBool() { } else if (GetValue() == "false" || GetValue() == "no" || GetValue() == "off") { return false; } else { - throw runtime_error("unexpected value in input stream"); + throw runtime_error("unexpected value in input stream: cannot cast " + GetValue() + " to bool"); } default: - throw runtime_error("unexpected token in input stream"); + { + stringstream s; + s << "unexpected token in input stream: cannot cast " << in.Current() << " to bool"; + throw runtime_error(s.str()); + } } }