Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Keep input instead of string in Match.

For traceability of matches inside the parsed string, we now keep
the Input instance instead of the source text in matches. This is
transparent for the Match class, as it simply expects its input to
behave like a String (to_str). That method has been added to Input
accordingly.
  • Loading branch information...
commit e7f2ba5d2708def99949bb8ae47e093908a5331f 1 parent 13ccfb3
@blambeau blambeau authored
Showing with 12 additions and 8 deletions.
  1. +12 −8 lib/citrus.rb
View
20 lib/citrus.rb
@@ -263,6 +263,9 @@ def test(rule)
events[-1]
end
+ # Returns the scanned string.
+ alias_method :to_str, :string
+
private
# Appends all events for +rule+ at the given +position+ to +events+.
@@ -636,7 +639,7 @@ def parse(string, options={})
raise ParseError, input
end
- Match.new(string, events, opts[:offset])
+ Match.new(input, events, opts[:offset])
end
# Tests whether or not this rule matches on the given +string+. Returns the
@@ -1239,8 +1242,8 @@ def to_citrus # :nodoc:
# instantiated as needed. This class provides several convenient tree
# traversal methods that help when examining and interpreting parse results.
class Match
- def initialize(source, events=[], offset = 0)
- @source = source
+ def initialize(input, events=[], offset = 0)
+ @input = input
@offset = offset
if events.length > 0
@@ -1258,16 +1261,17 @@ def initialize(source, events=[], offset = 0)
end
else
# Create a default stream of events for the given string.
- events = [Rule.for(source), CLOSE, source.length]
+ string = input.to_str
+ events = [Rule.for(string), CLOSE, string.length]
end
@events = events
end
# The main parsed text.
- attr_reader :source
+ attr_reader :input
- # The index of this match in the source text.
+ # The index of this match in the input text.
attr_reader :offset
# The array of events for this match.
@@ -1280,7 +1284,7 @@ def length
# Returns the slice of the source text that this match captures.
def string
- @string ||= @source[offset, length]
+ @string ||= input.to_str[offset, length]
end
# Returns a hash of capture names to arrays of matches with that name,
@@ -1431,7 +1435,7 @@ def process_events!
os = stack.pop
start = stack.pop
- match = Match.new(source, @events[start..index], @offset + os)
+ match = Match.new(input, @events[start..index], @offset + os)
capture!(rule, match)
if stack.size == 1
Please sign in to comment.
Something went wrong with that request. Please try again.