public
Fork of nathansobo/treetop
Description: A Ruby-based parsing DSL based on parsing expression grammars.
Homepage: http://treetop.rubyforge.org
Clone URL: git://github.com/juretta/treetop.git
Search Repo:
Added support for Ruby-style comments
Still seems to be an issue if a comment occurs before "grammar"/"module"
cjheath (author)
Tue Dec 11 15:16:31 -0800 2007
commit  9c5b6115c6681632ebf348a9bd8644bdf3bffacc
tree    161cfa8ec1ceccae23843a21b4677aa61fe3071a
parent  c8c6d1b35be7431069cd197be07c8850d4e425e2
...
1869
1870
1871
1872
 
 
 
 
 
 
 
1873
1874
1875
...
1987
1988
1989
1990
 
 
 
 
 
 
 
1991
1992
1993
...
2101
2102
2103
2104
 
 
 
 
 
 
 
2105
2106
2107
...
2240
2241
2242
2243
 
 
 
 
 
 
 
2244
2245
2246
...
2436
2437
2438
2439
 
 
 
 
 
 
 
2440
2441
2442
...
2569
2570
2571
2572
 
 
 
 
 
 
 
2573
2574
2575
...
2647
2648
2649
2650
2651
2652
 
 
 
 
2653
2654
 
 
 
 
 
 
 
2655
2656
2657
...
2671
2672
2673
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2674
2675
2676
...
2678
2679
2680
2681
2682
 
...
1869
1870
1871
 
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
...
1993
1994
1995
 
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
...
2113
2114
2115
 
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
...
2258
2259
2260
 
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
...
2460
2461
2462
 
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
...
2599
2600
2601
 
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
...
2683
2684
2685
 
 
 
2686
2687
2688
2689
2690
 
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
...
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
...
2827
2828
2829
 
2830
2831
0
@@ -1869,7 +1869,13 @@ module Treetop
0
                 if r8
0
                   r6 = r8
0
                 else
0
- r9 = parse_anything(SyntaxNode)
0
+ if index < input_length
0
+ r9 = (SyntaxNode).new(input, index...(index + 1))
0
+ @index += 1
0
+ else
0
+ terminal_parse_failure("any character")
0
+ r9 = nil
0
+ end
0
                   if r9
0
                     r6 = r9
0
                   else
0
@@ -1987,7 +1993,13 @@ module Treetop
0
                 if r8
0
                   r6 = r8
0
                 else
0
- r9 = parse_anything(SyntaxNode)
0
+ if index < input_length
0
+ r9 = (SyntaxNode).new(input, index...(index + 1))
0
+ @index += 1
0
+ else
0
+ terminal_parse_failure("any character")
0
+ r9 = nil
0
+ end
0
                   if r9
0
                     r6 = r9
0
                   else
0
@@ -2101,7 +2113,13 @@ module Treetop
0
               if r7
0
                 r6 = r7
0
               else
0
- r8 = parse_anything(SyntaxNode)
0
+ if index < input_length
0
+ r8 = (SyntaxNode).new(input, index...(index + 1))
0
+ @index += 1
0
+ else
0
+ terminal_parse_failure("any character")
0
+ r8 = nil
0
+ end
0
                 if r8
0
                   r6 = r8
0
                 else
0
@@ -2240,7 +2258,13 @@ module Treetop
0
               end
0
               s5 << r6
0
               if r6
0
- r8 = parse_anything(SyntaxNode)
0
+ if index < input_length
0
+ r8 = (SyntaxNode).new(input, index...(index + 1))
0
+ @index += 1
0
+ else
0
+ terminal_parse_failure("any character")
0
+ r8 = nil
0
+ end
0
                 s5 << r8
0
               end
0
               if s5.last
0
@@ -2436,7 +2460,13 @@ module Treetop
0
               end
0
               s5 << r6
0
               if r6
