Class: Parselly::Lexer::Token

Inherits:
Struct
  • Object
show all
Defined in:
lib/parselly/lexer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#positionObject

Returns the value of attribute position

Returns:

  • (Object)

    the current value of position



29
30
31
# File 'lib/parselly/lexer.rb', line 29

def position
  @position
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



29
30
31
# File 'lib/parselly/lexer.rb', line 29

def type
  @type
end

#valueObject

Returns the value of attribute value

Returns:

  • (Object)

    the current value of value



29
30
31
# File 'lib/parselly/lexer.rb', line 29

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/parselly/lexer.rb', line 61

def ==(other)
  return super unless other.respond_to?(:to_ary)

  other_type, other_value, other_position = other.to_ary
  return false unless type == other_type
  return false unless value == other_value
  return position == other_position unless position.is_a?(Hash) && other_position.is_a?(Hash)

  other_position.all? { |key, expected| position[key] == expected }
end

#[](index) ⇒ Object



30
31
32
# File 'lib/parselly/lexer.rb', line 30

def [](index)
  to_ary[index]
end

#[]=(index, new_value) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/parselly/lexer.rb', line 34

def []=(index, new_value)
  case index
  when 0
    self.type = new_value
  when 1
    self.value = new_value
  when 2
    self.position = new_value
  else
    raise IndexError, "index #{index} outside of token"
  end
end

#firstObject



47
48
49
# File 'lib/parselly/lexer.rb', line 47

def first
  type
end

#lastObject



51
52
53
# File 'lib/parselly/lexer.rb', line 51

def last
  position
end

#to_aryObject Also known as: to_a



55
56
57
# File 'lib/parselly/lexer.rb', line 55

def to_ary
  [type, value, position]
end