treetop 1.2.4 → 1.2.5
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +2 -17
- data/bin/tt +13 -10
- data/lib/treetop/compiler/metagrammar.rb +99 -31
- data/lib/treetop/compiler/metagrammar.treetop +2 -2
- data/lib/treetop/compiler/node_classes/anything_symbol.rb +1 -1
- data/lib/treetop/compiler/node_classes/character_class.rb +1 -1
- data/lib/treetop/compiler/node_classes/inline_module.rb +3 -3
- data/lib/treetop/compiler/node_classes/parsing_expression.rb +1 -1
- data/lib/treetop/compiler/node_classes/parsing_rule.rb +2 -2
- data/lib/treetop/compiler/node_classes/repetition.rb +1 -1
- data/lib/treetop/compiler/node_classes/sequence.rb +2 -2
- data/lib/treetop/compiler/node_classes/terminal.rb +1 -1
- data/lib/treetop/runtime/compiled_parser.rb +9 -1
- data/lib/treetop/runtime/syntax_node.rb +2 -2
- data/lib/treetop/runtime/terminal_parse_failure_debug.rb +21 -0
- data/lib/treetop/version.rb +9 -0
- metadata +6 -4
- data/lib/metagrammar.rb +0 -0
data/Rakefile
CHANGED
@@ -13,23 +13,8 @@ Spec::Rake::SpecTask.new do |t|
|
|
13
13
|
t.pattern = 'spec/**/*spec.rb'
|
14
14
|
end
|
15
15
|
|
16
|
-
|
17
|
-
s.name = "treetop"
|
18
|
-
s.version = "1.2.4"
|
19
|
-
s.author = "Nathan Sobo"
|
20
|
-
s.email = "nathansobo@gmail.com"
|
21
|
-
s.homepage = "http://functionalform.blogspot.com"
|
22
|
-
s.platform = Gem::Platform::RUBY
|
23
|
-
s.summary = "A Ruby-based text parsing and interpretation DSL"
|
24
|
-
s.files = FileList["README", "Rakefile", "{test,lib,bin,doc,examples}/**/*"].to_a
|
25
|
-
s.bindir = "bin"
|
26
|
-
s.executables = ["tt"]
|
27
|
-
s.require_path = "lib"
|
28
|
-
s.autorequire = "treetop"
|
29
|
-
s.has_rdoc = false
|
30
|
-
s.add_dependency "polyglot"
|
31
|
-
end
|
16
|
+
load "./treetop.gemspec"
|
32
17
|
|
33
|
-
Rake::GemPackageTask.new(gemspec) do |pkg|
|
18
|
+
Rake::GemPackageTask.new($gemspec) do |pkg|
|
34
19
|
pkg.need_tar = true
|
35
20
|
end
|
data/bin/tt
CHANGED
@@ -12,14 +12,17 @@ end
|
|
12
12
|
|
13
13
|
compiler = Treetop::Compiler::GrammarCompiler.new
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
15
|
+
while !ARGV.empty?
|
16
|
+
treetop_file = ARGV.shift
|
17
|
+
if !File.exist?(treetop_file)
|
18
|
+
puts "Error: file '#{treetop_file}' doesn't exist\n\n"
|
19
|
+
exit(2)
|
20
|
+
end
|
21
|
+
if ARGV.size >= 2 && ARGV[1] == '-o'
|
22
|
+
ARGV.shift # explicit output file name option
|
23
|
+
compiler.compile(treetop_file, ARGV.shift)
|
24
|
+
else
|
25
|
+
# list of input files option
|
26
|
+
compiler.compile(treetop_file)
|
27
|
+
end
|
25
28
|
end
|
@@ -232,6 +232,12 @@ module Treetop
|
|
232
232
|
def space
|
233
233
|
elements[1]
|
234
234
|
end
|
235
|
+
end
|
236
|
+
|
237
|
+
module Grammar1
|
238
|
+
def space
|
239
|
+
elements[1]
|
240
|
+
end
|
235
241
|
|
236
242
|
def grammar_name
|
237
243
|
elements[2]
|
@@ -242,7 +248,7 @@ module Treetop
|
|
242
248
|
end
|
243
249
|
|
244
250
|
def declaration_sequence
|
245
|
-
elements[
|
251
|
+
elements[5]
|
246
252
|
end
|
247
253
|
|
248
254
|
end
|
@@ -274,25 +280,53 @@ module Treetop
|
|
274
280
|
r4 = _nt_space
|
275
281
|
s0 << r4
|
276
282
|
if r4
|
277
|
-
|
283
|
+
i6, s6 = index, []
|
284
|
+
if input.index('do', index) == index
|
285
|
+
r7 = (SyntaxNode).new(input, index...(index + 2))
|
286
|
+
@index += 2
|
287
|
+
else
|
288
|
+
terminal_parse_failure('do')
|
289
|
+
r7 = nil
|
290
|
+
end
|
291
|
+
s6 << r7
|
292
|
+
if r7
|
293
|
+
r8 = _nt_space
|
294
|
+
s6 << r8
|
295
|
+
end
|
296
|
+
if s6.last
|
297
|
+
r6 = (SyntaxNode).new(input, i6...index, s6)
|
298
|
+
r6.extend(Grammar0)
|
299
|
+
else
|
300
|
+
self.index = i6
|
301
|
+
r6 = nil
|
302
|
+
end
|
303
|
+
if r6
|
304
|
+
r5 = r6
|
305
|
+
else
|
306
|
+
r5 = SyntaxNode.new(input, index...index)
|
307
|
+
end
|
278
308
|
s0 << r5
|
279
309
|
if r5
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
s0 << r6
|
287
|
-
if r6
|
288
|
-
if input.index('end', index) == index
|
289
|
-
r8 = (SyntaxNode).new(input, index...(index + 3))
|
290
|
-
@index += 3
|
310
|
+
r9 = _nt_declaration_sequence
|
311
|
+
s0 << r9
|
312
|
+
if r9
|
313
|
+
r11 = _nt_space
|
314
|
+
if r11
|
315
|
+
r10 = r11
|
291
316
|
else
|
292
|
-
|
293
|
-
|
317
|
+
r10 = SyntaxNode.new(input, index...index)
|
318
|
+
end
|
319
|
+
s0 << r10
|
320
|
+
if r10
|
321
|
+
if input.index('end', index) == index
|
322
|
+
r12 = (SyntaxNode).new(input, index...(index + 3))
|
323
|
+
@index += 3
|
324
|
+
else
|
325
|
+
terminal_parse_failure('end')
|
326
|
+
r12 = nil
|
327
|
+
end
|
328
|
+
s0 << r12
|
294
329
|
end
|
295
|
-
s0 << r8
|
296
330
|
end
|
297
331
|
end
|
298
332
|
end
|
@@ -301,7 +335,7 @@ module Treetop
|
|
301
335
|
end
|
302
336
|
if s0.last
|
303
337
|
r0 = (Grammar).new(input, i0...index, s0)
|
304
|
-
r0.extend(
|
338
|
+
r0.extend(Grammar1)
|
305
339
|
else
|
306
340
|
self.index = i0
|
307
341
|
r0 = nil
|
@@ -581,6 +615,12 @@ module Treetop
|
|
581
615
|
def space
|
582
616
|
elements[1]
|
583
617
|
end
|
618
|
+
end
|
619
|
+
|
620
|
+
module ParsingRule1
|
621
|
+
def space
|
622
|
+
elements[1]
|
623
|
+
end
|
584
624
|
|
585
625
|
def nonterminal
|
586
626
|
elements[2]
|
@@ -591,11 +631,11 @@ module Treetop
|
|
591
631
|
end
|
592
632
|
|
593
633
|
def parsing_expression
|
594
|
-
elements[
|
634
|
+
elements[5]
|
595
635
|
end
|
596
636
|
|
597
637
|
def space
|
598
|
-
elements[
|
638
|
+
elements[6]
|
599
639
|
end
|
600
640
|
|
601
641
|
end
|
@@ -627,20 +667,48 @@ module Treetop
|
|
627
667
|
r4 = _nt_space
|
628
668
|
s0 << r4
|
629
669
|
if r4
|
630
|
-
|
670
|
+
i6, s6 = index, []
|
671
|
+
if input.index('do', index) == index
|
672
|
+
r7 = (SyntaxNode).new(input, index...(index + 2))
|
673
|
+
@index += 2
|
674
|
+
else
|
675
|
+
terminal_parse_failure('do')
|
676
|
+
r7 = nil
|
677
|
+
end
|
678
|
+
s6 << r7
|
679
|
+
if r7
|
680
|
+
r8 = _nt_space
|
681
|
+
s6 << r8
|
682
|
+
end
|
683
|
+
if s6.last
|
684
|
+
r6 = (SyntaxNode).new(input, i6...index, s6)
|
685
|
+
r6.extend(ParsingRule0)
|
686
|
+
else
|
687
|
+
self.index = i6
|
688
|
+
r6 = nil
|
689
|
+
end
|
690
|
+
if r6
|
691
|
+
r5 = r6
|
692
|
+
else
|
693
|
+
r5 = SyntaxNode.new(input, index...index)
|
694
|
+
end
|
631
695
|
s0 << r5
|
632
696
|
if r5
|
633
|
-
|
634
|
-
s0 <<
|
635
|
-
if
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
697
|
+
r9 = _nt_parsing_expression
|
698
|
+
s0 << r9
|
699
|
+
if r9
|
700
|
+
r10 = _nt_space
|
701
|
+
s0 << r10
|
702
|
+
if r10
|
703
|
+
if input.index('end', index) == index
|
704
|
+
r11 = (SyntaxNode).new(input, index...(index + 3))
|
705
|
+
@index += 3
|
706
|
+
else
|
707
|
+
terminal_parse_failure('end')
|
708
|
+
r11 = nil
|
709
|
+
end
|
710
|
+
s0 << r11
|
642
711
|
end
|
643
|
-
s0 << r7
|
644
712
|
end
|
645
713
|
end
|
646
714
|
end
|
@@ -649,7 +717,7 @@ module Treetop
|
|
649
717
|
end
|
650
718
|
if s0.last
|
651
719
|
r0 = (ParsingRule).new(input, i0...index, s0)
|
652
|
-
r0.extend(
|
720
|
+
r0.extend(ParsingRule1)
|
653
721
|
else
|
654
722
|
self.index = i0
|
655
723
|
r0 = nil
|
@@ -18,7 +18,7 @@ module Treetop
|
|
18
18
|
end
|
19
19
|
|
20
20
|
rule grammar
|
21
|
-
'grammar' space grammar_name space declaration_sequence space? 'end' <Grammar>
|
21
|
+
'grammar' space grammar_name space ('do' space)? declaration_sequence space? 'end' <Grammar>
|
22
22
|
end
|
23
23
|
|
24
24
|
rule grammar_name
|
@@ -55,7 +55,7 @@ module Treetop
|
|
55
55
|
end
|
56
56
|
|
57
57
|
rule parsing_rule
|
58
|
-
'rule' space nonterminal space parsing_expression space 'end' <ParsingRule>
|
58
|
+
'rule' space nonterminal space ('do' space)? parsing_expression space 'end' <ParsingRule>
|
59
59
|
end
|
60
60
|
|
61
61
|
rule parsing_expression
|
@@ -4,7 +4,7 @@ module Treetop
|
|
4
4
|
def compile(address, builder, parent_expression = nil)
|
5
5
|
super
|
6
6
|
builder.if__ "index < input_length" do
|
7
|
-
assign_result "(#{node_class_name}
|
7
|
+
assign_result "instantiate_node(#{node_class_name},input, index...(index + 1))"
|
8
8
|
extend_result_with_inline_module
|
9
9
|
builder << "@index += 1"
|
10
10
|
end
|
@@ -5,7 +5,7 @@ module Treetop
|
|
5
5
|
super
|
6
6
|
|
7
7
|
builder.if__ "input.index(Regexp.new(#{single_quote(text_value)}), index) == index" do
|
8
|
-
assign_result "(#{node_class_name}
|
8
|
+
assign_result "instantiate_node(#{node_class_name},input, index...(index + 1))"
|
9
9
|
extend_result_with_inline_module
|
10
10
|
builder << "@index += 1"
|
11
11
|
end
|
@@ -3,7 +3,7 @@ module Treetop
|
|
3
3
|
module InlineModuleMixin
|
4
4
|
attr_reader :module_name
|
5
5
|
|
6
|
-
def compile(index,
|
6
|
+
def compile(index, builder, rule)
|
7
7
|
@module_name = "#{rule.name.treetop_camelize}#{index}"
|
8
8
|
end
|
9
9
|
end
|
@@ -12,7 +12,7 @@ module Treetop
|
|
12
12
|
|
13
13
|
include InlineModuleMixin
|
14
14
|
|
15
|
-
def compile(index,
|
15
|
+
def compile(index, builder, rule)
|
16
16
|
super
|
17
17
|
builder.module_declaration(module_name) do
|
18
18
|
builder << ruby_code.gsub(/\A\n/, '').rstrip
|
@@ -24,4 +24,4 @@ module Treetop
|
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
27
|
-
end
|
27
|
+
end
|
@@ -9,7 +9,7 @@ module Treetop
|
|
9
9
|
|
10
10
|
def compile_inline_module_declarations(builder)
|
11
11
|
parsing_expression.inline_modules.each_with_index do |inline_module, i|
|
12
|
-
inline_module.compile(i,
|
12
|
+
inline_module.compile(i, builder, self)
|
13
13
|
builder.newline
|
14
14
|
end
|
15
15
|
end
|
@@ -52,4 +52,4 @@ module Treetop
|
|
52
52
|
end
|
53
53
|
end
|
54
54
|
end
|
55
|
-
end
|
55
|
+
end
|
@@ -24,7 +24,7 @@ module Treetop
|
|
24
24
|
end
|
25
25
|
|
26
26
|
def assign_and_extend_result
|
27
|
-
assign_result "#{node_class_name}
|
27
|
+
assign_result "instantiate_node(#{node_class_name},input, #{start_index_var}...index, #{accumulator_var})"
|
28
28
|
extend_result_with_inline_module
|
29
29
|
end
|
30
30
|
end
|
@@ -7,7 +7,7 @@ module Treetop
|
|
7
7
|
use_vars :result, :start_index, :accumulator
|
8
8
|
compile_sequence_elements(sequence_elements)
|
9
9
|
builder.if__ "#{accumulator_var}.last" do
|
10
|
-
assign_result "(#{node_class_name}
|
10
|
+
assign_result "instantiate_node(#{node_class_name},input, #{start_index_var}...index, #{accumulator_var})"
|
11
11
|
extend_result sequence_element_accessor_module_name if sequence_element_accessor_module_name
|
12
12
|
extend_result_with_inline_module
|
13
13
|
end
|
@@ -50,7 +50,7 @@ module Treetop
|
|
50
50
|
@sequence_elements = sequence_elements
|
51
51
|
end
|
52
52
|
|
53
|
-
def compile(index,
|
53
|
+
def compile(index, builder, rule)
|
54
54
|
super
|
55
55
|
builder.module_declaration(module_name) do
|
56
56
|
sequence_elements.each_with_index do |element, index|
|
@@ -6,7 +6,7 @@ module Treetop
|
|
6
6
|
string_length = eval(text_value).length
|
7
7
|
|
8
8
|
builder.if__ "input.index(#{text_value}, index) == index" do
|
9
|
-
assign_result "(#{node_class_name}
|
9
|
+
assign_result "instantiate_node(#{node_class_name},input, index...(index + #{string_length}))"
|
10
10
|
extend_result_with_inline_module
|
11
11
|
builder << "@index += #{string_length}"
|
12
12
|
end
|
@@ -64,7 +64,7 @@ module Treetop
|
|
64
64
|
|
65
65
|
def parse_anything(node_class = SyntaxNode, inline_module = nil)
|
66
66
|
if index < input.length
|
67
|
-
result = node_class
|
67
|
+
result = instantiate_node(node_class,input, index...(index + 1))
|
68
68
|
result.extend(inline_module) if inline_module
|
69
69
|
@index += 1
|
70
70
|
result
|
@@ -73,6 +73,14 @@ module Treetop
|
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
76
|
+
def instantiate_node(node_type,*args)
|
77
|
+
if node_type.respond_to? :new
|
78
|
+
node_type.new(*args)
|
79
|
+
else
|
80
|
+
SyntaxNode.new(*args).extend(node_type)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
76
84
|
def terminal_parse_failure(expected_string)
|
77
85
|
return nil if index < max_terminal_failure_index
|
78
86
|
if index > max_terminal_failure_index
|
@@ -27,7 +27,7 @@ module Treetop
|
|
27
27
|
end
|
28
28
|
|
29
29
|
def empty?
|
30
|
-
|
30
|
+
interval.first == interval.last && interval.exclude_end?
|
31
31
|
end
|
32
32
|
|
33
33
|
def extension_modules
|
@@ -69,4 +69,4 @@ module Treetop
|
|
69
69
|
end
|
70
70
|
end
|
71
71
|
end
|
72
|
-
end
|
72
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Treetop
|
2
|
+
module Runtime
|
3
|
+
class TerminalParseFailure
|
4
|
+
attr_reader :index
|
5
|
+
|
6
|
+
def initialize(index, expected_string)
|
7
|
+
@index = index
|
8
|
+
@caller = caller
|
9
|
+
@expected_string = expected_string
|
10
|
+
end
|
11
|
+
|
12
|
+
def expected_string
|
13
|
+
"#{@expected_string} from #{@caller.map{|s| s.sub(/\A.*:([0-9]+):in `([^']*)'.*/,'\2:\1')}*" from "}\n\t"
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_s
|
17
|
+
"String matching #{expected_string} expected."
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: treetop
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nathan Sobo
|
@@ -9,11 +9,12 @@ autorequire: treetop
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2009-03-19 00:00:00 +11:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: polyglot
|
17
|
+
type: :runtime
|
17
18
|
version_requirement:
|
18
19
|
version_requirements: !ruby/object:Gem::Requirement
|
19
20
|
requirements:
|
@@ -32,7 +33,6 @@ extra_rdoc_files: []
|
|
32
33
|
files:
|
33
34
|
- README
|
34
35
|
- Rakefile
|
35
|
-
- lib/metagrammar.rb
|
36
36
|
- lib/treetop
|
37
37
|
- lib/treetop/bootstrap_gen_1_metagrammar.rb
|
38
38
|
- lib/treetop/compiler
|
@@ -74,8 +74,10 @@ files:
|
|
74
74
|
- lib/treetop/runtime/interval_skip_list.rb
|
75
75
|
- lib/treetop/runtime/syntax_node.rb
|
76
76
|
- lib/treetop/runtime/terminal_parse_failure.rb
|
77
|
+
- lib/treetop/runtime/terminal_parse_failure_debug.rb
|
77
78
|
- lib/treetop/runtime/terminal_syntax_node.rb
|
78
79
|
- lib/treetop/runtime.rb
|
80
|
+
- lib/treetop/version.rb
|
79
81
|
- lib/treetop.rb
|
80
82
|
- bin/tt
|
81
83
|
- doc/contributing_and_planned_features.markdown
|
@@ -120,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
120
122
|
requirements: []
|
121
123
|
|
122
124
|
rubyforge_project:
|
123
|
-
rubygems_version: 1.1
|
125
|
+
rubygems_version: 1.3.1
|
124
126
|
signing_key:
|
125
127
|
specification_version: 2
|
126
128
|
summary: A Ruby-based text parsing and interpretation DSL
|
data/lib/metagrammar.rb
DELETED
File without changes
|