eTokenType Lexer::scopeOperand(void) { t_token * newToken; if (this->_currentIt != this->_str.end()) newToken = this->_createNewToken(TK_OPERAND); if (*this->_currentIt == '(') throw LexicalException(); while (this->_currentIt != this->_str.end()) { if (this->_isSpace(*this->_currentIt)) throw LexicalException(); if (*this->_currentIt == ';') throw LexicalException(); if (*this->_currentIt == '(') { this->_currentIt++; if (*this->_currentIt == ';') throw LexicalException(); return this->_enterScope(LXS_ARGS, false); } newToken->value.push_back(*this->_currentIt); this->_currentIt++; } if (this->_currentIt != this->_str.end()) throw LexicalException(); return TK_NONE; }
eTokenType Lexer::scopeArgs(void) { t_token * newToken; if (this->_currentIt != this->_str.end()) newToken = this->_createNewToken(TK_ARGS); while (this->_currentIt != this->_str.end()) { if (this->_isSpace(*this->_currentIt)) throw LexicalException(); if (*this->_currentIt == ';') throw LexicalException(); if (*this->_currentIt == ')') { this->_currentIt++; return TK_ARGS; } newToken->value.push_back(*this->_currentIt); this->_currentIt++; } throw LexicalException(); return TK_NONE; }
t_tokens Lexer::getTokens(std::string const str) { this->_tokens.clear(); this->_str = str; this->_currentIt = this->_str.begin(); this->_enterScope(LXS_DEFAULT); this->_skipAllSpace(); if (*this->_currentIt == ';') this->_enterScope(LXS_COMMENT); if (this->_currentIt != this->_str.end()) { throw LexicalException(); } return this->_tokens; }
Token Lexico::ObtenerSiguienteTokenPascal() { Token token; string lexema; char c; int estado_automata = 0; //Estado inicial del automata int longitud_cadenas = 0; while (true) { c = codigo_fuente->LeerCaracter(); switch ( estado_automata ) { //Estado 0 se encarga de "comerse" los espacios en blanco y caracter de nueva linea case 0: if ( Util::EsLetra( c ) ) { lexema+= Util::ToUpper(c); estado_automata = 1; codigo_fuente->Avanzar(); } else if ( c == '+' )//Operadores aditivos { lexema+=c; estado_automata = 2; codigo_fuente->Avanzar(); } else if ( c == '-' ) { lexema+=c; codigo_fuente->Avanzar(); token.SetLexema(lexema); token.SetTipo(op_resta); return token; } else if( c == '*' ) { lexema+=c; codigo_fuente->Avanzar(); token.SetLexema(lexema); token.SetTipo( op_mul ); return token; } else if ( c == '=' ) { lexema+=c; codigo_fuente->Avanzar(); token.SetLexema(lexema); token.SetTipo( op_igual ); return token; } else if( c== '\n')//Se "come" los caracteres de nueva linea { linea_actual++; estado_automata = 0; codigo_fuente->Avanzar(); } else if( c== EOF ) //El archivo llego a su fin { estado_automata = 4; } else if ( c== ' ' || c=='\t' )//Espacios y tabulaciones { estado_automata = 0; codigo_fuente->Avanzar(); } else if( c=='/' )//Comentarios u operador de division { estado_automata = 5; codigo_fuente->Avanzar(); } else if( Util::EsDigito( c ) && c != '0' ) //Decimal { lexema+=c; estado_automata = 7; codigo_fuente->Avanzar(); } else if( c == '0')//Octal { lexema+=c; estado_automata = 8; codigo_fuente->Avanzar(); } else if ( c == '$' )//Hexadecimal { lexema+=c; estado_automata=12; codigo_fuente->Avanzar(); } else if ( c == '.' )//Punto flotante { lexema+=c; estado_automata = 24; codigo_fuente->Avanzar(); } else if ( c == '\'' )//string { lexema+=c; estado_automata = 17; codigo_fuente->Avanzar(); } else if ( c=='>')//operadores { lexema+=c; estado_automata =20; codigo_fuente->Avanzar(); } else if ( c == '[' || c== '}' || c== ';' || c==',' || c== '{' || c== '(' || c== ')' || c ==']' )//puntacion { lexema+=c; estado_automata = 22; codigo_fuente->Avanzar(); } else if ( c== ':' ) { lexema+=c; estado_automata = 23; codigo_fuente->Avanzar(); } else if ( c == '%' ) { lexema+=c; estado_automata = 29; codigo_fuente->Avanzar(); } else if ( c == '<' ) { lexema+=c; estado_automata = 31; codigo_fuente->Avanzar(); } else { codigo_fuente->Avanzar(); throw LexicalException( linea_actual,"Token no valido" ); } break; case 1: if ( Util::EsDigito( c ) || Util::EsLetra( c ) || c=='_' ) { lexema+= Util::ToUpper(c); estado_automata = 1; codigo_fuente->Avanzar(); } else { token.SetLexema( lexema ); token.SetTipo( id ); map<string,Token>::iterator it = palabras_reservadas.find( lexema ); if ( it != palabras_reservadas.end() ) return it->second; else return token; } break; case 2: token.SetTipo(op_suma); token.SetLexema(lexema); return token; case 3: if ( c != '*' ) { if ( c == '\n' ) linea_actual++; estado_automata =3; codigo_fuente->Avanzar(); } else { estado_automata = 19; codigo_fuente->Avanzar(); } break; case 4: token.SetLexema("EOF"); token.SetTipo(eof); return token; case 5: if ( c == '/' ) { estado_automata = 6; codigo_fuente->Avanzar(); } else if ( c == '*' ) { estado_automata = 3; codigo_fuente->Avanzar(); } else { token.SetLexema( "/" ); token.SetTipo( op_div ); return token; } break; case 6: if ( c == '\n' ) { linea_actual++; estado_automata = 0; codigo_fuente->Avanzar(); } else { codigo_fuente->Avanzar(); estado_automata = 6; } break; case 7: if ( Util::EsDigito( c ) ) { estado_automata = 7; lexema+=c; codigo_fuente->Avanzar(); } else if( c == '.' ) { //lexema+=c; estado_automata = 15; codigo_fuente->Avanzar(); } else if ( c== 'E' || c=='e' ) { lexema+=c; estado_automata = 26; codigo_fuente->Avanzar(); } else { token.SetLexema ( lexema ); token.SetTipo(lit_int); return token; } break; case 8: if ( Util::EsOctalDig(c) ) { lexema+= c ; estado_automata = 9; codigo_fuente->Avanzar(); } else if( c == '.' ) { lexema+=c; estado_automata = 15; codigo_fuente->Avanzar(); } else { token.SetLexema( lexema ); token.SetTipo( lit_int ); return token; } break; case 9: if ( Util::EsOctalDig(c) ) { lexema+=c; estado_automata = 10; codigo_fuente->Avanzar(); } else { token.SetLexema( lexema ); token.SetTipo( lit_int ); return token; } break; case 10: if( Util::EsOctalDig(c) ) { lexema+=c; estado_automata = 11; codigo_fuente->Avanzar(); } else { token.SetLexema(lexema); token.SetTipo( lit_int); return token; } break; case 11: token.SetLexema(lexema); token.SetTipo(lit_int); return token; case 12: if ( Util::EsHexDig( c ) ) { lexema+=c; estado_automata=13; codigo_fuente->Avanzar(); } else throw LexicalException(linea_actual,"Constante hexadecimal invalida"); break; case 13: if ( Util::EsHexDig( c ) ) { lexema+=c; estado_automata=14; codigo_fuente->Avanzar(); } else { token.SetLexema(lexema); token.SetTipo(lit_int); return token; } break; case 14: token.SetLexema(lexema); token.SetTipo(lit_int); return token; case 15: if ( Util::EsDigito( c ) ) { lexema+='.'; lexema+=c; estado_automata = 16; codigo_fuente->Avanzar(); } else if ( c == '.' ) { //lexema+=c; codigo_fuente->Retroceder(); token.SetLexema( lexema ); token.SetTipo( lit_int ); return token; } else { lexema+='.'; token.SetLexema( lexema ); token.SetTipo( lit_float ); return token; } break; case 16: if ( Util::EsDigito( c ) ) { lexema+=c; estado_automata = 16; codigo_fuente->Avanzar(); } else if ( c=='E' || c=='e' ) { lexema+=c; estado_automata = 26; codigo_fuente->Avanzar(); } else { token.SetLexema(lexema); token.SetTipo(lit_float); return token; } break; case 17: if ( c != '\'' ) { lexema+=c; longitud_cadenas++; estado_automata =17; codigo_fuente->Avanzar(); } else { lexema+=c; estado_automata = 18; codigo_fuente->Avanzar(); } break; case 18: if ( c == '\'' ) { longitud_cadenas++; estado_automata = 17; codigo_fuente->Avanzar(); } else { if ( longitud_cadenas <= 1) token.SetTipo(lit_caracter); else token.SetTipo( lit_string); token.SetLexema(lexema); return token; } break; case 19: if ( c != '/' ) { estado_automata = 3; codigo_fuente->Avanzar(); } else { estado_automata = 0; codigo_fuente->Avanzar(); } break; case 20: if ( c == '=' ) { lexema+=c; estado_automata =21; codigo_fuente->Avanzar(); } else { token.SetLexema( lexema ); token.SetTipo( op_mayorque ); return token; } break; case 21: token.SetLexema( lexema ); token.SetTipo( op_mayorigual); return token; case 22: if ( lexema.compare("[")==0 ) { token.SetTipo(punt_corchizq); } else if (lexema.compare("]")==0 ) { token.SetTipo( punt_corchder); } else if ( lexema.compare("(")==0 ) { token.SetTipo(punt_parentizq); } else if (lexema.compare(")")==0 ) { token.SetTipo(punt_parentder); } else if ( lexema.compare("{")==0 ) { token.SetTipo(punt_llaveizq); } else if (lexema.compare("}")==0 ) { token.SetTipo(punt_llaveder); } else if( lexema.compare(";")==0 ) { token.SetTipo(punt_puntocoma); } else if ( lexema.compare(",")==0 ) { token.SetTipo(punt_coma); } token.SetLexema(lexema); return token; case 23: if ( c == '=' ) { lexema+=c; codigo_fuente->Avanzar(); token.SetLexema(lexema); token.SetTipo(op_asignacion); return token; } else { token.SetLexema(lexema); token.SetTipo(punt_colon); return token; } break; case 24: if( c == '.' ) { lexema+=c; estado_automata = 25; codigo_fuente->Avanzar(); } else if ( Util::EsDigito( c ) ) { lexema+=c; estado_automata = 16; codigo_fuente->Avanzar(); } else { token.SetLexema(lexema); token.SetTipo(punt_punto); return token; } break; case 25: token.SetLexema(lexema); token.SetTipo(punt_doblepunto); return token; case 26: if ( c == '+' || c== '-' ) { lexema+=c; estado_automata = 27; codigo_fuente->Avanzar(); } else if ( Util::EsDigito( c ) ) { lexema+=c; estado_automata = 28; codigo_fuente->Avanzar(); } else { throw LexicalException(linea_actual,"Token de punto flotante invalido" ); } break; case 27: if ( Util::EsDigito( c ) ) { lexema+=c; estado_automata=28; codigo_fuente->Avanzar(); } else throw LexicalException(linea_actual,"Token de punto flotante invalido" ); break; case 28: if ( Util::EsDigito( c ) ) { lexema+=c; estado_automata = 28; codigo_fuente->Avanzar(); } else { token.SetLexema(lexema); token.SetTipo( lit_float ); return token; } break; case 29: if ( c == '>' ) { lexema+=c; estado_automata =30; codigo_fuente->Avanzar(); } else { throw LexicalException(linea_actual,"Token invalido"); } break; case 30: token.SetLexema(lexema); token.SetTipo(punt_delimpascalder); modo_html = true; return token; case 31: if ( c == '%' ) { lexema+=c; estado_automata = 32; codigo_fuente->Avanzar(); } else if ( c=='=') { lexema+=c; codigo_fuente->Avanzar(); token.SetLexema(lexema); token.SetTipo(op_menorigual); return token; } else if( c=='>' ) { lexema+=c; codigo_fuente->Avanzar(); token.SetLexema(lexema); token.SetTipo(op_distinto); return token; } else { token.SetLexema(lexema); token.SetTipo(op_menorque); return token; } break; case 32: token.SetLexema(lexema); token.SetTipo(punt_delimpascalizq); modo_html = false; return token; default: break; }//Fin switch } }