namespace loader {
bool Tokenizer::HasMore() {
+ if (isPutback) return true;
ScanSpace();
- return in;
+ if (!in) return false;
+
+ putback = ReadToken();
+ isPutback = true;
+ if (!skipComments || putback.type != Token::COMMENT) return true;
+
+ while (in && putback.type == Token::COMMENT) {
+ putback = ReadToken();
+ ScanSpace();
+ }
+ return putback.type != Token::COMMENT;
}
void Tokenizer::ScanSpace() {
}
Tokenizer::Token Tokenizer::GetNext() {
+ if (!HasMore()) {
+ throw LexerError(line, "read beyond last token");
+ }
if (isPutback) {
isPutback = false;
return putback;
case '"':
in.putback(c);
return ReadString();
+ case '/':
+ {
+ std::istream::char_type c2;
+ in.get(c2);
+ if (c2 == '/') {
+ return ReadComment();
+ } else if (c2 == '*') {
+ return ReadMultilineComment();
+ } else {
+ throw LexerError(line, std::string("Tokenizer: cannot parse token: ") + c + c2 + ": expected / or *");
+ }
+ }
+ break;
default:
in.putback(c);
{
return t;
}
+Tokenizer::Token Tokenizer::ReadComment() {
+ std::istream::char_type c;
+ while (in.get(c) && c != '\n');
+ ++line;
+ return Token(Token::COMMENT);
+}
+
+Tokenizer::Token Tokenizer::ReadMultilineComment() {
+ std::istream::char_type c;
+ while (in.get(c)) {
+ if (c == '*') {
+ std::istream::char_type c2;
+ if (in.get(c2) && c2 == '/') {
+ break;
+ }
+ } else if (c == '\n') {
+ ++line;
+ }
+ }
+ return Token(Token::COMMENT);
+}
+
bool Tokenizer::CheckKeyword(Token &t) {
if (t.str == "export") {
t.type = Token::KEYWORD_EXPORT;
class Tokenizer {
public:
- explicit Tokenizer(std::istream &in) : in(in), line(1), isPutback(false) { }
+ explicit Tokenizer(std::istream &in)
+ : in(in), line(1), isPutback(false), skipComments(true) { }
~Tokenizer() { }
private:
Tokenizer(const Tokenizer &);
KEYWORD_TRUE = 't',
IDENTIFIER = 'x',
TYPE_NAME = 'n',
+ COMMENT = 'c'
};
Token() : type(UNKNOWN), number(0) { }
Token ReadString();
Token ReadIdentifier();
+ Token ReadComment();
+ Token ReadMultilineComment();
+
bool CheckKeyword(Token &);
private:
Token putback;
int line;
bool isPutback;
+ bool skipComments;
};