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
|