Skip to content

Commit eeeae4e

Browse files
committed
idk it just works :/
1 parent b410922 commit eeeae4e

File tree

17 files changed

+152
-165
lines changed

17 files changed

+152
-165
lines changed

src/main/java/Files/TestFiles/asd.py

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

src/main/java/Files/TestFiles/basico.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ class ciclo_simple:
55
print("Valido:", cont)
66
cont += 1
77
else:
8-
print("Invalido:", cont)
9-
break;
8+
print("Invalido:", cont)

src/main/java/Files/TestFiles/test.py

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

src/main/java/GUI/CustomComponents/NumeroLinea.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package GUI.CustomComponents;
22

33
import java.awt.*;
4-
import java.awt.event.*;
54
import java.beans.*;
65
import java.util.HashMap;
76
import javax.swing.*;

src/main/java/ParserOperations/CompoundStatements.java

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,11 @@ public boolean isCompoundStatement() {
2020
|| isForStatement()
2121
|| isWhileStatement()
2222
|| isMatchStatement() || isCaseStatement()) {
23-
tool.indent();
2423
flag = true;
2524
}
2625
return flag;
2726
}
2827

29-
// public boolean isFunctionDef() {
30-
// boolean flag = false;
31-
// // TO-DO
32-
// if (tool.verifyToken(TokenType.DEF)) {
33-
// tool.incrementIndex();
34-
// if (tool.verifyToken(TokenType.IDENTIFICADOR_FUNCION)) {
35-
// tool.incrementIndex();
36-
// if (tool.verifyToken(TokenType.PARENTESIS_APERTURA)) {
37-
// tool.incrementIndex();
38-
// if (tool.verifyToken(TokenType.PARENTESIS_CIERRE)) {
39-
// tool.incrementIndex();
40-
// if (tool.verifyToken(TokenType.DOS_PUNTOS)) {
41-
// tool.incrementIndex();
42-
// flag = true;
43-
// }
44-
// }
45-
// }
46-
// }
47-
// }
48-
// return flag;
49-
// }
50-
5128
public boolean isClassDeclaration() {
5229
boolean flag = false;
5330
if (tool.verifyToken(TokenType.CLASS)) {
@@ -75,7 +52,6 @@ public boolean isIfStatement() {
7552
tool.incrementIndex();
7653
// Verificar si la expresión condicional es válida
7754
if (expParser.isValidExpression()) {
78-
// Verificar el token siguiente
7955
if (tool.getIndex() < tool.getTokenList().size()) {
8056
if (tool.verifyToken(TokenType.DOS_PUNTOS)) {
8157
flag = true;
@@ -100,7 +76,6 @@ public boolean isElifStatement() {
10076
tool.incrementIndex();
10177
// Verificar si la expresión condicional es válida
10278
if (expParser.isValidExpression()) {
103-
// Verificar el token siguiente
10479
if (tool.getIndex() < tool.getTokenList().size()) {
10580
if (tool.verifyToken(TokenType.DOS_PUNTOS)) {
10681
flag = true;
@@ -127,7 +102,7 @@ public boolean isElseStatement() {
127102
flag = true;
128103
tool.incrementIndex();
129104
// System.out.println("ELSE CORRECTO");
130-
}else{
105+
} else {
131106
tool.showError("Se esperaban ':'");
132107
}
133108
}
@@ -142,7 +117,6 @@ public boolean isWhileStatement() {
142117
if (tool.verifyToken(TokenType.DOS_PUNTOS)) {
143118
flag = true;
144119
tool.incrementIndex();
145-
// System.out.println("WHILE CORRECTO");
146120
} else {
147121
tool.showError("Se esperaban ':'");
148122
}
@@ -209,7 +183,6 @@ public boolean isForStatement() {
209183

210184
public boolean isMatchStatement() {
211185
boolean flag = false;
212-
// TO-DO
213186
if (tool.verifyToken(TokenType.MATCH)) {
214187
// int matchCol = tool.getCurrentToken().getColumn();
215188
tool.incrementIndex();
@@ -247,6 +220,28 @@ public boolean isCaseStatement() {
247220
return flag;
248221
}
249222

223+
// public boolean isFunctionDef() {
224+
// boolean flag = false;
225+
// // TO-DO
226+
// if (tool.verifyToken(TokenType.DEF)) {
227+
// tool.incrementIndex();
228+
// if (tool.verifyToken(TokenType.IDENTIFICADOR_FUNCION)) {
229+
// tool.incrementIndex();
230+
// if (tool.verifyToken(TokenType.PARENTESIS_APERTURA)) {
231+
// tool.incrementIndex();
232+
// if (tool.verifyToken(TokenType.PARENTESIS_CIERRE)) {
233+
// tool.incrementIndex();
234+
// if (tool.verifyToken(TokenType.DOS_PUNTOS)) {
235+
// tool.incrementIndex();
236+
// flag = true;
237+
// }
238+
// }
239+
// }
240+
// }
241+
// }
242+
// return flag;
243+
// }
244+
250245
// public boolean isTryStatement() {
251246
// boolean flag = false;
252247
// if (tool.verifyToken(TokenType.TRY)) {
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package ParserOperations;
2+
3+
import java.util.ArrayList;
4+
import java.util.Stack;
5+
6+
import Tokens.Token;
7+
8+
public class IndentationParser {
9+
private Stack<Integer> indentStack = new Stack<>();
10+
private static ArrayList<Token> listIndent;
11+
private int classCount = 0, expectedIndent = 1;
12+
private Utilities tool;
13+
14+
public IndentationParser(ArrayList<Token> list) {
15+
tool = new Utilities();
16+
IndentationParser.listIndent = list;
17+
}
18+
19+
protected void checkIndent() {
20+
int ifColumn = 0;
21+
for (int i = 0; i < listIndent.size(); i++) {
22+
Token token = listIndent.get(i);
23+
System.out.println(
24+
"Row: " + token.getRow() + " | Col: " + token.getColumn() + " | Exp.Indent: " + expectedIndent
25+
+ " | " + token.getLexeme());
26+
switch (token.getToken()) {
27+
case CLASS:
28+
checkClassDeclaration(token, classCount);
29+
classCount++;
30+
break;
31+
case WHILE:
32+
case FOR:
33+
checkIndentation(token);
34+
indentStack.push(expectedIndent);
35+
expectedIndent += 4;
36+
break;
37+
case IF:
38+
ifColumn = token.getColumn();
39+
checkIndentation(token);
40+
indentStack.push(expectedIndent);
41+
expectedIndent += 4;
42+
break;
43+
case ELSE:
44+
case ELIF:
45+
checkElseOrElif(token, ifColumn);
46+
break;
47+
case MATCH:
48+
case CASE:
49+
checkIndentation(token);
50+
indentStack.push(expectedIndent);
51+
expectedIndent += 4;
52+
break;
53+
case IDENTIFICADOR:
54+
checkIndentation(token);
55+
break;
56+
case PRINT:
57+
checkIndentation(token);
58+
break;
59+
}
60+
}
61+
}
62+
63+
private void checkIndentation(Token token) {
64+
if (!indentStack.isEmpty() && token.getColumn() < expectedIndent) {
65+
// Realizar dedentación
66+
expectedIndent = indentStack.pop();
67+
}
68+
69+
if (token.getColumn() != expectedIndent) {
70+
showIndentError(token);
71+
}
72+
}
73+
74+
private void checkClassDeclaration(Token token, int classCount) {
75+
if (classCount > 0) {
76+
tool.getConsole().setText(tool.getConsole().getText() +
77+
"Declaracion de clase no valida en la linea " + token.getRow()
78+
+ ". Ya se ha declarado una clase\n");
79+
} else if (token.getColumn() != 1) {
80+
tool.getConsole().setText(tool.getConsole().getText() +
81+
"Declaracion de clase incorrecta en la linea " + token.getRow() + "\n");
82+
} else {
83+
expectedIndent += 4;
84+
}
85+
}
86+
87+
private void checkElseOrElif(Token token, int ifColumn) {
88+
if (token.getColumn() != ifColumn && ifColumn == 0) {
89+
tool.getConsole().setText(tool.getConsole().getText() +
90+
"Sintaxis invalida, no se encontró 'IF' anterior a la linea " + token.getRow() + "\n");
91+
} else if (token.getColumn() != ifColumn) {
92+
showIndentError(token);
93+
}
94+
}
95+
96+
private void showIndentError(Token token) {
97+
// Mostrar un error de indentación en la consola
98+
tool.getConsole().setText(tool.getConsole().getText() +
99+
"Indentación no válida en la línea " + token.getRow() + "\n");
100+
}
101+
}

src/main/java/ParserOperations/Parser.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ public class Parser {
99
private Utilities tool;
1010
private CompoundStatements cs;
1111
private SimpleStatements ss;
12-
private static int currentLine;
12+
private IndentationParser ip;
13+
private ArrayList<Token> listIndent;
1314

1415
public Parser(ArrayList<Token> tokenList, JTextArea console) {
16+
listIndent = new ArrayList<>();
1517
tool = new Utilities();
1618
cs = new CompoundStatements();
1719
ss = new SimpleStatements();
18-
currentLine = 0;
20+
ip = new IndentationParser(listIndent);
1921
tool.setIndex(0);
2022
tool.setCurrentIndent(1);
2123
tool.setLine(1);
@@ -25,26 +27,25 @@ public Parser(ArrayList<Token> tokenList, JTextArea console) {
2527
}
2628

2729
public void parseCode() {
28-
System.out.println("\nEjecución");
30+
System.out.println("\nInicio Ejecución");
2931
statements();
32+
ip.checkIndent();
33+
System.out.println("Fin Ejecución");
3034
}
3135

3236
public void statements() {
33-
// System.out.println("INDENTACION -> " + tool.getCurrentIndent() + " - " +
34-
if (tool.getTokenList().get(tool.getTokenList().size() - 1).getRow() != tool.getCurrentToken().getRow()) {
37+
if (tool.getIndex() != tool.getTokenList().size() - 1) {
38+
listIndent.add(tool.getCurrentToken());
3539
if (cs.isCompoundStatement()) {
36-
System.out.println("Procesando declaración compuesta...");
37-
System.out.println(tool.getCurrentToken().getLexeme());
40+
// CompoundStatement correcto
3841
statements();
39-
// Aquí debes incluir la lógica que cambia el estado de cs
4042
} else if (ss.isSimpleStatement()) {
41-
System.out.println("Procesando declaración simple...");
42-
System.out.println(tool.getCurrentToken().getLexeme());
43+
// SimpleStatement correcto
4344
statements();
44-
// Aquí debes incluir la lógica que cambia el estado de ss
45-
} else {
46-
System.out.println("Fin de Ejecución");
4745
}
48-
}
46+
}
47+
// else {
48+
// System.out.println("Fin de Ejecución");
49+
// }
4950
}
5051
}

src/main/java/ParserOperations/Utilities.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ protected void showError(String errorMessage) {
1919
console.setText(console.getText() + errorMessage + " en la linea " + currentRow + "\n");
2020
}
2121

22+
protected boolean needIndent(Token tkn) {
23+
return tkn.getToken().equals(TokenType.CLASS)
24+
|| tkn.getToken().equals(TokenType.FOR)
25+
|| tkn.getToken().equals(TokenType.WHILE)
26+
|| tkn.getToken().equals(TokenType.MATCH)
27+
|| tkn.getToken().equals(TokenType.CASE)
28+
|| tkn.getToken().equals(TokenType.IF)
29+
|| tkn.getToken().equals(TokenType.ELIF)
30+
|| tkn.getToken().equals(TokenType.ELSE);
31+
}
32+
2233
protected boolean isAssignment(Token tkn) {
2334
return tkn.getToken().equals(TokenType.SUMA_ASIGNACION)
2435
|| tkn.getToken().equals(TokenType.RESTA_ASIGNACION)

0 commit comments

Comments
 (0)