abnf 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.travis.yml +16 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +194 -0
- data/Rakefile +18 -0
- data/abnf.gemspec +24 -0
- data/lib/abnf.rb +57 -0
- data/lib/abnf/abnf.rb +136 -0
- data/lib/abnf/corerules.rb +28 -0
- data/lib/abnf/grammar.rb +183 -0
- data/lib/abnf/parser.output +348 -0
- data/lib/abnf/parser.rb +821 -0
- data/lib/abnf/parser.y +156 -0
- data/lib/abnf/regexp.rb +394 -0
- data/lib/abnf/version.rb +3 -0
- data/lib/natset.rb +411 -0
- data/lib/regexptree.rb +530 -0
- data/sample/in-place.rb +26 -0
- data/sample/ipv6.rb +42 -0
- data/sample/multiples-of-3.rb +19 -0
- data/sample/uri.rb +75 -0
- data/test/abnf_test.rb +82 -0
- data/test/regexptree_test.rb +12 -0
- data/test/test_helper.rb +3 -0
- metadata +115 -0
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'abnf/abnf'
|
2
|
+
require 'abnf/parser'
|
3
|
+
|
4
|
+
class ABNF
|
5
|
+
CoreRules = ABNF.parse(<<'End', true) # taken from RFC 2234
|
6
|
+
ALPHA = %x41-5A / %x61-7A ; A-Z / a-z
|
7
|
+
BIT = "0" / "1"
|
8
|
+
CHAR = %x01-7F ; any 7-bit US-ASCII character, excluding NUL
|
9
|
+
CR = %x0D ; carriage return
|
10
|
+
CRLF = CR LF ; Internet standard newline
|
11
|
+
CTL = %x00-1F / %x7F ; controls
|
12
|
+
DIGIT = %x30-39 ; 0-9
|
13
|
+
DQUOTE = %x22 ; " (Double Quote)
|
14
|
+
HEXDIG = DIGIT / "A" / "B" / "C" / "D" / "E" / "F"
|
15
|
+
HTAB = %x09 ; horizontal tab
|
16
|
+
LF = %x0A ; linefeed
|
17
|
+
LWSP = *(WSP / CRLF WSP) ; linear white space (past newline)
|
18
|
+
OCTET = %x00-FF ; 8 bits of data
|
19
|
+
SP = %x20
|
20
|
+
VCHAR = %x21-7E ; visible (printing) characters
|
21
|
+
WSP = SP / HTAB ; white space
|
22
|
+
End
|
23
|
+
end
|
24
|
+
|
25
|
+
if $0 == __FILE__
|
26
|
+
require 'pp'
|
27
|
+
pp ABNF::CoreRules
|
28
|
+
end
|
data/lib/abnf/grammar.rb
ADDED
@@ -0,0 +1,183 @@
|
|
1
|
+
require 'natset'
|
2
|
+
|
3
|
+
class ABNF
|
4
|
+
class Elt
|
5
|
+
# A variable is assumed as not empty set.
|
6
|
+
def empty_set?
|
7
|
+
false
|
8
|
+
end
|
9
|
+
|
10
|
+
# A variable is assumed as not empty sequence.
|
11
|
+
def empty_sequence?
|
12
|
+
false
|
13
|
+
end
|
14
|
+
|
15
|
+
def +(other)
|
16
|
+
Seq.new(self, other)
|
17
|
+
end
|
18
|
+
|
19
|
+
def |(other)
|
20
|
+
Alt.new(self, other)
|
21
|
+
end
|
22
|
+
|
23
|
+
def *(n)
|
24
|
+
case n
|
25
|
+
when Integer
|
26
|
+
rep(n, n)
|
27
|
+
when Range
|
28
|
+
rep(n.first, n.last - (n.exclude_end? ? 1 : 0))
|
29
|
+
else
|
30
|
+
raise TypeError.new("Integer or Range expected: #{n}")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def rep(min=0, max=nil, greedy=true)
|
35
|
+
Rep.new(self, min, max, greedy)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
class Alt < Elt
|
40
|
+
class << Alt
|
41
|
+
alias _new new
|
42
|
+
end
|
43
|
+
|
44
|
+
def Alt.new(*elts)
|
45
|
+
elts2 = []
|
46
|
+
elts.each {|e|
|
47
|
+
if e.empty_set?
|
48
|
+
next
|
49
|
+
elsif Alt === e
|
50
|
+
elts2.concat e.elts
|
51
|
+
elsif Term === e
|
52
|
+
if Term === elts2.last
|
53
|
+
elts2[-1] = Term.new(elts2.last.natset + e.natset)
|
54
|
+
else
|
55
|
+
elts2 << e
|
56
|
+
end
|
57
|
+
else
|
58
|
+
elts2 << e
|
59
|
+
end
|
60
|
+
}
|
61
|
+
case elts2.length
|
62
|
+
when 0; EmptySet
|
63
|
+
when 1; elts2.first
|
64
|
+
else; Alt._new(*elts2)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def initialize(*elts)
|
69
|
+
@elts = elts
|
70
|
+
end
|
71
|
+
attr_reader :elts
|
72
|
+
|
73
|
+
def empty_set?
|
74
|
+
@elts.empty?
|
75
|
+
end
|
76
|
+
|
77
|
+
def each_var(&block) @elts.each {|elt| elt.each_var(&block)} end
|
78
|
+
def subst_var(&block) Alt.new(*@elts.map {|elt| elt.subst_var(&block)}) end
|
79
|
+
end
|
80
|
+
EmptySet = Alt._new
|
81
|
+
|
82
|
+
class Seq < Elt
|
83
|
+
class << Seq
|
84
|
+
alias _new new
|
85
|
+
end
|
86
|
+
|
87
|
+
def Seq.new(*elts)
|
88
|
+
elts2 = []
|
89
|
+
elts.each {|e|
|
90
|
+
if e.empty_sequence?
|
91
|
+
next
|
92
|
+
elsif Seq === e
|
93
|
+
elts2.concat e.elts
|
94
|
+
elsif e.empty_set?
|
95
|
+
return EmptySet
|
96
|
+
else
|
97
|
+
elts2 << e
|
98
|
+
end
|
99
|
+
}
|
100
|
+
case elts2.length
|
101
|
+
when 0; EmptySequence
|
102
|
+
when 1; elts2.first
|
103
|
+
else; Seq._new(*elts2)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def initialize(*elts)
|
108
|
+
@elts = elts
|
109
|
+
end
|
110
|
+
attr_reader :elts
|
111
|
+
|
112
|
+
def empty_sequence?
|
113
|
+
@elts.empty?
|
114
|
+
end
|
115
|
+
|
116
|
+
def each_var(&block) @elts.each {|elt| elt.each_var(&block)} end
|
117
|
+
def subst_var(&block) Seq.new(*@elts.map {|elt| elt.subst_var(&block)}) end
|
118
|
+
end
|
119
|
+
EmptySequence = Seq._new
|
120
|
+
|
121
|
+
class Rep < Elt
|
122
|
+
class << Rep
|
123
|
+
alias _new new
|
124
|
+
end
|
125
|
+
|
126
|
+
def Rep.new(elt, min=0, max=nil, greedy=true)
|
127
|
+
return EmptySequence if min == 0 && max == 0
|
128
|
+
return elt if min == 1 && max == 1
|
129
|
+
return EmptySequence if elt.empty_sequence?
|
130
|
+
if elt.empty_set?
|
131
|
+
return min == 0 ? EmptySequence : EmptySet
|
132
|
+
end
|
133
|
+
Rep._new(elt, min, max, greedy)
|
134
|
+
end
|
135
|
+
|
136
|
+
def initialize(elt, min=0, max=nil, greedy=true)
|
137
|
+
@elt = elt
|
138
|
+
@min = min
|
139
|
+
@max = max
|
140
|
+
@greedy = greedy
|
141
|
+
end
|
142
|
+
attr_reader :elt, :min, :max, :greedy
|
143
|
+
|
144
|
+
def each_var(&block) @elt.each_var(&block) end
|
145
|
+
def subst_var(&block) Rep.new(@elt.subst_var(&block), min, max, greedy) end
|
146
|
+
end
|
147
|
+
|
148
|
+
class Var < Elt
|
149
|
+
def initialize(name)
|
150
|
+
@name = name
|
151
|
+
end
|
152
|
+
attr_reader :name
|
153
|
+
|
154
|
+
def each_var(&block) yield @name end
|
155
|
+
def subst_var(&block) yield(@name) || self end
|
156
|
+
end
|
157
|
+
|
158
|
+
class Term < Elt
|
159
|
+
class << Term
|
160
|
+
alias _new new
|
161
|
+
end
|
162
|
+
|
163
|
+
def Term.new(natset)
|
164
|
+
if natset.empty?
|
165
|
+
EmptySet
|
166
|
+
else
|
167
|
+
Term._new(natset)
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
def initialize(natset)
|
172
|
+
@natset = natset
|
173
|
+
end
|
174
|
+
attr_reader :natset
|
175
|
+
|
176
|
+
def empty_set?
|
177
|
+
@natset.empty?
|
178
|
+
end
|
179
|
+
|
180
|
+
def each_var(&block) end
|
181
|
+
def subst_var(&block) self end
|
182
|
+
end
|
183
|
+
end
|
@@ -0,0 +1,348 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
-------- Grammar --------
|
4
|
+
|
5
|
+
rule 1 rulelist:
|
6
|
+
rule 2 rulelist: rulelist rule
|
7
|
+
rule 3 rule: defname assign alt
|
8
|
+
rule 4 alt: seq
|
9
|
+
rule 5 alt: alt altop seq
|
10
|
+
rule 6 seq: rep
|
11
|
+
rule 7 seq: seq rep
|
12
|
+
rule 8 rep: element
|
13
|
+
rule 9 rep: repeat element
|
14
|
+
rule 10 repeat: repop
|
15
|
+
rule 11 repeat: repop int
|
16
|
+
rule 12 repeat: int
|
17
|
+
rule 13 repeat: int repop
|
18
|
+
rule 14 repeat: int repop int
|
19
|
+
rule 15 element: name
|
20
|
+
rule 16 element: lparen alt rparen
|
21
|
+
rule 17 element: lbracket alt rbracket
|
22
|
+
rule 18 element: val
|
23
|
+
|
24
|
+
------- Symbols -------
|
25
|
+
|
26
|
+
**Nonterminals, with rules where they appear
|
27
|
+
|
28
|
+
$start (13)
|
29
|
+
on right:
|
30
|
+
on left :
|
31
|
+
rulelist (14)
|
32
|
+
on right: 2
|
33
|
+
on left : 1 2
|
34
|
+
rule (15)
|
35
|
+
on right: 2
|
36
|
+
on left : 3
|
37
|
+
alt (16)
|
38
|
+
on right: 3 5 16 17
|
39
|
+
on left : 4 5
|
40
|
+
seq (17)
|
41
|
+
on right: 4 5 7
|
42
|
+
on left : 6 7
|
43
|
+
rep (18)
|
44
|
+
on right: 6 7
|
45
|
+
on left : 8 9
|
46
|
+
element (19)
|
47
|
+
on right: 8 9
|
48
|
+
on left : 15 16 17 18
|
49
|
+
repeat (20)
|
50
|
+
on right: 9
|
51
|
+
on left : 10 11 12 13 14
|
52
|
+
|
53
|
+
**Terminals, with rules where they appear
|
54
|
+
|
55
|
+
$end (0)
|
56
|
+
error (1)
|
57
|
+
defname (2) 3
|
58
|
+
assign (3) 3
|
59
|
+
altop (4) 5
|
60
|
+
repop (5) 10 11 13 14
|
61
|
+
int (6) 11 12 13 14
|
62
|
+
name (7) 15
|
63
|
+
lparen (8) 16
|
64
|
+
rparen (9) 16
|
65
|
+
lbracket (10) 17
|
66
|
+
rbracket (11) 17
|
67
|
+
val (12) 18
|
68
|
+
|
69
|
+
--------- State ---------
|
70
|
+
|
71
|
+
state 0
|
72
|
+
|
73
|
+
|
74
|
+
$default reduce using rule 1 (rulelist)
|
75
|
+
|
76
|
+
rulelist go to state 1
|
77
|
+
|
78
|
+
state 1
|
79
|
+
|
80
|
+
2) rulelist : rulelist _ rule
|
81
|
+
|
82
|
+
$end shift, and go to state 2
|
83
|
+
defname shift, and go to state 4
|
84
|
+
|
85
|
+
rule go to state 3
|
86
|
+
|
87
|
+
state 2
|
88
|
+
|
89
|
+
|
90
|
+
$end shift, and go to state 5
|
91
|
+
|
92
|
+
|
93
|
+
state 3
|
94
|
+
|
95
|
+
2) rulelist : rulelist rule _
|
96
|
+
|
97
|
+
$default reduce using rule 2 (rulelist)
|
98
|
+
|
99
|
+
|
100
|
+
state 4
|
101
|
+
|
102
|
+
3) rule : defname _ assign alt
|
103
|
+
|
104
|
+
assign shift, and go to state 6
|
105
|
+
|
106
|
+
|
107
|
+
state 5
|
108
|
+
|
109
|
+
|
110
|
+
$default accept
|
111
|
+
|
112
|
+
|
113
|
+
state 6
|
114
|
+
|
115
|
+
3) rule : defname assign _ alt
|
116
|
+
|
117
|
+
repop shift, and go to state 12
|
118
|
+
int shift, and go to state 13
|
119
|
+
name shift, and go to state 14
|
120
|
+
lparen shift, and go to state 15
|
121
|
+
lbracket shift, and go to state 16
|
122
|
+
val shift, and go to state 17
|
123
|
+
|
124
|
+
alt go to state 7
|
125
|
+
seq go to state 8
|
126
|
+
rep go to state 9
|
127
|
+
element go to state 10
|
128
|
+
repeat go to state 11
|
129
|
+
|
130
|
+
state 7
|
131
|
+
|
132
|
+
3) rule : defname assign alt _
|
133
|
+
5) alt : alt _ altop seq
|
134
|
+
|
135
|
+
altop shift, and go to state 18
|
136
|
+
$default reduce using rule 3 (rule)
|
137
|
+
|
138
|
+
|
139
|
+
state 8
|
140
|
+
|
141
|
+
4) alt : seq _
|
142
|
+
7) seq : seq _ rep
|
143
|
+
|
144
|
+
repop shift, and go to state 12
|
145
|
+
int shift, and go to state 13
|
146
|
+
name shift, and go to state 14
|
147
|
+
lparen shift, and go to state 15
|
148
|
+
lbracket shift, and go to state 16
|
149
|
+
val shift, and go to state 17
|
150
|
+
$default reduce using rule 4 (alt)
|
151
|
+
|
152
|
+
rep go to state 19
|
153
|
+
element go to state 10
|
154
|
+
repeat go to state 11
|
155
|
+
|
156
|
+
state 9
|
157
|
+
|
158
|
+
6) seq : rep _
|
159
|
+
|
160
|
+
$default reduce using rule 6 (seq)
|
161
|
+
|
162
|
+
|
163
|
+
state 10
|
164
|
+
|
165
|
+
8) rep : element _
|
166
|
+
|
167
|
+
$default reduce using rule 8 (rep)
|
168
|
+
|
169
|
+
|
170
|
+
state 11
|
171
|
+
|
172
|
+
9) rep : repeat _ element
|
173
|
+
|
174
|
+
name shift, and go to state 14
|
175
|
+
lparen shift, and go to state 15
|
176
|
+
lbracket shift, and go to state 16
|
177
|
+
val shift, and go to state 17
|
178
|
+
|
179
|
+
element go to state 20
|
180
|
+
|
181
|
+
state 12
|
182
|
+
|
183
|
+
10) repeat : repop _
|
184
|
+
11) repeat : repop _ int
|
185
|
+
|
186
|
+
int shift, and go to state 21
|
187
|
+
$default reduce using rule 10 (repeat)
|
188
|
+
|
189
|
+
|
190
|
+
state 13
|
191
|
+
|
192
|
+
12) repeat : int _
|
193
|
+
13) repeat : int _ repop
|
194
|
+
14) repeat : int _ repop int
|
195
|
+
|
196
|
+
repop shift, and go to state 22
|
197
|
+
$default reduce using rule 12 (repeat)
|
198
|
+
|
199
|
+
|
200
|
+
state 14
|
201
|
+
|
202
|
+
15) element : name _
|
203
|
+
|
204
|
+
$default reduce using rule 15 (element)
|
205
|
+
|
206
|
+
|
207
|
+
state 15
|
208
|
+
|
209
|
+
16) element : lparen _ alt rparen
|
210
|
+
|
211
|
+
repop shift, and go to state 12
|
212
|
+
int shift, and go to state 13
|
213
|
+
name shift, and go to state 14
|
214
|
+
lparen shift, and go to state 15
|
215
|
+
lbracket shift, and go to state 16
|
216
|
+
val shift, and go to state 17
|
217
|
+
|
218
|
+
seq go to state 8
|
219
|
+
alt go to state 23
|
220
|
+
rep go to state 9
|
221
|
+
element go to state 10
|
222
|
+
repeat go to state 11
|
223
|
+
|
224
|
+
state 16
|
225
|
+
|
226
|
+
17) element : lbracket _ alt rbracket
|
227
|
+
|
228
|
+
repop shift, and go to state 12
|
229
|
+
int shift, and go to state 13
|
230
|
+
name shift, and go to state 14
|
231
|
+
lparen shift, and go to state 15
|
232
|
+
lbracket shift, and go to state 16
|
233
|
+
val shift, and go to state 17
|
234
|
+
|
235
|
+
seq go to state 8
|
236
|
+
alt go to state 24
|
237
|
+
rep go to state 9
|
238
|
+
element go to state 10
|
239
|
+
repeat go to state 11
|
240
|
+
|
241
|
+
state 17
|
242
|
+
|
243
|
+
18) element : val _
|
244
|
+
|
245
|
+
$default reduce using rule 18 (element)
|
246
|
+
|
247
|
+
|
248
|
+
state 18
|
249
|
+
|
250
|
+
5) alt : alt altop _ seq
|
251
|
+
|
252
|
+
repop shift, and go to state 12
|
253
|
+
int shift, and go to state 13
|
254
|
+
name shift, and go to state 14
|
255
|
+
lparen shift, and go to state 15
|
256
|
+
lbracket shift, and go to state 16
|
257
|
+
val shift, and go to state 17
|
258
|
+
|
259
|
+
seq go to state 25
|
260
|
+
rep go to state 9
|
261
|
+
element go to state 10
|
262
|
+
repeat go to state 11
|
263
|
+
|
264
|
+
state 19
|
265
|
+
|
266
|
+
7) seq : seq rep _
|
267
|
+
|
268
|
+
$default reduce using rule 7 (seq)
|
269
|
+
|
270
|
+
|
271
|
+
state 20
|
272
|
+
|
273
|
+
9) rep : repeat element _
|
274
|
+
|
275
|
+
$default reduce using rule 9 (rep)
|
276
|
+
|
277
|
+
|
278
|
+
state 21
|
279
|
+
|
280
|
+
11) repeat : repop int _
|
281
|
+
|
282
|
+
$default reduce using rule 11 (repeat)
|
283
|
+
|
284
|
+
|
285
|
+
state 22
|
286
|
+
|
287
|
+
13) repeat : int repop _
|
288
|
+
14) repeat : int repop _ int
|
289
|
+
|
290
|
+
int shift, and go to state 26
|
291
|
+
$default reduce using rule 13 (repeat)
|
292
|
+
|
293
|
+
|
294
|
+
state 23
|
295
|
+
|
296
|
+
5) alt : alt _ altop seq
|
297
|
+
16) element : lparen alt _ rparen
|
298
|
+
|
299
|
+
altop shift, and go to state 18
|
300
|
+
rparen shift, and go to state 27
|
301
|
+
|
302
|
+
|
303
|
+
state 24
|
304
|
+
|
305
|
+
5) alt : alt _ altop seq
|
306
|
+
17) element : lbracket alt _ rbracket
|
307
|
+
|
308
|
+
altop shift, and go to state 18
|
309
|
+
rbracket shift, and go to state 28
|
310
|
+
|
311
|
+
|
312
|
+
state 25
|
313
|
+
|
314
|
+
5) alt : alt altop seq _
|
315
|
+
7) seq : seq _ rep
|
316
|
+
|
317
|
+
repop shift, and go to state 12
|
318
|
+
int shift, and go to state 13
|
319
|
+
name shift, and go to state 14
|
320
|
+
lparen shift, and go to state 15
|
321
|
+
lbracket shift, and go to state 16
|
322
|
+
val shift, and go to state 17
|
323
|
+
$default reduce using rule 5 (alt)
|
324
|
+
|
325
|
+
rep go to state 19
|
326
|
+
element go to state 10
|
327
|
+
repeat go to state 11
|
328
|
+
|
329
|
+
state 26
|
330
|
+
|
331
|
+
14) repeat : int repop int _
|
332
|
+
|
333
|
+
$default reduce using rule 14 (repeat)
|
334
|
+
|
335
|
+
|
336
|
+
state 27
|
337
|
+
|
338
|
+
16) element : lparen alt rparen _
|
339
|
+
|
340
|
+
$default reduce using rule 16 (element)
|
341
|
+
|
342
|
+
|
343
|
+
state 28
|
344
|
+
|
345
|
+
17) element : lbracket alt rbracket _
|
346
|
+
|
347
|
+
$default reduce using rule 17 (element)
|
348
|
+
|