personify 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (130) hide show
  1. data/.gitignore +1 -0
  2. data/LICENSE +20 -0
  3. data/README.md +172 -0
  4. data/Rakefile +53 -0
  5. data/VERSION +1 -0
  6. data/doc/syntax_ideas.md +141 -0
  7. data/lib/personify/context.rb +55 -0
  8. data/lib/personify/parser/personify.rb +1071 -0
  9. data/lib/personify/parser/personify.treetop +107 -0
  10. data/lib/personify/parser/personify_node_classes.rb +121 -0
  11. data/lib/personify/template.rb +17 -0
  12. data/lib/personify.rb +8 -0
  13. data/script/generate_parser.rb +6 -0
  14. data/test/context_test.rb +122 -0
  15. data/test/fixtures/multiple_tags.txt +8 -0
  16. data/test/parse_runner.rb +60 -0
  17. data/test/parser_test.rb +291 -0
  18. data/test/test_helper.rb +16 -0
  19. data/vendor/treetop/.gitignore +5 -0
  20. data/vendor/treetop/History.txt +9 -0
  21. data/vendor/treetop/README +164 -0
  22. data/vendor/treetop/Rakefile +20 -0
  23. data/vendor/treetop/Treetop.tmbundle/Snippets/grammar ___ end.tmSnippet +20 -0
  24. data/vendor/treetop/Treetop.tmbundle/Snippets/rule ___ end.tmSnippet +18 -0
  25. data/vendor/treetop/Treetop.tmbundle/Syntaxes/Treetop Grammar.tmLanguage +251 -0
  26. data/vendor/treetop/Treetop.tmbundle/info.plist +10 -0
  27. data/vendor/treetop/bin/tt +28 -0
  28. data/vendor/treetop/doc/contributing_and_planned_features.markdown +103 -0
  29. data/vendor/treetop/doc/grammar_composition.markdown +65 -0
  30. data/vendor/treetop/doc/index.markdown +90 -0
  31. data/vendor/treetop/doc/pitfalls_and_advanced_techniques.markdown +51 -0
  32. data/vendor/treetop/doc/semantic_interpretation.markdown +189 -0
  33. data/vendor/treetop/doc/site.rb +110 -0
  34. data/vendor/treetop/doc/sitegen.rb +60 -0
  35. data/vendor/treetop/doc/syntactic_recognition.markdown +100 -0
  36. data/vendor/treetop/doc/using_in_ruby.markdown +21 -0
  37. data/vendor/treetop/examples/lambda_calculus/arithmetic.rb +551 -0
  38. data/vendor/treetop/examples/lambda_calculus/arithmetic.treetop +97 -0
  39. data/vendor/treetop/examples/lambda_calculus/arithmetic_node_classes.rb +7 -0
  40. data/vendor/treetop/examples/lambda_calculus/arithmetic_test.rb +54 -0
  41. data/vendor/treetop/examples/lambda_calculus/lambda_calculus +0 -0
  42. data/vendor/treetop/examples/lambda_calculus/lambda_calculus.rb +718 -0
  43. data/vendor/treetop/examples/lambda_calculus/lambda_calculus.treetop +132 -0
  44. data/vendor/treetop/examples/lambda_calculus/lambda_calculus_node_classes.rb +5 -0
  45. data/vendor/treetop/examples/lambda_calculus/lambda_calculus_test.rb +89 -0
  46. data/vendor/treetop/examples/lambda_calculus/test_helper.rb +18 -0
  47. data/vendor/treetop/lib/treetop/bootstrap_gen_1_metagrammar.rb +45 -0
  48. data/vendor/treetop/lib/treetop/compiler/grammar_compiler.rb +40 -0
  49. data/vendor/treetop/lib/treetop/compiler/lexical_address_space.rb +17 -0
  50. data/vendor/treetop/lib/treetop/compiler/metagrammar.rb +2955 -0
  51. data/vendor/treetop/lib/treetop/compiler/metagrammar.treetop +404 -0
  52. data/vendor/treetop/lib/treetop/compiler/node_classes/anything_symbol.rb +20 -0
  53. data/vendor/treetop/lib/treetop/compiler/node_classes/atomic_expression.rb +14 -0
  54. data/vendor/treetop/lib/treetop/compiler/node_classes/character_class.rb +22 -0
  55. data/vendor/treetop/lib/treetop/compiler/node_classes/choice.rb +31 -0
  56. data/vendor/treetop/lib/treetop/compiler/node_classes/declaration_sequence.rb +24 -0
  57. data/vendor/treetop/lib/treetop/compiler/node_classes/grammar.rb +28 -0
  58. data/vendor/treetop/lib/treetop/compiler/node_classes/inline_module.rb +27 -0
  59. data/vendor/treetop/lib/treetop/compiler/node_classes/nonterminal.rb +13 -0
  60. data/vendor/treetop/lib/treetop/compiler/node_classes/optional.rb +19 -0
  61. data/vendor/treetop/lib/treetop/compiler/node_classes/parenthesized_expression.rb +9 -0
  62. data/vendor/treetop/lib/treetop/compiler/node_classes/parsing_expression.rb +138 -0
  63. data/vendor/treetop/lib/treetop/compiler/node_classes/parsing_rule.rb +55 -0
  64. data/vendor/treetop/lib/treetop/compiler/node_classes/predicate.rb +45 -0
  65. data/vendor/treetop/lib/treetop/compiler/node_classes/repetition.rb +55 -0
  66. data/vendor/treetop/lib/treetop/compiler/node_classes/sequence.rb +68 -0
  67. data/vendor/treetop/lib/treetop/compiler/node_classes/terminal.rb +20 -0
  68. data/vendor/treetop/lib/treetop/compiler/node_classes/transient_prefix.rb +9 -0
  69. data/vendor/treetop/lib/treetop/compiler/node_classes/treetop_file.rb +9 -0
  70. data/vendor/treetop/lib/treetop/compiler/node_classes.rb +19 -0
  71. data/vendor/treetop/lib/treetop/compiler/ruby_builder.rb +113 -0
  72. data/vendor/treetop/lib/treetop/compiler.rb +6 -0
  73. data/vendor/treetop/lib/treetop/ruby_extensions/string.rb +42 -0
  74. data/vendor/treetop/lib/treetop/ruby_extensions.rb +2 -0
  75. data/vendor/treetop/lib/treetop/runtime/compiled_parser.rb +95 -0
  76. data/vendor/treetop/lib/treetop/runtime/interval_skip_list/head_node.rb +15 -0
  77. data/vendor/treetop/lib/treetop/runtime/interval_skip_list/interval_skip_list.rb +200 -0
  78. data/vendor/treetop/lib/treetop/runtime/interval_skip_list/node.rb +164 -0
  79. data/vendor/treetop/lib/treetop/runtime/interval_skip_list.rb +4 -0
  80. data/vendor/treetop/lib/treetop/runtime/syntax_node.rb +72 -0
  81. data/vendor/treetop/lib/treetop/runtime/terminal_parse_failure.rb +16 -0
  82. data/vendor/treetop/lib/treetop/runtime/terminal_syntax_node.rb +17 -0
  83. data/vendor/treetop/lib/treetop/runtime.rb +5 -0
  84. data/vendor/treetop/lib/treetop/version.rb +9 -0
  85. data/vendor/treetop/lib/treetop.rb +11 -0
  86. data/vendor/treetop/script/generate_metagrammar.rb +14 -0
  87. data/vendor/treetop/script/svnadd +11 -0
  88. data/vendor/treetop/script/svnrm +11 -0
  89. data/vendor/treetop/spec/compiler/and_predicate_spec.rb +36 -0
  90. data/vendor/treetop/spec/compiler/anything_symbol_spec.rb +52 -0
  91. data/vendor/treetop/spec/compiler/character_class_spec.rb +188 -0
  92. data/vendor/treetop/spec/compiler/choice_spec.rb +80 -0
  93. data/vendor/treetop/spec/compiler/circular_compilation_spec.rb +28 -0
  94. data/vendor/treetop/spec/compiler/failure_propagation_functional_spec.rb +21 -0
  95. data/vendor/treetop/spec/compiler/grammar_compiler_spec.rb +84 -0
  96. data/vendor/treetop/spec/compiler/grammar_spec.rb +41 -0
  97. data/vendor/treetop/spec/compiler/nonterminal_symbol_spec.rb +40 -0
  98. data/vendor/treetop/spec/compiler/not_predicate_spec.rb +38 -0
  99. data/vendor/treetop/spec/compiler/one_or_more_spec.rb +35 -0
  100. data/vendor/treetop/spec/compiler/optional_spec.rb +37 -0
  101. data/vendor/treetop/spec/compiler/parenthesized_expression_spec.rb +19 -0
  102. data/vendor/treetop/spec/compiler/parsing_rule_spec.rb +32 -0
  103. data/vendor/treetop/spec/compiler/sequence_spec.rb +115 -0
  104. data/vendor/treetop/spec/compiler/terminal_spec.rb +81 -0
  105. data/vendor/treetop/spec/compiler/terminal_symbol_spec.rb +37 -0
  106. data/vendor/treetop/spec/compiler/test_grammar.treetop +7 -0
  107. data/vendor/treetop/spec/compiler/test_grammar.tt +7 -0
  108. data/vendor/treetop/spec/compiler/test_grammar_do.treetop +7 -0
  109. data/vendor/treetop/spec/compiler/zero_or_more_spec.rb +56 -0
  110. data/vendor/treetop/spec/composition/a.treetop +11 -0
  111. data/vendor/treetop/spec/composition/b.treetop +11 -0
  112. data/vendor/treetop/spec/composition/c.treetop +10 -0
  113. data/vendor/treetop/spec/composition/d.treetop +10 -0
  114. data/vendor/treetop/spec/composition/grammar_composition_spec.rb +26 -0
  115. data/vendor/treetop/spec/ruby_extensions/string_spec.rb +32 -0
  116. data/vendor/treetop/spec/runtime/compiled_parser_spec.rb +101 -0
  117. data/vendor/treetop/spec/runtime/interval_skip_list/delete_spec.rb +147 -0
  118. data/vendor/treetop/spec/runtime/interval_skip_list/expire_range_spec.rb +349 -0
  119. data/vendor/treetop/spec/runtime/interval_skip_list/insert_and_delete_node.rb +385 -0
  120. data/vendor/treetop/spec/runtime/interval_skip_list/insert_spec.rb +660 -0
  121. data/vendor/treetop/spec/runtime/interval_skip_list/interval_skip_list_spec.graffle +6175 -0
  122. data/vendor/treetop/spec/runtime/interval_skip_list/interval_skip_list_spec.rb +58 -0
  123. data/vendor/treetop/spec/runtime/interval_skip_list/palindromic_fixture.rb +23 -0
  124. data/vendor/treetop/spec/runtime/interval_skip_list/palindromic_fixture_spec.rb +164 -0
  125. data/vendor/treetop/spec/runtime/interval_skip_list/spec_helper.rb +84 -0
  126. data/vendor/treetop/spec/runtime/syntax_node_spec.rb +53 -0
  127. data/vendor/treetop/spec/spec_helper.rb +106 -0
  128. data/vendor/treetop/spec/spec_suite.rb +4 -0
  129. data/vendor/treetop/treetop.gemspec +18 -0
  130. metadata +196 -0
