Class: Aspen::CustomGrammar::Matcher
- Inherits:
-
Object
- Object
- Aspen::CustomGrammar::Matcher
- Defined in:
- lib/aspen/custom_grammar/matcher.rb
Instance Attribute Summary collapse
-
#expression ⇒ Object
Returns the value of attribute expression.
-
#labelreg ⇒ Object
Returns the value of attribute labelreg.
-
#pattern ⇒ Object
Returns the value of attribute pattern.
-
#template ⇒ Object
Returns the value of attribute template.
-
#typereg ⇒ Object
Returns the value of attribute typereg.
Instance Method Summary collapse
-
#captures(str) ⇒ Object
(also: #results)
Compare against narrative line to get captures Example results: { a: , amt: , b: }.
- #captures!(str) ⇒ Object (also: #results!)
-
#initialize(expression:, template:, pattern:) ⇒ Matcher
constructor
A new instance of Matcher.
- #match?(str) ⇒ Boolean
Constructor Details
#initialize(expression:, template:, pattern:) ⇒ Matcher
Returns a new instance of Matcher.
9 10 11 12 13 14 15 16 17 |
# File 'lib/aspen/custom_grammar/matcher.rb', line 9 def initialize(expression: , template: , pattern: ) @expression = expression @template = template # SMELL: I don't like this design. compiled_grammar = Aspen::CustomGrammar.compile(expression) @pattern = compiled_grammar[:pattern] @typereg = compiled_grammar[:type_registry] @labelreg = compiled_grammar[:label_registry] end |
Instance Attribute Details
#expression ⇒ Object
Returns the value of attribute expression.
7 8 9 |
# File 'lib/aspen/custom_grammar/matcher.rb', line 7 def expression @expression end |
#labelreg ⇒ Object
Returns the value of attribute labelreg.
7 8 9 |
# File 'lib/aspen/custom_grammar/matcher.rb', line 7 def labelreg @labelreg end |
#pattern ⇒ Object
Returns the value of attribute pattern.
7 8 9 |
# File 'lib/aspen/custom_grammar/matcher.rb', line 7 def pattern @pattern end |
#template ⇒ Object
Returns the value of attribute template.
7 8 9 |
# File 'lib/aspen/custom_grammar/matcher.rb', line 7 def template @template end |
#typereg ⇒ Object
Returns the value of attribute typereg.
7 8 9 |
# File 'lib/aspen/custom_grammar/matcher.rb', line 7 def typereg @typereg end |
Instance Method Details
#captures(str) ⇒ Object Also known as: results
Compare against narrative line to get captures Example results: { a: , amt: , b: }
25 26 27 |
# File 'lib/aspen/custom_grammar/matcher.rb', line 25 def captures(str) pattern.match(str).named_captures end |
#captures!(str) ⇒ Object Also known as: results!
30 31 32 33 34 35 36 |
# File 'lib/aspen/custom_grammar/matcher.rb', line 30 def captures!(str) unless match?(str) raise Aspen::MatchError, Aspen::Errors.(:no_grammar_match, pattern, str) end captures(str) end |
#match?(str) ⇒ Boolean
19 20 21 |
# File 'lib/aspen/custom_grammar/matcher.rb', line 19 def match?(str) pattern.match?(str) end |