Class: MetaParse::Matcher

Inherits:
Object show all
Defined in:
lib/meta_parse.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Matcher

Returns a new instance of Matcher.



133
134
135
# File 'lib/meta_parse.rb', line 133

def initialize(data)
  @spec = data
end

Instance Attribute Details

#specObject

Returns the value of attribute spec.



101
102
103
# File 'lib/meta_parse.rb', line 101

def spec
  @spec
end

Class Method Details

.compile(spec) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/meta_parse.rb', line 103

def self.compile(spec)
  case spec
  when Matcher
    spec
  when String, Regexp
    Matcher.new spec
  when Array
    case spec[0]
    when :or
        compiled_body = spec[1..-1].map { |x| self.compile x }
      AlternativeMatcher.new(compiled_body)
    when :and
        compiled_body = spec[1..-1].map { |x| self.compile x }
      SequentialMatcher.new(compiled_body)
    when :*, :+, :'?'
      compiled_body = self.compile(spec[1])
      case spec[0]
      when :'?'
        min = 0
        max = 1
      when :+
        min = 1
      end
      RepetitionMatcher.new(compiled_body, min, max, *spec[4..-1])
    end
  when Proc, Symbol
    FunctionMatcher.new spec
  end
end

Instance Method Details

#inspectObject



141
142
143
# File 'lib/meta_parse.rb', line 141

def inspect
  "<match #{show}>"
end

#m(string) ⇒ Object



171
172
173
# File 'lib/meta_parse.rb', line 171

def m(string)
  match MetaScanner.new(string)
end

#m?(string) ⇒ Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/meta_parse.rb', line 167

def m?(string)
  match? MetaScanner.new(string)
end

#match(scanner, context = nil) ⇒ Object



159
160
161
# File 'lib/meta_parse.rb', line 159

def match(scanner, context=nil)
  (stateful ? clone : self).match? scanner, context
end

#match?(scanner, context = nil) ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/meta_parse.rb', line 145

def match?(scanner, context=nil)
  case scanner
  when MetaScanner
    result = scanner.scan spec
    result
  else
    raise "match? requires scanner"
    # FIXME: Why doesn't the coercion below work?
  # when String
  #   match?(MetaParse::MetaScanner.new(scanner), context)
  #   (MetaScanner.new(scanner)).scan spec
  end
end

#showObject



137
138
139
# File 'lib/meta_parse.rb', line 137

def show
    spec.inspect
end

#statefulObject



163
164
165
# File 'lib/meta_parse.rb', line 163

def stateful
  false
end