X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Floader%2FTokenizer.cpp;h=995a7e2f20469d3ddb1f76231c0cae64c9523058;hb=24ca4a4d63d0a706c6c7676192588dd21893ca32;hp=5f74d74e51cdc3158e685d6f86a2daa395bccf6d;hpb=93971b3ba23e6b51319b07d7d82dcbcf976f8b9a;p=l2e.git diff --git a/src/loader/Tokenizer.cpp b/src/loader/Tokenizer.cpp index 5f74d74..995a7e2 100644 --- a/src/loader/Tokenizer.cpp +++ b/src/loader/Tokenizer.cpp @@ -8,23 +8,45 @@ #include "Tokenizer.h" #include -#include namespace loader { bool Tokenizer::HasMore() { + ScanSpace(); return in; } +void Tokenizer::ScanSpace() { + std::istream::char_type c; + in.get(c); + while (in && std::isspace(c)) { + if (c == '\n') { + ++line; + } + in.get(c); + } + if (in) { + in.putback(c); + } +} + void Tokenizer::Putback(const Token &t) { if (isPutback) { - throw std::runtime_error("Tokenizer: double putback not supported"); + throw LexerError(line, "Tokenizer: double putback not supported"); } else { putback = t; isPutback = true; } } +const Tokenizer::Token &Tokenizer::Peek() { + if (!isPutback) { + putback = GetNext(); + isPutback = true; + } + return putback; +} + Tokenizer::Token Tokenizer::GetNext() { if (isPutback) { isPutback = false; @@ -35,9 +57,9 @@ Tokenizer::Token Tokenizer::GetNext() { } Tokenizer::Token Tokenizer::ReadToken() { + ScanSpace(); std::istream::char_type c; in.get(c); - while (std::isspace(c)) in.get(c); switch (c) { case Token::ANGLE_BRACKET_OPEN: case Token::ANGLE_BRACKET_CLOSE: @@ -47,7 +69,9 @@ Tokenizer::Token Tokenizer::ReadToken() { case Token::COMMA: case Token::BRACKET_OPEN: case Token::BRACKET_CLOSE: - return (Token::Type) c; + case Token::PARENTHESIS_OPEN: + case Token::PARENTHESIS_CLOSE: + return Token ((Token::Type) c); case '+': case '-': case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': @@ -65,7 +89,7 @@ Tokenizer::Token Tokenizer::ReadToken() { } else if (std::islower(c)) { CheckKeyword(t); } else { - throw std::runtime_error(std::string("Tokenizer: cannot parse token: ") + c); + throw LexerError(line, std::string("Tokenizer: cannot parse token: ") + c); } return t; } @@ -105,7 +129,7 @@ Tokenizer::Token Tokenizer::ReadString() { std::istream::char_type c; in.get(c); if (c != '"') { - throw std::runtime_error("Tokenizer: strings must begin with '\"'"); + throw LexerError(line, "Tokenizer: strings must begin with '\"'"); } while (in.get(c)) {