class Fear::Extractor::Pattern

Parse pattern. Used within Fear[]

Constants

DEFAULT_PATTERN_CACHE_SIZE

Attributes

pattern_cache[R]
matcher[R]

Public Class Methods

new(pattern) click to toggle source
# File lib/fear/extractor/pattern.rb, line 17
def initialize(pattern)
  @matcher = compile_pattern(pattern)
end

Public Instance Methods

===(other) click to toggle source
# File lib/fear/extractor/pattern.rb, line 38
def ===(other)
  matcher.defined_at?(other)
end
and_then(other) click to toggle source
# File lib/fear/extractor/pattern.rb, line 42
def and_then(other)
  Fear::PartialFunction::Combined.new(matcher, other)
end
failure_reason(other) click to toggle source
# File lib/fear/extractor/pattern.rb, line 46
def failure_reason(other)
  matcher.failure_reason(other).get_or_else { "It matches" }
end

Private Instance Methods

compile_pattern(pattern) click to toggle source
# File lib/fear/extractor/pattern.rb, line 23
        def compile_pattern(pattern)
  self.class.pattern_cache.getset(pattern) do
    compile_pattern_without_cache(pattern)
  end
end
compile_pattern_without_cache(pattern) click to toggle source
# File lib/fear/extractor/pattern.rb, line 29
        def compile_pattern_without_cache(pattern)
  parser = Extractor::GrammarParser.new
  if (result = parser.parse(pattern))
    result.to_matcher
  else
    raise PatternSyntaxError, syntax_error_message(parser, pattern)
  end
end
syntax_error_message(parser, pattern) click to toggle source
# File lib/fear/extractor/pattern.rb, line 50
        def syntax_error_message(parser, pattern)
  parser.failure_reason =~ /^(Expected .+) after/m
  "#{Regexp.last_match(1).gsub("\n", "$NEWLINE")}:\n" +
  pattern.split("\n")[parser.failure_line - 1] + "\n" \
  "#{"~" * (parser.failure_column - 1)}^\n"
end