Skip to content

Commit

Permalink
Repeated subrules produce serial-numbered accessor functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cjheath committed Aug 20, 2009
1 parent 4b0b91f commit 30a86fc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
16 changes: 8 additions & 8 deletions lib/treetop/compiler/metagrammar.rb
@@ -1,5 +1,5 @@
# Autogenerated from a Treetop grammar. Edits may be lost.
# Created: Sat Aug 01 18:04:21 +1000 2009


module Treetop
module Compiler
Expand Down Expand Up @@ -228,11 +228,11 @@ def _nt_require_statement
end

module ModuleDeclaration0
def space
def space1
elements[1]
end

def space
def space2
elements[4]
end
end
Expand Down Expand Up @@ -380,15 +380,15 @@ def space
end

module Grammar1
def space
def space1
elements[1]
end

def grammar_name
elements[2]
end

def space
def space2
elements[3]
end

Expand Down Expand Up @@ -763,23 +763,23 @@ def space
end

module ParsingRule1
def space
def space1
elements[1]
end

def nonterminal
elements[2]
end

def space
def space2
elements[3]
end

def parsing_expression
elements[5]
end

def space
def space3
elements[6]
end

Expand Down
5 changes: 4 additions & 1 deletion lib/treetop/compiler/node_classes/sequence.rb
Expand Up @@ -53,9 +53,12 @@ def initialize(sequence_elements)
def compile(index, builder, rule)
super
builder.module_declaration(module_name) do
elements_by_name = sequence_elements.inject({}){|h,e| (h[e.label_name] ||= []) << e; h}
sequence_elements.each_with_index do |element, index|
if element.label_name
builder.method_declaration(element.label_name) do
repetitions = elements_by_name[element.label_name]
label_name = element.label_name + (repetitions.size > 1 ? (repetitions.index(element)+1).to_s : "")
builder.method_declaration(label_name) do
builder << "elements[#{index}]"
end
builder.newline unless index == sequence_elements.size - 1
Expand Down
29 changes: 29 additions & 0 deletions spec/compiler/repeated_subrule_spec.rb
@@ -0,0 +1,29 @@
require File.expand_path("#{File.dirname(__FILE__)}/../spec_helper")

module RepeatedSubruleSpec
describe "a repeated subrule" do
testing_grammar %{
grammar Foo
rule foo
a:'a' space b:'b' space 'c'
end
rule space
' '
end
end
}

it "should produce a parser having sequence-numbered node accessor methods" do
parse("a b c") do |result|
result.should_not be_nil
result.should respond_to(:space1)
result.should respond_to(:space2)
result.should_not respond_to(:space)
result.should respond_to(:a)
result.should respond_to(:b)
result.should_not respond_to(:c)
end
end
end
end

0 comments on commit 30a86fc

Please sign in to comment.