Class: Twig::ExpressionParser::Infix::SquareBracket

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



60
61
62
# File 'lib/twig/expression_parser/infix/square_bracket.rb', line 60

def associativity
  LEFT
end

#descriptionObject



52
53
54
# File 'lib/twig/expression_parser/infix/square_bracket.rb', line 52

def description
  'Array access'
end

#nameObject



48
49
50
# File 'lib/twig/expression_parser/infix/square_bracket.rb', line 48

def name
  '['
end

#parse(parser, left, 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
45
46
# File 'lib/twig/expression_parser/infix/square_bracket.rb', line 7

def parse(parser, left, token)
  stream = parser.stream
  lineno = token.lineno
  arguments = Node::Expression::Array.new({}, lineno)

  slice = false
  if stream.test(Token::PUNCTUATION_TYPE, ':')
    slice = true
    attribute = Node::Expression::Constant.new(0, token.lineno)
  else
    attribute = parser.parse_expression
  end

  if stream.next_if(Token::PUNCTUATION_TYPE, ':')
    slice = true
  end

  if slice || stream.test(Token::SYMBOL_TYPE)
    length = if stream.test(Token::PUNCTUATION_TYPE, ']')
               Node::Expression::Constant.new(nil, token.lineno)
             elsif stream.test(Token::SYMBOL_TYPE)
               token = stream.next
               Node::Expression::Variable::Context.new(token.value, token.lineno)
             else
               parser.parse_expression
             end

    filter = parser.filter('slice', token.lineno)
    arguments = Node::Nodes.new(AutoHash.new.add(attribute, length))
    filter = filter.node_class.new(left, filter, arguments, token.lineno)

    stream.expect(Token::PUNCTUATION_TYPE, ']')

    return filter
  end

  stream.expect(Token::PUNCTUATION_TYPE, ']')

  Node::Expression::GetAttribute.new(left, attribute, arguments, Template::ARRAY_CALL, lineno)
end

#precedenceObject



56
57
58
# File 'lib/twig/expression_parser/infix/square_bracket.rb', line 56

def precedence
  512
end