class Aspen::CustomGrammar::Grammar

Attributes

registry[R]

API Surface:

#add?
#match? (Boolean) -> Does the grammar cover this string, match this case?
#match  (Matcher) -> Request the matcher for this string.
#compile_pattern (Regexp) -> Given a matcher expression, return the Regexp pattern that can match against a string.

Public Class Methods

new() click to toggle source
# File lib/aspen/custom_grammar/grammar.rb, line 15
def initialize()
  @registry = []
  @slug_counters = Hash.new { 1 }
end

Public Instance Methods

add(maybe_matchers) click to toggle source
# File lib/aspen/custom_grammar/grammar.rb, line 44
def add(maybe_matchers)
  matchers = Array(maybe_matchers).flatten
  raise unless matchers.all? { |m| m.is_a? Aspen::CustomGrammar::Matcher }
  matchers.each { |matcher| @registry << matcher }
end
count() click to toggle source
# File lib/aspen/custom_grammar/grammar.rb, line 40
def count
  registry.count
end
inspect() click to toggle source
# File lib/aspen/custom_grammar/grammar.rb, line 36
def inspect
  "#<Aspen::Grammar matchers: #{count}>"
end
match(text) click to toggle source
# File lib/aspen/custom_grammar/grammar.rb, line 27
def match(text)
  results = @registry.select { |m| m.match?(text) }
  warn "Found #{results.count} matches" if results.count > 1
  # raise Aspen::Error, "No results." if results.empty?
  return results.first
end
Also aliased as: matcher_for
match!(text) click to toggle source
# File lib/aspen/custom_grammar/grammar.rb, line 61
      def match!(text)
        unless match(text)
          raise Aspen::MatchError, <<~ERROR
            Couldn't find an Aspen grammar that matched the line:

                #{text}

            For more details (if you can), try running this to see all the match patterns:

                Aspen::Grammar.registry.map(&:pattern)

          ERROR
        end
      end
match?(string) click to toggle source

Does the given text match a matcher?

# File lib/aspen/custom_grammar/grammar.rb, line 21
def match?(string)
  !!match(string)
rescue Aspen::Error
  false
end
matcher_for(text)
Alias for: match
render(content) click to toggle source

This doesn’t quite work, because var results is untyped.

# File lib/aspen/custom_grammar/grammar.rb, line 51
def render(content)
  matcher  = matcher_for(content)
  results  = results_for(content)
  template = matcher.template
end
results_for(text) click to toggle source
# File lib/aspen/custom_grammar/grammar.rb, line 57
def results_for(text)
  matcher_for(text).captures!(text)
end