Class: Neg::Parser::LookaheadParser

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

Instance Method Summary collapse

Methods inherited from SubParser

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

Constructor Details

#initialize(child, presence) ⇒ LookaheadParser



347
348
349
350
351
# File 'lib/neg/parser.rb', line 347

def initialize(child, presence)

  @child = child
  @presence = presence
end

Instance Method Details

#do_parse(i, opts) ⇒ Object



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/neg/parser.rb', line 353

def do_parse(i, opts)

  start = i.position

  r = @child.parse(i, opts)
  i.rewind(start)

  success = r[2]
  success = ! success if ! @presence

  result = if success
    '' # for NonTerminal#reduce not to continue
  else
    [
      @child.to_s(nil), 'is not', @presence ? 'present' : 'absent'
    ].join(' ')
  end

  [ success, result, [ r ] ]
end

#to_s(parent = nil) ⇒ Object



374
375
376
377
# File 'lib/neg/parser.rb', line 374

def to_s(parent=nil)

  "#{@presence ? '~' : '-'}#{@child.to_s(self)}"
end