pegex 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,414 @@
1
+ require 'json'
2
+ require 'pegex/grammar'
3
+
4
+ class Pegex::Pegex;end
5
+
6
+ class Pegex::Pegex::Grammar < Pegex::Grammar
7
+ file = '../../pegex-pgx/pegex.pgx'
8
+
9
+ def make_tree
10
+ JSON.load <<'...'
11
+ {
12
+ "+grammar" : "pegex",
13
+ "+toprule" : "grammar",
14
+ "+version" : "0.2.0",
15
+ "ERROR_all_group" : {
16
+ "+min" : 1,
17
+ ".ref" : "ERROR_rule_part",
18
+ ".sep" : {
19
+ ".rgx" : "(?:\\s|\\#.*(?:\\n|\\z))*"
20
+ }
21
+ },
22
+ "ERROR_any_group" : {
23
+ "+min" : "2",
24
+ ".ref" : "ERROR_all_group",
25
+ ".sep" : {
26
+ ".rgx" : "(?:\\s|\\#.*(?:\\n|\\z))*\\|(?:\\s|\\#.*(?:\\n|\\z))*"
27
+ }
28
+ },
29
+ "ERROR_bracketed_group" : {
30
+ ".any" : [
31
+ {
32
+ ".all" : [
33
+ {
34
+ ".rgx" : "(?!\\.)(?=[^\\w\\(\\)</\\~\\|`\\s]\\()"
35
+ },
36
+ {
37
+ ".err" : "Illegal group rule modifier (can only use .)"
38
+ }
39
+ ]
40
+ },
41
+ {
42
+ ".all" : [
43
+ {
44
+ ".rgx" : "(\\.?)\\((?:\\s|\\#.*(?:\\n|\\z))*"
45
+ },
46
+ {
47
+ ".ref" : "rule_group"
48
+ },
49
+ {
50
+ ".any" : [
51
+ {
52
+ ".all" : [
53
+ {
54
+ "+asr" : 1,
55
+ ".ref" : "doc_ending"
56
+ },
57
+ {
58
+ ".err" : "Runaway rule group no ending parens at EOF"
59
+ }
60
+ ]
61
+ },
62
+ {
63
+ ".all" : [
64
+ {
65
+ ".rgx" : "(?=(?:\\s|\\#.*(?:\\n|\\z))*\\)[^\\w\\(\\)</\\~\\|`\\s\\*\\+\\?!=\\+\\-\\.:;])"
66
+ },
67
+ {
68
+ ".err" : "Illegal character in group rule quantifier"
69
+ }
70
+ ]
71
+ }
72
+ ]
73
+ }
74
+ ]
75
+ }
76
+ ]
77
+ },
78
+ "ERROR_error_message" : {
79
+ ".any" : [
80
+ {
81
+ ".all" : [
82
+ {
83
+ ".rgx" : "(?=`[^`\\r\\n]*[\\r\\n][^`]*`)"
84
+ },
85
+ {
86
+ ".err" : "Multi-line error messages not allowed!"
87
+ }
88
+ ]
89
+ },
90
+ {
91
+ ".all" : [
92
+ {
93
+ ".rgx" : "(?=`[^`]*(?:\\s|\\#.*(?:\\n|\\z))*\\z)"
94
+ },
95
+ {
96
+ ".err" : "Runaway error message no ending grave at EOF"
97
+ }
98
+ ]
99
+ }
100
+ ]
101
+ },
102
+ "ERROR_meta_definition" : {
103
+ ".all" : [
104
+ {
105
+ ".rgx" : "(?=%\\w+)"
106
+ },
107
+ {
108
+ ".err" : "Illegal meta rule"
109
+ }
110
+ ]
111
+ },
112
+ "ERROR_regular_expression" : {
113
+ ".all" : [
114
+ {
115
+ ".rgx" : "(?=/([^/]*)(?:\\s|\\#.*(?:\\n|\\z))*\\z)"
116
+ },
117
+ {
118
+ ".err" : "Runaway regular expression no ending slash at EOF"
119
+ }
120
+ ]
121
+ },
122
+ "ERROR_rule_definition" : {
123
+ ".all" : [
124
+ {
125
+ ".ref" : "ERROR_rule_start"
126
+ },
127
+ {
128
+ ".ref" : "ERROR_rule_group"
129
+ },
130
+ {
131
+ ".any" : [
132
+ {
133
+ ".ref" : "ending"
134
+ },
135
+ {
136
+ ".err" : "Rule ending syntax error"
137
+ }
138
+ ]
139
+ }
140
+ ]
141
+ },
142
+ "ERROR_rule_group" : {
143
+ ".any" : [
144
+ {
145
+ ".ref" : "ERROR_any_group"
146
+ },
147
+ {
148
+ ".ref" : "ERROR_all_group"
149
+ }
150
+ ]
151
+ },
152
+ "ERROR_rule_item" : {
153
+ ".any" : [
154
+ {
155
+ ".ref" : "rule_item"
156
+ },
157
+ {
158
+ ".ref" : "ERROR_rule_reference"
159
+ },
160
+ {
161
+ ".ref" : "ERROR_regular_expression"
162
+ },
163
+ {
164
+ ".ref" : "ERROR_bracketed_group"
165
+ },
166
+ {
167
+ ".ref" : "ERROR_error_message"
168
+ }
169
+ ]
170
+ },
171
+ "ERROR_rule_part" : {
172
+ "+max" : "2",
173
+ "+min" : "1",
174
+ ".ref" : "ERROR_rule_item",
175
+ ".sep" : {
176
+ ".rgx" : "(?:\\s|\\#.*(?:\\n|\\z))+(%{1,2})(?:\\s|\\#.*(?:\\n|\\z))+"
177
+ }
178
+ },
179
+ "ERROR_rule_reference" : {
180
+ ".any" : [
181
+ {
182
+ ".all" : [
183
+ {
184
+ ".rgx" : "(?=[!=\\+\\-\\.]?<[a-zA-Z]\\w*\\b(?!>))"
185
+ },
186
+ {
187
+ ".err" : "Missing > in rule reference"
188
+ }
189
+ ]
190
+ },
191
+ {
192
+ ".all" : [
193
+ {
194
+ ".rgx" : "(?=[!=\\+\\-\\.]?[a-zA-Z]\\w*\\b>)"
195
+ },
196
+ {
197
+ ".err" : "Missing < in rule reference"
198
+ }
199
+ ]
200
+ },
201
+ {
202
+ ".all" : [
203
+ {
204
+ ".rgx" : "(?=[!=\\+\\-\\.]?(?:[a-zA-Z]\\w*\\b|<[a-zA-Z]\\w*\\b>)[^\\w\\(\\)</\\~\\|`\\s\\*\\+\\?!=\\+\\-\\.:;])"
205
+ },
206
+ {
207
+ ".err" : "Illegal character in rule quantifier"
208
+ }
209
+ ]
210
+ },
211
+ {
212
+ ".all" : [
213
+ {
214
+ ".rgx" : "(?=[!=\\+\\-\\.]?[a-zA-Z]\\w*\\b\\-)"
215
+ },
216
+ {
217
+ ".err" : "Unprotected rule name with numeric quantifier please use <rule>#-# syntax!"
218
+ }
219
+ ]
220
+ },
221
+ {
222
+ ".all" : [
223
+ {
224
+ "+asr" : -1,
225
+ ".ref" : "rule_modifier"
226
+ },
227
+ {
228
+ ".rgx" : "(?=[^\\w\\(\\)</\\~\\|`\\s](?:[a-zA-Z]\\w*\\b|<[a-zA-Z]\\w*\\b>)(?:[\\*\\+\\?]|[0-9]+(?:\\-[0-9]+|\\+)?)?(?![\\ \\t]*:))"
229
+ },
230
+ {
231
+ ".err" : "Illegal rule modifier (must be [=!.-+]?)"
232
+ }
233
+ ]
234
+ }
235
+ ]
236
+ },
237
+ "ERROR_rule_start" : {
238
+ ".any" : [
239
+ {
240
+ ".rgx" : "([a-zA-Z]\\w*\\b)[\\ \\t]*:(?:\\s|\\#.*(?:\\n|\\z))*"
241
+ },
242
+ {
243
+ ".err" : "Rule header syntax error"
244
+ }
245
+ ]
246
+ },
247
+ "all_group" : {
248
+ ".all" : [
249
+ {
250
+ ".ref" : "rule_part"
251
+ },
252
+ {
253
+ "+min" : 0,
254
+ ".all" : [
255
+ {
256
+ ".rgx" : "(?:\\s|\\#.*(?:\\n|\\z))*"
257
+ },
258
+ {
259
+ ".ref" : "rule_part"
260
+ }
261
+ ]
262
+ }
263
+ ]
264
+ },
265
+ "any_group" : {
266
+ ".all" : [
267
+ {
268
+ ".ref" : "all_group"
269
+ },
270
+ {
271
+ "+min" : 0,
272
+ ".all" : [
273
+ {
274
+ ".rgx" : "(?:\\s|\\#.*(?:\\n|\\z))*\\|(?:\\s|\\#.*(?:\\n|\\z))*"
275
+ },
276
+ {
277
+ ".ref" : "all_group"
278
+ }
279
+ ]
280
+ }
281
+ ]
282
+ },
283
+ "bracketed_group" : {
284
+ ".all" : [
285
+ {
286
+ ".rgx" : "(\\.?)\\((?:\\s|\\#.*(?:\\n|\\z))*"
287
+ },
288
+ {
289
+ ".ref" : "rule_group"
290
+ },
291
+ {
292
+ ".rgx" : "(?:\\s|\\#.*(?:\\n|\\z))*\\)((?:[\\*\\+\\?]|[0-9]+(?:\\-[0-9]+|\\+)?)?)"
293
+ }
294
+ ]
295
+ },
296
+ "doc_ending" : {
297
+ ".rgx" : "(?:\\s|\\#.*(?:\\n|\\z))*\\z"
298
+ },
299
+ "ending" : {
300
+ ".rgx" : "(?:\\s|\\#.*(?:\\n|\\z))*?(?:\\n(?:\\s|\\#.*(?:\\n|\\z))*;?(?:\\s|\\#.*(?:\\n|\\z))*|\\#.*(?:\\n|\\z)(?:\\s|\\#.*(?:\\n|\\z))*;?(?:\\s|\\#.*(?:\\n|\\z))*|;(?:\\s|\\#.*(?:\\n|\\z))*|\\z)"
301
+ },
302
+ "error_message" : {
303
+ ".rgx" : "`([^`\\r\\n]*)`"
304
+ },
305
+ "grammar" : {
306
+ ".all" : [
307
+ {
308
+ ".ref" : "meta_section"
309
+ },
310
+ {
311
+ ".ref" : "rule_section"
312
+ },
313
+ {
314
+ ".any" : [
315
+ {
316
+ ".ref" : "doc_ending"
317
+ },
318
+ {
319
+ ".ref" : "ERROR_rule_definition"
320
+ }
321
+ ]
322
+ }
323
+ ]
324
+ },
325
+ "meta_definition" : {
326
+ ".rgx" : "%(grammar|extends|include|version)[\\ \\t]+[\\ \\t]*([^;\\n]*?)[\\ \\t]*(?:\\s|\\#.*(?:\\n|\\z))*?(?:\\n(?:\\s|\\#.*(?:\\n|\\z))*;?(?:\\s|\\#.*(?:\\n|\\z))*|\\#.*(?:\\n|\\z)(?:\\s|\\#.*(?:\\n|\\z))*;?(?:\\s|\\#.*(?:\\n|\\z))*|;(?:\\s|\\#.*(?:\\n|\\z))*|\\z)"
327
+ },
328
+ "meta_section" : {
329
+ "+min" : 0,
330
+ ".any" : [
331
+ {
332
+ ".ref" : "meta_definition"
333
+ },
334
+ {
335
+ ".rgx" : "(?:\\s|\\#.*(?:\\n|\\z))+"
336
+ },
337
+ {
338
+ ".ref" : "ERROR_meta_definition"
339
+ }
340
+ ]
341
+ },
342
+ "regular_expression" : {
343
+ ".rgx" : "/([^/]*)/"
344
+ },
345
+ "rule_definition" : {
346
+ ".all" : [
347
+ {
348
+ ".ref" : "rule_start"
349
+ },
350
+ {
351
+ ".ref" : "rule_group"
352
+ },
353
+ {
354
+ ".ref" : "ending"
355
+ }
356
+ ]
357
+ },
358
+ "rule_group" : {
359
+ ".ref" : "any_group"
360
+ },
361
+ "rule_item" : {
362
+ ".any" : [
363
+ {
364
+ ".ref" : "rule_reference"
365
+ },
366
+ {
367
+ ".ref" : "regular_expression"
368
+ },
369
+ {
370
+ ".ref" : "bracketed_group"
371
+ },
372
+ {
373
+ ".ref" : "whitespace_token"
374
+ },
375
+ {
376
+ ".ref" : "error_message"
377
+ }
378
+ ]
379
+ },
380
+ "rule_modifier" : {
381
+ ".rgx" : "[!=\\+\\-\\.]"
382
+ },
383
+ "rule_part" : {
384
+ "+max" : "2",
385
+ "+min" : "1",
386
+ ".ref" : "rule_item",
387
+ ".sep" : {
388
+ ".rgx" : "(?:\\s|\\#.*(?:\\n|\\z))+(%{1,2})(?:\\s|\\#.*(?:\\n|\\z))+"
389
+ }
390
+ },
391
+ "rule_reference" : {
392
+ ".rgx" : "([!=\\+\\-\\.]?)(?:([a-zA-Z]\\w*\\b)|(?:<([a-zA-Z]\\w*\\b)>))((?:[\\*\\+\\?]|[0-9]+(?:\\-[0-9]+|\\+)?)?)(?![\\ \\t]*:)"
393
+ },
394
+ "rule_section" : {
395
+ "+min" : 0,
396
+ ".any" : [
397
+ {
398
+ ".ref" : "rule_definition"
399
+ },
400
+ {
401
+ ".rgx" : "(?:\\s|\\#.*(?:\\n|\\z))+"
402
+ }
403
+ ]
404
+ },
405
+ "rule_start" : {
406
+ ".rgx" : "([a-zA-Z]\\w*\\b)[\\ \\t]*:(?:\\s|\\#.*(?:\\n|\\z))*"
407
+ },
408
+ "whitespace_token" : {
409
+ ".rgx" : "(\\~+)"
410
+ }
411
+ }
412
+ ...
413
+ end
414
+ end
@@ -0,0 +1,7 @@
1
+ class Pegex::Receiver
2
+ attr_accessor :parser
3
+
4
+ def flatten array
5
+ return array.flatten!
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ require 'pegex/tree'
2
+
3
+ class Pegex::Tree::Wrap < Pegex::Tree
4
+ def gotrule got
5
+ return got if @parser.parent['-pass']
6
+ return $pegex_nil unless got
7
+ return @parser.rule => got
8
+ end
9
+
10
+ def final got
11
+ return got || {@parser.rule => []}
12
+ end
13
+ end
data/lib/pegex/tree.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'pegex/receiver'
2
+
3
+ class Pegex::Tree < Pegex::Receiver
4
+ def gotrule got=nil
5
+ return $pegex_nil if got.nil?
6
+
7
+ if self.parser.parent['-wrap']
8
+ return {self.parser.rule => (got || [])}
9
+ else
10
+ return got
11
+ end
12
+ end
13
+
14
+ def final got=nil
15
+ return got || []
16
+ end
17
+ end
data/lib/pegex.rb ADDED
@@ -0,0 +1,18 @@
1
+ module Pegex
2
+ VERSION = '0.0.1'
3
+ end
4
+
5
+ require 'pegex/parser'
6
+ require 'pegex/grammar'
7
+
8
+ def pegex grammar_text, receiver=nil
9
+ unless receiver
10
+ require 'pegex/tree/wrap'
11
+ receiver = Pegex::Tree::Wrap.new
12
+ end
13
+ receiver = receiver.new if receiver.class == Class
14
+ return Pegex::Parser.new { |o|
15
+ o.grammar = Pegex::Grammar.new {|g| g.text = grammar_text}
16
+ o.receiver = receiver
17
+ }
18
+ end