-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathparser.rb
381 lines (351 loc) · 8.96 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#
# Parser
#
# Turns this:
# `You have { numBananas, plural,
# =0 {no bananas}
# one {a banana}
# other {# bananas}
# } for sale`
#
# into this:
# [ "You have ", [ "numBananas", "plural", 0, {
# "=0": [ "no bananas" ],
# "one": [ "a banana" ],
# "other": [ [ '#' ], " bananas" ]
# } ], " for sale." ]
#
module MessageFormat
class Parser
def initialize ()
@pattern = nil
@length = 0
@index = 0
end
def parse ( pattern )
if !pattern.is_a?(String)
raise_expected('String pattern', pattern.class.to_s)
end
@pattern = pattern
@length = pattern.length
@index = 0
parse_message("message")
end
def is_digit ( char )
char == '0' or
char == '1' or
char == '2' or
char == '3' or
char == '4' or
char == '5' or
char == '6' or
char == '7' or
char == '8' or
char == '9'
end
def is_whitespace ( char )
char == "\s" or
char == "\t" or
char == "\n" or
char == "\r" or
char == "\f" or
char == "\v" or
char == "\u00A0" or
char == "\u2028" or
char == "\u2029"
end
def skip_whitespace ()
while @index < @length and is_whitespace(@pattern[@index])
@index += 1
end
end
def parse_text ( parent_type )
is_hash_special = (parent_type == 'plural' or parent_type == 'selectordinal')
is_arg_style = (parent_type == 'style')
text = ''
while @index < @length
char = @pattern[@index]
if (
char == '{' or
char == '}' or
(is_hash_special and char == '#') or
(is_arg_style and is_whitespace(char))
)
break
elsif char == '\''
@index += 1
char = @pattern[@index]
if char == '\'' # double is always 1 '
text += char
@index += 1
elsif (
# only when necessary
char == '{' or
char == '}' or
(is_hash_special and char == '#') or
(is_arg_style and is_whitespace(char))
)
text += char
while @index + 1 < @length
@index += 1
char = @pattern[@index]
if @pattern.slice(@index, 2) == '\'\'' # double is always 1 '
text += char
@index += 1
elsif char == '\'' # end of quoted
@index += 1
break
else
text += char
end
end
else # lone ' is just a '
text += '\''
# already incremented
end
else
text += char
@index += 1
end
end
text
end
def parse_argument ()
if @pattern[@index] == '#'
@index += 1 # move passed #
return [ '#' ]
end
@index += 1 # move passed {
id = parse_arg_id()
char = @pattern[@index]
if char == '}' # end argument
@index += 1 # move passed }
return [ id ]
end
if char != ','
raise_expected(',')
end
@index += 1 # move passed ,
type = parse_arg_type()
char = @pattern[@index]
if char == '}' # end argument
if (
type == 'plural' or
type == 'selectordinal' or
type == 'select'
)
raise_expected(type + ' message options')
end
@index += 1 # move passed }
return [ id, type ]
end
if char != ','
raise_expected(',')
end
@index += 1 # move passed ,
format = nil
offset = nil
if type == 'plural' or type == 'selectordinal'
offset = parse_plural_offset()
format = parse_sub_messages(type)
elsif type == 'select'
format = parse_sub_messages(type)
else
format = parse_simple_format()
end
char = @pattern[@index]
if char != '}' # not ended argument
raise_expected('}')
end
@index += 1 # move passed
(type == 'plural' or type == 'selectordinal') ?
[ id, type, offset, format ] :
[ id, type, format ]
end
def parse_arg_id ()
skip_whitespace()
id = ''
while @index < @length
char = @pattern[@index]
if char == '{' or char == '#' or char == '.' or char == '(' or char == ')'
raise_expected('argument id')
end
if char == '}' or char == ',' or is_whitespace(char)
break
end
id += char
@index += 1
end
if id.empty?
raise_expected('argument id')
end
skip_whitespace()
id
end
def parse_arg_type ()
skip_whitespace()
arg_type = nil
types = [
'number', 'date', 'time', 'ordinal', 'duration', 'spellout', 'plural', 'selectordinal', 'select'
]
types.each do |type|
if @pattern.slice(@index, type.length) == type
arg_type = type
@index += type.length
break
end
end
if !arg_type
raise_expected(types.join(', '))
end
skip_whitespace()
arg_type
end
def parse_simple_format ()
skip_whitespace()
style = parse_text('style')
if style.empty?
raise_expected('argument style name')
end
skip_whitespace()
style
end
def parse_plural_offset ()
skip_whitespace()
offset = 0
if @pattern.slice(@index, 7) == 'offset:'
@index += 7 # move passed offset:
skip_whitespace()
start = @index
while (
@index < @length and
is_digit(@pattern[@index])
)
@index += 1
end
if start == @index
raise_expected('offset number')
end
offset = @pattern[start..@index].to_i
skip_whitespace()
end
offset
end
def parse_sub_messages ( parent_type )
skip_whitespace()
options = {}
has_subs = false
while (
@index < @length and
@pattern[@index] != '}'
)
selector = parse_selector()
skip_whitespace()
options[selector] = parse_sub_message(parent_type)
has_subs = true
skip_whitespace()
end
if !has_subs
raise_expected(parent_type + ' message options')
end
if !options.has_key?('other') # does not have an other selector
raise_expected(nil, nil, '"other" option must be specified in ' + parent_type)
end
options
end
def parse_selector ()
selector = ''
while @index < @length
char = @pattern[@index]
if char == '}' or char == ','
raise_expected('{')
end
if char == '{' or is_whitespace(char)
break
end
selector += char
@index += 1
end
if selector.empty?
raise_expected('selector')
end
skip_whitespace()
selector
end
def parse_sub_message ( parent_type )
char = @pattern[@index]
if char != '{'
raise_expected('{')
end
@index += 1 # move passed {
message = parse_message(parent_type)
char = @pattern[@index]
if char != '}'
raise_expected('}')
end
@index += 1 # move passed }
message
end
def parse_message ( parent_type )
elements = []
text = parse_text(parent_type)
if !text.empty?
elements.push(text)
end
while @index < @length
if @pattern[@index] == '}'
if parent_type == 'message'
raise_expected()
end
break
end
elements.push(parse_argument())
text = parse_text(parent_type)
if !text.empty?
elements.push(text)
end
end
elements
end
def raise_expected ( expected=nil, found=nil, message=nil )
lines = @pattern[0..@index].split(/\r?\n/)
line = lines.length
column = lines.last.length
if !found
found = @index < @length ? @pattern[@index] : 'end of input'
end
if !message
message = error_message(expected, found)
end
message += ' in "' + @pattern.gsub(/\r?\n/, "\n") + '"'
raise SyntaxError.new(message, expected, found, @index, line, column)
end
def error_message ( expected=nil, found )
expected ?
"Expected \"#{ expected }\" but found \"#{ found }\"" :
"Unexpected \"#{ found }\" found"
end
def self.parse ( pattern )
Parser.new().parse(pattern)
end
#
# Syntax Error
# Holds information about bad syntax found in a message pattern
#
class SyntaxError < StandardError
attr_reader :expected
attr_reader :found
attr_reader :offset
attr_reader :line
attr_reader :column
def initialize (message, expected, found, offset, line, column)
super(message)
@expected = expected
@found = found
@offset = offset
@line = line
@column = column
end
end
end
end