@@ -0,0 +1,404 @@
1
+ module Treetop
2
+ module Compiler
3
+ grammar Metagrammar
4
+ rule treetop_file
5
+ prefix:space? module_or_grammar:(module_declaration / grammar) suffix:space? {
6
+ def compile
7
+ prefix.text_value + module_or_grammar.compile + suffix.text_value
8
+ end
9
+ }
10
+ end
11
+
12
+ rule module_declaration
13
+ prefix:('module' space [A-Z] alphanumeric_char* space) module_contents:(module_declaration / grammar) suffix:(space 'end') {
14
+ def compile
15
+ prefix.text_value + module_contents.compile + suffix.text_value
16
+ end
17
+ }
18
+ end
19
+
20
+ rule grammar
21
+ 'grammar' space grammar_name space ('do' space)? declaration_sequence space? 'end' <Grammar>
22
+ end
23
+
24
+ rule grammar_name
25
+ ([A-Z] alphanumeric_char*)
26
+ end
27
+
28
+ rule declaration_sequence
29
+ head:declaration tail:(space declaration)* <DeclarationSequence> {
30
+ def declarations
31
+ [head] + tail
32
+ end
33
+
34
+ def tail
35
+ super.elements.map { |elt| elt.declaration }
36
+ end
37
+ }
38
+ /
39
+ '' {
40
+ def compile(builder)
41
+ end
42
+ }
43
+ end
44
+
45
+ rule declaration
46
+ parsing_rule / include_declaration
47
+ end
48
+
49
+ rule include_declaration
50
+ 'include' space [A-Z] (alphanumeric_char / '::')* {
51
+ def compile(builder)
52
+ builder << text_value
53
+ end
54
+ }
55
+ end
56
+
57
+ rule parsing_rule
58
+ 'rule' space nonterminal space ('do' space)? parsing_expression space 'end' <ParsingRule>
59
+ end
60
+
61
+ rule parsing_expression
62
+ choice / sequence / primary
63
+ end
64
+
65
+ rule choice
66
+ head:alternative tail:(space? '/' space? alternative)+ <Choice> {
67
+ def alternatives
68
+ [head] + tail
69
+ end
70
+
71
+ def tail
72
+ super.elements.map {|elt| elt.alternative}
73
+ end
74
+
75
+ def inline_modules
76
+ (alternatives.map {|alt| alt.inline_modules }).flatten
77
+ end
78
+ }
79
+ end
80
+
81
+ rule sequence
82
+ head:labeled_sequence_primary tail:(space labeled_sequence_primary)+ node_class_declarations <Sequence> {
83
+ def sequence_elements
84
+ [head] + tail
85
+ end
86
+
87
+ def tail
88
+ super.elements.map {|elt| elt.labeled_sequence_primary }
89
+ end
90
+
91
+ def inline_modules
92
+ (sequence_elements.map {|elt| elt.inline_modules}).flatten +
93
+ [sequence_element_accessor_module] +
94
+ node_class_declarations.inline_modules
95
+ end
96
+
97
+ def inline_module_name
98
+ node_class_declarations.inline_module_name
99
+ end
100
+ }
101
+ end
102
+
103
+ rule alternative
104
+ sequence / primary
105
+ end
106
+
107
+ rule primary
108
+ prefix atomic {
109
+ def compile(address, builder, parent_expression=nil)
110
+ prefix.compile(address, builder, self)
111
+ end
112
+
113
+ def prefixed_expression
114
+ atomic
115
+ end
116
+
117
+ def inline_modules
118
+ atomic.inline_modules
119
+ end
120
+
121
+ def inline_module_name
122
+ nil
123
+ end
124
+ }
125
+ /
126
+ atomic suffix node_class_declarations {
127
+ def compile(address, builder, parent_expression=nil)
128
+ suffix.compile(address, builder, self)
129
+ end
130
+
131
+ def optional_expression
132
+ atomic
133
+ end
134
+
135
+ def node_class_name
136
+ node_class_declarations.node_class_name
137
+ end
138
+
139
+ def inline_modules
140
+ atomic.inline_modules + node_class_declarations.inline_modules
141
+ end
142
+
143
+ def inline_module_name
144
+ node_class_declarations.inline_module_name
145
+ end
146
+ }
147
+ /
148
+ atomic node_class_declarations {
149
+ def compile(address, builder, parent_expression=nil)
150
+ atomic.compile(address, builder, self)
151
+ end
152
+
153
+ def node_class_name
154
+ node_class_declarations.node_class_name
155
+ end
156
+
157
+ def inline_modules
158
+ atomic.inline_modules + node_class_declarations.inline_modules
159
+ end
160
+
161
+ def inline_module_name
162
+ node_class_declarations.inline_module_name
163
+ end
164
+ }
165
+ end
166
+
167
+ rule labeled_sequence_primary
168
+ label sequence_primary {
169
+ def compile(lexical_address, builder)
170
+ sequence_primary.compile(lexical_address, builder)
171
+ end
172
+
173
+ def inline_modules
174
+ sequence_primary.inline_modules
175
+ end
176
+
177
+ def label_name
178
+ if label.name
179
+ label.name
180
+ elsif sequence_primary.instance_of?(Nonterminal)
181
+ sequence_primary.text_value
182
+ else
183
+ nil
184
+ end
185
+ end
186
+ }
187
+ end
188
+
189
+ rule label
190
+ (alpha_char alphanumeric_char*) ':' {
191
+ def name
192
+ elements[0].text_value
193
+ end
194
+ }
195
+ /
196
+ '' {
197
+ def name
198
+ nil
199
+ end
200
+ }
201
+ end
202
+
203
+ rule sequence_primary
204
+ prefix atomic {
205
+ def compile(lexical_address, builder)
206
+ prefix.compile(lexical_address, builder, self)
207
+ end
208
+
209
+ def prefixed_expression
210
+ elements[1]
211
+ end
212
+
213
+ def inline_modules
214
+ atomic.inline_modules
215
+ end
216
+
217
+ def inline_module_name
218
+ nil
219
+ end
220
+ }
221
+ /
222
+ atomic suffix {
223
+ def compile(lexical_address, builder)
224
+ suffix.compile(lexical_address, builder, self)
225
+ end
226
+
227
+ def node_class_name
228
+ nil
229
+ end
230
+
231
+ def inline_modules
232
+ atomic.inline_modules
233
+ end
234
+
235
+ def inline_module_name
236
+ nil
237
+ end
238
+ }
239
+ /
240
+ atomic
241
+ end
242
+
243
+ rule suffix
244
+ repetition_suffix / optional_suffix
245
+ end
246
+
247
+ rule optional_suffix
248
+ '?' <Optional>
249
+ end
250
+
251
+ rule node_class_declarations
252
+ node_class_expression trailing_inline_module {
253
+ def node_class_name
254
+ node_class_expression.node_class_name
255
+ end
256
+
257
+ def inline_modules
258
+ trailing_inline_module.inline_modules
259
+ end
260
+
261
+ def inline_module
262
+ trailing_inline_module.inline_module
263
+ end
264
+
265
+ def inline_module_name
266
+ inline_module.module_name if inline_module
267
+ end
268
+ }
269
+ end
270
+
271
+ rule repetition_suffix
272
+ '+' <OneOrMore> / '*' <ZeroOrMore>
273
+ end
274
+
275
+ rule prefix
276
+ '&' <AndPredicate> / '!' <NotPredicate> / '~' <TransientPrefix>
277
+ end
278
+
279
+ rule atomic
280
+ terminal
281
+ /
282
+ nonterminal
283
+ /
284
+ parenthesized_expression
285
+ end
286
+
287
+ rule parenthesized_expression
288
+ '(' space? parsing_expression space? ')' <ParenthesizedExpression> {
289
+ def inline_modules
290
+ parsing_expression.inline_modules
291
+ end
292
+ }
293
+ end
294
+
295
+ rule nonterminal
296
+ !keyword_inside_grammar (alpha_char alphanumeric_char*) <Nonterminal>
297
+ end
298
+
299
+ rule terminal
300
+ quoted_string / character_class / anything_symbol
301
+ end
302
+
303
+ rule quoted_string
304
+ (single_quoted_string / double_quoted_string) {
305
+ def string
306
+ super.text_value
307
+ end
308
+ }
309
+ end
310
+
311
+ rule double_quoted_string
312
+ '"' string:(!'"' ("\\\\" / '\"' / .))* '"' <Terminal>
313
+ end
314
+
315
+ rule single_quoted_string
316
+ "'" string:(!"'" ("\\\\" / "\\'" / .))* "'" <Terminal>
317
+ end
318
+
319
+ rule character_class
320
+ '[' characters:(!']' ('\\' . /!'\\' .))+ ']' <CharacterClass> {
321
+ def characters
322
+ super.text_value
323
+ end
324
+ }
325
+ end
326
+
327
+ rule anything_symbol
328
+ '.' <AnythingSymbol>
329
+ end
330
+
331
+ rule node_class_expression
332
+ space '<' (!'>' .)+ '>' {
333
+ def node_class_name
334
+ elements[2].text_value
335
+ end
336
+ }
337
+ /
338
+ '' {
339
+ def node_class_name
340
+ nil
341
+ end
342
+ }
343
+ end
344
+
345
+ rule trailing_inline_module
346
+ space inline_module {
347
+ def inline_modules
348
+ [inline_module]
349
+ end
350
+
351
+ def inline_module_name
352
+ inline_module.module_name
353
+ end
354
+ }
355
+ /
356
+ '' {
357
+ def inline_modules
358
+ []
359
+ end
360
+
361
+ def inline_module
362
+ nil
363
+ end
364
+
365
+ def inline_module_name
366
+ nil
367
+ end
368
+ }
369
+ end
370
+
371
+ rule inline_module
372
+ '{' (inline_module / ![{}] .)* '}' <InlineModule>
373
+ end
374
+
375
+ rule keyword_inside_grammar
376
+ ('rule' / 'end') !non_space_char
377
+ end
378
+
379
+ rule non_space_char
380
+ !space .
381
+ end
382
+
383
+ rule alpha_char
384
+ [A-Za-z_]
385
+ end
386
+
387
+ rule alphanumeric_char
388
+ alpha_char / [0-9]
389
+ end
390
+
391
+ rule space
392
+ (white / comment_to_eol)+
393
+ end
394
+
395
+ rule comment_to_eol
396
+ '#' (!"\n" .)*
397
+ end
398
+
399
+ rule white
400
+ [ \t\n\r]
401
+ end
402
+ end
403
+ end
404
+ end
@@ -0,0 +1,20 @@
1
+ module Treetop
2
+ module Compiler
3
+ class AnythingSymbol < AtomicExpression
4
+ def compile(address, builder, parent_expression = nil)
5
+ super
6
+ builder.if__ "index < input_length" do
7
+ builder << 'next_character = index + input[index..-1].match(/\A(.)/um).end(1)'
8
+ assign_result "instantiate_node(#{node_class_name},input, index...next_character)"
9
+ extend_result_with_inline_module
10
+ builder << "@index = next_character"
11
+ end
12
+ builder.else_ do
13
+ builder << 'terminal_parse_failure("any character")'
14
+ assign_result 'nil'
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,14 @@
1
+ module Treetop
2
+ module Compiler
3
+ class AtomicExpression < ParsingExpression
4
+ def inline_modules
5
+ []
6
+ end
7
+
8
+ def single_quote(string)
9
+ # Double any backslashes, then backslash any single-quotes:
10
+ "'#{string.gsub(/\\/) { '\\\\' }.gsub(/'/) { "\\'"}}'"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,22 @@
1
+ module Treetop
2
+ module Compiler
3
+ class CharacterClass < AtomicExpression
4
+ def compile(address, builder, parent_expression = nil)
5
+ super
6
+
7
+ builder.if__ "input.index(Regexp.new(#{single_quote(text_value)},nil,'u'), index) == index" do
8
+ builder << 'next_character = index + input[index..-1].match(/\A(.)/um).end(1)'
9
+ assign_result "instantiate_node(#{node_class_name},input, index...next_character)"
10
+ extend_result_with_inline_module
11
+ builder << "@index = next_character"
12
+ end
13
+ builder.else_ do
14
+ "terminal_parse_failure(#{single_quote(characters)})"
15
+ assign_result 'nil'
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+
@@ -0,0 +1,31 @@
1
+ module Treetop
2
+ module Compiler
3
+ class Choice < ParsingExpression
4
+ def compile(address, builder, parent_expression = nil)
5
+ super
6
+ begin_comment(self)
7
+ use_vars :result, :start_index
8
+ compile_alternatives(alternatives)
9
+ end_comment(self)
10
+ end
11
+
12
+ def compile_alternatives(alternatives)
13
+ obtain_new_subexpression_address
14
+ alternatives.first.compile(subexpression_address, builder)
15
+ builder.if__ subexpression_success? do
16
+ assign_result subexpression_result_var
17
+ extend_result_with_declared_module
18
+ extend_result_with_inline_module
19
+ end
20
+ builder.else_ do
21
+ if alternatives.size == 1
22
+ reset_index
23
+ assign_failure start_index_var
24
+ else
25
+ compile_alternatives(alternatives[1..-1])
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,24 @@
1
+ module Treetop
2
+ module Compiler
3
+ class DeclarationSequence < Runtime::SyntaxNode
4
+
5
+ def compile(builder)
6
+ unless rules.empty?
7
+ builder.method_declaration("root") do
8
+ builder << "@root || :#{rules.first.name}"
9
+ end
10
+ builder.newline
11
+ end
12
+
13
+ declarations.each do |declaration|
14
+ declaration.compile(builder)
15
+ builder.newline
16
+ end
17
+ end
18
+
19
+ def rules
20
+ declarations.select { |declaration| declaration.instance_of?(ParsingRule) }
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,28 @@
1
+ module Treetop
2
+ module Compiler
3
+ class Grammar < Runtime::SyntaxNode
4
+ def compile
5
+ builder = RubyBuilder.new
6
+
7
+ builder.module_declaration "#{grammar_name.text_value}" do
8
+ builder.in(indent_level) # account for initial indentation of grammar declaration
9
+ builder << "include Treetop::Runtime"
10
+ builder.newline
11
+ declaration_sequence.compile(builder)
12
+ end
13
+ builder.newline
14
+ builder.class_declaration "#{parser_name} < Treetop::Runtime::CompiledParser" do
15
+ builder << "include #{grammar_name.text_value}"
16
+ end
17
+ end
18
+
19
+ def indent_level
20
+ input.column_of(interval.begin) - 1
21
+ end
22
+
23
+ def parser_name
24
+ grammar_name.text_value + 'Parser'
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,27 @@
1
+ module Treetop
2
+ module Compiler
3
+ module InlineModuleMixin
4
+ attr_reader :module_name
5
+
6
+ def compile(index, builder, rule)
7
+ @module_name = "#{rule.name.treetop_camelize}#{index}"
8
+ end
9
+ end
10
+
11
+ class InlineModule < Runtime::SyntaxNode
12
+
13
+ include InlineModuleMixin
14
+
15
+ def compile(index, builder, rule)
16
+ super
17
+ builder.module_declaration(module_name) do
18
+ builder << ruby_code.gsub(/\A\n/, '').rstrip
19
+ end
20
+ end
21
+
22
+ def ruby_code
23
+ elements[1].text_value
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,13 @@
1
+ module Treetop
2
+ module Compiler
3
+ class Nonterminal < AtomicExpression
4
+ def compile(address, builder, parent_expression = nil)
5
+ super
6
+ use_vars :result
7
+ assign_result text_value == 'super' ? 'super' : "_nt_#{text_value}"
8
+ extend_result_with_declared_module
9
+ extend_result_with_inline_module
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ module Treetop
2
+ module Compiler
3
+ class Optional < ParsingExpression
4
+ def compile(address, builder, parent_expression)
5
+ super
6
+ use_vars :result
7
+ obtain_new_subexpression_address
8
+ parent_expression.atomic.compile(subexpression_address, builder)
9
+
10
+ builder.if__ subexpression_success? do
11
+ assign_result subexpression_result_var
12
+ end
13
+ builder.else_ do
14
+ assign_result epsilon_node
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,9 @@
1
+ module Treetop
2
+ module Compiler
3
+ class ParenthesizedExpression < ParsingExpression
4
+ def compile(address, builder, parent_expression = nil)
5
+ elements[2].compile(address, builder, parent_expression)
6
+ end
7
+ end
8
+ end
9
+ end