regex-treetop 1.4.8

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.
Files changed (65) hide show
  1. data/LICENSE +19 -0
  2. data/README.md +164 -0
  3. data/Rakefile +19 -0
  4. data/bin/tt +112 -0
  5. data/doc/contributing_and_planned_features.markdown +103 -0
  6. data/doc/grammar_composition.markdown +65 -0
  7. data/doc/index.markdown +90 -0
  8. data/doc/pitfalls_and_advanced_techniques.markdown +51 -0
  9. data/doc/semantic_interpretation.markdown +189 -0
  10. data/doc/site.rb +112 -0
  11. data/doc/sitegen.rb +65 -0
  12. data/doc/syntactic_recognition.markdown +100 -0
  13. data/doc/using_in_ruby.markdown +21 -0
  14. data/examples/lambda_calculus/arithmetic.rb +551 -0
  15. data/examples/lambda_calculus/arithmetic.treetop +97 -0
  16. data/examples/lambda_calculus/arithmetic_node_classes.rb +7 -0
  17. data/examples/lambda_calculus/arithmetic_test.rb +54 -0
  18. data/examples/lambda_calculus/lambda_calculus +0 -0
  19. data/examples/lambda_calculus/lambda_calculus.rb +718 -0
  20. data/examples/lambda_calculus/lambda_calculus.treetop +132 -0
  21. data/examples/lambda_calculus/lambda_calculus_node_classes.rb +5 -0
  22. data/examples/lambda_calculus/lambda_calculus_test.rb +89 -0
  23. data/examples/lambda_calculus/test_helper.rb +18 -0
  24. data/lib/treetop.rb +16 -0
  25. data/lib/treetop/bootstrap_gen_1_metagrammar.rb +45 -0
  26. data/lib/treetop/compiler.rb +6 -0
  27. data/lib/treetop/compiler/grammar_compiler.rb +44 -0
  28. data/lib/treetop/compiler/lexical_address_space.rb +17 -0
  29. data/lib/treetop/compiler/metagrammar.rb +3392 -0
  30. data/lib/treetop/compiler/metagrammar.treetop +454 -0
  31. data/lib/treetop/compiler/node_classes.rb +21 -0
  32. data/lib/treetop/compiler/node_classes/anything_symbol.rb +18 -0
  33. data/lib/treetop/compiler/node_classes/atomic_expression.rb +14 -0
  34. data/lib/treetop/compiler/node_classes/character_class.rb +28 -0
  35. data/lib/treetop/compiler/node_classes/choice.rb +31 -0
  36. data/lib/treetop/compiler/node_classes/declaration_sequence.rb +24 -0
  37. data/lib/treetop/compiler/node_classes/grammar.rb +28 -0
  38. data/lib/treetop/compiler/node_classes/inline_module.rb +27 -0
  39. data/lib/treetop/compiler/node_classes/nonterminal.rb +13 -0
  40. data/lib/treetop/compiler/node_classes/optional.rb +19 -0
  41. data/lib/treetop/compiler/node_classes/parenthesized_expression.rb +9 -0
  42. data/lib/treetop/compiler/node_classes/parsing_expression.rb +146 -0
  43. data/lib/treetop/compiler/node_classes/parsing_rule.rb +55 -0
  44. data/lib/treetop/compiler/node_classes/predicate.rb +45 -0
  45. data/lib/treetop/compiler/node_classes/predicate_block.rb +16 -0
  46. data/lib/treetop/compiler/node_classes/regex.rb +23 -0
  47. data/lib/treetop/compiler/node_classes/repetition.rb +55 -0
  48. data/lib/treetop/compiler/node_classes/sequence.rb +71 -0
  49. data/lib/treetop/compiler/node_classes/terminal.rb +20 -0
  50. data/lib/treetop/compiler/node_classes/transient_prefix.rb +9 -0
  51. data/lib/treetop/compiler/node_classes/treetop_file.rb +9 -0
  52. data/lib/treetop/compiler/ruby_builder.rb +113 -0
  53. data/lib/treetop/ruby_extensions.rb +2 -0
  54. data/lib/treetop/ruby_extensions/string.rb +42 -0
  55. data/lib/treetop/runtime.rb +5 -0
  56. data/lib/treetop/runtime/compiled_parser.rb +118 -0
  57. data/lib/treetop/runtime/interval_skip_list.rb +4 -0
  58. data/lib/treetop/runtime/interval_skip_list/head_node.rb +15 -0
  59. data/lib/treetop/runtime/interval_skip_list/interval_skip_list.rb +200 -0
  60. data/lib/treetop/runtime/interval_skip_list/node.rb +164 -0
  61. data/lib/treetop/runtime/syntax_node.rb +114 -0
  62. data/lib/treetop/runtime/terminal_parse_failure.rb +16 -0
  63. data/lib/treetop/runtime/terminal_syntax_node.rb +17 -0
  64. data/lib/treetop/version.rb +9 -0
  65. metadata +138 -0
