11package ParserOperations ;
22
3+ import Tokens .Constants ;
34import Tokens .Token ;
45import Tokens .TokenType ;
56
67import java .util .ArrayList ;
8+ import java .util .Map ;
9+
710import javax .swing .JTextArea ;
811
912public class Parser {
@@ -29,49 +32,60 @@ public Parser(ArrayList<Token> tokenList, JTextArea console) {
2932 }
3033
3134 public void parseCode () {
32- System .out .println ("\n Inicio Ejecución" );
35+ // System.out.println("\nInicio Ejecución");
3336 statements ();
3437 ip .checkIndent ();
35- System .out .println ("Fin Ejecución" );
38+ // System.out.println("Fin Ejecución");
3639 }
3740
3841 public void statements () {
39- if (tool .getIndex () != tool .getTokenList ().size () - 1 ) {
40- // System.out.println(tool.getCurrentToken());
41- listIndent .add (tool .getCurrentToken ());
42- if (cs .isCompoundStatement ()) {
43- // CompoundStatement correcto
44- if (tool .getCurrentToken ().getRow () != 1
45- && tool .getTokenList ().get (tool .getIndex () - 1 ).getRow () != tool .getCurrentToken ().getRow ()) {
46- statements ();
47- } else {
48- if (tool .getCurrentToken ().getRow () != tool .getTokenList ().get (tool .getTokenList ().size () - 1 )
49- .getRow ()) {
50- tool .showError ("Se esperaba un salto de linea" );
51- }
52- }
5342
54- } else if (ss .isSimpleStatement ()) {
55- // SimpleStatement correcto
56- if (tool .getCurrentToken ().getRow () != 1
57- && tool .getTokenList ().get (tool .getIndex () - 1 ).getRow () != tool .getCurrentToken ().getRow ()) {
58- statements ();
59- } else {
60- if (tool .getCurrentToken ().getRow () != tool .getTokenList ().get (tool .getTokenList ().size () - 1 )
61- .getRow ()) {
62- tool .showError ("Se esperaba un salto de linea" );
63- }
43+ // System.out.println(tool.getCurrentToken());
44+ listIndent .add (tool .getCurrentToken ());
45+ if (cs .isCompoundStatement ()) {
46+ // CompoundStatement correcto
47+ if (tool .getCurrentToken ().getRow () != 1
48+ && tool .getTokenList ().get (tool .getIndex () - 1 ).getRow () != tool .getCurrentToken ().getRow ()) {
49+ statements ();
50+ } else {
51+ if (tool .verifyToken (TokenType .DESCONOCIDO )) {
52+ tool .showError ("No se reconoce '" + tool .getCurrentToken ().getLexeme () + "'" );
53+ } else if (tool .getCurrentToken ().getRow () != tool .getTokenList ().get (tool .getTokenList ().size () - 1 )
54+ .getRow ()) {
55+ tool .showError ("Se esperaba un salto de linea" );
6456 }
57+ }
6558
66- } else if (tool .verifyToken (TokenType .DESCONOCIDO )) {
67- tool .showError ("No se reconoce '" + tool .getCurrentToken ().getLexeme () + "'" );
68- } else if (tool .isAssignment (tool .getCurrentToken ()) || tool .isOperator (tool .getCurrentToken ())
69- || tool .isRelationalOperator (tool .getCurrentToken ())) {
70- tool .showError ("Se encontró '" + tool .getCurrentToken ().getLexeme () + "' fuera de lugar" );
71- } else if (tool .verifyToken (TokenType .DOS_PUNTOS )) {
72- tool .showError ("Se esperaba instruccion antes de ':'" );
59+ } else if (ss .isSimpleStatement ()) {
60+ // SimpleStatement correcto
61+ if (tool .getCurrentToken ().getRow () != 1
62+ && tool .getTokenList ().get (tool .getIndex () - 1 ).getRow () != tool .getCurrentToken ().getRow ()) {
63+ statements ();
64+ } else {
65+ if (tool .verifyToken (TokenType .DESCONOCIDO )) {
66+ tool .showError ("No se reconoce '" + tool .getCurrentToken ().getLexeme () + "'" );
67+ } else if (tool .getCurrentToken ().getRow () != tool .getTokenList ().get (tool .getTokenList ().size () - 1 )
68+ .getRow ()) {
69+ tool .showError ("Se esperaba un salto de linea" );
70+ }
7371 }
7472
73+ } else if (tool .verifyToken (TokenType .DESCONOCIDO )) {
74+ tool .showError ("No se reconoce '" + tool .getCurrentToken ().getLexeme () + "'" );
75+ } else if (!tool .verifyToken (TokenType .PUNTO_Y_COMA )) {
76+ checkTokensUnfound (Constants .DELIMITERS );
77+ checkTokensUnfound (Constants .OPERATORS );
78+ checkTokensUnfound (Constants .RESERVED_WORDS );
79+ }
80+ }
81+
82+ private void checkTokensUnfound (Map <String , TokenType > tokenMap ) {
83+ for (TokenType tkn : tokenMap .values ()) {
84+ if (tkn == tool .getCurrentToken ().getToken ()) {
85+ tool .showError ("Se encontró '" + tool .getCurrentToken ().getLexeme ()
86+ + "' fuera de un bloque de codigo" );
87+ tool .incrementIndex ();
88+ }
7589 }
7690 }
7791}
0 commit comments