Class: Twig::ExpressionParser::Prefix::Literal

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

Instance Method Summary collapse

Methods inherited from Twig::ExpressionParser::PrefixExpressionParser

#type

Methods inherited from Base

#aliases, #to_s, #type

Instance Method Details

#nameObject



97
98
99
# File 'lib/twig/expression_parser/prefix/literal.rb', line 97

def name
  @type || :literal
end

#parse(parser, token) ⇒ Object

Raises:



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/twig/expression_parser/prefix/literal.rb', line 7

def parse(parser, token)
  case token.type
  when Token::SYMBOL_TYPE
    parser.stream.next
    @type = :constant

    return Node::Expression::Constant.new(token.value.to_sym, token.lineno)
  when Token::CLASS_VAR_TYPE
    parser.stream.next
    @type = :variable

    return Node::Expression::Variable::Context.new(token.value, token.lineno)
  when Token::NAME_TYPE
    parser.stream.next

    # We can adjust for a ternary with no space here like a? b : c, we should however ignore function
    # nodes because a?() is fine
    token_value = token.value
    if token.value.end_with?('?') && parser.current_token.value != '('
      parser.stream.inject([Token.new(Token::OPERATOR_TYPE, '?', token.lineno)])
      token_value = token.value.chomp('?')
    end

    case token_value
    when 'true', 'TRUE'
      @type = :constant

      return Node::Expression::Constant.new(true, token.lineno)
    when 'false', 'FALSE'
      @type = :constant

      return Node::Expression::Constant.new(false, token.lineno)
    when 'null', 'NULL', 'nil', 'none', 'NONE'
      @type = :constant

      return Node::Expression::Constant.new(nil, token.lineno)
    else
      @type = :variable

      return Node::Expression::Variable::Context.new(token_value, token.lineno)
    end
  when Token::NUMBER_TYPE
    parser.stream.next
    @type = :constant

    return Node::Expression::Constant.new(token.value, token.lineno)
  when Token::STRING_TYPE, Token::INTERPOLATION_START_TYPE
    @type = :string

    return parse_string_expression(parser)
  when Token::PUNCTUATION_TYPE
    if token.value == '{'
      return parse_mapping_expression(parser)
    else
      raise Error::Syntax.new(
        "Unexpected token \"#{token.to_english}\" of value \"#{token.value}\".",
        token.lineno,
        parser.stream.source
      )
    end
  when Token::OPERATOR_TYPE
    if token.value == '['
      return parse_sequence_expression(parser)
    end

    if (match = token.value.match(Lexer::REGEX_NAME)) && match.to_s == token.value
      # in this context, string operators are variable names
      parser.stream.next
      @type = :variable

      return Node::Expression::Variable::Context.new(token.value, token.lineno)
    end

    if token.value == '=' && %w[== !=].include?(parser.stream.look(-1).value)
      raise Error::Syntax.new(
        "Unexpected operator of value \"#{token.value}\". Did you try to use \"===\" or \"!==\" for " \
        'strict comparison? Use "is same as(value)" instead.',
        token.lineno,
        parser.stream.source
      )
    end
  end

  raise Error::Syntax.new(
    "Unexpected token \"#{token.type}\" of value \"#{token.value}\".",
    token.lineno,
    parser.stream.source
  )
end

#precedenceObject



101
102
103
# File 'lib/twig/expression_parser/prefix/literal.rb', line 101

def precedence
  0
end