Skip to content

Commit 5d52d9c

Browse files
committed
[UPDATE] Refactor code and reorganice methods for parsing
1 parent 9faeacc commit 5d52d9c

26 files changed

+412
-241
lines changed

src/main/java/LexerOperations/Lexer.java

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ private void analizeLine(String line) {
7575
char[] chars = line.toCharArray();
7676
stringActive = false;
7777

78-
// Separamos la linea en lexemas(palabras) para poder obtener el token de una manera mas sencilla
78+
// Separamos la linea en lexemas(palabras) para poder obtener el token de una
79+
// manera mas sencilla
7980
while (index != chars.length) {
8081
// Ignoramos los comentarios en linea que comienzan con '#'
8182
if (!stringActive && chars[index] == '#') {
@@ -87,8 +88,10 @@ private void analizeLine(String line) {
8788
stringActive = !stringActive;
8889
}
8990

90-
// Separamos los lexemas cada que encontramos los caracteres de espacio en blanco, delimitadores u operadores
91-
if (!stringActive && (Character.isWhitespace(chars[index]) || isOperator(Character.toString(chars[index])) || isDelimiter(Character.toString(chars[index])))) {
91+
// Separamos los lexemas cada que encontramos los caracteres de espacio en
92+
// blanco, delimitadores u operadores
93+
if (!stringActive && (Character.isWhitespace(chars[index]) || isOperator(Character.toString(chars[index]))
94+
|| isDelimiter(Character.toString(chars[index])))) {
9295
if (!lexeme.isBlank()) {
9396
column = index + 1 - lexeme.length();
9497
tokenList.add(new Token(lexeme, TokenType.DESCONOCIDO, row, column));
@@ -98,11 +101,14 @@ private void analizeLine(String line) {
98101
if (index != tokenList.size() - 1) {
99102
if (isOperator(Character.toString(chars[index + 1]))) {
100103
column = index + 1;
101-
tokenList.add(new Token(Character.toString(chars[index]) + Character.toString(chars[index + 1]), TokenType.DESCONOCIDO, row, column));
104+
tokenList.add(
105+
new Token(Character.toString(chars[index]) + Character.toString(chars[index + 1]),
106+
TokenType.DESCONOCIDO, row, column));
102107
index++;
103108
} else {
104109
column = index + 1;
105-
tokenList.add(new Token(Character.toString(chars[index]), TokenType.DESCONOCIDO, row, column));
110+
tokenList.add(
111+
new Token(Character.toString(chars[index]), TokenType.DESCONOCIDO, row, column));
106112
}
107113
}
108114
} else if (isDelimiter(Character.toString(chars[index]))) {
@@ -155,7 +161,7 @@ private void analizeLine(String line) {
155161
*
156162
* @param lexeme El lexema a comparar con las palabras reservadas
157163
* @return 'True' si el lexema actual es una palabra reservada, 'False' si
158-
* no lo es.
164+
* no lo es.
159165
*/
160166
private boolean isReservedWord(String lexeme) {
161167
return checkAndSetTokenType(lexeme, Constants.RESERVED_WORDS);
@@ -167,7 +173,7 @@ private boolean isReservedWord(String lexeme) {
167173
*
168174
* @param lexeme El lexema a comparar con las estructuras de control
169175
* @return 'True' si el lexema actual es una estructura de control, 'False'
170-
* si no lo es.
176+
* si no lo es.
171177
*/
172178
private boolean isControlStructure(String lexeme) {
173179
return checkAndSetTokenType(lexeme, Constants.CONTROL_STRUCTURES);
@@ -188,7 +194,7 @@ private boolean isOperator(String lexeme) {
188194
*
189195
* @param lexeme El lexema a comparar con los delimitadores
190196
* @return 'True' si el lexema actual es un delimitador, 'False' si no lo
191-
* es.
197+
* es.
192198
*/
193199
private boolean isDelimiter(String lexeme) {
194200
return checkAndSetTokenType(lexeme, Constants.DELIMITERS);
@@ -199,16 +205,24 @@ private boolean isDelimiter(String lexeme) {
199205
* base a su posicion de modo que asi puede determinar el tipo de
200206
* identificador
201207
*
202-
* @param lexeme El lexema a comparar
208+
* @param lexeme El lexema a comparar
203209
* @param tknIndex La posicion del token a comparar
204210
* @return 'True' si el lexema actual es un identificador, 'False' si no lo
205-
* es.
211+
* es.
206212
*/
207213
private boolean isIdentifier(String lexeme, int tknIndex) {
208214
boolean val = false;
209215
try {
210216
if (tokenList.get(tknIndex + 1).getLexeme().equals("=")) {
211-
tokenType = TokenType.IDENTIFICADOR;
217+
if (tokenList.get(tknIndex + 2).getLexeme().equals("{")) {
218+
tokenType = TokenType.IDENTIFICADOR_CONJUNTO;
219+
} else if (tokenList.get(tknIndex + 2).getLexeme().equals("(")) {
220+
tokenType = TokenType.IDENTIFICADOR_TUPLA;
221+
} else if (tokenList.get(tknIndex + 2).getLexeme().equals("[")) {
222+
tokenType = TokenType.IDENTIFICADOR_LISTA;
223+
} else {
224+
tokenType = TokenType.IDENTIFICADOR;
225+
}
212226
variableNames.add(lexeme);
213227
val = !val;
214228
} else if (variableNames.contains(lexeme)) {
@@ -226,10 +240,10 @@ private boolean isIdentifier(String lexeme, int tknIndex) {
226240
* base a su posicion de modo que asi puede determinar si es un
227241
* identificador de clase
228242
*
229-
* @param lexeme El lexema a comparar
243+
* @param lexeme El lexema a comparar
230244
* @param tknIndex La posicion del token a comparar
231245
* @return 'True' si el lexema actual es un identificador de clase, 'False'
232-
* si no lo es.
246+
* si no lo es.
233247
*/
234248
private boolean isClassIdentifier(String lexeme, int tknIndex) {
235249
boolean val = false;
@@ -242,6 +256,9 @@ private boolean isClassIdentifier(String lexeme, int tknIndex) {
242256
} else if (tokenList.get(tknIndex - 1).getToken() == TokenType.FOR) {
243257
tokenType = TokenType.IDENTIFICADOR;
244258
val = !val;
259+
} else if (tokenList.get(tknIndex - 1).getToken() == TokenType.DEF) {
260+
tokenType = TokenType.IDENTIFICADOR_FUNCION;
261+
val = !val;
245262
}
246263
} catch (IndexOutOfBoundsException e) {
247264

@@ -253,7 +270,7 @@ private boolean isClassIdentifier(String lexeme, int tknIndex) {
253270
* Este método verifica si el lexema se encuentra en alguno de los mapas de
254271
* tokens y establece su tipo de token una vez lo haya comparado.
255272
*
256-
* @param lexeme el lexema a comparar con el mapa seleccionado.
273+
* @param lexeme el lexema a comparar con el mapa seleccionado.
257274
* @param tokenMap el mapa a utilizar para la busqueda y asignación.
258275
* @return Un booleano dependiendo de si se ha encontrado en el mapa o no.
259276
*/
@@ -268,7 +285,7 @@ private boolean checkAndSetTokenType(String lexeme, Map<String, TokenType> token
268285
*
269286
* @param lexeme El lexema a comparar si es un valor númerico
270287
* @return 'True' si el lexema actual es un valor númerico, 'False' si no lo
271-
* es.
288+
* es.
272289
*/
273290
private boolean isNumber(String lexeme) {
274291
int cont = 0, dots = 0;
@@ -299,11 +316,13 @@ private boolean isNumber(String lexeme) {
299316
*
300317
* @param lexeme El lexema a verificar
301318
* @return 'True' si el lexema actual es una cadena de caracteres, 'False'
302-
* si no lo es.
319+
* si no lo es.
303320
*/
304321
private boolean isString(String lexeme) {
305322
char[] aux = lexeme.toCharArray();
306-
tokenType = (aux[0] == '"' && aux[aux.length - 1] == '"') || (aux[0] == '\'' && aux[aux.length - 1] == '\'') ? TokenType.CADENA : TokenType.DESCONOCIDO;
323+
tokenType = (aux[0] == '"' && aux[aux.length - 1] == '"') || (aux[0] == '\'' && aux[aux.length - 1] == '\'')
324+
? TokenType.CADENA
325+
: TokenType.DESCONOCIDO;
307326
return (aux[0] == '"' && aux[aux.length - 1] == '"') || (aux[0] == '\'' && aux[aux.length - 1] == '\'');
308327
}
309328

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package ParserOperations;
2+
3+
public class Assignments {
4+
public boolean isArithmeticOperation() {
5+
boolean flag = false;
6+
return flag;
7+
}
8+
9+
public boolean isValueAssign() {
10+
boolean flag = false;
11+
return flag;
12+
}
13+
14+
public boolean isAugAssign() {
15+
boolean flag = false;
16+
return flag;
17+
}
18+
19+
public boolean isCallFunction() {
20+
boolean flag = false;
21+
return flag;
22+
}
23+
24+
public boolean isDataCollection() {
25+
boolean flag = false;
26+
return flag;
27+
}
28+
29+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package ParserOperations;
2+
3+
import Tokens.TokenType;
4+
5+
public class Block {
6+
private SimpleStatements simpleStmt;
7+
private CompoundStatements compStmt;
8+
9+
public Block() {
10+
simpleStmt = new SimpleStatements();
11+
compStmt = new CompoundStatements();
12+
}
13+
14+
public boolean isValidBlock() {
15+
if (simpleStmt.isSimpleStatement() || compStmt.isCompoundStatement()) {
16+
return true;
17+
} else {
18+
return false;
19+
}
20+
}
21+
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package ParserOperations;
2+
3+
import Tokens.TokenType;
4+
5+
public class CompoundStatements {
6+
7+
private Utilities tool;
8+
private ExpressionParser expParser;
9+
10+
public CompoundStatements() {
11+
tool = new Utilities();
12+
expParser = new ExpressionParser();
13+
}
14+
15+
public boolean isCompoundStatement() {
16+
if (isFunctionDef() || isClassDeclaration() || isIfStatement() || isForStatement()
17+
|| isWhileStatement() || isMatchStatement() || isTryStatement()) {
18+
return true;
19+
} else {
20+
return false;
21+
}
22+
}
23+
24+
public boolean isFunctionDef() {
25+
boolean flag = false;
26+
// TO-DO
27+
return flag;
28+
}
29+
30+
public boolean isClassDeclaration() {
31+
boolean flag = false;
32+
if (tool.verifyToken(TokenType.CLASS)) {
33+
tool.incrementIndex();
34+
if (tool.verifyToken(TokenType.IDENTIFICADOR_CLASE)) {
35+
tool.incrementIndex();
36+
if (tool.verifyToken(TokenType.DOS_PUNTOS)) {
37+
// Fin del arbol sintáctico
38+
} else {
39+
tool.showError("Se esperaba ':'");
40+
}
41+
} else {
42+
tool.showError("Se esperaba identificador de clase");
43+
}
44+
}
45+
return flag;
46+
}
47+
48+
public boolean isIfStatement() {
49+
boolean flag = false;
50+
51+
// Verificar si el token actual es 'IF'
52+
if (tool.verifyToken(TokenType.IF)) {
53+
tool.incrementIndex();
54+
// Verificar si la expresión condicional es válida
55+
if (expParser.isCompoundExpression()) {
56+
// Verificar el token siguiente
57+
if (tool.getIndex() < tool.getTokenList().size()) {
58+
if (tool.verifyToken(TokenType.DOS_PUNTOS)) {
59+
flag = true; // La sentencia IF es correcta
60+
System.out.println("IF CORRECTO");
61+
} else if (tool.verifyToken(TokenType.PARENTESIS_CIERRE)) {
62+
tool.showError("No se encontró '('");
63+
} else {
64+
tool.showError("Se esperaban ':'");
65+
}
66+
} else {
67+
tool.showError("Se esperaban ':'");
68+
}
69+
} else {
70+
tool.showError("Expresión condicional no válida");
71+
}
72+
}
73+
return flag;
74+
}
75+
76+
public boolean isWhileStatement() {
77+
boolean flag = false;
78+
if (tool.verifyToken(TokenType.WHILE)) {
79+
tool.incrementIndex();
80+
if (expParser.isCompoundExpression()) {
81+
if (tool.verifyToken(TokenType.DOS_PUNTOS)) {
82+
System.out.println("WHILE CORRECTO");
83+
} else {
84+
tool.showError("Se esperaban ':'");
85+
}
86+
}
87+
}
88+
return flag;
89+
}
90+
91+
public boolean isForStatement() {
92+
boolean flag = false;
93+
if (tool.verifyToken(TokenType.FOR)) {
94+
tool.incrementIndex();
95+
if (tool.verifyToken(TokenType.IDENTIFICADOR)) {
96+
tool.incrementIndex();
97+
if (tool.verifyToken(TokenType.IN)) {
98+
tool.incrementIndex();
99+
if (tool.verifyToken(TokenType.RANGE)) {
100+
tool.incrementIndex();
101+
if (tool.verifyToken(TokenType.PARENTESIS_APERTURA)) {
102+
tool.incrementIndex();
103+
if (tool.verifyToken(TokenType.ENTERO)) {
104+
tool.incrementIndex();
105+
if (tool.verifyToken(TokenType.PARENTESIS_CIERRE)) {
106+
tool.incrementIndex();
107+
if (tool.verifyToken(TokenType.DOS_PUNTOS)) {
108+
// FIN DEL ARBOL SINTACTICO
109+
} else {
110+
tool.showError("Se esperaban ':'");
111+
}
112+
} else {
113+
tool.showError("Se esperaban ')'");
114+
}
115+
} else {
116+
tool.showError("Se esperaban valor iterable");
117+
}
118+
} else {
119+
tool.showError("Se esperaban '('");
120+
}
121+
} else if (tool.verifyToken(TokenType.CADENA)) {
122+
tool.incrementIndex();
123+
if (tool.verifyToken(TokenType.DOS_PUNTOS)) {
124+
// FIN DEL ARBOL SINTACTICO
125+
}
126+
} else {
127+
tool.showError("Se esperaban expresión");
128+
}
129+
} else {
130+
tool.showError("Se esperaban palabra reservada");
131+
}
132+
} else {
133+
tool.showError("Se esperaban iterador");
134+
}
135+
}
136+
return flag;
137+
}
138+
139+
public boolean isMatchStatement() {
140+
boolean flag = false;
141+
// TO-DO
142+
return flag;
143+
}
144+
145+
public boolean isTryStatement() {
146+
boolean flag = false;
147+
// TO-DO
148+
return flag;
149+
}
150+
}

src/main/java/ParserOperations/ConditionalParser.java

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)