0
- r8 = parse_anything(SyntaxNode)
0
+ if index < input_length
0
+ r8 = (SyntaxNode).new(input, index...(index + 1))
0
+ @index += 1
0
+ else
0
+ terminal_parse_failure("any character")
0
+ r8 = nil
0
+ end
0
                 s5 << r8
0
               end
0
               if s5.last
0
@@ -2569,7 +2599,13 @@ module Treetop
0
         end
0
         s0 << r1
0
         if r1
0
- r3 = parse_anything(SyntaxNode)
0
+ if index < input_length
0
+ r3 = (SyntaxNode).new(input, index...(index + 1))
0
+ @index += 1
0
+ else
0
+ terminal_parse_failure("any character")
0
+ r3 = nil
0
+ end
0
           s0 << r3
0
         end
0
         if s0.last
0
@@ -2647,11 +2683,18 @@ module Treetop
0
         
0
         s0, i0 = [], index
0
         loop do
0
- if input.index(/[ \t\n\r]/, index) == index
0
- r1 = (SyntaxNode).new(input, index...(index + 1))
0
- @index += 1
0
+ i1 = index
0
+ r2 = _nt_white
0
+ if r2
0
+ r1 = r2
0
           else
0
- r1 = nil
0
+ r3 = _nt_comment_to_eol
0
+ if r3
0
+ r1 = r3
0
+ else
0
+ self.index = i1
0
+ r1 = nil
0
+ end
0
           end
0
           if r1
0
             s0 << r1
0
@@ -2671,6 +2714,112 @@ module Treetop
0
         return r0
0
       end
0
       
0
+ module CommentToEol0
0
+ end
0
+
0
+ module CommentToEol1
0
+ end
0
+
0
+ def _nt_comment_to_eol
0
+ start_index = index
0
+ cached = node_cache[:comment_to_eol][index]
0
+ if cached
0
+ @index = cached.interval.end
0
+ return cached
0
+ end
0
+
0
+ i0, s0 = index, []
0
+ if input.index('#', index) == index
0
+ r1 = (SyntaxNode).new(input, index...(index + 1))
0
+ @index += 1
0
+ else
0
+ terminal_parse_failure('#')
0
+ r1 = nil
0
+ end
0
+ s0 << r1
0
+ if r1
0
+ s2, i2 = [], index
0
+ loop do
0
+ i3, s3 = index, []
0
+ i4 = index
0
+ if input.index("\n", index) == index
0
+ r5 = (SyntaxNode).new(input, index...(index + 1))
0
+ @index += 1
0
+ else
0
+ terminal_parse_failure("\n")
0
+ r5 = nil
0
+ end
0
+ if r5
0
+ r4 = nil
0
+ else
0
+ self.index = i4
0
+ r4 = SyntaxNode.new(input, index...index)
0
+ end
0
+ s3 << r4
0
+ if r4
0
+ if index < input_length
0
+ r6 = (SyntaxNode).new(input, index...(index + 1))
0
+ @index += 1
0
+ else
0
+ terminal_parse_failure("any character")
0
+ r6 = nil
0
+ end
0
+ s3 << r6
0
+ end
0
+ if s3.last
0
+ r3 = (SyntaxNode).new(input, i3...index, s3)
0
+ r3.extend(CommentToEol0)
0
+ else
0
+ self.index = i3
0
+ r3 = nil
0
+ end
0
+ if r3
0
+ s2 << r3
0
+ else
0
+ break
0
+ end
0
+ end
0
+ if s2.empty?
0
+ self.index = i2
0
+ r2 = nil
0
+ else
0
+ r2 = SyntaxNode.new(input, i2...index, s2)
0
+ end
0
+ s0 << r2
0
+ end
0
+ if s0.last
0
+ r0 = (SyntaxNode).new(input, i0...index, s0)
0
+ r0.extend(CommentToEol1)
0
+ else
0
+ self.index = i0
0
+ r0 = nil
0
+ end
0
+
0
+ node_cache[:comment_to_eol][start_index] = r0
0
+
0
+ return r0
0
+ end
0
+
0
+ def _nt_white
0
+ start_index = index
0
+ cached = node_cache[:white][index]
0
+ if cached
0
+ @index = cached.interval.end
0
+ return cached
0
+ end
0
+
0
+ if input.index(/[ \t\n\r]/, index) == index
0
+ r0 = (SyntaxNode).new(input, index...(index + 1))
0
+ @index += 1
0
+ else
0
+ r0 = nil
0
+ end
0
+
0
+ node_cache[:white][start_index] = r0
0
+
0
+ return r0
0
+ end
0
+
0
     end
