Module: MetaParse::ClassMethods

Defined in:
lib/meta_parse.rb

Instance Method Summary collapse

Instance Method Details

#alt(*args) ⇒ Object

Return an AlternativeMatcher.



64
65
66
# File 'lib/meta_parse.rb', line 64

def alt(*args)
  Matcher.compile([:or, *args])
end

#comp(spec) ⇒ Object

Compile a Matcher spec into corresponding matcher.



71
72
73
# File 'lib/meta_parse.rb', line 71

def comp(spec)
  Matcher.compile(spec)
end

#match_method(name, &block) ⇒ Object

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.



29
30
31
32
33
34
# 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) ⇒ Object

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



39
40
41
# File 'lib/meta_parse.rb', line 39

def rep(*args, &block)
  Matcher.compile([:*, *args])
end

#seq(*args, &block) ⇒ Object

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.



49
50
51
52
53
54
55
56
57
58
59
# 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