-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrasta.rb
317 lines (251 loc) · 5.07 KB
/
rasta.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
module Rasta
require 'strscan'
# We are going to need a really flexible buffer implementation
# for incremental parsing, why the native ruby StringScanner doesn't
# work on streams is beyond me. however we use it for now.
# class Buffer
#
# end
def Builder; end
class Rule
attr_accessor :name
def initialize
@debug = false
end
def parse?(string)
ss = StringScanner.new(string)
x = self.parse(ss, Builder.new)
if @debug && x.class == Failure
p x
end
x.class != Failure && ss.eos?
end
def parse_str(string, builder = Builder.new)
ss = StringScanner.new(string)
self.parse(ss, builder)
end
# This function returns a piece of ast on success
# or a Failure object, nil means this rule doesn't
# generate any AST
def parse(buffer, builder)
nil
end
def plus
More.new(self, 1)
end
def star
More.new(self, 0)
end
def opt
More.new(self, 0, 1)
end
def >>(rule)
s = Sequence.new(self)
s >> rule
end
def <<(rule)
s = Sequence.new(self)
s << rule
end
def |(rule)
Choice.new(self, rule)
end
def fail(rule, inner_failure = nil, info = {})
Failure.new(inner_failure, info.merge(:rule => rule))
end
end
class Failure
attr_reader :context
def initialize(inner_failure, info = {})
@context = Array.new
@context += inner_failure.context if inner_failure
@context << info
end
def inspect
"Parse Failure:\n" +
@context.join("\n")
end
end
class Terminal < Rule
def initialize(terminal)
super()
self.name = terminal.inspect
if terminal.class == String
@terminal = /#{Regexp.quote(terminal)}/
else
@terminal = terminal
end
end
def parse(buffer, builder)
if s = buffer.scan(@terminal)
builder.node(self, nil, :string => s)
else
fail(self)
end
end
end
def t(rule)
Terminal.new(rule)
end
class BlockRule < Rule
def initialize(&block)
super()
@block = block
end
def parse(buffer, builder)
@block[buffer, builder]
end
end
# This is a special class that allows bindings wrangling
class RuleRef < BlockRule
def parse(buffer, builder)
ref = @block.call
ref.parse(buffer, builder)
end
end
def ref(&block)
RuleRef.new(&block)
end
class MultiRule < Rule
attr_accessor :rules
def initialize(*rules)
super()
@rules = rules.flatten
end
end
# Covers sequence rules
class Sequence < MultiRule
# TODO Sequences seem to know too much now
attr_reader :flattens
attr_accessor :trans
def initialize(*rules)
super
@flattens = {}
end
def parse(buffer, builder)
c = []
for x in @rules
n = x.parse(buffer, builder)
if n.class == Failure
return fail(self, n)
else
if @flattens[x] then
# TODO find a more elegant way to do this
if n.class == Array then
c += n
elsif n.respond_to?(:children)
c += n.children
end
# TODO throw an error or something here
else
c << n
end
end
end
c.compact!
if @trans && c.length == 1
c[0]
else
builder.node(self, c)
end
end
def >>(rule)
@rules << rule
self
end
def <<(rule)
if rule.class == Sequence
@rules = @rules + rule.rules
@flattens.merge!(rule.flattens)
else
@rules << rule
@flattens[rule] = true
end
self
end
end
def seq(*rules)
Sequence.new(*rules)
end
# class Array
# def seq
# Sequence.new(self)
# end
# end
# Covers ordered choice rules
class Choice < MultiRule
def parse(buffer, builder)
n = nil
for x in @rules
pos = buffer.pos
n = x.parse(buffer, builder)
if n.class != Failure
return n
else
buffer.pos = pos
end
end
fail(self, n)
end
def |(rule)
@rules << rule
self
end
end
def choose(*rules)
Sequence.new(*rules)
end
# Covers zero-or-more, one-or-more and optional
class More < Rule
def initialize(rule, min = 0, max = -1)
super()
@rule = rule
@min = min
@max = max
end
def parse(buffer, builder)
pos = buffer.pos
c = []
while (n = @rule.parse(buffer, builder)).class != Failure
c << n
if @max > 0 && c.length >= @max then
break
end
end
if c.length >= @min
c.compact!
builder.node(self, c)
else
buffer.pos = pos
fail(self)
end
end
end
# Covers and and not syntactic predicates
class Peek < Rule
def initialize(rule, negate = false)
super()
@rule = rule
@negate = negate
end
def parse(buffer, builder)
pos = buffer.pos
n = @rule.parse(buffer, builder)
buffer.pos = pos
if (n.class != Failure) ^ @negate then
nil
else
fail(self)
end
end
end
def has(rule)
Peek.new(rule)
end
def has_not(rule)
Peek.new(rule, true)
end
require 'builder'
require 'action'
require 'inspect'
end # module Rasta