Browse files

added some comment to code

  • Loading branch information...
1 parent 4f8fc14 commit 5e34cd1f2a8f7030c1642f430a0cdbce162a25a4 @xli committed Aug 19, 2011
Showing with 19 additions and 10 deletions.
  1. +19 −10 lib/parslet/atoms/rule/position.rb
View
29 lib/parslet/atoms/rule/position.rb
@@ -36,7 +36,7 @@ def detected?
self.head != nil
end
- def setup(lr_stack)
+ def setup_for_re_eval_involved_rules(lr_stack)
self.head ||= Head.new(rule, [], [])
lr_stack.top_down do |lr|
return if lr.head == self.head
@@ -70,12 +70,12 @@ def lr_stack
include Context
- # Eval rule body with LR supported by
- # placing a LR flag before eval rule body
- # and growing LR seed after detected LR
def apply_rule
result = recall
if result.nil?
+ # Eval rule body with LR supported by
+ # placing a LR flag before eval rule body
+ # and growing LR seed after detected LR
lr = LR.new(fail('left recursion detected'), self.rule, self.pos)
lr_stack.push(lr)
self.entry = lr
@@ -86,7 +86,11 @@ def apply_rule
end
result = self.entry
elsif result.is_a?(LR)
- result.setup(lr_stack)
+ # Find out all involved lrs in stack
+ # Collect rules of involved lrs
+ # And set head of involved lrs for re-eval
+ # rules in recall process
+ result.setup_for_re_eval_involved_rules(lr_stack)
end
source.pos = result.pos
result.answer
@@ -119,19 +123,24 @@ def grow_lr(h)
loop do
h.reset_eval_rules
entry = eval_rule_body
- break if entry.error? || entry.pos <= self.entry.pos
+ break if entry.error? || no_progress?(entry)
self.entry = entry
end
self.head = nil
end
- def fail(message)
- MemoEntry.new(rule.error(source, message), self.pos)
- end
-
def eval_rule_body
source.pos = self.pos
answer = rule.eval_rule_body(source, context)
MemoEntry.new(answer, source.pos)
end
+
+ def no_progress?(entry)
+ entry.pos <= self.entry.pos
+ end
+
+ def fail(message)
+ MemoEntry.new(rule.error(source, message), self.pos)
+ end
+
end

0 comments on commit 5e34cd1

Please sign in to comment.