Remove space and atWord tokens

`atWord` wasn't used at all and spaces should be eaten by the lexer

And added `unspecified` token
This commit is contained in:
Luis Ángel San Martín 2021-01-14 08:39:16 +01:00
parent ddb140d430
commit f09c5955d8
4 changed files with 8 additions and 23 deletions

View File

@ -7,17 +7,16 @@ QueryLexer::QueryLexer(const std::string &input)
Token QueryLexer::next() Token QueryLexer::next()
{ {
while (isSpace(peek())) {
get();
}
switch (peek()) { switch (peek()) {
case '\0': case '\0':
return Token(Token::Type::eof); return Token(Token::Type::eof);
case '(': case '(':
case ')': case ')':
return single(Token::Type::opcode); return single(Token::Type::opcode);
case ' ':
case '\t':
case '\r':
case '\n':
return space();
case '"': case '"':
return quotedWord(); return quotedWord();
default: default:
@ -40,15 +39,6 @@ Token QueryLexer::single(Token::Type type)
return Token(type, input.substr(index++, 1)); return Token(type, input.substr(index++, 1));
} }
Token QueryLexer::space()
{
auto start = index;
get();
while (isSpace(peek()))
get();
return Token(Token::Type::space, input.substr(start, index - start));
}
Token QueryLexer::word() Token QueryLexer::word()
{ {
auto start = index; auto start = index;

View File

@ -9,10 +9,9 @@ public:
enum class Type { enum class Type {
eof, eof,
opcode, opcode,
atWord,
word, word,
quotedWord, quotedWord,
space undefined
}; };
Token(Type type, std::string lexeme = "") Token(Type type, std::string lexeme = "")
@ -49,7 +48,6 @@ private:
char get(); char get();
Token single(Token::Type type); Token single(Token::Type type);
Token space();
Token word(); Token word();
Token quotedWord(); Token quotedWord();

View File

@ -136,9 +136,6 @@ bool QueryParser::isEof() const
void QueryParser::advance() void QueryParser::advance()
{ {
currentToken = lexer.next(); currentToken = lexer.next();
if (tokenType() == Token::Type::space)
advance();
} }
QueryParser::FieldType QueryParser::fieldType(const std::string &str) QueryParser::FieldType QueryParser::fieldType(const std::string &str)
@ -184,7 +181,7 @@ QueryParser::TreeNode QueryParser::andExpression()
return { "and", { lhs, andExpression() } }; return { "and", { lhs, andExpression() } };
} }
if ((isIn(tokenType(), { Token::Type::atWord, Token::Type::word, Token::Type::quotedWord }) || token() == "(") && lcaseToken() != "or") { if ((isIn(tokenType(), { Token::Type::word, Token::Type::quotedWord }) || token() == "(") && lcaseToken() != "or") {
return { "and", { lhs, andExpression() } }; return { "and", { lhs, andExpression() } };
} }
@ -210,7 +207,7 @@ QueryParser::TreeNode QueryParser::locationExpression()
} }
return res; return res;
} }
if (!isIn(tokenType(), { Token::Type::atWord, Token::Type::word, Token::Type::quotedWord })) { if (!isIn(tokenType(), { Token::Type::word, Token::Type::quotedWord })) {
throw std::invalid_argument("Invalid syntax. Expected a lookup name or a word"); throw std::invalid_argument("Invalid syntax. Expected a lookup name or a word");
} }
return baseToken(); return baseToken();

View File

@ -62,7 +62,7 @@ private:
void advance(); void advance();
QueryLexer lexer = QueryLexer(""); QueryLexer lexer = QueryLexer("");
Token currentToken = Token(Token::Type::eof); Token currentToken = Token(Token::Type::undefined);
template<typename T> template<typename T>
static bool isIn(const T &e, const std::list<T> &v) static bool isIn(const T &e, const std::list<T> &v)