Permalink
Browse files

Fix parser crash when lazily instantiated node is used from the cache.

  • Loading branch information...
1 parent 0135c7a commit 2eb685b6ef58ba2254c432889e3dd7bd97505b3d @jgarber jgarber committed Nov 25, 2009
Showing with 33 additions and 1 deletion.
  1. +4 −1 lib/treetop/compiler/node_classes/parsing_rule.rb
  2. +29 −0 spec/compiler/parsing_rule_spec.rb
View
5 lib/treetop/compiler/node_classes/parsing_rule.rb
@@ -34,7 +34,10 @@ def generate_method_definition(builder)
def generate_cache_lookup(builder)
builder.if_ "node_cache[:#{name}].has_key?(index)" do
builder.assign 'cached', "node_cache[:#{name}][index]"
- builder << '@index = cached.interval.end if cached'
+ builder.if_ "cached" do
+ builder << 'cached = SyntaxNode.new(input, index...(index + 1)) if cached == true'
+ builder << '@index = cached.interval.end'
+ end
builder << 'return cached'
end
end
View
29 spec/compiler/parsing_rule_spec.rb
@@ -29,4 +29,33 @@ module ParsingRuleSpec
parser.index.should == cached_node.interval.end
end
end
+
+
+ describe "a grammar with choice that uses the cache and has a subsequent expression" do
+ testing_grammar %{
+ grammar Logic
+ rule expression
+ value_plus
+ /
+ value
+ end
+
+ rule value_plus
+ value "something else"
+ end
+
+ rule value
+ [a-z]
+ /
+ "foobar" # the subsequent expression that needs cached.interval.end
+ end
+ end
+ }
+
+ it "parses a single-character value and generates a node from the cache" do
+ result = parse('a')
+ result.should be_a(Treetop::Runtime::SyntaxNode)
+ result.elements.should be_nil
+ end
+ end
end

0 comments on commit 2eb685b

Please sign in to comment.