Пример #1
0
  private Factor parseFactor() throws CompileException {
    boolean isNegative = false;

    while (true) {
      switch (currentToken.getTokenKind()) {
        case TERMOPERATOR:
          if (currentToken.getSpelling().equals("-")) {
            acceptIt();
            isNegative = true;
            break;
          } else
            throw new CompileException(
                ErrorType.UNEXPECTED_TOKEN,
                currentToken.getPosition(),
                "Unexpected token: " + currentToken.getSpelling());

        case LPAREN:
          acceptIt();
          ArithmeticExpression arithmeticExpression = parseArithmeticExpression();
          accept(TokenKind.RPAREN);
          return new Factor(arithmeticExpression, isNegative);

        case INTLITERAL:
          return new Factor(parseNumber(), isNegative);

        case IDENTIFIER:
          Identifier identifierArea = parseIdentifier();
          if (currentToken.getTokenKind() == TokenKind.DOT) {
            acceptIt();
            return new Factor(identifierArea, parseIdentifier(), parseParameters(), isNegative);
          } else {
            return new Factor(identifierArea, isNegative);
          }

        default:
          throw new CompileException(
              ErrorType.UNEXPECTED_TOKEN,
              currentToken.getPosition(),
              "Wrong token, in parseFactor: " + currentToken.getTokenKind());
      }
    }
  }
Пример #2
0
  private ExpressionBlock parseExpressionBlock() throws CompileException {
    boolean isNegative = false;

    while (true) {
      switch (currentToken.getTokenKind()) {
        case TERMOPERATOR:
          if (currentToken.getSpelling().equals("-")) {
            acceptIt();
            isNegative = true;
            break;
          } else
            throw new CompileException(
                ErrorType.UNEXPECTED_TOKEN,
                currentToken.getPosition(),
                "Unexpected token: " + currentToken.getSpelling());

        case IDENTIFIER:
          Identifier identifier = parseIdentifier();
          if (currentToken.getTokenKind() == TokenKind.DOT) {
            acceptIt();
            return new ExpressionBlock(
                identifier, parseIdentifier(), parseParameters(), isNegative);
          } else return new ExpressionBlock(identifier, isNegative);
        case STRINGLITERAL:
        case INTLITERAL:
        case BOOLLITERAL:
          return new ExpressionBlock(parseLiteral(), isNegative);
        case LPAREN:
          acceptIt();
          Expression expression = parseExpression();
          accept(TokenKind.RPAREN);

          return new ExpressionBlock(expression, isNegative);
        default:
          throw new CompileException(
              ErrorType.UNEXPECTED_TOKEN,
              currentToken.getPosition(),
              "Wrong token, in parseExpressionBlock: " + currentToken.getTokenKind());
      }
    }
  }
Пример #3
0
 private Literal parseLiteral() throws CompileException {
   switch (currentToken.getTokenKind()) {
     case TERMOPERATOR:
       if (currentToken.getSpelling().equals("-")) {
         acceptIt();
         return new Literal(parseNumber());
       }
     case INTLITERAL:
       return new Literal(parseNumber());
     case STRINGLITERAL:
       return new Literal(parseString());
     case BOOLLITERAL:
       return new Literal(parseBoolean());
     default:
       throw new CompileException(
           ErrorType.UNEXPECTED_TOKEN,
           currentToken.getPosition(),
           "Wrong token, in parseLiteral, got " + currentToken.getTokenKind());
   }
 }