Exemplo n.º 1
0
  private Type type(SymbolTable sym) throws ParseException {
    start("type");
    Type result = null;

    if (lex.isIdentifier()) {
      result = sym.lookupType(lex.tokenText());
      lex.nextLex(); // we are a terminal, so call nextLex to setup next state
    } else if (lex.match("^")) {
      lex.nextLex();
      result = new PointerType(type(sym));
    } else if (lex.match("[")) {
      lex.nextLex();
      if (lex.tokenCategory() != Lexer.intToken)
        throw new ParseException(32); // expected an integer
      int lower = (new Integer(lex.tokenText()).intValue());
      lex.nextLex();

      if (!lex.match(":")) throw new ParseException(19); // needed to see :
      lex.nextLex();

      if (lex.tokenCategory() != Lexer.intToken)
        throw new ParseException(32); // expected an integer
      int upper = (new Integer(lex.tokenText()).intValue());
      lex.nextLex();

      if (!lex.match("]")) throw new ParseException(24); // should have seen ]
      lex.nextLex();

      result = sym.lookupType(lex.tokenText());
      // type(sym); //might use this instead of nextLex();
      // gotta advance so we are at next token
      lex.nextLex();
      result = new ArrayType(lower, upper, result);
    } else {
      throw new ParseException(30); // we expected a type name
    }
    stop("type");
    return result;
  }