Class: Twig::ExpressionParser::Infix::ConditionalTernary

Inherits:
Twig::ExpressionParser::InfixExpressionParser show all
Defined in:
lib/twig/expression_parser/infix/conditional_ternary.rb

Constant Summary

Constants inherited from Twig::ExpressionParser::InfixExpressionParser

Twig::ExpressionParser::InfixExpressionParser::LEFT, Twig::ExpressionParser::InfixExpressionParser::RIGHT

Instance Method Summary collapse

Methods inherited from Twig::ExpressionParser::InfixExpressionParser

#left?, #right?, #type

Methods inherited from Base

#aliases, #to_s, #type

Instance Method Details

#associativityObject



33
34
35
# File 'lib/twig/expression_parser/infix/conditional_ternary.rb', line 33

def associativity
  LEFT
end

#descriptionObject



25
26
27
# File 'lib/twig/expression_parser/infix/conditional_ternary.rb', line 25

def description
  'Conditional operator (a ? b : c)'
end

#nameObject



21
22
23
# File 'lib/twig/expression_parser/infix/conditional_ternary.rb', line 21

def name
  '?'
end

#parse(parser, left, token) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/twig/expression_parser/infix/conditional_ternary.rb', line 7

def parse(parser, left, token)
  then_expr = parser.parse_expression(precedence)

  else_expr = if parser.stream.next_if(Token::PUNCTUATION_TYPE, ':')
                # Ternary operator (expr ? expr2 : expr3)
                parser.parse_expression(precedence)
              else
                # Ternary without else (expr ? expr2)
                Node::Expression::Constant.new('', token.lineno)
              end

  Node::Expression::Ternary.new(left, then_expr, else_expr, token.lineno)
end

#precedenceObject



29
30
31
# File 'lib/twig/expression_parser/infix/conditional_ternary.rb', line 29

def precedence
  0
end