Пример #1
0
 private void classDeclaration(SymbolTable sym) throws ParseException {
   start("classDeclaration");
   if (!lex.match("class")) throw new ParseException(5);
   lex.nextLex();
   if (!lex.isIdentifier()) throw new ParseException(27);
   lex.nextLex();
   classBody(sym);
   stop("classDeclaration");
 }
Пример #2
0
 private void reference(SymbolTable sym) throws ParseException {
   start("reference");
   if (!lex.isIdentifier()) {
     throw new ParseException(27); // expected an identifier
   }
   lex.nextLex();
   referencePrime(sym);
   stop("reference");
 }
Пример #3
0
 private void argumentList(SymbolTable sym) throws ParseException {
   start("argumentList");
   if (lex.isIdentifier()) {
     while (!lex.match(")")) {
       nameDeclaration(sym);
       if (lex.match(",")) lex.nextLex();
     }
   }
   stop("argumentList");
 }
Пример #4
0
 private void nameDeclaration(SymbolTable sym) throws ParseException {
   start("nameDeclaration");
   if (!lex.isIdentifier()) // TODO do we have to save the identifier?
   throw new ParseException(27);
   lex.nextLex();
   if (!lex.match(":")) throw new ParseException(19); // need a colon between identifier and type
   lex.nextLex();
   type(sym);
   stop("nameDeclaration");
 }
Пример #5
0
 private void functionDeclaration(SymbolTable sym) throws ParseException {
   start("functionDeclaration");
   if (!lex.match("function")) throw new ParseException(10);
   lex.nextLex();
   if (!lex.isIdentifier()) throw new ParseException(27);
   lex.nextLex();
   arguments(sym);
   returnType(sym);
   functionBody(sym);
   stop("functionDeclaration");
 }
Пример #6
0
 private void statement(SymbolTable sym) throws ParseException {
   start("statement");
   if (lex.match("return")) {
     returnStatement(sym);
   } else if (lex.match("if")) {
     ifStatement(sym);
   } else if (lex.match("while")) {
     whileStatement(sym);
   } else if (lex.match("begin")) {
     compoundStatement(sym);
   } else if (lex.isIdentifier()) {
     assignOrFunction(sym);
   } else {
     throw new ParseException(34); // expecting statement
   }
   stop("statement");
 }
Пример #7
0
 private void constantDeclaration(SymbolTable sym) throws ParseException {
   start("constantDeclaration");
   if (!lex.match("const")) throw new ParseException(6);
   lex.nextLex();
   if (!lex.isIdentifier()) throw new ParseException(27);
   lex.nextLex();
   if (!lex.match("="))
     throw new ParseException(
         20); // TODO: i think this is the wrong exception.  check againset buds using test100
   lex.nextLex();
   int tid = lex.tokenCategory();
   if (tid != Lexer.stringToken || tid != Lexer.realToken || tid != Lexer.intToken)
     throw new ParseException(31);
   lex.nextLex();
   if (sym.nameDefined(lex.tokenText())) throw new ParseException(35, lex.tokenText());
   // TODO enterConstant
   stop("constantDeclaration");
 }
Пример #8
0
 private void referencePrime(SymbolTable sym) throws ParseException {
   // TODO: should this call start and stop?  probably not...
   if (lex.match(".")) {
     lex.nextLex();
     if (!lex.isIdentifier()) {
       throw new ParseException(27);
     }
     lex.nextLex();
   } else if (lex.match("[")) {
     lex.nextLex();
     expression(sym);
     if (!lex.match("]")) {
       throw new ParseException(23); // expected left bracket
     }
     lex.nextLex();
   } else if (lex.match("^")) {
     // do nothing, since it's just a terminal
     lex.nextLex();
   }
 }
Пример #9
0
 private void parameterList(SymbolTable sym) throws ParseException {
   start("parameterList");
   int tid = lex.tokenCategory();
   if (lex.match("not")
       || lex.match("new")
       || lex.match("(")
       || lex.match("-")
       || lex.match("&")
       || lex.isIdentifier()
       || tid == Lexer.realToken
       || tid == Lexer.intToken
       || tid == Lexer.stringToken) {
     expression(sym);
     while (lex.match(",")) {
       lex.nextLex();
       expression(sym);
     }
   }
   stop("parameterList");
 }
Пример #10
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;
  }
Пример #11
0
  private void term(SymbolTable sym) throws ParseException {
    start("term");
    int tid = lex.tokenCategory();

    if (lex.match("(")) {
      lex.nextLex();
      expression(sym);
      if (!lex.match(")")) throw new ParseException(22);
      lex.nextLex();
    } else if (lex.match("not")) {
      lex.nextLex();
      term(sym);
    } else if (lex.match("new")) {
      lex.nextLex();
      type(sym);
    } else if (lex.match("-")) {
      lex.nextLex();
      term(sym);
    } else if (lex.match("&")) {
      lex.nextLex();
      reference(sym);
    } else if (lex.isIdentifier()) {
      reference(sym);
      if (lex.match("(")) {
        lex.nextLex();
        parameterList(sym);
        if (!lex.match(")")) throw new ParseException(22);
        lex.nextLex();
      }
    } else if (tid == Lexer.intToken || tid == Lexer.realToken || tid == Lexer.stringToken) {
      lex.nextLex();
    } else {
      throw new ParseException(33); // TODO: is this right?!?!
    }
    stop("term");
  }