Class: Twig::ExpressionParser::Prefix::Grouping

Inherits:
Twig::ExpressionParser::PrefixExpressionParser show all
Defined in:
lib/twig/expression_parser/prefix/grouping.rb

Instance Method Summary collapse

Methods inherited from Twig::ExpressionParser::PrefixExpressionParser

#type

Methods inherited from Base

#aliases, #to_s, #type

Instance Method Details

#descriptionObject



50
51
52
# File 'lib/twig/expression_parser/prefix/grouping.rb', line 50

def description
  'Explicit group expression (a)'
end

#nameObject



46
47
48
# File 'lib/twig/expression_parser/prefix/grouping.rb', line 46

def name
  '('
end

#parse(parser, token) ⇒ Object



7
8
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
# File 'lib/twig/expression_parser/prefix/grouping.rb', line 7

def parse(parser, token)
  stream = parser.stream
  expr = parser.parse_expression(precedence)

  if stream.next_if(Token::PUNCTUATION_TYPE, ')')
    unless stream.test(Token::OPERATOR_TYPE, '=>')
      return expr.set_explicit_parentheses
    end

    return Node::Expression::Array.new(AutoHash.new.add(expr), token.lineno)
  end

  # determine if we are parsing arrow function arguments
  unless stream.test(Token::PUNCTUATION_TYPE, ',')
    stream.expect(Token::PUNCTUATION_TYPE, ')', 'An opened parenthesis is not properly closed.')
  end

  names = AutoHash.new.add(expr)
  loop do
    if stream.next_if(Token::PUNCTUATION_TYPE, ')')
      break
    end

    stream.expect(Token::PUNCTUATION_TYPE, ',')
    token = stream.expect(Token::NAME_TYPE)
    names << Node::Expression::Variable::Context.new(token.value, token.lineno)
  end

  unless stream.test(Token::OPERATOR_TYPE, '=>')
    raise Error::Syntax.new(
      'A list of variables must be followed by an arrow.',
      stream.current.lineno,
      stream.source
    )
  end

  Node::Expression::Array.new(names, token.lineno)
end

#precedenceObject



54
55
56
# File 'lib/twig/expression_parser/prefix/grouping.rb', line 54

def precedence
  0
end