Class: Packrat::InterpretingParser

Inherits:
Object
  • Object
show all
Defined in:
lib/packrat/grammar.rb

Direct Known Subclasses

ErrorLoggingInterpretingParser

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ InterpretingParser

Returns a new instance of InterpretingParser.



486
487
488
489
490
# File 'lib/packrat/grammar.rb', line 486

def initialize(string)
  @str = string
  @s = StringScanner.new(string)
  @grammar = self.class.grammar
end

Class Attribute Details

.grammarObject

Returns the value of attribute grammar.



471
472
473
# File 'lib/packrat/grammar.rb', line 471

def grammar
  @grammar
end

Instance Attribute Details

#grammarObject (readonly)

Returns the value of attribute grammar.



484
485
486
# File 'lib/packrat/grammar.rb', line 484

def grammar
  @grammar
end

#resultsObject (readonly)

Returns the value of attribute results.



484
485
486
# File 'lib/packrat/grammar.rb', line 484

def results
  @results
end

Class Method Details

.new_subclass(grammar) ⇒ Object



472
473
474
475
476
# File 'lib/packrat/grammar.rb', line 472

def new_subclass(grammar)
  klass = Class.new(self)
  klass.grammar = grammar
  klass
end

.parse_string(str, startSymbol = nil) ⇒ Object



477
478
479
480
481
# File 'lib/packrat/grammar.rb', line 477

def parse_string(str, startSymbol = nil)
  # We always add a whitespace since StringScanner cannot match /\s*/
  # (typically used as whitespace) at EOS
  new(str + " ").parse_string(startSymbol)
end

Instance Method Details

#eos?Boolean

Returns:

  • (Boolean)


501
# File 'lib/packrat/grammar.rb', line 501

def eos?; @s.eos?; end

#lexeme(pos, len) ⇒ Object

Extract a lexeme of length <len> from the given <pos>.



504
505
506
# File 'lib/packrat/grammar.rb', line 504

def lexeme(pos, len)
  @str[pos, len]
end

#parse_string(startSymbol = nil) ⇒ Object



492
493
494
495
# File 'lib/packrat/grammar.rb', line 492

def parse_string(startSymbol = nil)
  startSymbol ||= @grammar.start
  @grammar[startSymbol].parse(self)
end

#posObject

Get and Set current position in string.



498
# File 'lib/packrat/grammar.rb', line 498

def pos; @s.pos; end

#pos=(p) ⇒ Object



499
# File 'lib/packrat/grammar.rb', line 499

def pos=(p); @s.pos = p; end

#skip(re) ⇒ Object

Skip using <re> at the current position in the string. Returns nil if the re did not match or the length of the match if it matched.



510
511
512
# File 'lib/packrat/grammar.rb', line 510

def skip(re)
  @s.skip(re)
end