Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Remember to update the input position when restoring a memoized rule …

…from the cache.

Also update tests to verify this behavior.
  • Loading branch information...
commit 70f039b017ad73aeb6d95ba62b0156c8cd3abe2f 1 parent b2a6c35
@joachimm joachimm authored committed
Showing with 27 additions and 2 deletions.
  1. +5 −1 lib/citrus.rb
  2. +22 −1 test/memoized_input_test.rb
View
6 lib/citrus.rb
@@ -308,7 +308,11 @@ def apply_rule(rule, position, events) # :nodoc:
if memo[position]
@cache_hits += 1
- events.concat(memo[position])
+ c = memo[position]
+ unless c.empty?
+ events.concat(c)
+ self.pos += events[-1]
+ end
else
index = events.size
rule.exec(self, events)
View
23 test/memoized_input_test.rb
@@ -26,7 +26,7 @@ def test_memoized?
def test_cache_hits1
input = MemoizedInput.new('a')
input.exec(LetterA.rule(:top))
- assert_equal(2, input.cache_hits)
+ assert_equal(3, input.cache_hits)
end
def test_cache_hits2
@@ -40,4 +40,25 @@ def test_cache_hits3
input.exec(LetterA.rule(:top))
assert_equal(0, input.cache_hits)
end
+
+ grammar :LettersABC do
+ rule :top do
+ any(all(:a,:b,:c), all(:b,:a,:c), all(:b,:c,:a))
+ end
+ rule :a do
+ "a"
+ end
+ rule :b do
+ "b"
+ end
+ rule :c do
+ "c"
+ end
+ end
+
+ def test_memoization
+ match = LettersABC.parse('bca',{:memoize=>true})
+ assert_equal('bca',match)
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.