Class: Liquid2::Identifier

Inherits:
Expression show all
Defined in:
lib/liquid2/expressions/identifier.rb

Overview

A single word identifier.

Instance Attribute Summary collapse

Attributes inherited from Expression

#token

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Expression

#children, #scope

Constructor Details

#initialize(token) ⇒ Identifier

Returns a new instance of Identifier.

Parameters:

  • token ([Symbol, String?, Integer])


34
35
36
37
# File 'lib/liquid2/expressions/identifier.rb', line 34

def initialize(token)
  super
  @name = token[1]
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/liquid2/expressions/identifier.rb', line 8

def name
  @name
end

Class Method Details

.from(expr, trailing_question: true) ⇒ Object

Try to cast expr to an Identifier.

Parameters:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/liquid2/expressions/identifier.rb', line 12

def self.from(expr, trailing_question: true)
  # TODO: trailing question
  # TODO: expr might not have a token if its not a path.
  unless expr.is_a?(Path) && expr.segments.empty?
    raise LiquidSyntaxError.new("expected an identifier, found #{expr}", expr.token)
  end

  val = expr.head

  unless val.is_a?(String)
    raise LiquidSyntaxError.new("expected an identifier, found #{val}", expr.token)
  end

  # TODO: optimize
  unless val.to_s.match?(/[\u0080-\uFFFFa-zA-Z_][\u0080-\uFFFFa-zA-Z0-9_-]*/)
    raise LiquidSyntaxError.new("invalid identifier", expr.token)
  end

  new(expr.token)
end

Instance Method Details

#evaluate(_context) ⇒ Object



39
40
41
# File 'lib/liquid2/expressions/identifier.rb', line 39

def evaluate(_context)
  @name
end