Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add Match#source as a shortcut for input.source.

This method allows locating the original parsed source during the
production of semantic values. It is roughly a shortcut for
`input.source` but is robust to situations where matches would be
created with strings (such as in the tests).
  • Loading branch information...
commit e23958e28636b0430bedd8fa03b5ccd410025624 1 parent e23b0ce
@blambeau blambeau authored
Showing with 14 additions and 0 deletions.
  1. +5 −0 lib/citrus.rb
  2. +9 −0 test/match_test.rb
View
5 lib/citrus.rb
@@ -1301,6 +1301,11 @@ def length
@events.last
end
+ # Convenient shortcut for +input.source+
+ def source
+ (input.respond_to?(:source) && input.source) || input
+ end
+
# Returns the slice of the source text that this match captures.
def string
@string ||= input.to_str[offset, length]
View
9 test/match_test.rb
@@ -20,6 +20,15 @@ def test_match_inequality
assert_equal(false, match2 == match1)
end
+ def test_source
+ match1 = Match.new('abcdef')
+ assert_equal 'abcdef', match1.source
+
+ path = Struct.new(:to_path).new(__FILE__)
+ match2 = Match.new(Input.new(path))
+ assert_equal path, match2.source
+ end
+
def test_string
match1 = Match.new('abcdef')
assert_equal 'abcdef', match1.string
Please sign in to comment.
Something went wrong with that request. Please try again.