class MetaParse::Matcher
General class to match a simple pattern against a scanner. Subclasses implement compound matching.
Attributes
spec[RW]
Public Class Methods
compile(spec)
click to toggle source
Compile a Matcher
specification into a concrete Matcher
or subclass.
# File lib/meta_parse.rb, line 157 def self.compile(spec) case spec when Matcher spec when String, Regexp Matcher.new spec when Array case spec[0] when :or compiled_body = spec[1..-1].map { |x| self.compile x } AlternativeMatcher.new(compiled_body) when :and compiled_body = spec[1..-1].map { |x| self.compile x } SequentialMatcher.new(compiled_body) when :*, :+, :'?' compiled_body = self.compile(spec[1]) case spec[0] when :'?' min = 0 max = 1 when :+ min = 1 end RepetitionMatcher.new(compiled_body, min, max, *spec[4..-1]) end when Proc, Symbol FunctionMatcher.new spec end end
Public Instance Methods
inspect()
click to toggle source
# File lib/meta_parse.rb, line 198 def inspect "<match #{show}>" end
m(string)
click to toggle source
Syntactic sugar to create MetaScanner
and match self against string.
# File lib/meta_parse.rb, line 247 def m(string) match MetaScanner.new(string) end
m?(string)
click to toggle source
Syntactic sugar to create MetaScanner
and match? self against string.
# File lib/meta_parse.rb, line 240 def m?(string) match? MetaScanner.new(string) end
match(scanner, context=nil)
click to toggle source
Like match? but clone self first if stateful.
Subclasses should not implement match, but should call it on peers.
# File lib/meta_parse.rb, line 226 def match(scanner, context=nil) (stateful ? clone : self).match? scanner, context end
match?(scanner, context=nil)
click to toggle source
Try to match own pattern/combination against supplied scanner. Context is unused.
Subclasses should implement match? but not call it on peers.
# File lib/meta_parse.rb, line 207 def match?(scanner, context=nil) case scanner when MetaScanner result = scanner.scan spec result else raise "match? requires scanner" # FIXME: Why doesn't the coercion below work? # when String # match?(MetaParse::MetaScanner.new(scanner), context) # (MetaScanner.new(scanner)).scan spec end end
show()
click to toggle source
# File lib/meta_parse.rb, line 194 def show spec.inspect end
stateful()
click to toggle source
Is this Matcher
stateful? A class attribute which may be overriden by subclasses.
# File lib/meta_parse.rb, line 233 def stateful false end