Class: Twig::ExpressionParser::Infix::Function

Inherits:
Twig::ExpressionParser::InfixExpressionParser show all
Includes:
ParsesArguments
Defined in:
lib/twig/expression_parser/infix/function.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



61
62
63
# File 'lib/twig/expression_parser/infix/function.rb', line 61

def associativity
  LEFT
end

#descriptionObject



53
54
55
# File 'lib/twig/expression_parser/infix/function.rb', line 53

def description
  'Twig function call'
end

#nameObject



49
50
51
# File 'lib/twig/expression_parser/infix/function.rb', line 49

def name
  '('
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/twig/expression_parser/infix/function.rb', line 9

def parse(parser, left, token)
  line = token.lineno

  unless left.is_a?(Node::Expression::Variable::Context)
    raise Error::Syntax.new(
      "Function name must be an identifier. got #{left.inspect}",
      line,
      parser.stream.source
    )
  end

  name = left.attributes[:name]

  if (aliased = parser.imported_symbol(:function, name))
    return Node::Expression::MacroReference.new(
      aliased[:node].nodes[:var],
      aliased[:name],
      parse_callable_arguments(parser, line, parse_open_parenthesis: false),
      line
    )
  end

  args = parse_named_arguments(parser, parse_open_parenthesis: false)
  function = parser.function(name, args, line)

  # Helper method returned
  if function.is_a?(Node::Expression::Base)
    return function
  end

  if (callable = function.parser_callable)
    fake_node = Node::Empty.new(line)
    fake_node.source_context = parser.stream.source

    return callable.call(parser, fake_node, args, line)
  end

  function.node_class.new(function, args, line)
end

#precedenceObject



57
58
59
# File 'lib/twig/expression_parser/infix/function.rb', line 57

def precedence
  512
end