diff options
Diffstat (limited to 'lexer.cpp')
-rw-r--r-- | lexer.cpp | 21 |
1 files changed, 17 insertions, 4 deletions
@@ -16,6 +16,7 @@ Lexer::Lexer(string text): current_token(nil, {}){ void Lexer::setText(string text){ this->text = text; // Inicializamos el lexer. + current_char = 0; current_token = nextToken(); var_name = '\0'; } @@ -24,8 +25,8 @@ Token Lexer::getCurrentToken(){ return current_token; } -bool Lexer::matchFunction(string function_name){ - string name = text.substr(current_char, 2); +bool Lexer::matchFunction(const string &function_name){ + string name = text.substr(current_char, 3); bool matched = name == function_name; // Si hubo una coincidencia movemos el puntero. @@ -48,8 +49,12 @@ string Lexer::createNumber(){ } Token Lexer::match(){ + // Si ya consumimos todo el texto, + // regresamos nil. + if(current_char >= text.length()) + return Token(nil, {}); // Probamos las funciones. - if(current_char + 2 < text.length()) + if(isalpha(text[current_char]) && current_char + 2 <= text.length()) { if(matchFunction("sin")) return Token(function, "sin"); @@ -71,7 +76,12 @@ Token Lexer::match(){ /* Si es la primera vez que se lee un token variable o si la variable leída es igual a la anteriormente leída. */ if(var_name == '\0' || var_name == text[current_char]) + { + var_name = text[current_char]; + ++current_char; + return Token(variable, string(1, var_name)); + } // Error, se introdujo una expresión con dos o más variables. throw LexerException("Encontrada más de una variable."); @@ -103,11 +113,14 @@ Token Lexer::match(){ ++current_char; return Token(right_parens, {}); default: - throw LexerException("Carácter inválido."); + return Token(nil, {}); } } Token Lexer::nextToken(){ + if(text[current_char] == ' ') + ++current_char; + current_token = match(); return current_token; |