lib/arugula/parts.rb in arugula-0.2.1 vs lib/arugula/parts.rb in arugula-0.3.0
- old
+ new
@@ -22,53 +22,61 @@
def to_s
literal.gsub('\\', '\\\\')
end
- def match(str, index)
+ def match(str, index, _match_data)
length = literal.size
matches = str[index, length] == literal
[matches, index + (matches ? length : 0)]
end
end
- class AndPart < Part
+ module MatchAll
attr_accessor :parts
def initialize
@parts = []
end
- def to_s
- parts.join
- end
-
- def match(str, index)
+ def match(str, index, match_data)
parts.each do |part|
- match, index = part.match(str, index)
+ match, index = part.match(str, index, match_data)
return false, index unless match
end
[true, index]
end
end
+ class AndPart < Part
+ include MatchAll
+ def to_s
+ parts.join
+ end
+ end
+
module MatchAny
attr_accessor :parts
def initialize
@parts = []
end
- def match(str, index)
+ def match(str, index, match_data)
parts.each do |part|
- match, match_index = part.match(str, index)
+ match, match_index = part.match(str, index, match_data)
return true, match_index if match
end
[false, index]
end
end
class OrPart < Part
include MatchAny
+ def initialize(*parts)
+ super()
+ @parts += parts
+ end
+
def to_s
parts.join '|'
end
end
@@ -86,11 +94,11 @@
def to_s
"#{@range.begin}-#{@range.end}"
end
- def match(str, index)
+ def match(str, index, _match_data)
matches = @range.member?(str[index])
[matches, index + (matches ? 1 : 0)]
end
end
@@ -112,47 +120,57 @@
def initialize(metachar)
@metachar = metachar.to_sym
end
- def match(str, index)
- [MATCHERS[@metachar][str, index], index + OFFSETS[@metachar]]
+ def match(str, index, _match_data)
+ matches = MATCHERS[@metachar][str, index]
+ [matches, index + (matches ? OFFSETS[@metachar] : 0)]
end
def to_s
"\\#{@metachar}"
end
end
- class MatchPart < Part
- attr_accessor :parts
- def initialize
- @parts = AndPart.new
+ class CapturePart < Part
+ include MatchAll
+ attr_reader :name
+
+ def initialize(name)
+ @name = name
+ super()
end
def to_s
"(#{parts.join})"
end
+
+ def match(str, index, match_data)
+ matches, end_index = super
+ match_data.add_capture(@name, index, end_index) if matches
+ [matches, end_index]
+ end
end
class EOLPart < Part
def to_s
'$'
end
- def match(str, index)
+ def match(str, index, _match_data)
matches = str[index] == "\n" || index == str.size
- return true, index + 1 if matches
+ return true, index if matches
[false, index]
end
end
class SOLPart < Part
def to_s
'^'
end
- def match(str, index)
+ def match(str, index, _match_data)
matches = (index == 0) || (str[index - 1] == "\n")
[matches, index]
end
end
@@ -167,13 +185,13 @@
include Wrapping
def to_s
"#{wrapped}*"
end
- def match(str, index)
+ def match(str, index, match_data)
loop do
- matches, index = wrapped.match(str, index)
+ matches, index = wrapped.match(str, index, match_data)
return true, index unless matches
end
end
end
@@ -181,14 +199,14 @@
include Wrapping
def to_s
"#{wrapped}+"
end
- def match(str, index)
+ def match(str, index, match_data)
has_matched = false
loop do
- matches, index = wrapped.match(str, index)
+ matches, index = wrapped.match(str, index, match_data)
has_matched = true if matches
return has_matched, index unless matches
end
end
end
@@ -196,10 +214,10 @@
class DotPart < Part
def to_s
'.'
end
- def match(str, index)
+ def match(str, index, _match_data)
matches = index < str.size
[matches, index + (matches ? 1 : 0)]
end
end
end