Package com.stuffwithstuff.bantam.expressions

Examples of com.stuffwithstuff.bantam.expressions.Expression


/**
* Parses parentheses used to group an expression, like "a * (b + c)".
*/
public class GroupParselet implements PrefixParselet {
  public Expression parse(Parser parser, Token token) {
    Expression expression = parser.parseExpression();
    parser.consume(TokenType.RIGHT_PAREN);
    return expression;
  }
View Full Code Here


  public Expression parse(Parser parser, Token token) {
    // To handle right-associative operators like "^", we allow a slightly
    // lower precedence when parsing the right-hand side. This will let a
    // parselet with the same precedence appear on the right, which will then
    // take *this* parselet's result as its left-hand argument.
    Expression right = parser.parseExpression(mPrecedence);
   
    return new PrefixExpression(token.getType(), right);
  }
View Full Code Here

/**
* Parselet for the condition or "ternary" operator, like "a ? b : c".
*/
public class ConditionalParselet implements InfixParselet {
  public Expression parse(Parser parser, Expression left, Token token) {
    Expression thenArm = parser.parseExpression();
    parser.consume(TokenType.COLON);
    Expression elseArm = parser.parseExpression(Precedence.CONDITIONAL - 1);
   
    return new ConditionalExpression(left, thenArm, elseArm);
  }
View Full Code Here

* expression must be a simple name like "a", and expressions are
* right-associative. (In other words, "a = b = c" is parsed as "a = (b = c)").
*/
public class AssignParselet implements InfixParselet {
  public Expression parse(Parser parser, Expression left, Token token) {
    Expression right = parser.parseExpression(Precedence.ASSIGNMENT - 1);
   
    if (!(left instanceof NameExpression)) throw new ParseException(
        "The left-hand side of an assignment must be a name.");
   
    String name = ((NameExpression)left).getName();
View Full Code Here

  public Expression parse(Parser parser, Expression left, Token token) {
    // To handle right-associative operators like "^", we allow a slightly
    // lower precedence when parsing the right-hand side. This will let a
    // parselet with the same precedence appear on the right, which will then
    // take *this* parselet's result as its left-hand argument.
    Expression right = parser.parseExpression(
        mPrecedence - (mIsRight ? 1 : 0));
   
    return new OperatorExpression(left, token.getType(), right);
  }
View Full Code Here

    PrefixParselet prefix = mPrefixParselets.get(token.getType());
   
    if (prefix == null) throw new ParseException("Could not parse \"" +
        token.getText() + "\".");
   
    Expression left = prefix.parse(this, token);
   
    while (precedence < getPrecedence()) {
      token = consume();
     
      InfixParselet infix = mInfixParselets.get(token.getType());
View Full Code Here

  public static void test(String source, String expected) {
    Lexer lexer = new Lexer(source);
    Parser parser = new BantamParser(lexer);
   
    try {
      Expression result = parser.parseExpression();
      StringBuilder builder = new StringBuilder();
      result.print(builder);
      String actual = builder.toString();
     
      if (expected.equals(actual)) {
        sPassed++;
      } else {
View Full Code Here

TOP

Related Classes of com.stuffwithstuff.bantam.expressions.Expression

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.