Package com.stuffwithstuff.bantam.parselets

Source Code of com.stuffwithstuff.bantam.parselets.ConditionalParselet

package com.stuffwithstuff.bantam.parselets;

import com.stuffwithstuff.bantam.Parser;
import com.stuffwithstuff.bantam.Precedence;
import com.stuffwithstuff.bantam.Token;
import com.stuffwithstuff.bantam.TokenType;
import com.stuffwithstuff.bantam.expressions.ConditionalExpression;
import com.stuffwithstuff.bantam.expressions.Expression;

/**
* 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);
  }

  public int getPrecedence() {
    return Precedence.CONDITIONAL;
  }
}
TOP

Related Classes of com.stuffwithstuff.bantam.parselets.ConditionalParselet

TOP
Copyright © 2018 www.massapi.com. 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.