Class: Neg::Parser::NonTerminalParser

Inherits:
SubParser
  • Object
show all
Defined in:
lib/neg/parser.rb

Instance Method Summary collapse

Methods inherited from SubParser

#*, #+, #-@, #[], #|, #~

Constructor Details

#initialize(name, child = nil) ⇒ NonTerminalParser

Returns a new instance of NonTerminalParser.



145
146
147
148
149
# File 'lib/neg/parser.rb', line 145

def initialize(name, child=nil)

  @name = name
  @child = child
end

Instance Method Details

#==(pa) ⇒ Object



151
152
153
154
# File 'lib/neg/parser.rb', line 151

def ==(pa)

  @child = pa
end

#do_parse(i, opts) ⇒ Object

Raises:



169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/neg/parser.rb', line 169

def do_parse(i, opts)

  raise ParserError.new("\"#{@name}\" is missing") if @child.nil?

  r = @child.do_parse(i, opts)

  return r if r[0] == false
  return r if r[1].is_a?(String)

  report = refine(r[2])
  report = report.include?(false) ? nil : report.join

  [ true, report, r[2] ]
end

#parse(input_or_string, opts) ⇒ Object



184
185
186
187
188
189
190
# File 'lib/neg/parser.rb', line 184

def parse(input_or_string, opts)

  r = super
  r[0] = @name

  r
end

#refine(children_results) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/neg/parser.rb', line 156

def refine(children_results)

  children_results.collect { |cr|
    if cr[0] && cr[0].to_s.match(/^_/).nil?
      false
    elsif cr[2]
      cr[3] ? cr[3] : refine(cr[4])
    else
      nil
    end
  }.flatten.compact
end

#to_s(parent = nil) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/neg/parser.rb', line 192

def to_s(parent=nil)

  child = @child ? @child.to_s(self) : '<missing>'

  if @name.is_a?(String)
    "#{child}[#{@name.inspect}]"
  elsif parent
    @name.to_s
  else #if @name.is_a?(Symbol)
    "#{@name} == #{child}"
  end
end