Пример #1
0
 private List<Expression> parseExpressionList() {
   List<Expression> expressions = new ArrayList<Expression>();
   do {
     Expression expression = parseExpression();
     expressions.add(expression);
   } while (myLexer.nextToken() == TokenType.COMMA);
   myLexer.pushBack();
   return expressions;
 }
Пример #2
0
 private void tokenExpected(TokenType type, String text) {
   myLexer.nextToken();
   if (type != myLexer.getTokenType()) {
     throw new ParsingException(type.getName() + " expected but " + myLexer.getToken() + " found");
   }
   if (text != null && !text.equals(myLexer.getToken())) {
     throw new ParsingException(text + " expected but " + myLexer.getToken() + " found");
   }
 }
Пример #3
0
 private BooleanExpression parseBooleanExpression() {
   Expression left = parseExpression();
   TokenType type = myLexer.nextToken();
   ComparisonOperation operation = BOOLEAN_OPERATIONS.get(type);
   if (operation == null) {
     throw new ParsingException(
         "'<', '>', '<=', '>=', '!=' or '==' expected but " + myLexer.getToken() + " found");
   }
   Expression right = parseExpression();
   return new ComparisonExpression(operation, left, right);
 }
Пример #4
0
 /** Provides a command line interface to the lexer */
 public static void main(String[] args) throws IOException {
   // check for legal argument list
   Lexer in;
   if (args.length == 0) {
     in = new Lexer();
   } else in = new Lexer(args[0]);
   do {
     Token t = in.readToken();
     if (t == null) break;
     System.out.println("Token " + t + " in " + t.getClass());
   } while (true);
 }
Пример #5
0
 private Expression parseMultExpression() {
   Expression expression = parsePrimaryExpression();
   TokenType type = myLexer.nextToken();
   while (type == TokenType.MULT || type == TokenType.DIV) {
     Expression next = parsePrimaryExpression();
     expression =
         new BinaryExpression(
             type == TokenType.MULT ? Operations.MULT : Operations.DIV, expression, next);
     type = myLexer.nextToken();
   }
   myLexer.pushBack();
   return expression;
 }
Пример #6
0
 private StatementsNode parseStatements(boolean insideGroup) {
   List<Statement> statements = new ArrayList<Statement>();
   do {
     Statement statement = parseStatement();
     statements.add(statement);
     TokenType type = myLexer.nextToken();
     myLexer.pushBack();
     if (type == TokenType.EOF || insideGroup && type == TokenType.RBRACE) {
       break;
     }
   } while (true);
   return new StatementsNode(statements);
 }
Пример #7
0
 private Statement parseIf() {
   tokenExpected(TokenType.LPAREN);
   BooleanExpression condition = parseBooleanExpression();
   tokenExpected(TokenType.RPAREN);
   Statement thenClause = parseStatement();
   Statement elseClause = null;
   if (myLexer.nextToken() == TokenType.IDENTIFIER && myLexer.getToken().equals("else")) {
     elseClause = parseStatement();
   } else {
     myLexer.pushBack();
   }
   return new IfStatement(condition, thenClause, elseClause);
 }
Пример #8
0
 private Expression parseAddExpression() {
   Expression expression = parseMultExpression();
   TokenType type = myLexer.nextToken();
   while (type == TokenType.PLUS || type == TokenType.MINUS) {
     Expression next = parseMultExpression();
     expression =
         new BinaryExpression(
             type == TokenType.PLUS ? Operations.ADD : Operations.SUB, expression, next);
     type = myLexer.nextToken();
   }
   myLexer.pushBack();
   return expression;
 }
Пример #9
0
 private void updateDisplay() {
   // first, set block colours
   textView.setBackground(getColour(backgroundColour("text-background-colour")));
   textView.setForeground(getColour(foregroundColour("text-foreground-colour")));
   textView.setCaretColor(getColour(foregroundColour("text-caret-colour")));
   problemsView.setBackground(getColour(backgroundColour("problems-background-colour")));
   problemsView.setForeground(getColour(foregroundColour("problems-foreground-colour")));
   consoleView.setBackground(getColour(backgroundColour("console-background-colour")));
   consoleView.setForeground(getColour(foregroundColour("console-foreground-colour")));
   // second, set colours on the code!
   java.util.List<Lexer.Token> tokens = Lexer.tokenise(textView.getText(), true);
   int pos = 0;
   for (Lexer.Token t : tokens) {
     int len = t.toString().length();
     if (t instanceof Lexer.RightBrace || t instanceof Lexer.LeftBrace) {
       highlightArea(pos, len, foregroundColour("text-brace-colour"));
     } else if (t instanceof Lexer.Strung) {
       highlightArea(pos, len, foregroundColour("text-string-colour"));
     } else if (t instanceof Lexer.Comment) {
       highlightArea(pos, len, foregroundColour("text-comment-colour"));
     } else if (t instanceof Lexer.Quote) {
       highlightArea(pos, len, foregroundColour("text-quote-colour"));
     } else if (t instanceof Lexer.Comma) {
       highlightArea(pos, len, foregroundColour("text-comma-colour"));
     } else if (t instanceof Lexer.Identifier) {
       highlightArea(pos, len, foregroundColour("text-identifier-colour"));
     } else if (t instanceof Lexer.Integer) {
       highlightArea(pos, len, foregroundColour("text-integer-colour"));
     }
     pos += len;
   }
 }
