Class: Twig::ExpressionParser::Infix::Dot

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



66
67
68
# File 'lib/twig/expression_parser/infix/dot.rb', line 66

def associativity
  LEFT
end

#descriptionObject



58
59
60
# File 'lib/twig/expression_parser/infix/dot.rb', line 58

def description
  'Get an attribute on a variable'
end

#nameObject



54
55
56
# File 'lib/twig/expression_parser/infix/dot.rb', line 54

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
48
49
50
51
52
# File 'lib/twig/expression_parser/infix/dot.rb', line 9

def parse(parser, left, _token)
  stream = parser.stream
  token = stream.current
  lineno = token.lineno
  arguments = Node::Expression::Array.new(AutoHash.new, lineno)
  type = Template::ANY_CALL

  if stream.next_if(Token::OPERATOR_TYPE, '(')
    attribute = parser.parse_expression
    stream.expect(Token::PUNCTUATION_TYPE, ')')
  else
    token = stream.next

    if [Token::NAME_TYPE, Token::NUMBER_TYPE].include?(token.type) ||
       (token.type == Token::OPERATOR_TYPE && token.value.match(/\A#{Lexer::REGEX_NAME}/))
      attribute = Node::Expression::Constant.new(token.value, token.lineno)
    else
      raise Error::Syntax.new(
        "Expected name or number, got value \"#{token.value}\" of type #{token.type}.",
        token.lineno,
        stream.source
      )
    end
  end

  if stream.test(Token::OPERATOR_TYPE, '(')
    type = Template::METHOD_CALL
    arguments = parse_callable_arguments(parser, token.lineno)
  end

  if left.is_a?(Node::Expression::Name) && (
    parser.imported_symbol(:template, left.attributes[:name]) ||
      (left.attributes[:name] == '_self' && attribute.is_a?(Node::Expression::Constant))
  )
    return Node::Expression::MacroReference.new(
      Node::Expression::Variable::Template.new(left.attributes[:name], left.lineno),
      "macro_#{attribute.attributes[:value]}",
      arguments,
      left.lineno
    )
  end

  Node::Expression::GetAttribute.new(left, attribute, arguments, type, token.lineno)
end

#precedenceObject



62
63
64
# File 'lib/twig/expression_parser/infix/dot.rb', line 62

def precedence
  512
end