Module: MetaParse::ClassMethods

Defined in:
lib/meta_parse.rb

Instance Method Summary collapse

Instance Method Details

#alt(*args) ⇒ Object



50
51
52
# File 'lib/meta_parse.rb', line 50

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

#comp(spec) ⇒ Object



54
55
56
# File 'lib/meta_parse.rb', line 54

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

#define_matcher_method(name, matcher) ⇒ Object



26
27
28
29
30
# File 'lib/meta_parse.rb', line 26

def define_matcher_method(name, matcher)
  self.send(:define_method, name) do |scanner, context=nil|
    matcher.match scanner, context
  end
end

#match_method(name, &block) ⇒ Object

Defined a method which takes a scanner. 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.



19
20
21
22
23
24
# File 'lib/meta_parse.rb', line 19

def match_method(name, &block)
  match_spec = yield
  matcher = MetaParse::Matcher.compile(match_spec)
  
  define_matcher_method(name, matcher)
end

#rep(*args, &block) ⇒ Object



32
33
34
# File 'lib/meta_parse.rb', line 32

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

#seq(*args, &block) ⇒ Object

Return a sequential matcher. 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.



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/meta_parse.rb', line 38

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