@@ -0,0 +1,454 @@
1
+ module Treetop
2
+ module Compiler
3
+ grammar Metagrammar
4
+ rule treetop_file
5
+ requires:(space? require_statement)* prefix:space? module_or_grammar:(module_declaration / grammar) suffix:space? {
6
+ def compile
7
+ requires.text_value + prefix.text_value + module_or_grammar.compile + suffix.text_value
8
+ end
9
+ }
10
+ end
11
+
12
+ rule require_statement
13
+ prefix:space? "require" [ \t]+ [^\n\r]+ [\n\r]
14
+ end
15
+
16
+ rule module_declaration
17
+ prefix:('module' space [A-Z] alphanumeric_char* space) module_contents:(module_declaration / grammar) suffix:(space 'end') {
18
+ def compile
19
+ prefix.text_value + module_contents.compile + suffix.text_value
20
+ end
21
+ }
22
+ end
23
+
24
+ rule grammar
25
+ 'grammar' space grammar_name space ('do' space)? declaration_sequence space? 'end' <Grammar>
26
+ end
27
+
28
+ rule grammar_name
29
+ ([A-Z] alphanumeric_char*)
30
+ end
31
+
32
+ rule declaration_sequence
33
+ head:declaration tail:(space declaration)* <DeclarationSequence> {
34
+ def declarations
35
+ [head] + tail
36
+ end
37
+
38
+ def tail
39
+ super.elements.map { |elt| elt.declaration }
40
+ end
41
+ }
42
+ /
43
+ '' {
44
+ def compile(builder)
45
+ end
46
+ }
47
+ end
48
+
49
+ rule declaration
50
+ parsing_rule / include_declaration
51
+ end
52
+
53
+ rule include_declaration
54
+ 'include' space [A-Z] (alphanumeric_char / '::')* {
55
+ def compile(builder)
56
+ builder << text_value
57
+ end
58
+ }
59
+ end
60
+
61
+ rule parsing_rule
62
+ 'rule' space nonterminal space ('do' space)? parsing_expression space 'end' <ParsingRule>
63
+ end
64
+
65
+ rule parsing_expression
66
+ choice / sequence / primary
67
+ end
68
+
69
+ rule choice
70
+ head:alternative tail:(space? '/' space? alternative)+ <Choice> {
71
+ def alternatives
72
+ [head] + tail
73
+ end
74
+
75
+ def tail
76
+ super.elements.map {|elt| elt.alternative}
77
+ end
78
+
79
+ def inline_modules
80
+ (alternatives.map {|alt| alt.inline_modules }).flatten
81
+ end
82
+ }
83
+ end
84
+
85
+ rule sequence
86
+ head:labeled_sequence_primary tail:(space labeled_sequence_primary)+ node_class_declarations <Sequence> {
87
+ def sequence_elements
88
+ [head] + tail
89
+ end
90
+
91
+ def tail
92
+ super.elements.map {|elt| elt.labeled_sequence_primary }
93
+ end
94
+
95
+ def inline_modules
96
+ (sequence_elements.map {|elt| elt.inline_modules}).flatten +
97
+ [sequence_element_accessor_module] +
98
+ node_class_declarations.inline_modules
99
+ end
100
+
101
+ def inline_module_name
102
+ node_class_declarations.inline_module_name
103
+ end
104
+ }
105
+ end
106
+
107
+ rule alternative
108
+ sequence / primary
109
+ end
110
+
111
+ rule primary
112
+ prefix atomic {
113
+ def compile(address, builder, parent_expression=nil)
114
+ prefix.compile(address, builder, self)
115
+ end
116
+
117
+ def prefixed_expression
118
+ atomic
119
+ end
120
+
121
+ def inline_modules
122
+ atomic.inline_modules
123
+ end
124
+
125
+ def inline_module_name
126
+ nil
127
+ end
128
+ }
129
+ /
130
+ prefix space? predicate_block {
131
+ def compile(address, builder, parent_expression=nil)
132
+ prefix.compile(address, builder, self)
133
+ end
134
+ def prefixed_expression
135
+ predicate_block
136
+ end
137
+ def inline_modules
138
+ []
139
+ end
140
+ }
141
+ /
142
+ atomic suffix node_class_declarations {
143
+ def compile(address, builder, parent_expression=nil)
144
+ suffix.compile(address, builder, self)
145
+ end
146
+
147
+ def optional_expression
148
+ atomic
149
+ end
150
+
151
+ def node_class_name
152
+ node_class_declarations.node_class_name
153
+ end
154
+
155
+ def inline_modules
156
+ atomic.inline_modules + node_class_declarations.inline_modules
157
+ end
158
+
159
+ def inline_module_name
160
+ node_class_declarations.inline_module_name
161
+ end
162
+ }
163
+ /
164
+ atomic node_class_declarations {
165
+ def compile(address, builder, parent_expression=nil)
166
+ atomic.compile(address, builder, self)
167
+ end
168
+
169
+ def node_class_name
170
+ node_class_declarations.node_class_name
171
+ end
172
+
173
+ def inline_modules
174
+ atomic.inline_modules + node_class_declarations.inline_modules
175
+ end
176
+
177
+ def inline_module_name
178
+ node_class_declarations.inline_module_name
179
+ end
180
+ }
181
+ end
182
+
183
+ rule labeled_sequence_primary
184
+ label sequence_primary {
185
+ def compile(lexical_address, builder)
186
+ sequence_primary.compile(lexical_address, builder)
187
+ end
188
+
189
+ def inline_modules
190
+ sequence_primary.inline_modules
191
+ end
192
+
193
+ def label_name
194
+ if label.name
195
+ label.name
196
+ elsif sequence_primary.instance_of?(Nonterminal)
197
+ sequence_primary.text_value
198
+ else
199
+ nil
200
+ end
201
+ end
202
+ }
203
+ end
204
+
205
+ rule label
206
+ (alpha_char alphanumeric_char*) ':' {
207
+ def name
208
+ elements[0].text_value
209
+ end
210
+ }
211
+ /
212
+ '' {
213
+ def name
214
+ nil
215
+ end
216
+ }
217
+ end
218
+
219
+ rule sequence_primary
220
+ prefix atomic {
221
+ def compile(lexical_address, builder)
222
+ prefix.compile(lexical_address, builder, self)
223
+ end
224
+
225
+ def prefixed_expression
226
+ elements[1]
227
+ end
228
+
229
+ def inline_modules
230
+ atomic.inline_modules
231
+ end
232
+
233
+ def inline_module_name
234
+ nil
235
+ end
236
+ }
237
+ /
238
+ prefix space? predicate_block {
239
+ def compile(address, builder, parent_expression=nil)
240
+ prefix.compile(address, builder, self)
241
+ end
242
+ def prefixed_expression
243
+ predicate_block
244
+ end
245
+ def inline_modules
246
+ []
247
+ end
248
+ }
249
+ /
250
+ atomic suffix {
251
+ def compile(lexical_address, builder)
252
+ suffix.compile(lexical_address, builder, self)
253
+ end
254
+
255
+ def node_class_name
256
+ nil
257
+ end
258
+
259
+ def inline_modules
260
+ atomic.inline_modules
261
+ end
262
+
263
+ def inline_module_name
264
+ nil
265
+ end
266
+ }
267
+ /
268
+ atomic
269
+ end
270
+
271
+ rule suffix
272
+ repetition_suffix / optional_suffix
273
+ end
274
+
275
+ rule optional_suffix
276
+ '?' <Optional>
277
+ end
278
+
279
+ rule node_class_declarations
280
+ node_class_expression trailing_inline_module {
281
+ def node_class_name
282
+ node_class_expression.node_class_name
283
+ end
284
+
285
+ def inline_modules
286
+ trailing_inline_module.inline_modules
287
+ end
288
+
289
+ def inline_module
290
+ trailing_inline_module.inline_module
291
+ end
292
+
293
+ def inline_module_name
294
+ inline_module.module_name if inline_module
295
+ end
296
+ }
297
+ end
298
+
299
+ rule repetition_suffix
300
+ '+' <OneOrMore> / '*' <ZeroOrMore>
301
+ end
302
+
303
+ rule prefix
304
+ '&' <AndPredicate> / '!' <NotPredicate> / '~' <TransientPrefix>
305
+ end
306
+
307
+ rule atomic
308
+ regex
309
+ /
310
+ terminal
311
+ /
312
+ nonterminal
313
+ /
314
+ parenthesized_expression
315
+ end
316
+
317
+ rule regex
318
+ 'r(' regex_body ')' <Regex> {
319
+ def inline_modules
320
+ []
321
+ end
322
+ }
323
+ end
324
+
325
+ rule regex_body
326
+ (nested_parens_in_regex / [^)])*
327
+ end
328
+
329
+ rule nested_parens_in_regex
330
+ '(' regex_body ')'
331
+ end
332
+
333
+ rule parenthesized_expression
334
+ '(' space? parsing_expression space? ')' <ParenthesizedExpression> {
335
+ def inline_modules
336
+ parsing_expression.inline_modules
337
+ end
338
+ }
339
+ end
340
+
341
+ rule nonterminal
342
+ !keyword_inside_grammar (alpha_char alphanumeric_char*) <Nonterminal>
343
+ end
344
+
345
+ rule terminal
346
+ quoted_string / character_class / anything_symbol
347
+ end
348
+
349
+ rule quoted_string
350
+ (single_quoted_string / double_quoted_string) {
351
+ def string
352
+ super.text_value
353
+ end
354
+ }
355
+ end
356
+
357
+ rule double_quoted_string
358
+ '"' string:(!'"' ("\\\\" / '\"' / .))* '"' <Terminal>
359
+ end
360
+
361
+ rule single_quoted_string
362
+ "'" string:(!"'" ("\\\\" / "\\'" / .))* "'" <Terminal>
363
+ end
364
+
365
+ rule character_class
366
+ '[' characters:(!']' ('\\' . /!'\\' .))+ ']' <CharacterClass> {
367
+ def characters
368
+ super.text_value
369
+ end
370
+ }
371
+ end
372
+
373
+ rule anything_symbol
374
+ '.' <AnythingSymbol>
375
+ end
376
+
377
+ rule node_class_expression
378
+ space '<' (!'>' .)+ '>' {
379
+ def node_class_name
380
+ elements[2].text_value
381
+ end
382
+ }
383
+ /
384
+ '' {
385
+ def node_class_name
386
+ nil
387
+ end
388
+ }
389
+ end
390
+
391
+ rule trailing_inline_module
392
+ space inline_module {
393
+ def inline_modules
394
+ [inline_module]
395
+ end
396
+
397
+ def inline_module_name
398
+ inline_module.module_name
399
+ end
400
+ }
401
+ /
402
+ '' {
403
+ def inline_modules
404
+ []
405
+ end
406
+
407
+ def inline_module
408
+ nil
409
+ end
410
+
411
+ def inline_module_name
412
+ nil
413
+ end
414
+ }
415
+ end
416
+
417
+ rule predicate_block
418
+ '' inline_module <PredicateBlock>
419
+ end
420
+
421
+ rule inline_module
422
+ '{' (inline_module / ![{}] .)* '}' <InlineModule>
423
+ end
424
+
425
+ rule keyword_inside_grammar
426
+ ('rule' / 'end') !non_space_char
427
+ end
428
+
429
+ rule non_space_char
430
+ !space .
431
+ end
432
+
433
+ rule alpha_char
434
+ [A-Za-z_]
435
+ end
436
+
437
+ rule alphanumeric_char
438
+ alpha_char / [0-9]
439
+ end
440
+
441
+ rule space
442
+ (white / comment_to_eol)+
443
+ end
444
+
445
+ rule comment_to_eol
446
+ '#' (!"\n" .)*
447
+ end
448
+
449
+ rule white
450
+ [ \t\n\r]
451
+ end
452
+ end
453
+ end
454
+ end