Пример #10
0
 private Expression parsePrimaryExpression() {
   TokenType type = myLexer.nextToken();
   if (type == TokenType.LPAREN) {
     Expression expression = parseExpression();
     tokenExpected(TokenType.RPAREN);
     return expression;
   }
   if (type == TokenType.IDENTIFIER) {
     String name = myLexer.getToken();
     if (myLexer.nextToken() == TokenType.LPAREN) {
       List<Expression> arguments = parseExpressionList();
       tokenExpected(TokenType.RPAREN);
       return new FunctionCallExpression(name, arguments);
     }
     myLexer.pushBack();
     return new VariableExpression(name);
   }
   if (type == TokenType.INT) {
     try {
       int value = Integer.parseInt(myLexer.getToken());
       return new IntegerLiteralExpression(value);
     } catch (NumberFormatException e) {
       throw new ParsingException(myLexer.getToken() + " is not an integer");
     }
   }
   throw new ParsingException(
       "'(' or number or identifier expected but " + myLexer.getToken() + " found");
 }
  public Object getNext() {
    // getNext() gets the next item on stack, pointed to by the stackPointer:
    // - if stack is empty, it gets a token from Lexer and returns it, setting SP = 1
    // **(in this case, the newly returned element is put on stack)
    // - if SP is pointing to an existing element of stack, that element is returned, setting
    // SP=SP+1
    //
    // - if SP is pointing right above stack, a new token from Lexer is returned, setting SP=SP+1
    // **(in this case, the newly returned element is put on stack)
    //
    // if SP points to something that is way above stack, it returns an EOFToken().

    if (stack.size() == 0) {
      // System.out.println("I read " + lexer.peekNextToken());
      if (lexer.hasMoreTokens()) {
        stack.add(lexer.getNextToken());
        stackPointer++;
        return stack.get(stackPointer - 1);
      } else return new EOFToken();
    } else if (stackPointer < stack.size()) {
      return stack.get(stackPointer++);
    } else if (stackPointer == stack.size()) // execute this case only if
    // stackpointer points to element right on top of stack, i.e. asks for a new element
    {
      if (lexer.hasMoreTokens()) {
        stack.add(lexer.getNextToken());
        return stack.get(stackPointer++);
      } else return new EOFToken();
    } else { // execute this case if stackpointer is point somewhere absurdly above the top of stack
      if (diag2)
        System.out.println(
            "**Diag warning: invalid stack ref: getNext(), SP="
                + stackPointer
                + " for a stack of size "
                + stack.size());
      return new EOFToken();
    }
  }
Пример #12
0
  private Statement parseStatement() {
    TokenType type = myLexer.nextToken();
    if (type == TokenType.IDENTIFIER) {
      String token = myLexer.getToken();
      if (token.equals("print")) {
        Expression expression = parseExpression();
        tokenExpected(TokenType.SEMICOLON);
        return new PrintStatement(expression);
      } else if (token.equals("return")) {
        Expression expression = parseExpression();
        tokenExpected(TokenType.SEMICOLON);
        return new ReturnStatement(expression);
      } else if (token.equals("fun")) {
        return parseFunctionDeclaration();
      } else if (token.equals("if")) {
        return parseIf();
      } else if (token.equals("while")) {
        return parseWhile();
      }
      if (myLexer.nextToken() == TokenType.ASSIGN) {
        Expression expression = parseExpression();
        tokenExpected(TokenType.SEMICOLON);
        return new AssignmentStatement(token, expression);
      }
      myLexer.pushBack();
      myLexer.pushBack(token, TokenType.IDENTIFIER);
    } else if (type == TokenType.LBRACE) {
      StatementsNode statement = parseStatements(true);
      tokenExpected(TokenType.RBRACE);
      return statement;
    } else {
      myLexer.pushBack();
    }

    Expression expression = parseExpression();
    tokenExpected(TokenType.SEMICOLON);
    return new ExpressionStatement(expression);
  }
Пример #13
-1
 private Statement parseFunctionDeclaration() {
   tokenExpected(TokenType.IDENTIFIER);
   String name = myLexer.getToken();
   tokenExpected(TokenType.LPAREN);
   List<String> parameters = new ArrayList<String>();
   do {
     tokenExpected(TokenType.IDENTIFIER);
     parameters.add(myLexer.getToken());
   } while (myLexer.nextToken() == TokenType.COMMA);
   myLexer.pushBack();
   tokenExpected(TokenType.RPAREN);
   Statement body = parseStatement();
   return new FunctionDeclaration(name, parameters, body);
 }