Class: Aspen::CustomGrammar::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/aspen/custom_grammar/matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#expressionObject

Returns the value of attribute expression.



7
8
9
# File 'lib/aspen/custom_grammar/matcher.rb', line 7

def expression
  @expression
end

#labelregObject

Returns the value of attribute labelreg.



7
8
9
# File 'lib/aspen/custom_grammar/matcher.rb', line 7

def labelreg
  @labelreg
end

#patternObject

Returns the value of attribute pattern.



7
8
9
# File 'lib/aspen/custom_grammar/matcher.rb', line 7

def pattern
  @pattern
end

#templateObject

Returns the value of attribute template.



7
8
9
# File 'lib/aspen/custom_grammar/matcher.rb', line 7

def template
  @template
end

#typeregObject

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.messages(:no_grammar_match, pattern, str)
  end
  captures(str)
end

#match?(str) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/aspen/custom_grammar/matcher.rb', line 19

def match?(str)
  pattern.match?(str)
end