|
5 | 5 | */ |
6 | 6 | package correctorcodigodoc; |
7 | 7 |
|
| 8 | +import parser.ParserStatement; |
| 9 | +import parser.DeclarationStatement; |
| 10 | +import parser.NullStatement; |
| 11 | +import compiladorpseu.Tokens.Token; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | +import parser.Expression; |
| 15 | +import parser.Identifier; |
| 16 | + |
8 | 17 | /** |
9 | 18 | * |
10 | 19 | * @author Sebastian |
11 | 20 | */ |
12 | 21 | public class Parser { |
| 22 | + public static class ParseException extends Exception { |
| 23 | + public String message; |
| 24 | + |
| 25 | + public ParseException(String what) { |
| 26 | + this.message = what; |
| 27 | + } |
| 28 | + } |
| 29 | + |
13 | 30 | private Lexer lexer; |
| 31 | + private Token current_token; |
14 | 32 |
|
15 | 33 | public Parser(Lexer lexer) { |
16 | 34 | this.lexer = lexer; |
| 35 | + this.current_token = lexer.nextToken(); |
| 36 | + } |
| 37 | + |
| 38 | + public ParserStatement parseStatement() throws ParseException { |
| 39 | + if (current_token == null) |
| 40 | + return null; |
| 41 | + |
| 42 | + Token next_token = lexer.nextToken(); |
| 43 | + ParserStatement statement = null; |
| 44 | + |
| 45 | + switch (current_token.id) { |
| 46 | + case INICIO: // Nada que hacer |
| 47 | + case PROC: // Nada que hacer |
| 48 | + case FIN: // Nada que hacer |
| 49 | + case NUEVA_LINEA: // Nada que hacer |
| 50 | + case DOS_PUNTOS: |
| 51 | + statement = new NullStatement(); |
| 52 | + break; |
| 53 | + |
| 54 | + case TIPO_DATO: |
| 55 | + // El siguiente token debe ser un identificador o una lista |
| 56 | + List<Identifier> tokens = parseIdentifierList(next_token); |
| 57 | + if (tokens.isEmpty()) { |
| 58 | + throw new ParseException("Error de sintaxis en la lista de identificadores"); |
| 59 | + } |
| 60 | + statement = new DeclarationStatement(tokens); |
| 61 | + |
| 62 | + // Ya nos comimos el último token, por tanto tenemos que retroceder una posición |
| 63 | + next_token = lexer.prevToken(); |
| 64 | + break; |
| 65 | + } |
| 66 | + |
| 67 | + current_token = next_token; |
| 68 | + return statement; |
| 69 | + } |
| 70 | + |
| 71 | + /* |
| 72 | + * Parsea una lista de identificadores de la forma I1,I2,I3,I4...In |
| 73 | + * Tira una excepción si la lista está malformada |
| 74 | + * @param token_inicial El token que inicia la lista |
| 75 | + * @returns Lista de identificadores parseada |
| 76 | + */ |
| 77 | + public List<Identifier> parseIdentifierList(Token token_inicial) throws ParseException { |
| 78 | + List<Identifier> ret = new ArrayList<Identifier>(); |
| 79 | + |
| 80 | + Token token = token_inicial; |
| 81 | + |
| 82 | + while (token.id == Token.Ids.IDENTIFIER) { |
| 83 | + ret.add(parseIdentifier(token)); |
| 84 | + |
| 85 | + token = lexer.nextToken(); |
| 86 | + |
| 87 | + // La lista termina con una nueva linea |
| 88 | + if (token.id == Token.Ids.NUEVA_LINEA) |
| 89 | + break; |
| 90 | + |
| 91 | + // Si lo que sigue no es una coma, tenemos un error de sintaxis |
| 92 | + if (token.id != Token.Ids.COMA) { |
| 93 | + throw new ParseException("Error de sintaxis en la lista de identificadores"); |
| 94 | + } |
| 95 | + |
| 96 | + token = lexer.nextToken(); |
| 97 | + } |
| 98 | + |
| 99 | + return ret; |
| 100 | + } |
| 101 | + |
| 102 | + /* |
| 103 | + * Parsea un identifier a partir de un token |
| 104 | + * el identifier puede ser un array o una variable normal |
| 105 | + * @param token_inicial El token por el que comienza el identifier |
| 106 | + * @returns Identifier parseado |
| 107 | + */ |
| 108 | + public Identifier parseIdentifier(Token token_inicial) throws ParseException { |
| 109 | + Token next_token = lexer.peekToken(); |
| 110 | + |
| 111 | + if (next_token.id == Token.Ids.PARENTESIS_ABRE) { |
| 112 | + // El identificador es un arreglo, parsea los parametros hasta el ( |
| 113 | + // Comete el ( |
| 114 | + lexer.nextToken(); |
| 115 | + List<Expression> parametros = parseExpressionList(lexer.nextToken(), Token.Ids.PARENTESIS_CIERRA); |
| 116 | + if (parametros.isEmpty()) { |
| 117 | + throw new ParseException("Error de sintaxis en la lista de identificadores"); |
| 118 | + } |
| 119 | + return new Identifier(parametros); |
| 120 | + } |
| 121 | + |
| 122 | + return new Identifier(null); |
| 123 | + } |
| 124 | + |
| 125 | + public List<Expression> parseExpressionList(Token token_inicial, Token.Ids terminador) { |
| 126 | + return null; |
| 127 | + } |
| 128 | + |
| 129 | + public Expression parseExpression() { |
| 130 | + return null; |
17 | 131 | } |
18 | 132 | } |
0 commit comments