Class: Twig::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/twig/token.rb

Constant Summary collapse

EOF_TYPE =
:eof
TEXT_TYPE =
:text
BLOCK_START_TYPE =
:block_start
VAR_START_TYPE =
:var_start
BLOCK_END_TYPE =
:block_end
VAR_END_TYPE =
:var_end
NAME_TYPE =
:name
SYMBOL_TYPE =
:symbol
CLASS_VAR_TYPE =
:class_var
NUMBER_TYPE =
:number
STRING_TYPE =
:string
OPERATOR_TYPE =
:operator
PUNCTUATION_TYPE =
:punctuation
INTERPOLATION_START_TYPE =
:interpolation_start
INTERPOLATION_END_TYPE =
:interpolation_end
TOKEN_TO_ENGLISH =
{
  EOF_TYPE => 'end of template',
  TEXT_TYPE => 'text',
  BLOCK_START_TYPE => 'begin of statement block',
  VAR_START_TYPE => 'begin of print statement',
  BLOCK_END_TYPE => 'end of statement block',
  VAR_END_TYPE => 'end of print statement',
  NAME_TYPE => 'name',
  NUMBER_TYPE => 'number',
  STRING_TYPE => 'string',
  OPERATOR_TYPE => 'operator',
  PUNCTUATION_TYPE => 'punctuation',
  INTERPOLATION_START_TYPE => 'begin of string interpolation',
  INTERPOLATION_END_TYPE => 'end of string interpolation',
  SYMBOL_TYPE => 'symbol',
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, value, lineno) ⇒ Token



40
41
42
43
44
# File 'lib/twig/token.rb', line 40

def initialize(type, value, lineno)
  @type = type
  @value = value
  @lineno = lineno
end

Instance Attribute Details

#linenoObject (readonly)

Returns the value of attribute lineno.



38
39
40
# File 'lib/twig/token.rb', line 38

def lineno
  @lineno
end

#typeObject (readonly)

Returns the value of attribute type.



38
39
40
# File 'lib/twig/token.rb', line 38

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



38
39
40
# File 'lib/twig/token.rb', line 38

def value
  @value
end

Class Method Details

.type_to_english(type) ⇒ Object



68
69
70
71
72
# File 'lib/twig/token.rb', line 68

def self.type_to_english(type)
  TOKEN_TO_ENGLISH.fetch(type) do
    raise ArgumentError, "Token of type \"#{type}\" does not exist."
  end
end

Instance Method Details

#debugObject



60
61
62
# File 'lib/twig/token.rb', line 60

def debug
  [type, value]
end

#test(type, values = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/twig/token.rb', line 47

def test(type, values = nil)
  if values.nil? && !type.is_a?(Symbol)
    values = type
    type = NAME_TYPE
  end

  @type == type && (
    values.nil? ||
      (values.is_a?(Array) && values.include?(@value)) ||
      (@value == values)
  )
end

#to_englishObject



64
65
66
# File 'lib/twig/token.rb', line 64

def to_english
  self.class.type_to_english(@type)
end