Class: RubyJsonParser::Token

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/ruby_json_parser/token.rb

Overview

Represents a single token (word) produced by the lexer.

Constant Summary collapse

DIGITS =

String containing all valid decimal digits

'0123456789'
HEX_DIGITS =

String containing all valid hexadecimal digits

'0123456789abcdefABCDEF'
KEYWORDS =

Set of all JSON keywords

T.let(
  Set[
    'false',
    'true',
    'null',
  ],
  T::Set[String],
)
NONE =

Represents no token, a placeholder

:none
END_OF_FILE =

Signifies that the entire string/file has been processed, there will be no more tokens

:end_of_file
ERROR =

Holds an error message, means that the string/file could not be successfully processed

:error
LBRACKET =

Left bracket ‘[`

:lbracket
RBRACKET =

Right bracket ‘]`

:rbracket
LBRACE =

Right brace ‘{`

:lbrace
RBRACE =

Left brace ‘}`

:rbrace
COMMA =

Comma ‘,`

:comma
COLON =

Colon ‘:`

:colon
DOT =

Dot ‘.`

:dot
NUMBER =

Number literal eg. ‘123`

:number
STRING =

String literal eg. ‘“foo”`

:string
FALSE =

False literal ‘false`

:false
TRUE =

True literal ‘true`

:true
NULL =

Null literal ‘null`

:null

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, value = nil) ⇒ Token

Returns a new instance of Token.



61
62
63
64
# File 'lib/ruby_json_parser/token.rb', line 61

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

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



55
56
57
# File 'lib/ruby_json_parser/token.rb', line 55

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



58
59
60
# File 'lib/ruby_json_parser/token.rb', line 58

def value
  @value
end

Class Method Details

.type_to_string(type) ⇒ Object



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
# File 'lib/ruby_json_parser/token.rb', line 16

def type_to_string(type)
  case type
  when NONE
    'NONE'
  when END_OF_FILE
    'END_OF_FILE'
  when ERROR
    'ERROR'
  when LBRACKET
    '['
  when RBRACKET
    ']'
  when LBRACE
    '{'
  when RBRACE
    '}'
  when COMMA
    ','
  when COLON
    ':'
  when DOT
    '.'
  when NUMBER
    'NUMBER'
  when STRING
    'STRING'
  when false
    'false'
  when true
    'true'
  when NULL
    'null'
  else
    '<invalid>'
  end
end

Instance Method Details

#==(other) ⇒ Object



67
68
69
70
71
# File 'lib/ruby_json_parser/token.rb', line 67

def ==(other)
  return false unless other.is_a?(Token)

  type == other.type && value == other.value
end

#inspectObject



74
75
76
77
78
# File 'lib/ruby_json_parser/token.rb', line 74

def inspect
  return "Token(#{type.inspect})" if value.nil?

  "Token(#{type.inspect}, #{value.inspect})"
end

#to_sObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ruby_json_parser/token.rb', line 82

def to_s
  case type
  when NONE
    'NONE'
  when END_OF_FILE
    'END_OF_FILE'
  when ERROR
    "<error: #{value}>"
  when LBRACKET
    '['
  when RBRACKET
    ']'
  when LBRACE
    '{'
  when RBRACE
    '}'
  when COMMA
    ','
  when COLON
    ':'
  when DOT
    '.'
  when NUMBER
    value.to_s
  when STRING
    T.cast(value.inspect, String)
  when false
    'false'
  when true
    'true'
  when NULL
    'null'
  else
    '<invalid>'
  end
end