Class: Twig::ExpressionParser::Infix::Is

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

Direct Known Subclasses

IsNot

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



47
48
49
# File 'lib/twig/expression_parser/infix/is.rb', line 47

def associativity
  LEFT
end

#descriptionObject



39
40
41
# File 'lib/twig/expression_parser/infix/is.rb', line 39

def description
  'Twig tests'
end

#nameObject



35
36
37
# File 'lib/twig/expression_parser/infix/is.rb', line 35

def name
  'is'
end

#parse(parser, left, token) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/twig/expression_parser/infix/is.rb', line 9

def parse(parser, left, token)
  stream = parser.stream
  test = parser.test(left.lineno)

  arguments = nil
  if stream.test(Token::OPERATOR_TYPE, '(')
    arguments = parse_named_arguments(parser)
  elsif test.one_mandatory_argument?
    arguments = Node::Nodes.new(AutoHash.new.add(
      parser.parse_expression(precedence)
    ))
  end

  if test.name == 'defined' && left.is_a?(Node::Expression::Variable::Context) &&
     !(aliased = parser.imported_symbol(:function, left.attributes[:name])).nil?
    left = Node::Expression::MacroReference.new(
      aliased[:node].nodes[:var],
      aliased[:name],
      Node::Expression::Array.new(AutoHash.new, left.lineno),
      left.lineno
    )
  end

  test.node_class.new(left, test, arguments, parser.current_token.lineno)
end

#precedenceObject



43
44
45
# File 'lib/twig/expression_parser/infix/is.rb', line 43

def precedence
  100
end