class Aspen::CustomGrammar::Matcher

Attributes

expression[RW]
labelreg[RW]
pattern[RW]
template[RW]
typereg[RW]

Public Class Methods

new(expression: , template: , pattern: ) click to toggle source
# 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

Public Instance Methods

captures(str) click to toggle source

Compare against narrative line to get captures Example results: { a: , amt: , b: }

# File lib/aspen/custom_grammar/matcher.rb, line 25
def captures(str)
  pattern.match(str).named_captures
end
Also aliased as: results
captures!(str) click to toggle source
# File lib/aspen/custom_grammar/matcher.rb, line 30
def captures!(str)
  unless match?(str)
    raise Aspen::MatchError,
      Aspen::Errors.messages(:no_grammar_match, pattern, str)
  end
  captures(str)
end
Also aliased as: results!
match?(str) click to toggle source
# File lib/aspen/custom_grammar/matcher.rb, line 19
def match?(str)
  pattern.match?(str)
end
results(str)
Alias for: captures
results!(str)
Alias for: captures!