-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.rb
221 lines (170 loc) · 4.09 KB
/
parser.rb
1
2
3
4
5
6
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
require './constants'
class JSONParser
include Constants
def parse(input)
@pos = 0
@input = input
parse_node
end
def parse_node
skip_whitespace
value = parse_string || parse_number || parse_object || parse_array || parse_literals
skip_whitespace
raise unexpected_token if value == :nil
value
end
def char
@input[@pos]
end
# https://datatracker.ietf.org/doc/html/rfc7159#section-4
def parse_object
return nil unless char == '{'
@pos += 1
skip_whitespace
first_field = true
hash = {}
until char == '}'
raise unexpected_end unless char
skip_comma_and_whitespace unless first_field
first_field = false
key = parse_hash_key
value = parse_node
expect 'hash value' if value == :nil
hash[key] = value
end
@pos += 1
hash
end
# https://datatracker.ietf.org/doc/html/rfc7159#section-5
def parse_array
return nil unless char == '['
@pos += 1
skip_whitespace
array = []
first_value = true
until char == ']'
raise unexpected_end unless char
parse_comma unless first_value
value = parse_node
next if value == :nil
array << value
first_value = false
end
@pos += 1
array
end
# https://datatracker.ietf.org/doc/html/rfc7159#section-6
def parse_number
start = @pos
is_float = false
if char == '-'
@pos += 1
raise unexpected_token unless number?
end
if char == '0'
@pos += 1
unexpected_token 'number' if char == '0'
elsif number?(true)
@pos += 1
@pos += 1 while number?
end
if char == '.'
@pos += 1
is_float = true
unexpected_token 'number' unless number?
end
@pos += 1 while number?
if @pos > start
no = @input[start, @pos]
is_float ? no.to_f : no.to_i
end
end
# https://datatracker.ietf.org/doc/html/rfc7159#section-3
def parse_literals
LITERALS.each do |key, value|
len = key.to_s.length
match = key.to_s == @input[@pos, len]
next unless match
@pos += len
return value
end
:nil
end
# https://datatracker.ietf.org/doc/html/rfc7159#section-7
def parse_string
return nil unless char == '"'
value = ''
@pos += 1
until char == '"'
raise unexpected_end unless char
# \\n, \\r ...
if char == '\\'
@pos += 1
if ESCAPE_CHARS[char&.to_sym]
value += ESCAPE_CHARS[char&.to_sym].to_i(16).chr(Encoding::UTF_8)
# if unicode character -> u hex hex hex hex eg u1F6A
elsif char == 'u'
chars = @input[@pos + 1, 4]
valid_hex_sequence = chars.chars.select(&hexadecimal?).count == 4
unless valid_hex_sequence
@pos += 1 # to show meaningful error
expect 'unicode character'
end
value += chars.to_i(16).chr(Encoding::UTF_8)
@pos += 4
else
expect 'escape character'
end
else
value += char
end
@pos += 1
end
@pos += 1
value
end
def hexadecimal?
lambda do |c|
return c >= '0' && c <= '9' || c.downcase >= 'a' && c.downcase <= 'f'
end
end
# check if a given string is a (natural) number between 0 - 9
def number?(natural = false)
return nil unless char
char >= (natural ? '1' : '0') && char <= '9'
end
def skip_whitespace
@pos += 1 while WHITESPACE_CHARS[char&.to_sym]
nil
end
def parse_comma
expect 'comma(,)' unless char == COMMA
@pos += 1
nil
end
def parse_colon
expect 'colon(:)' unless char == COLON
@pos += 1
nil
end
def parse_hash_key
key = parse_string
skip_whitespace
parse_colon
skip_whitespace
key
end
def skip_comma_and_whitespace
parse_comma
skip_whitespace
end
def expect(value)
raise "#{value} expected in JSON at position #{@pos}, got '#{char}' instead"
end
def unexpected_token(type = 'token')
raise "unexpected #{type} #{char} in JSON at position #{@pos}"
end
def unexpected_end
raise UNEXPECTED_END_OF_JSON
end
end