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.



136
137
138
139
140
# File 'lib/neg/parser.rb', line 136

def initialize(name, child=nil)

  @name = name
  @child = child
end

Instance Method Details

#==(pa) ⇒ Object



142
143
144
145
# File 'lib/neg/parser.rb', line 142

def ==(pa)

  @child = pa
end

#do_parse(i, opts) ⇒ Object

Raises:



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/neg/parser.rb', line 160

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 = reduce(r[2])

  return r if report.include?(false)

  [ true, report.join, opts[:noreduce] ? r[2] : [] ]
end

#parse(input_or_string, opts) ⇒ Object



176
177
178
179
180
181
182
# File 'lib/neg/parser.rb', line 176

def parse(input_or_string, opts)

  r = super
  r[0] = @name

  r
end

#reduce(children_results) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/neg/parser.rb', line 147

def reduce(children_results)

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

#to_s(parent = nil) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/neg/parser.rb', line 184

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