Skip to content

Commit f60b2fa

Browse files
committed
Identifica correctamente keywords y variables con "_" en el nombre.
Añadidos nuevos tokens al lexer.
1 parent b563477 commit f60b2fa

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

src/correctorcodigodoc/Lexer.java

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ public class Lexer {
2626
TokensAtomicos.put(',', Token.Ids.COMA);
2727
TokensAtomicos.put('\n', Token.Ids.NUEVA_LINEA);
2828
TokensAtomicos.put('←', Token.Ids.ASIGNACION);
29+
TokensAtomicos.put('=', Token.Ids.ASIGNACION);
30+
TokensAtomicos.put('+', Token.Ids.MAS_BINARIO);
31+
TokensAtomicos.put('%', Token.Ids.MODULO);
32+
TokensAtomicos.put('<', Token.Ids.MENOR_QUE);
33+
TokensAtomicos.put('>', Token.Ids.MAYOR_QUE);
34+
TokensAtomicos.put('.', Token.Ids.PUNTO);
35+
TokensAtomicos.put(':', Token.Ids.DOS_PUNTOS);
2936

3037
TiposToken.put("lea", Token.Ids.LEA);
3138
TiposToken.put("leo", Token.Ids.LEA);
@@ -52,6 +59,12 @@ public class Lexer {
5259

5360
TiposToken.put("real", Token.Ids.TIPO_DATO);
5461
TiposToken.put("reales", Token.Ids.TIPO_DATO);
62+
63+
TiposToken.put("mod", Token.Ids.MODULO);
64+
65+
TiposToken.put("proc", Token.Ids.PROC);
66+
67+
TiposToken.put("fin_proc", Token.Ids.FIN_PROC);
5568
}
5669

5770
public Lexer(String program) {
@@ -67,36 +80,41 @@ private Token nextTokenInternal() {
6780
return null;
6881

6982
// Salta los espacios en blanco
70-
while (ultimo_caracter == ' ')
83+
while (ultimo_caracter == ' ' || ultimo_caracter == '\t' || ultimo_caracter == '\r')
7184
ultimo_caracter = getChar();
7285

7386
// Si encontramos un comentario, salta a la siguiente linea
7487
if (ultimo_caracter == '/') {
7588
if (hasNextChar() && peekChar() == '/') {
7689
eatUntilNextLine();
7790
// Devuelve un token de nueva linea
78-
return new Token(Token.Ids.NUEVA_LINEA, "\n");
91+
return new Token(Token.Ids.NUEVA_LINEA, "\n", lex_index);
7992
}
8093
}
8194

8295
// Identifica los casos especiales , ( ) ← \n
8396
if (TokensAtomicos.containsKey(ultimo_caracter)) {
84-
Token ret = new Token(TokensAtomicos.get(ultimo_caracter), String.valueOf(ultimo_caracter));
97+
Token ret = new Token(TokensAtomicos.get(ultimo_caracter), String.valueOf(ultimo_caracter), lex_index);
8598
eatNextChar();
8699
return ret;
87100
}
88101

89102
// Parsea una palabra vorazmente
90103
if (Character.isLetter(ultimo_caracter)) {
91-
String identificador = String.valueOf(ultimo_caracter);
92-
while (hasNextChar() && Character.isLetterOrDigit(ultimo_caracter = getChar()))
93-
identificador += ultimo_caracter;
104+
String identificador = "";
105+
do {
106+
identificador += String.valueOf(ultimo_caracter);
107+
108+
if (!hasNextChar())
109+
break;
110+
ultimo_caracter = getChar();
111+
} while (Character.isLetterOrDigit(ultimo_caracter) || ultimo_caracter == '_');
94112

95113
// Verifica si la palabra es una keyword o una variable
96114
if (TiposToken.containsKey(identificador.toLowerCase())) {
97-
return new Token(TiposToken.get(identificador.toLowerCase()), identificador);
115+
return new Token(TiposToken.get(identificador.toLowerCase()), identificador, lex_index);
98116
} else {
99-
return new Token(Token.Ids.IDENTIFIER, identificador);
117+
return new Token(Token.Ids.IDENTIFIER, identificador, lex_index);
100118
}
101119
} else {
102120
// Parsea el menos unario y binario
@@ -105,7 +123,7 @@ private Token nextTokenInternal() {
105123
if (ultimo_token != null && (ultimo_token.id == Token.Ids.NUMERO || ultimo_token.id == Token.Ids.IDENTIFIER)) {
106124
// Nos comemos el -
107125
eatNextChar();
108-
return new Token(Token.Ids.MENOS_BINARIO, "-");
126+
return new Token(Token.Ids.MENOS_BINARIO, "-", lex_index);
109127
}
110128

111129
// De lo contrario tenemos un menos unario, que es un número, fallback al siguiente caso
@@ -119,14 +137,16 @@ private Token nextTokenInternal() {
119137
ultimo_caracter = getChar();
120138
} while (hasNextChar() && (Character.isDigit(ultimo_caracter) || ultimo_caracter == '.'));
121139

122-
return new Token(Token.Ids.NUMERO, numero);
140+
return new Token(Token.Ids.NUMERO, numero, lex_index);
123141
}
124142
}
125143

144+
Token ret = new Token(Token.Ids.UNKNOWN, String.valueOf(ultimo_caracter), lex_index);
145+
126146
if (hasNextChar())
127147
eatNextChar();
128148

129-
return new Token(Token.Ids.UNKNOWN, "");
149+
return ret;
130150
}
131151

132152
public Token nextToken() {
@@ -144,7 +164,8 @@ private boolean hasNextChar() {
144164
}
145165

146166
private void eatNextChar() {
147-
ultimo_caracter = getChar();
167+
if (hasNextChar())
168+
ultimo_caracter = getChar();
148169
}
149170

150171
private char peekChar() {

0 commit comments

Comments
 (0)