Skip to content

Commit 45b6b70

Browse files
committed
[UPDATE] Refactor parsing methods to use in example code
1 parent 716f672 commit 45b6b70

File tree

20 files changed

+308
-155
lines changed

20 files changed

+308
-155
lines changed

src/main/java/LexerOperations/Lexer.java

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,15 @@ private void analizeLine(String line) {
9898
lexeme = "";
9999
}
100100
if (isOperator(Character.toString(chars[index]))) {
101-
if (index != tokenList.size() - 1) {
102-
if (isOperator(Character.toString(chars[index + 1]))) {
103-
column = index + 1;
104-
tokenList.add(
105-
new Token(Character.toString(chars[index]) + Character.toString(chars[index + 1]),
106-
TokenType.DESCONOCIDO, row, column));
107-
index++;
108-
} else {
109-
column = index + 1;
110-
tokenList.add(
111-
new Token(Character.toString(chars[index]), TokenType.DESCONOCIDO, row, column));
112-
}
101+
column = index + 1;
102+
String lex;
103+
if (index < chars.length - 1 && isOperator(Character.toString(chars[index + 1]))) {
104+
lex = Character.toString(chars[index]) + Character.toString(chars[index + 1]);
105+
index++;
106+
} else {
107+
lex = Character.toString(chars[index]);
113108
}
109+
tokenList.add(new Token(lex, TokenType.DESCONOCIDO, row, column));
114110
} else if (isDelimiter(Character.toString(chars[index]))) {
115111
column = index + 1;
116112
tokenList.add(new Token(Character.toString(chars[index]), TokenType.DESCONOCIDO, row, column));
@@ -212,25 +208,25 @@ private boolean isDelimiter(String lexeme) {
212208
*/
213209
private boolean isIdentifier(String lexeme, int tknIndex) {
214210
boolean val = false;
215-
try {
216-
if (tokenList.get(tknIndex + 1).getLexeme().equals("=")) {
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-
}
226-
variableNames.add(lexeme);
227-
val = !val;
228-
} else if (variableNames.contains(lexeme)) {
211+
if (tknIndex >= 1 && tokenList.get(tknIndex - 1).getToken() == TokenType.EXCEPT) {
212+
tokenType = TokenType.IDENTIFICADOR_EXCEPCION;
213+
val = !val;
214+
}
215+
if (tknIndex < tokenList.size() - 1 && tokenList.get(tknIndex + 1).getLexeme().equals("=")) {
216+
if (tknIndex < tokenList.size() - 2 && tokenList.get(tknIndex + 2).getLexeme().equals("{")) {
217+
tokenType = TokenType.IDENTIFICADOR_CONJUNTO;
218+
} else if (tknIndex < tokenList.size() - 2 && tokenList.get(tknIndex + 2).getLexeme().equals("(")) {
219+
tokenType = TokenType.IDENTIFICADOR_TUPLA;
220+
} else if (tknIndex < tokenList.size() - 2 && tokenList.get(tknIndex + 2).getLexeme().equals("[")) {
221+
tokenType = TokenType.IDENTIFICADOR_LISTA;
222+
} else {
229223
tokenType = TokenType.IDENTIFICADOR;
230-
val = !val;
231224
}
232-
} catch (IndexOutOfBoundsException e) {
233-
225+
variableNames.add(lexeme);
226+
val = !val;
227+
} else if (variableNames.contains(lexeme)) {
228+
tokenType = TokenType.IDENTIFICADOR;
229+
val = !val;
234230
}
235231
return val;
236232
}
@@ -247,21 +243,17 @@ private boolean isIdentifier(String lexeme, int tknIndex) {
247243
*/
248244
private boolean isClassIdentifier(String lexeme, int tknIndex) {
249245
boolean val = false;
250-
try {
251-
if (tokenList.get(tknIndex - 1).getToken() == TokenType.CLASS) {
252-
if (!variableNames.contains(lexeme)) {
253-
tokenType = TokenType.IDENTIFICADOR_CLASE;
254-
val = !val;
255-
}
256-
} else if (tokenList.get(tknIndex - 1).getToken() == TokenType.FOR) {
257-
tokenType = TokenType.IDENTIFICADOR;
258-
val = !val;
259-
} else if (tokenList.get(tknIndex - 1).getToken() == TokenType.DEF) {
260-
tokenType = TokenType.IDENTIFICADOR_FUNCION;
246+
if (tknIndex < tokenList.size() - 1 && tokenList.get(tknIndex - 1).getToken() == TokenType.CLASS) {
247+
if (!variableNames.contains(lexeme)) {
248+
tokenType = TokenType.IDENTIFICADOR_CLASE;
261249
val = !val;
262250
}
263-
} catch (IndexOutOfBoundsException e) {
264-
251+
} else if (tknIndex < tokenList.size() - 1 && tokenList.get(tknIndex - 1).getToken() == TokenType.FOR) {
252+
tokenType = TokenType.IDENTIFICADOR;
253+
val = !val;
254+
} else if (tknIndex < tokenList.size() - 1 && tokenList.get(tknIndex - 1).getToken() == TokenType.DEF) {
255+
tokenType = TokenType.IDENTIFICADOR_FUNCION;
256+
val = !val;
265257
}
266258
return val;
267259
}

src/main/java/ParserOperations/Assignments.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public Assignments() {
1010
}
1111

1212
public boolean isAssignment() {
13-
if (isArithmeticOperation() || isAugAssign() || isValueAssign() || isDataCollection() || isCallFunction()) {
13+
if (isArithmeticOperation() || isValueAssign() || isDataCollection() || isCallFunction()) {
1414
return true;
1515
} else {
1616
return false;
@@ -19,7 +19,7 @@ public boolean isAssignment() {
1919

2020
public boolean isArithmeticOperation() {
2121
boolean flag = false;
22-
//TO-DO
22+
// TO-DO
2323
return flag;
2424
}
2525

@@ -31,29 +31,24 @@ public boolean isValueAssign() {
3131
tool.incrementIndex();
3232
if (tool.isValueToken(tool.getCurrentToken())) {
3333
flag = true;
34+
System.out.println("VARIABLE DECLARADA");
35+
tool.incrementIndex();
36+
3437
} else {
3538
tool.showError("Se esperaba valor");
3639
}
37-
} else {
38-
tool.showError("La variable no se encuentra definida");
39-
}
40-
}
41-
return flag;
42-
}
43-
44-
public boolean isAugAssign() {
45-
boolean flag = false;
46-
if (tool.verifyToken(TokenType.IDENTIFICADOR)) {
47-
tool.incrementIndex();
48-
if (tool.isAssignment(tool.getCurrentToken())) {
40+
} else if (tool.isAssignment(tool.getCurrentToken())) {
4941
tool.incrementIndex();
5042
if (tool.isValueToken(tool.getCurrentToken())) {
43+
System.out.println("VARIABLE AUGASSIGN");
5144
flag = true;
45+
tool.incrementIndex();
46+
5247
} else {
5348
tool.showError("Se esperaba valor");
5449
}
5550
} else {
56-
tool.showError("Se esperaba operador de asignacion");
51+
tool.showError("Se esperaba operador");
5752
}
5853
}
5954
return flag;
@@ -76,9 +71,10 @@ public boolean isDataCollection() {
7671
if (tool.isValueToken(tool.getCurrentToken())) {
7772
tool.incrementIndex();
7873
if (checkValuesList()) {
79-
tool.incrementIndex();
74+
// tool.incrementIndex();
8075
if (tool.verifyToken(TokenType.PARENTESIS_CIERRE)) {
8176
flag = true;
77+
System.out.println("ESTRUCTURA DE DATOS DECLARADA");
8278
} else {
8379
tool.showError("Se esperaba ')'");
8480
}
@@ -98,9 +94,10 @@ public boolean isDataCollection() {
9894
if (tool.isValueToken(tool.getCurrentToken())) {
9995
tool.incrementIndex();
10096
if (checkValuesList()) {
101-
tool.incrementIndex();
97+
// tool.incrementIndex();
10298
if (tool.verifyToken(TokenType.LLAVE_CIERRE)) {
10399
flag = true;
100+
System.out.println("ESTRUCTURA DE DATOS DECLARADA");
104101
} else {
105102
tool.showError("Se esperaba '}'");
106103
}
@@ -120,9 +117,10 @@ public boolean isDataCollection() {
120117
if (tool.isValueToken(tool.getCurrentToken())) {
121118
tool.incrementIndex();
122119
if (checkValuesList()) {
123-
tool.incrementIndex();
120+
// tool.incrementIndex();
124121
if (tool.verifyToken(TokenType.CORCHETE_CIERRE)) {
125122
flag = true;
123+
System.out.println("ESTRUCTURA DE DATOS DECLARADA");
126124
} else {
127125
tool.showError("Se esperaba ']'");
128126
}

src/main/java/ParserOperations/Block.java

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

0 commit comments

Comments
 (0)