Class: Symbol

Inherits:
Object
  • Object
show all
Includes:
Walrus::Grammar::ParsletCombining
Defined in:
lib/walrus/grammar/additions/symbol.rb

Instance Method Summary collapse

Methods included from Walrus::Grammar::ParsletCombining

#&, #>>, #and?, #and_predicate, #choice, #memoizing_parse, #merge, #not!, #not_predicate, #omission, #one_or_more, #optional, #parse, #repeat, #repeat_with_default, #repetition, #repetition_with_default, #sequence, #skip, #zero_or_more, #zero_or_one, #|

Instance Method Details

#build(superclass, *params) ⇒ Object

Dynamically creates a subclass named after the receiver, with parent class superclass, taking params.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/walrus/grammar/additions/symbol.rb', line 31

def build(superclass, *params)    
  
  # first use the continuation trick to find out what grammar (namespace) receiver is being messaged in
  # Ruby 1.9/2.0 will not support continuations, so may need to come up with an alternative
  continuation  = nil
  value         = callcc { |c| continuation = c }         
  if value == continuation                                # first time that we're here
    raise Walrus::Grammar::ContinuationWrapperException.new(continuation)  # give higher up a chance to help us
  else # value is the Grammar instance passed to us from higher up using call on the continuation
    grammar = value
  end
  
  # actually create the subclass
  grammar.const_get(superclass.to_s.to_class_name.to_s).subclass(self.to_s.to_class_name.to_s, grammar, *params)
  self
  
end

#to_parseableObject

Returns a SymbolParslet based on the receiver. Symbols can be used in Grammars when specifying rules and productions to refer to other rules and productions that have not been defined yet. They can also be used to allow self-references within rules and productions (recursion); for example:

rule :thing & :thing.optional & :other_thing

Basically these SymbolParslets allow deferred evaluation of a rule or production (deferred until parsing takes place) rather than being evaluated at the time a rule or production is defined.



26
27
28
# File 'lib/walrus/grammar/additions/symbol.rb', line 26

def to_parseable
  Walrus::Grammar::SymbolParslet.new(self)
end