module MetaParse::ClassMethods

Public Instance Methods

alt(*args) click to toggle source

Return an AlternativeMatcher.

# File lib/meta_parse.rb, line 64
def alt(*args)
  Matcher.compile([:or, *args])
end
comp(spec) click to toggle source

Compile a Matcher spec into corresponding matcher.

# File lib/meta_parse.rb, line 71
def comp(spec)
  Matcher.compile(spec)
end
match_method(name) { || ... } click to toggle source

Defines a method which takes a scanner from provided block. The block passed should return a Matcher or Matcher spec, which is compiled to a Matcher if necessary. The result of calling the defined method is the same as calling match? on the resulting Matcher.

# File lib/meta_parse.rb, line 29
def match_method(name, &block)
  match_spec = yield
  matcher = MetaParse::Matcher.compile(match_spec)
  
  define_matcher_method(name, matcher)
end
rep(*args, &block) click to toggle source

Returns a RepetitionMatcher which matches the pattern specified by its arguments zero-or-more-times.

# File lib/meta_parse.rb, line 39
def rep(*args, &block)
  Matcher.compile([:*, *args])
end
seq(*args, &block) click to toggle source

Return a SequentialMatcher. If block is supplied, it defines a function which will be passed an array of all matched values and which should return a non-nil result for the match as a whole. Sequential matching with terminal block is the mechanism by which arbitrary values can be produced at any step in parsing.

# File lib/meta_parse.rb, line 49
def seq(*args, &block)
  if block_given?
    wrapped = lambda { |scanner, context|
      result = block.call context.matches
      context.matches = []
      result
    }
    args << wrapped
  end
  Matcher.compile([:and, *args])
end

Private Instance Methods

define_matcher_method(name, matcher) click to toggle source

Define a match method using supplied Matcher. Used by ::match_method, which should be used instead.

# File lib/meta_parse.rb, line 81
def define_matcher_method(name, matcher)
  self.send(:define_method, name) do |scanner_spec, context=nil|
    scanner = scanner_spec.meta
    scanner.parser ||= self
    matcher.match scanner, context
  end
end