0
     
0
     class MetagrammarParser < Treetop::Runtime::CompiledParser
0
@@ -2678,4 +2827,4 @@ module Treetop
0
     end
0
 
0
   end
0
-end
0
\ No newline at end of file
0
+end
...
389
390
391
392
 
 
 
 
 
 
 
 
 
393
394
395
396
397
 
...
389
390
391
 
392
393
394
395
396
397
398
399
400
401
402
403
 
404
405
0
@@ -389,8 +389,16 @@ module Treetop
0
       end
0
 
0
       rule space
0
- [ \t\n\r]+
0
+ (white / comment_to_eol)+
0
+ end
0
+
0
+ rule comment_to_eol
0
+ '#' (!"\n" .)+
0
+ end
0
+
0
+ rule white
0
+ [ \t\n\r]
0
       end
0
     end
0
   end
0
-end
0
\ No newline at end of file
0
+end
...
3
4
5
6
 
7
8
9
 
10
11
 
12
13
14
15
 
16
17
18
19
 
20
21
22
23
24
25
 
26
27
28
29
30
 
31
32
33
34
 
35
36
37
38
39
40
 
...
3
4
5
 
6
7
8
9
10
11
 
12
13
14
15
 
16
17
18
19
 
20
21
22
23
24
25
 
26
27
28
29
30
 
31
32
33
34
 
35
36
37
38
39
 
40
41
0
@@ -3,37 +3,38 @@ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
0
 module GrammarSpec
0
   module Bar
0
   end
0
-
0
+
0
   describe "a grammar" do
0
     testing_grammar %{
0
       grammar Foo
0
+ # This is a grammar for Foo
0
         include GrammarSpec::Bar
0
-
0
+
0
         rule foo
0
           bar / baz
0
         end
0
-
0
+
0
         rule bar
0
           'bar' 'bar'
0
         end
0
-
0
+
0
         rule baz
0
           'baz' 'baz'
0
         end
0
       end
0
     }
0
-
0
+
0
     it "parses matching input" do
0
       parse('barbar').should_not be_nil
0
       parse('bazbaz').should_not be_nil
0
     end
0
-
0
+
0
     it "fails if it does not parse all input" do
0
       parse('barbarbazbaz').should be_nil
0
     end
0
-
0
+
0
     it "mixes in included modules" do
0
       self.class.const_get(:Foo).ancestors.should include(GrammarSpec::Bar)
0
     end
0
   end
0
-end
0
\ No newline at end of file
0
+end
...
36
37
38
39
 
40
41
42
...
74
75
76
77
78
 
...
36
37
38
 
39
40
41
42
...
74
75
76
 
77
78
0
@@ -36,7 +36,7 @@ module Treetop
0
         parser = Treetop::Compiler::MetagrammarParser.new
0
         parser.root = root
0
         node = parser.parse(input)
0
- raise "#{input} cannot be parsed by the metagrammar: #{parser.terminal_failures.map {|f| f.to_s}.join("\n")}" unless node
0
+ raise parser.failure_reason unless node
0
         node
0
       end
0
     end
0
@@ -74,4 +74,4 @@ module Treetop
0
     Spec::Example::ExampleGroupFactory.register(:compiler, self)
0
     Spec::Example::ExampleGroupFactory.register(:parser, self)
0
   end
0
-end
0
\ No newline at end of file
0
+end

Comments

    No one has commented yet.