Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Regen'd parser with kpeg 0.8.3.

  • Loading branch information...
commit 5c14c981e794a41300f6272dc6f3a4f157c9708f 1 parent 7c157f6
Brian Shirai authored
Showing with 47 additions and 5 deletions.
  1. +47 −5 lib/poetics/parser/parser.rb
52 lib/poetics/parser/parser.rb
View
@@ -20,8 +20,8 @@ def initialize(str, debug=false)
end
attr_reader :string
- attr_reader :result, :failing_rule_offset
- attr_accessor :pos
+ attr_reader :failing_rule_offset
+ attr_accessor :result, :pos
# STANDALONE START
def current_column(target=pos)
@@ -234,6 +234,7 @@ def external_invoke(other, rule, *args)
begin
if val = __send__(rule, *args)
other.pos = @pos
+ other.result = @result
else
other.set_failed_rule "#{self.class}##{rule}"
end
@@ -244,6 +245,43 @@ def external_invoke(other, rule, *args)
end
end
+ def apply_with_args(rule, *args)
+ memo_key = [rule, args]
+ if m = @memoizations[memo_key][@pos]
+ m.inc!
+
+ prev = @pos
+ @pos = m.pos
+ if m.ans.kind_of? LeftRecursive
+ m.ans.detected = true
+ return nil
+ end
+
+ @result = m.result
+
+ return m.ans
+ else
+ lr = LeftRecursive.new(false)
+ m = MemoEntry.new(lr, @pos)
+ @memoizations[memo_key][@pos] = m
+ start_pos = @pos
+
+ ans = __send__ rule, *args
+
+ m.move! ans, @pos, @result
+
+ # Don't bother trying to grow the left recursion
+ # if it's failing straight away (thus there is no seed)
+ if ans and lr.detected
+ return grow_lr(rule, args, start_pos, m)
+ else
+ return ans
+ end
+
+ return ans
+ end
+ end
+
def apply(rule)
if m = @memoizations[rule][@pos]
m.inc!
@@ -271,7 +309,7 @@ def apply(rule)
# Don't bother trying to grow the left recursion
# if it's failing straight away (thus there is no seed)
if ans and lr.detected
- return grow_lr(rule, start_pos, m)
+ return grow_lr(rule, nil, start_pos, m)
else
return ans
end
@@ -280,12 +318,16 @@ def apply(rule)
end
end
- def grow_lr(rule, start_pos, m)
+ def grow_lr(rule, args, start_pos, m)
while true
@pos = start_pos
@result = m.result
- ans = __send__ rule
+ if args
+ ans = __send__ rule, *args
+ else
+ ans = __send__ rule
+ end
return nil unless ans
break if @pos <= m.pos
Please sign in to comment.
Something went wrong with that request. Please try again.