Class: MetaParse::Matcher
- Inherits:
-
Object
- Object
- MetaParse::Matcher
- Defined in:
- lib/meta_parse.rb
Overview
General class to match a simple pattern against a scanner. Subclasses implement compound matching.
Direct Known Subclasses
AlternativeMatcher, FunctionMatcher, RepetitionMatcher, SequentialMatcher
Instance Attribute Summary collapse
-
#spec ⇒ Object
Returns the value of attribute spec.
Class Method Summary collapse
-
.compile(spec) ⇒ Object
Compile a Matcher specification into a concrete Matcher or subclass.
Instance Method Summary collapse
-
#initialize(spec) ⇒ Matcher
constructor
Initialize Matcher with Matcher spec.
- #inspect ⇒ Object
-
#m(string) ⇒ Object
Syntactic sugar to create MetaScanner and match self against string.
-
#m?(string) ⇒ Boolean
Syntactic sugar to create MetaScanner and match? self against string.
-
#match(scanner, context = nil) ⇒ Object
Like match? but clone self first if stateful.
-
#match?(scanner, context = nil) ⇒ Boolean
Try to match own pattern/combination against supplied scanner.
- #show ⇒ Object
-
#stateful ⇒ Object
Is this Matcher stateful? A class attribute which may be overriden by subclasses.
Constructor Details
#initialize(spec) ⇒ Matcher
Initialize Matcher with Matcher spec.
190 191 192 |
# File 'lib/meta_parse.rb', line 190 def initialize(spec) @spec = spec end |
Instance Attribute Details
#spec ⇒ Object
Returns the value of attribute spec.
152 153 154 |
# File 'lib/meta_parse.rb', line 152 def spec @spec end |
Class Method Details
.compile(spec) ⇒ Object
Compile a Matcher specification into a concrete Matcher or subclass.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# 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 |
Instance Method Details
#inspect ⇒ Object
198 199 200 |
# File 'lib/meta_parse.rb', line 198 def inspect "<match #{show}>" end |
#m(string) ⇒ Object
Syntactic sugar to create MetaScanner and match self against string.
247 248 249 |
# File 'lib/meta_parse.rb', line 247 def m(string) match MetaScanner.new(string) end |
#m?(string) ⇒ Boolean
Syntactic sugar to create MetaScanner and match? self against string.
240 241 242 |
# File 'lib/meta_parse.rb', line 240 def m?(string) match? MetaScanner.new(string) end |
#match(scanner, context = nil) ⇒ Object
Like match? but clone self first if stateful.
Subclasses should not implement match, but should call it on peers.
226 227 228 |
# File 'lib/meta_parse.rb', line 226 def match(scanner, context=nil) (stateful ? clone : self).match? scanner, context end |
#match?(scanner, context = nil) ⇒ Boolean
Try to match own pattern/combination against supplied scanner. Context is unused.
Subclasses should implement match? but not call it on peers.
207 208 209 210 211 212 213 214 215 216 217 218 219 |
# 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 ⇒ Object
194 195 196 |
# File 'lib/meta_parse.rb', line 194 def show spec.inspect end |
#stateful ⇒ Object
Is this Matcher stateful? A class attribute which may be overriden by subclasses.
233 234 235 |
# File 'lib/meta_parse.rb', line 233 def stateful false end |