rparsec 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/rparsec.rb +2 -2
- data/rparsec/context.rb +81 -66
- data/rparsec/error.rb +22 -19
- data/rparsec/expressions.rb +179 -136
- data/rparsec/functors.rb +270 -249
- data/rparsec/id_monad.rb +13 -11
- data/rparsec/keywords.rb +101 -89
- data/rparsec/locator.rb +36 -32
- data/rparsec/misc.rb +109 -102
- data/rparsec/monad.rb +57 -51
- data/rparsec/operators.rb +116 -109
- data/rparsec/parser.rb +892 -794
- data/rparsec/parser_monad.rb +19 -16
- data/rparsec/parsers.rb +614 -603
- data/rparsec/token.rb +23 -20
- data/test/src/simple_monad_test.rb +21 -21
- metadata +49 -41
data/rparsec/monad.rb
CHANGED
@@ -1,52 +1,58 @@
|
|
1
|
-
#
|
2
|
-
# module for Monad
|
3
|
-
#
|
4
|
-
module Monad
|
5
|
-
attr_reader :this
|
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
|
-
|
1
|
+
#
|
2
|
+
# module for Monad
|
3
|
+
#
|
4
|
+
module Monad
|
5
|
+
attr_reader :this
|
6
|
+
|
7
|
+
#
|
8
|
+
# To initialize with a monad implementation and an object that obeys the monad law.
|
9
|
+
#
|
10
|
+
def initMonad(m, v)
|
11
|
+
raise ArgumentError, 'monad cannot be nil' if m.nil?
|
12
|
+
@monad = m;
|
13
|
+
@this = v;
|
14
|
+
end
|
15
|
+
|
16
|
+
#
|
17
|
+
# To create a value based on the monad impl.
|
18
|
+
#
|
19
|
+
def value v
|
20
|
+
@monad.value v
|
21
|
+
end
|
22
|
+
|
23
|
+
#
|
24
|
+
# Run the _bind_ operation on the encapsulated object following the monad law.
|
25
|
+
#
|
26
|
+
def bind(&binder)
|
27
|
+
@monad.bind(@this, &binder)
|
28
|
+
end
|
29
|
+
|
30
|
+
#
|
31
|
+
# Run the _seq_ operation on the encapsulated object following the monad law.
|
32
|
+
# If _seq_ is not defined by the monad impl, use _bind_ to implement.
|
33
|
+
#
|
34
|
+
def seq(other)
|
35
|
+
if @monad.respond_to? :seq
|
36
|
+
@monad.seq(other)
|
37
|
+
else bind {|x|other}
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
#
|
42
|
+
# Run the _map_ operation on the encapsulated object following the monad law.
|
43
|
+
# _bind_ is used to implement.
|
44
|
+
#
|
45
|
+
def map(&mapper)
|
46
|
+
bind do |v|
|
47
|
+
result = mapper.call v;
|
48
|
+
value(result);
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
#
|
53
|
+
# Run the _plus_ operation on the encapsulated object following the MonadPlus law.
|
54
|
+
#
|
55
|
+
def plus other
|
56
|
+
@monad.mplus(@this, other.this)
|
57
|
+
end
|
52
58
|
end
|
data/rparsec/operators.rb
CHANGED
@@ -1,110 +1,117 @@
|
|
1
|
-
require 'rparsec/parser'
|
2
|
-
|
3
|
-
#
|
4
|
-
# utility functions for string manipulations.
|
5
|
-
#
|
6
|
-
module StringUtils
|
7
|
-
#
|
8
|
-
# Does _str_ starts with the _sub_ string?
|
9
|
-
#
|
10
|
-
def self.starts_with? str, sub
|
11
|
-
return true if sub.nil?
|
12
|
-
len = sub.length
|
13
|
-
return false if len > str.length
|
14
|
-
for i in (0...len)
|
15
|
-
return false if str[i] != sub[i]
|
16
|
-
end
|
17
|
-
true
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
#
|
22
|
-
# This class helps building lexer and parser for operators.
|
23
|
-
# The case that one operator (++ for example) contains another operator (+)
|
24
|
-
# is automatically handled so client code don't have to worry about ambiguity.
|
25
|
-
#
|
26
|
-
class Operators
|
27
|
-
#
|
28
|
-
# To create an instance of Operators for the given operators.
|
29
|
-
# The _block_ parameter, if present, is used to convert the token text to another object
|
30
|
-
# when the token is recognized during grammar parsing phase.
|
31
|
-
#
|
32
|
-
def initialize(ops, &block)
|
33
|
-
@lexers = {}
|
34
|
-
@parsers = {}
|
35
|
-
sorted = Operators.sort(ops)
|
36
|
-
lexers = sorted.map do |op|
|
37
|
-
symbol = op.to_sym
|
38
|
-
result = nil
|
39
|
-
if op.length == 1
|
40
|
-
result = Parsers.char(op)
|
41
|
-
else
|
42
|
-
result = Parsers.str(op)
|
43
|
-
end
|
44
|
-
result = result.token(symbol)
|
45
|
-
@lexers[symbol] = result
|
46
|
-
@parsers[symbol] = Parsers.token(symbol, &block)
|
47
|
-
result
|
48
|
-
end
|
49
|
-
@lexer = Parsers.sum(*lexers)
|
50
|
-
end
|
51
|
-
|
52
|
-
#
|
53
|
-
#
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
result
|
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
|
-
|
1
|
+
require 'rparsec/parser'
|
2
|
+
|
3
|
+
#
|
4
|
+
# utility functions for string manipulations.
|
5
|
+
#
|
6
|
+
module StringUtils
|
7
|
+
#
|
8
|
+
# Does _str_ starts with the _sub_ string?
|
9
|
+
#
|
10
|
+
def self.starts_with? str, sub
|
11
|
+
return true if sub.nil?
|
12
|
+
len = sub.length
|
13
|
+
return false if len > str.length
|
14
|
+
for i in (0...len)
|
15
|
+
return false if str[i] != sub[i]
|
16
|
+
end
|
17
|
+
true
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
#
|
22
|
+
# This class helps building lexer and parser for operators.
|
23
|
+
# The case that one operator (++ for example) contains another operator (+)
|
24
|
+
# is automatically handled so client code don't have to worry about ambiguity.
|
25
|
+
#
|
26
|
+
class Operators
|
27
|
+
#
|
28
|
+
# To create an instance of Operators for the given operators.
|
29
|
+
# The _block_ parameter, if present, is used to convert the token text to another object
|
30
|
+
# when the token is recognized during grammar parsing phase.
|
31
|
+
#
|
32
|
+
def initialize(ops, &block)
|
33
|
+
@lexers = {}
|
34
|
+
@parsers = {}
|
35
|
+
sorted = Operators.sort(ops)
|
36
|
+
lexers = sorted.map do |op|
|
37
|
+
symbol = op.to_sym
|
38
|
+
result = nil
|
39
|
+
if op.length == 1
|
40
|
+
result = Parsers.char(op)
|
41
|
+
else
|
42
|
+
result = Parsers.str(op)
|
43
|
+
end
|
44
|
+
result = result.token(symbol)
|
45
|
+
@lexers[symbol] = result
|
46
|
+
@parsers[symbol] = Parsers.token(symbol, &block)
|
47
|
+
result
|
48
|
+
end
|
49
|
+
@lexer = Parsers.sum(*lexers)
|
50
|
+
end
|
51
|
+
|
52
|
+
#
|
53
|
+
# Get the parser for the given operator.
|
54
|
+
#
|
55
|
+
def parser(op)
|
56
|
+
result = @parsers[op.to_sym]
|
57
|
+
raise ArgumentError, "parser not found for #{op}" if result.nil?
|
58
|
+
result
|
59
|
+
end
|
60
|
+
|
61
|
+
alias [] parser
|
62
|
+
|
63
|
+
#
|
64
|
+
# Get the lexer that lexes operators.
|
65
|
+
# If an operator is specified, the lexer for that operator is returned.
|
66
|
+
#
|
67
|
+
def lexer(op=nil)
|
68
|
+
return @lexer if op.nil?
|
69
|
+
@lexers[op.to_sym]
|
70
|
+
end
|
71
|
+
|
72
|
+
#
|
73
|
+
# Sort an array of operators so that contained operator appears after containers.
|
74
|
+
# When no containment exist between two operators, the shorter one takes precedence.
|
75
|
+
#
|
76
|
+
def self.sort(ops)
|
77
|
+
#sort the array by longer-string-first.
|
78
|
+
ordered = ops.sort {|x, y|y.length <=> x.length}
|
79
|
+
suites = []
|
80
|
+
# loop from the longer to shorter string
|
81
|
+
ordered.each do |s|
|
82
|
+
populate_suites(suites, s)
|
83
|
+
end
|
84
|
+
# suites are populated with bigger suite first
|
85
|
+
to_array suites
|
86
|
+
end
|
87
|
+
|
88
|
+
private
|
89
|
+
|
90
|
+
def self.populate_suites(suites, s)
|
91
|
+
# populate the suites so that bigger suite first
|
92
|
+
# this way we can use << operator for non-contained strings.
|
93
|
+
|
94
|
+
# we need to start from bigger suite. So loop in reverse order
|
95
|
+
for suite in suites
|
96
|
+
return if populate_suite(suite, s)
|
97
|
+
end
|
98
|
+
suites << [s]
|
99
|
+
end
|
100
|
+
|
101
|
+
def self.populate_suite(suite, s)
|
102
|
+
# loop from the tail of the suite
|
103
|
+
for i in (1..suite.length)
|
104
|
+
ind = suite.length - i
|
105
|
+
cur = suite[ind]
|
106
|
+
if StringUtils.starts_with? cur, s
|
107
|
+
suite.insert(ind+1, s) unless cur == s
|
108
|
+
return true
|
109
|
+
end
|
110
|
+
end
|
111
|
+
false
|
112
|
+
end
|
113
|
+
|
114
|
+
def self.to_array suites
|
115
|
+
suites.reverse!.flatten!
|
116
|
+
end
|
110
117
|
end
|
data/rparsec/parser.rb
CHANGED
@@ -1,794 +1,892 @@
|
|
1
|
-
%w{
|
2
|
-
monad misc error context locator token functors parser_monad
|
3
|
-
}.each {|lib| require "rparsec/#{lib}"}
|
4
|
-
require 'strscan'
|
5
|
-
|
6
|
-
|
7
|
-
#
|
8
|
-
# Represents a parser that parses a certain grammar rule.
|
9
|
-
#
|
10
|
-
class Parser
|
11
|
-
include Functors
|
12
|
-
include Monad
|
13
|
-
extend Signature
|
14
|
-
extend DefHelper
|
15
|
-
MyMonad = ParserMonad.new
|
16
|
-
attr_accessor :name
|
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
|
-
def
|
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
|
-
end
|
74
|
-
|
75
|
-
#
|
76
|
-
#
|
77
|
-
#
|
78
|
-
#
|
79
|
-
def
|
80
|
-
|
81
|
-
|
82
|
-
end
|
83
|
-
|
84
|
-
#
|
85
|
-
#
|
86
|
-
# to
|
87
|
-
|
88
|
-
#
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
#
|
95
|
-
#
|
96
|
-
#
|
97
|
-
# to the
|
98
|
-
#
|
99
|
-
#
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
#
|
107
|
-
#
|
108
|
-
#
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
#
|
119
|
-
#
|
120
|
-
#
|
121
|
-
def
|
122
|
-
|
123
|
-
end
|
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
|
-
def
|
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
|
-
# To create a parser that repeats self for
|
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
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
#
|
392
|
-
#
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
#
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
#
|
421
|
-
#
|
422
|
-
#
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
#
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
#
|
434
|
-
#
|
435
|
-
#
|
436
|
-
def
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
def
|
462
|
-
|
463
|
-
end
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
#
|
472
|
-
# A parser that
|
473
|
-
#
|
474
|
-
def
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
#
|
480
|
-
#
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
#
|
488
|
-
#
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
#
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
#
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
#
|
521
|
-
#
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
#
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
#
|
539
|
-
#
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
end
|
545
|
-
|
546
|
-
#
|
547
|
-
# A parser that
|
548
|
-
#
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
end
|
557
|
-
|
558
|
-
#
|
559
|
-
# A parser that
|
560
|
-
#
|
561
|
-
def
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
#
|
571
|
-
# A parser that
|
572
|
-
#
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
#
|
580
|
-
#
|
581
|
-
#
|
582
|
-
#
|
583
|
-
def
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
#
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
#
|
597
|
-
#
|
598
|
-
#
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
#
|
603
|
-
# A parser that
|
604
|
-
#
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
#
|
616
|
-
#
|
617
|
-
#
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
#
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
#
|
633
|
-
#
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
#
|
640
|
-
#
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
#
|
652
|
-
#
|
653
|
-
|
654
|
-
|
655
|
-
|
656
|
-
end
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
662
|
-
#
|
663
|
-
#
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
#
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
#
|
693
|
-
#
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
699
|
-
#
|
700
|
-
#
|
701
|
-
# and
|
702
|
-
#
|
703
|
-
|
704
|
-
|
705
|
-
|
706
|
-
|
707
|
-
#
|
708
|
-
# A
|
709
|
-
#
|
710
|
-
#
|
711
|
-
#
|
712
|
-
def
|
713
|
-
|
714
|
-
end
|
715
|
-
|
716
|
-
#
|
717
|
-
#
|
718
|
-
|
719
|
-
#
|
720
|
-
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
#
|
725
|
-
#
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
#
|
733
|
-
|
734
|
-
#
|
735
|
-
|
736
|
-
|
737
|
-
|
738
|
-
|
739
|
-
|
740
|
-
|
741
|
-
|
742
|
-
|
743
|
-
#
|
744
|
-
#
|
745
|
-
#
|
746
|
-
|
747
|
-
|
748
|
-
|
749
|
-
|
750
|
-
|
751
|
-
|
752
|
-
|
753
|
-
|
754
|
-
|
755
|
-
|
756
|
-
|
757
|
-
|
758
|
-
|
759
|
-
|
760
|
-
|
761
|
-
|
762
|
-
|
763
|
-
#
|
764
|
-
# that
|
765
|
-
|
766
|
-
|
767
|
-
|
768
|
-
|
769
|
-
|
770
|
-
|
771
|
-
|
772
|
-
|
773
|
-
|
774
|
-
|
775
|
-
|
776
|
-
|
777
|
-
|
778
|
-
|
779
|
-
|
780
|
-
|
781
|
-
|
782
|
-
|
783
|
-
|
784
|
-
|
785
|
-
|
786
|
-
|
787
|
-
|
788
|
-
|
789
|
-
|
790
|
-
|
791
|
-
|
792
|
-
|
793
|
-
|
794
|
-
|
1
|
+
%w{
|
2
|
+
monad misc error context locator token functors parser_monad
|
3
|
+
}.each {|lib| require "rparsec/#{lib}"}
|
4
|
+
require 'strscan'
|
5
|
+
|
6
|
+
|
7
|
+
#
|
8
|
+
# Represents a parser that parses a certain grammar rule.
|
9
|
+
#
|
10
|
+
class Parser
|
11
|
+
include Functors
|
12
|
+
include Monad
|
13
|
+
extend Signature
|
14
|
+
extend DefHelper
|
15
|
+
MyMonad = ParserMonad.new
|
16
|
+
attr_accessor :name
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def initialize
|
21
|
+
initMonad(MyMonad, self)
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.init(*vars)
|
25
|
+
parser_checker = {}
|
26
|
+
vars.each_with_index do |var, i|
|
27
|
+
name = var.to_s
|
28
|
+
parser_checker[i] = var if name.include?('parser') && !name.include?('parsers')
|
29
|
+
end
|
30
|
+
define_method(:initialize) do |*params|
|
31
|
+
super()
|
32
|
+
vars.each_with_index do |var, i|
|
33
|
+
param = params[i]
|
34
|
+
if parser_checker.include? i
|
35
|
+
TypeChecker.check_arg_type Parser, param, self, i
|
36
|
+
end
|
37
|
+
instance_variable_set("@"+var.to_s, param)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def _display_current_input(input, code, index)
|
43
|
+
return 'EOF' if input.nil?
|
44
|
+
c = input
|
45
|
+
case c when Fixnum then "'"<<c<<"'" when Token then c.text else c.to_s end
|
46
|
+
end
|
47
|
+
|
48
|
+
def _add_encountered_error(msg, encountered)
|
49
|
+
result = msg.dup
|
50
|
+
result << ', ' unless msg.strip.length == 0 || msg =~ /.*(\.|,)\s*$/
|
51
|
+
"#{result}#{encountered}"
|
52
|
+
end
|
53
|
+
|
54
|
+
def _add_location_to_error(locator, ctxt, msg, code)
|
55
|
+
line, col = locator.locate(ctxt.error.index)
|
56
|
+
msg << " at line #{line}, col #{col}."
|
57
|
+
end
|
58
|
+
|
59
|
+
public
|
60
|
+
|
61
|
+
#
|
62
|
+
# parses a string.
|
63
|
+
#
|
64
|
+
def parse(src)
|
65
|
+
ctxt = ParseContext.new(src)
|
66
|
+
return ctxt.result if _parse ctxt
|
67
|
+
ctxt.prepare_error
|
68
|
+
locator = CodeLocator.new(src)
|
69
|
+
raise ParserException.new(ctxt.error.index),
|
70
|
+
_add_location_to_error(locator, ctxt,
|
71
|
+
_add_encountered_error(ctxt.to_msg,
|
72
|
+
_display_current_input(ctxt.error.input, src, ctxt.index)), src)
|
73
|
+
end
|
74
|
+
|
75
|
+
#
|
76
|
+
# Set name for the parser.
|
77
|
+
# self is returned.
|
78
|
+
#
|
79
|
+
def setName(nm)
|
80
|
+
@name = nm
|
81
|
+
self
|
82
|
+
end
|
83
|
+
|
84
|
+
#
|
85
|
+
# a.map{|x|x+1} will first execute parser a, when it succeeds,
|
86
|
+
# the associated block is executed to transform the result to a new value
|
87
|
+
# (increment it in this case).
|
88
|
+
#
|
89
|
+
def map(&block)
|
90
|
+
return self unless block
|
91
|
+
MapParser.new(self, block)
|
92
|
+
end
|
93
|
+
|
94
|
+
#
|
95
|
+
# _self_ is first executed, the parser result is then passed as parameter to the associated block,
|
96
|
+
# which evaluates to another Parser object at runtime. This new Parser object is then executed
|
97
|
+
# to get the final parser result.
|
98
|
+
#
|
99
|
+
# Different from _bind_, parser result of _self_ will be expanded first if it is an array.
|
100
|
+
#
|
101
|
+
def bindn(&block)
|
102
|
+
return self unless block
|
103
|
+
BoundnParser.new(self, block)
|
104
|
+
end
|
105
|
+
|
106
|
+
#
|
107
|
+
# a.mapn{|x,y|x+y} will first execute parser a, when it succeeds,
|
108
|
+
# the array result (if any) is expanded and passed as parameters
|
109
|
+
# to the associated block. The result of the block is then used
|
110
|
+
# as the parsing result.
|
111
|
+
#
|
112
|
+
def mapn(&block)
|
113
|
+
return self unless block
|
114
|
+
MapnParser.new(self, block)
|
115
|
+
end
|
116
|
+
|
117
|
+
#
|
118
|
+
# Create a new parser that's atomic.,
|
119
|
+
# meaning that when it fails, input consumption is undone.
|
120
|
+
#
|
121
|
+
def atomize
|
122
|
+
AtomParser.new(self).setName(@name)
|
123
|
+
end
|
124
|
+
|
125
|
+
#
|
126
|
+
# Create a new parser that looks at inputs whthout consuming them.
|
127
|
+
#
|
128
|
+
def peek
|
129
|
+
PeekParser.new(self).setName(@name)
|
130
|
+
end
|
131
|
+
|
132
|
+
#
|
133
|
+
# To create a new parser that succeed only if self fails.
|
134
|
+
#
|
135
|
+
def not(msg="#{self} unexpected")
|
136
|
+
NotParser.new(self, msg)
|
137
|
+
end
|
138
|
+
|
139
|
+
#
|
140
|
+
# To create a parser that does "look ahead" for n inputs.
|
141
|
+
#
|
142
|
+
def lookahead n
|
143
|
+
self
|
144
|
+
end
|
145
|
+
|
146
|
+
#
|
147
|
+
# To create a parser that fails with a given error message.
|
148
|
+
#
|
149
|
+
def expect msg
|
150
|
+
ExpectParser.new(self, msg)
|
151
|
+
end
|
152
|
+
|
153
|
+
#
|
154
|
+
# a.followed b will sequentially run a and b;
|
155
|
+
# result of a is preserved as the ultimate return value.
|
156
|
+
#
|
157
|
+
def followed(other)
|
158
|
+
FollowedParser.new(self, other)
|
159
|
+
end
|
160
|
+
def_sig :followed, Parser
|
161
|
+
|
162
|
+
#
|
163
|
+
# To create a parser that repeats self for a minimum _min_ times,
|
164
|
+
# and maximally _max_ times.
|
165
|
+
# Only the return value of the last execution is preserved.
|
166
|
+
#
|
167
|
+
def repeat_(min, max=min)
|
168
|
+
return Parsers.failure("min=#{min}, max=#{max}") if min > max
|
169
|
+
if(min==max)
|
170
|
+
return Parsers.one if max <= 0
|
171
|
+
return self if max == 1
|
172
|
+
Repeat_Parser.new(self, max)
|
173
|
+
else
|
174
|
+
Some_Parser.new(self, min, max)
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
#
|
179
|
+
# To create a parser that repeats self for a minimum _min_ times,
|
180
|
+
# and maximally _max_ times.
|
181
|
+
# All return values are collected in an array.
|
182
|
+
#
|
183
|
+
def repeat(min, max=min)
|
184
|
+
return Parsers.failure("min=#{min}, max=#{max}") if min > max
|
185
|
+
if(min==max)
|
186
|
+
RepeatParser.new(self, max)
|
187
|
+
else
|
188
|
+
SomeParser.new(self, min, max)
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
#
|
193
|
+
# To create a parser that repeats self for at least _least_ times.
|
194
|
+
# parser.many_ is equivalent to bnf notation "parser*".
|
195
|
+
# Only the return value of the last execution is preserved.
|
196
|
+
#
|
197
|
+
def many_(least=0)
|
198
|
+
Many_Parser.new(self, least)
|
199
|
+
end
|
200
|
+
|
201
|
+
#
|
202
|
+
# To create a parser that repeats self for at least _least_ times.
|
203
|
+
# All return values are collected in an array.
|
204
|
+
#
|
205
|
+
def many(least=0)
|
206
|
+
ManyParser.new(self, least)
|
207
|
+
end
|
208
|
+
|
209
|
+
#
|
210
|
+
# To create a parser that repeats self for at most _max_ times.
|
211
|
+
# Only the return value of the last execution is preserved.
|
212
|
+
#
|
213
|
+
def some_(max)
|
214
|
+
repeat_(0, max)
|
215
|
+
end
|
216
|
+
|
217
|
+
#
|
218
|
+
# To create a parser that repeats self for at most _max_ times.
|
219
|
+
# All return values are collected in an array.
|
220
|
+
#
|
221
|
+
def some(max)
|
222
|
+
repeat(0, max)
|
223
|
+
end
|
224
|
+
|
225
|
+
#
|
226
|
+
# To create a parser that repeats self for unlimited times,
|
227
|
+
# with the pattern recognized by _delim_ as separator that separates each occurrence.
|
228
|
+
# self has to match for at least once.
|
229
|
+
# Return values of self are collected in an array.
|
230
|
+
#
|
231
|
+
def separated1 delim
|
232
|
+
rest = delim >> self
|
233
|
+
self.bind do |v0|
|
234
|
+
result = [v0]
|
235
|
+
(rest.map {|v| result << v}).many_ >> value(result)
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
#
|
240
|
+
# To create a parser that repeats self for unlimited times,
|
241
|
+
# with the pattern recognized by _delim_ as separator that separates each occurrence.
|
242
|
+
# Return values of self are collected in an array.
|
243
|
+
#
|
244
|
+
def separated delim
|
245
|
+
separated1(delim).plus value([])
|
246
|
+
end
|
247
|
+
|
248
|
+
#
|
249
|
+
# To create a parser that repeats self for unlimited times,
|
250
|
+
# with the pattern recognized by _delim_ as separator that separates each occurrence
|
251
|
+
# and also possibly ends the pattern.
|
252
|
+
# self has to match for at least once.
|
253
|
+
# Return values of self are collected in an array.
|
254
|
+
#
|
255
|
+
def delimited1 delim
|
256
|
+
rest = delim >> (self.plus Parsers.throwp(:__end_delimiter__))
|
257
|
+
self.bind do |v0|
|
258
|
+
result = [v0]
|
259
|
+
(rest.map {|v| result << v}).many_.catchp(:__end_delimiter__) >> value(result)
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
#
|
264
|
+
# To create a parser that repeats self for unlimited times,
|
265
|
+
# with the pattern recognized by _delim_ as separator that separates each occurrence
|
266
|
+
# and also possibly ends the pattern.
|
267
|
+
# Return values of self are collected in an array.
|
268
|
+
#
|
269
|
+
def delimited delim
|
270
|
+
delimited1(delim).plus value([])
|
271
|
+
end
|
272
|
+
|
273
|
+
#
|
274
|
+
# String representation
|
275
|
+
#
|
276
|
+
def to_s
|
277
|
+
return name unless name.nil?
|
278
|
+
self.class.to_s
|
279
|
+
end
|
280
|
+
|
281
|
+
#
|
282
|
+
# a | b will run b when a fails.
|
283
|
+
# b is auto-boxed to Parser when it is not of type Parser.
|
284
|
+
#
|
285
|
+
def | other
|
286
|
+
AltParser.new([self, autobox_parser(other)])
|
287
|
+
end
|
288
|
+
|
289
|
+
#
|
290
|
+
# a.optional(default) is equivalent to a.plus(value(default))
|
291
|
+
#
|
292
|
+
def optional(default=nil)
|
293
|
+
self.plus(value(default))
|
294
|
+
end
|
295
|
+
|
296
|
+
#
|
297
|
+
# a.catchp(:somesymbol) will catch the :somesymbol thrown by a.
|
298
|
+
#
|
299
|
+
def catchp(symbol)
|
300
|
+
CatchParser.new(symbol, self)
|
301
|
+
end
|
302
|
+
|
303
|
+
#
|
304
|
+
# a.fragment will return the string matched by a.
|
305
|
+
#
|
306
|
+
def fragment
|
307
|
+
FragmentParser.new(self)
|
308
|
+
end
|
309
|
+
|
310
|
+
#
|
311
|
+
# a.nested b will feed the token array returned by parser a to parser b
|
312
|
+
# for a nested parsing.
|
313
|
+
#
|
314
|
+
def nested(parser)
|
315
|
+
NestedParser.new(self, parser)
|
316
|
+
end
|
317
|
+
|
318
|
+
#
|
319
|
+
# a.lexeme(delim) will parse _a_ for 0 or more times and ignore all
|
320
|
+
# patterns recognized by _delim_.
|
321
|
+
# Values returned by _a_ are collected in an array.
|
322
|
+
#
|
323
|
+
def lexeme(delim = Parsers.whitespaces)
|
324
|
+
delim = delim.many_
|
325
|
+
delim >> self.delimited(delim)
|
326
|
+
end
|
327
|
+
|
328
|
+
#
|
329
|
+
# For prefix unary operator.
|
330
|
+
# a.prefix op will run parser _op_ for 0 or more times and eventually run parser _a_
|
331
|
+
# for one time.
|
332
|
+
# _op_ should return a Proc that accepts one parameter.
|
333
|
+
# Proc objects returned by _op_ is then fed with the value returned by _a_
|
334
|
+
# from right to left.
|
335
|
+
# The final result is returned as return value.
|
336
|
+
#
|
337
|
+
def prefix(op)
|
338
|
+
Parsers.sequence(op.many, self) do |funcs, v|
|
339
|
+
funcs.reverse_each {|f|v=f.call(v)}
|
340
|
+
v
|
341
|
+
end
|
342
|
+
end
|
343
|
+
|
344
|
+
#
|
345
|
+
# For postfix unary operator.
|
346
|
+
# a.postfix op will run parser _a_ for once and then _op_ for 0 or more times.
|
347
|
+
# _op_ should return a Proc that accepts one parameter.
|
348
|
+
# Proc objects returned by _op_ is then fed with the value returned by _a_
|
349
|
+
# from left to right.
|
350
|
+
# The final result is returned as return value.
|
351
|
+
#
|
352
|
+
def postfix(op)
|
353
|
+
Parsers.sequence(self, op.many) do |v, funcs|
|
354
|
+
funcs.each{|f|v=f.call(v)}
|
355
|
+
v
|
356
|
+
end
|
357
|
+
end
|
358
|
+
|
359
|
+
#
|
360
|
+
# For non-associative infix binary operator.
|
361
|
+
# _op_ has to return a Proc that takes two parameters, who
|
362
|
+
# are returned by the _self_ parser as operands.
|
363
|
+
#
|
364
|
+
def infixn(op)
|
365
|
+
bind do |v1|
|
366
|
+
bin = Parsers.sequence(op, self) do |f, v2|
|
367
|
+
f.call(v1,v2)
|
368
|
+
end
|
369
|
+
bin | value(v1)
|
370
|
+
end
|
371
|
+
end
|
372
|
+
|
373
|
+
#
|
374
|
+
# For left-associative infix binary operator.
|
375
|
+
# _op_ has to return a Proc that takes two parameters, who
|
376
|
+
# are returned by the _self_ parser as operands.
|
377
|
+
#
|
378
|
+
def infixl(op)
|
379
|
+
Parsers.sequence(self, _infix_rest(op, self).many) do |v, rests|
|
380
|
+
rests.each do |r|
|
381
|
+
f, v1 = *r
|
382
|
+
v = f.call(v,v1)
|
383
|
+
end
|
384
|
+
v
|
385
|
+
end
|
386
|
+
end
|
387
|
+
|
388
|
+
#
|
389
|
+
# For right-associative infix binary operator.
|
390
|
+
# _op_ has to return a Proc that takes two parameters, who
|
391
|
+
# are returned by the _self_ parser as operands.
|
392
|
+
#
|
393
|
+
def infixr(op)
|
394
|
+
Parsers.sequence(self, _infix_rest(op, self).many) do |v, rests|
|
395
|
+
if rests.empty?
|
396
|
+
v
|
397
|
+
else
|
398
|
+
f, seed = *rests.last
|
399
|
+
for i in (0...rests.length-1)
|
400
|
+
cur = rests.length-2-i
|
401
|
+
f1, v1 = *rests[cur]
|
402
|
+
seed = f.call(v1, seed)
|
403
|
+
f = f1
|
404
|
+
end
|
405
|
+
f.call(v, seed)
|
406
|
+
end
|
407
|
+
end
|
408
|
+
end
|
409
|
+
|
410
|
+
#
|
411
|
+
# a.token(:word_token) will return a Token object when _a_ succeeds.
|
412
|
+
# The matched string (or the string returned by _a_, if any) is
|
413
|
+
# encapsulated in the token, together with the :word_token symbol and
|
414
|
+
# the starting index of the match.
|
415
|
+
#
|
416
|
+
def token(kind)
|
417
|
+
TokenParser.new(kind, self)
|
418
|
+
end
|
419
|
+
|
420
|
+
#
|
421
|
+
# a.seq b will sequentially run a then b.
|
422
|
+
# The result of b is preserved as return value.
|
423
|
+
# If a block is associated, values returned by _a_ and _b_
|
424
|
+
# are passed into the block and the return value of
|
425
|
+
# the block is used as the final result of the parser.
|
426
|
+
#
|
427
|
+
def seq(other, &block)
|
428
|
+
# TypeChecker.check_arg_type Parser, other, :seq
|
429
|
+
Parsers.sequence(self, other, &block)
|
430
|
+
end
|
431
|
+
def_sig :seq, Parser
|
432
|
+
|
433
|
+
#
|
434
|
+
# Similar to _seq_. _other_ is auto-boxed if it is not of type Parser.
|
435
|
+
#
|
436
|
+
def >> (other)
|
437
|
+
seq(autobox_parser(other))
|
438
|
+
end
|
439
|
+
|
440
|
+
private
|
441
|
+
|
442
|
+
def autobox_parser(val)
|
443
|
+
return Parsers.value(val) unless val.kind_of? Parser
|
444
|
+
val
|
445
|
+
end
|
446
|
+
|
447
|
+
def _infix_rest(operator, operand)
|
448
|
+
Parsers.sequence(operator, operand, &Idn)
|
449
|
+
end
|
450
|
+
|
451
|
+
public
|
452
|
+
|
453
|
+
alias ~ not
|
454
|
+
alias << followed
|
455
|
+
alias * repeat_
|
456
|
+
|
457
|
+
def_sig :plus, Parser
|
458
|
+
|
459
|
+
private
|
460
|
+
|
461
|
+
def _parse(ctxt)
|
462
|
+
false
|
463
|
+
end
|
464
|
+
end
|
465
|
+
#
|
466
|
+
# This module provides all out-of-box parser implementations.
|
467
|
+
#
|
468
|
+
module Parsers
|
469
|
+
extend Signature
|
470
|
+
|
471
|
+
#
|
472
|
+
# A parser that always fails with the given error message.
|
473
|
+
#
|
474
|
+
def failure msg
|
475
|
+
FailureParser.new(msg)
|
476
|
+
end
|
477
|
+
|
478
|
+
#
|
479
|
+
# A parser that always succeeds with the given return value.
|
480
|
+
#
|
481
|
+
def value v
|
482
|
+
ValueParser.new(v)
|
483
|
+
end
|
484
|
+
|
485
|
+
#
|
486
|
+
# A parser that calls alternative parsers until one succeed,
|
487
|
+
# or any failure with input consumption beyond the current look-ahead.
|
488
|
+
#
|
489
|
+
def sum(*alts)
|
490
|
+
# TypeChecker.check_vararg_type Parser, alts, :sum
|
491
|
+
PlusParser.new(alts)
|
492
|
+
end
|
493
|
+
def_sig :sum, [Parser]
|
494
|
+
|
495
|
+
#
|
496
|
+
# A parser that calls alternative parsers until one succeeds.
|
497
|
+
#
|
498
|
+
def alt(*alts)
|
499
|
+
AltParser.new(alts)
|
500
|
+
end
|
501
|
+
def_sig :alt, [Parser]
|
502
|
+
|
503
|
+
#
|
504
|
+
# A parser that succeeds when the given predicate returns true
|
505
|
+
# (with the current input as the parameter).
|
506
|
+
# _expected_ is the error message when _pred_ returns false.
|
507
|
+
#
|
508
|
+
def satisfies(expected, &pred)
|
509
|
+
SatisfiesParser.new(pred, expected)
|
510
|
+
end
|
511
|
+
|
512
|
+
#
|
513
|
+
# A parser that succeeds when the the current input is equal to the given value.
|
514
|
+
# _expected_ is the error message when _pred_ returns false.
|
515
|
+
#
|
516
|
+
def is(v, expected="#{v} expected")
|
517
|
+
satisfies(expected) {|c|c==v}
|
518
|
+
end
|
519
|
+
|
520
|
+
#
|
521
|
+
# A parser that succeeds when the the current input is not equal to the given value.
|
522
|
+
# _expected_ is the error message when _pred_ returns false.
|
523
|
+
#
|
524
|
+
def isnt(v, expected="#{v} unexpected")
|
525
|
+
satisfies(expected) {|c|c!=v}
|
526
|
+
end
|
527
|
+
|
528
|
+
#
|
529
|
+
# A parser that succeeds when the the current input is among the given values.
|
530
|
+
#
|
531
|
+
def among(*vals)
|
532
|
+
expected="one of [#{vals.join(', ')}] expected"
|
533
|
+
vals = as_list vals
|
534
|
+
satisfies(expected) {|c|vals.include? c}
|
535
|
+
end
|
536
|
+
|
537
|
+
#
|
538
|
+
# A parser that succeeds when the the current input is not among the given values.
|
539
|
+
#
|
540
|
+
def not_among(*vals)
|
541
|
+
expected = "one of [#{vals.join(', ')}] unexpected"
|
542
|
+
vals = as_list vals
|
543
|
+
satisfies(expected) {|c|!vals.include? c}
|
544
|
+
end
|
545
|
+
|
546
|
+
#
|
547
|
+
# A parser that succeeds when the the current input is the given character.
|
548
|
+
#
|
549
|
+
def char(c)
|
550
|
+
if c.kind_of? Fixnum
|
551
|
+
nm = c.chr
|
552
|
+
is(c, "'#{nm}' expected").setName(nm)
|
553
|
+
else
|
554
|
+
is(c[0], "'#{c}' expected").setName(c)
|
555
|
+
end
|
556
|
+
end
|
557
|
+
|
558
|
+
#
|
559
|
+
# A parser that succeeds when the the current input is not the given character.
|
560
|
+
#
|
561
|
+
def not_char(c)
|
562
|
+
if c.kind_of? Fixnum
|
563
|
+
nm = c.chr
|
564
|
+
isnt(c, "'#{nm}' unexpected").setName("~#{nm}")
|
565
|
+
else
|
566
|
+
isnt(c[0], "'#{c}' unexpected").setName("~#{c}")
|
567
|
+
end
|
568
|
+
end
|
569
|
+
|
570
|
+
#
|
571
|
+
# A parser that succeeds when there's no input available.
|
572
|
+
#
|
573
|
+
def eof(expected="EOF expected")
|
574
|
+
EofParser.new(expected).setName('EOF')
|
575
|
+
end
|
576
|
+
|
577
|
+
#
|
578
|
+
# A parser that tries to match the current inputs one by one
|
579
|
+
# with the given values.
|
580
|
+
# It succeeds only when all given values are matched, in which case all the
|
581
|
+
# matched inputs are consumed.
|
582
|
+
#
|
583
|
+
def are(vals, expected="#{vals} expected")
|
584
|
+
AreParser.new(vals, expected)
|
585
|
+
end
|
586
|
+
|
587
|
+
#
|
588
|
+
# A parser that makes sure that the given values don't match
|
589
|
+
# the current inputs. One input is consumed if it succeeds.
|
590
|
+
#
|
591
|
+
def arent(vals, expected="#{vals} unexpected")
|
592
|
+
are(vals, '').not(expected) >> any
|
593
|
+
end
|
594
|
+
|
595
|
+
#
|
596
|
+
# A parser that matches the given string.
|
597
|
+
#
|
598
|
+
def string(str, msg = "\"#{str}\" expected")
|
599
|
+
are(str, msg).setName(str)
|
600
|
+
end
|
601
|
+
|
602
|
+
#
|
603
|
+
# A parser that makes sure that the current input doesn't match a string.
|
604
|
+
# One character is consumed if it succeeds.
|
605
|
+
#
|
606
|
+
def not_string(str, msg="\"#{str}\" unexpected")
|
607
|
+
string(str).not(msg) >> any
|
608
|
+
end
|
609
|
+
|
610
|
+
alias str string
|
611
|
+
|
612
|
+
#
|
613
|
+
# A parser that sequentially run the given parsers.
|
614
|
+
# The result of the last parser is used as return value.
|
615
|
+
# If a block is given, the results of the parsers are passed
|
616
|
+
# into the block as parameters, and the block return value
|
617
|
+
# is used as result instead.
|
618
|
+
#
|
619
|
+
def sequence(*parsers, &proc)
|
620
|
+
# TypeChecker.check_vararg_type Parser, parsers, :sequence
|
621
|
+
SequenceParser.new(parsers, proc)
|
622
|
+
end
|
623
|
+
def_sig :sequence, [Parser]
|
624
|
+
|
625
|
+
#
|
626
|
+
# A parser that returns the current input index (starting from 0).
|
627
|
+
#
|
628
|
+
def get_index
|
629
|
+
GetIndexParser.new.setName('get_index')
|
630
|
+
end
|
631
|
+
|
632
|
+
#
|
633
|
+
# A parser that moves the current input pointer to a certain index.
|
634
|
+
#
|
635
|
+
def set_index ind
|
636
|
+
SetIndexParser.new(ind).setName('set_index')
|
637
|
+
end
|
638
|
+
|
639
|
+
#
|
640
|
+
# A parser that tries all given alternative parsers
|
641
|
+
# and picks the one with the longest match.
|
642
|
+
#
|
643
|
+
def longest(*parsers)
|
644
|
+
# TypeChecker.check_vararg_type Parser, parsers, :longest
|
645
|
+
BestParser.new(parsers, true)
|
646
|
+
end
|
647
|
+
def_sig :longest, [Parser]
|
648
|
+
|
649
|
+
#
|
650
|
+
# A parser that tries all given alternative parsers
|
651
|
+
# and picks the one with the shortest match.
|
652
|
+
#
|
653
|
+
def shortest(*parsers)
|
654
|
+
# TypeChecker.check_vararg_type Parser, parsers, :shortest
|
655
|
+
BestParser.new(parsers, false)
|
656
|
+
end
|
657
|
+
def_sig :shortest, [Parser]
|
658
|
+
|
659
|
+
alias shorter shortest
|
660
|
+
alias longer longest
|
661
|
+
|
662
|
+
#
|
663
|
+
# A parser that consumes one input.
|
664
|
+
#
|
665
|
+
def any
|
666
|
+
AnyParser.new
|
667
|
+
end
|
668
|
+
|
669
|
+
#
|
670
|
+
# A parser that always fails.
|
671
|
+
#
|
672
|
+
def zero
|
673
|
+
ZeroParser.new
|
674
|
+
end
|
675
|
+
|
676
|
+
#
|
677
|
+
# A parser that always succeeds.
|
678
|
+
#
|
679
|
+
def one
|
680
|
+
OneParser.new
|
681
|
+
end
|
682
|
+
|
683
|
+
#
|
684
|
+
# A parser that succeeds if the current input is within a certain range.
|
685
|
+
#
|
686
|
+
def range(from, to, msg="#{as_char from}..#{as_char to} expected")
|
687
|
+
from, to = as_num(from), as_num(to)
|
688
|
+
satisfies(msg) {|c| c <= to && c >= from}
|
689
|
+
end
|
690
|
+
|
691
|
+
#
|
692
|
+
# A parser that throws a symbol.
|
693
|
+
#
|
694
|
+
def throwp(symbol)
|
695
|
+
ThrowParser.new(symbol)
|
696
|
+
end
|
697
|
+
|
698
|
+
#
|
699
|
+
# A parser that succeeds if the current inputs match
|
700
|
+
# the given regular expression.
|
701
|
+
# The matched string is consumed and returned as result.
|
702
|
+
#
|
703
|
+
def regexp(ptn, expected="/#{ptn.to_s}/ expected")
|
704
|
+
RegexpParser.new(as_regexp(ptn), expected).setName(expected)
|
705
|
+
end
|
706
|
+
|
707
|
+
#
|
708
|
+
# A parser that parses a word
|
709
|
+
# (starting with alpha or underscore, followed by 0 or more alpha, number or underscore).
|
710
|
+
# and return the matched word as string.
|
711
|
+
#
|
712
|
+
def word(expected='word expected')
|
713
|
+
regexp(/[a-zA-Z_]\w*/, expected)
|
714
|
+
end
|
715
|
+
|
716
|
+
#
|
717
|
+
# A parser that parses an integer
|
718
|
+
# and return the matched integer as string.
|
719
|
+
#
|
720
|
+
def integer(expected='integer expected')
|
721
|
+
regexp(/\d+(?!\w)/, expected)
|
722
|
+
end
|
723
|
+
|
724
|
+
#
|
725
|
+
# A parser that parses a number (integer, or decimal number)
|
726
|
+
# and return the matched number as string.
|
727
|
+
#
|
728
|
+
def number(expected='number expected')
|
729
|
+
regexp(/\d+(\.\d+)?/, expected)
|
730
|
+
end
|
731
|
+
|
732
|
+
#
|
733
|
+
# A parser that matches the given string, case insensitively.
|
734
|
+
#
|
735
|
+
def string_nocase(str, expected="'#{str}' expected")
|
736
|
+
StringCaseInsensitiveParser.new(str, expected).setName(str)
|
737
|
+
end
|
738
|
+
|
739
|
+
#
|
740
|
+
# A parser that succeeds when the current input
|
741
|
+
# is a token with one of the the given token kinds.
|
742
|
+
# If a block is given, the token text is passed to the block
|
743
|
+
# as parameter, and the block return value is used as result.
|
744
|
+
# Otherwise, the token object is used as result.
|
745
|
+
#
|
746
|
+
def token(*kinds, &proc)
|
747
|
+
expected="#{kinds.join(' or ')} expected"
|
748
|
+
recognizer = nil
|
749
|
+
if kinds.length==1
|
750
|
+
kind = kinds[0]
|
751
|
+
recognizer = satisfies(expected) do |tok|
|
752
|
+
tok.respond_to? :kind, :text and kind == tok.kind
|
753
|
+
end
|
754
|
+
else
|
755
|
+
recognizer = satisfies(expected) do |tok|
|
756
|
+
tok.respond_to? :kind, :text and kinds.include? tok.kind
|
757
|
+
end
|
758
|
+
end
|
759
|
+
recognizer = recognizer.map{|tok|proc.call(tok.text)} if proc
|
760
|
+
recognizer
|
761
|
+
end
|
762
|
+
|
763
|
+
#
|
764
|
+
# A parser that parses a white space character.
|
765
|
+
#
|
766
|
+
def whitespace(expected="whitespace expected")
|
767
|
+
satisfies(expected) {|c| Whitespaces.include? c}
|
768
|
+
end
|
769
|
+
|
770
|
+
#
|
771
|
+
# A parser that parses 1 or more white space characters.
|
772
|
+
#
|
773
|
+
def whitespaces(expected="whitespace(s) expected")
|
774
|
+
whitespace(expected).many_(1)
|
775
|
+
end
|
776
|
+
|
777
|
+
#
|
778
|
+
# A parser that parses a line started with _start_.
|
779
|
+
# nil is the result.
|
780
|
+
#
|
781
|
+
def comment_line start
|
782
|
+
string(start) >> not_char(?\n).many_ >> char(?\n).optional >> value(nil)
|
783
|
+
end
|
784
|
+
|
785
|
+
#
|
786
|
+
# A parser that parses a chunk of text started with _open_
|
787
|
+
# and ended by _close_.
|
788
|
+
# nil is the result.
|
789
|
+
#
|
790
|
+
def comment_block open, close
|
791
|
+
string(open) >> not_string(close).many_ >> string(close) >> value(nil)
|
792
|
+
end
|
793
|
+
|
794
|
+
#
|
795
|
+
# A lazy parser, when executed, calls the given block
|
796
|
+
# to get a parser object and delegate the call to this lazily
|
797
|
+
# instantiated parser.
|
798
|
+
#
|
799
|
+
def lazy(&block)
|
800
|
+
LazyParser.new(block)
|
801
|
+
end
|
802
|
+
|
803
|
+
#
|
804
|
+
# A parser that watches the current parser result without changing it.
|
805
|
+
# The following assert will succeed:
|
806
|
+
##
|
807
|
+
# char(?a) >> watch{|x|assert_equal(?a, x)}
|
808
|
+
##
|
809
|
+
# watch can also be used as a handy tool to print trace information,
|
810
|
+
# for example:
|
811
|
+
##
|
812
|
+
# some_parser >> watch {puts "some_parser succeeded."}
|
813
|
+
#
|
814
|
+
def watch(&block)
|
815
|
+
return one unless block
|
816
|
+
WatchParser.new(block)
|
817
|
+
end
|
818
|
+
|
819
|
+
#
|
820
|
+
# A parser that watches the current parser result without changing it.
|
821
|
+
# The following assert will succeed:
|
822
|
+
##
|
823
|
+
# char(?a).repeat(2) >> watchn{|x,y|assert_equal([?a,?a], [x,y])}
|
824
|
+
##
|
825
|
+
# Slightly different from _watch_, _watchn_ expands the current parser result
|
826
|
+
# before passing it into the associated block.
|
827
|
+
#
|
828
|
+
def watchn(&block)
|
829
|
+
return one unless block
|
830
|
+
WatchnParser.new(block)
|
831
|
+
end
|
832
|
+
|
833
|
+
#
|
834
|
+
# A parser that maps current parser result to a new result using
|
835
|
+
# the given block.
|
836
|
+
##
|
837
|
+
# Different from Parser#map, this method does not need to be combined
|
838
|
+
# with any Parser object. It is rather an independent Parser object
|
839
|
+
# that maps the _current_ parser result.
|
840
|
+
##
|
841
|
+
# parser1.map{|x|...} is equivalent to parser1 >> map{|x|...}
|
842
|
+
#
|
843
|
+
def map(&block)
|
844
|
+
return one unless block
|
845
|
+
MapCurrentParser.new(block)
|
846
|
+
end
|
847
|
+
|
848
|
+
#
|
849
|
+
# A parser that maps current parser result to a new result using
|
850
|
+
# the given block. If the current parser result is an array, the array
|
851
|
+
# elements are expanded and then passed as parameters to the block.
|
852
|
+
##
|
853
|
+
# Different from Parser#mapn, this method does not need to be combined
|
854
|
+
# with any Parser object. It is rather an independent Parser object
|
855
|
+
# that maps the _current_ parser result.
|
856
|
+
##
|
857
|
+
# parser1.mapn{|x,y|...} is equivalent to parser1 >> mapn{|x,y|...}
|
858
|
+
#
|
859
|
+
def mapn(&block)
|
860
|
+
return one unless block
|
861
|
+
MapnCurrentParser.new(block)
|
862
|
+
end
|
863
|
+
|
864
|
+
private
|
865
|
+
|
866
|
+
#
|
867
|
+
# characters considered white space.
|
868
|
+
#
|
869
|
+
Whitespaces = " \t\r\n"
|
870
|
+
|
871
|
+
def as_regexp ptn
|
872
|
+
case ptn when String then Regexp.new(ptn) else ptn end
|
873
|
+
end
|
874
|
+
|
875
|
+
def as_char c
|
876
|
+
case c when String then c else c.chr end
|
877
|
+
end
|
878
|
+
|
879
|
+
def as_num c
|
880
|
+
case c when String: c[0] else c end
|
881
|
+
end
|
882
|
+
|
883
|
+
def as_list vals
|
884
|
+
return vals unless vals.length==1
|
885
|
+
val = vals[0]
|
886
|
+
return vals unless val.kind_of? String
|
887
|
+
val
|
888
|
+
end
|
889
|
+
|
890
|
+
extend self
|
891
|
+
end
|
892
|
+
|