| Class | OOParser::Production |
| In: |
lib/ooparser/production.rb
(CVS)
|
| Parent: | Object |
A production in an OOParser grammar.
| SVNRev | = | %q$Rev: 4 $ | SVN Revision | |
| SVNId | = | %q$Id: production.rb 4 2005-09-08 07:36:41Z ged $ | SVN Id |
| action | [R] | The action to take if the production’s pattern matches |
Create a new production object.
# File lib/ooparser/production.rb, line 46 def initialize( pattern, &action ) # Parse the pattern into items unless it's an item or Regexp, which # don't need to be transformed (much) if pattern.is_a?( OOParser::Item ) @items = [ pattern ] elsif pattern.is_a?( Regexp ) @items = [ OOParser::Item::create('terminal', pattern) ] else @items = self.parse_pattern( pattern ) end @action = action end
Attempt to match the target in the context of the given state, returning the result of the production’s action if there is one, or the match object that would have been given to the action if there isn’t.
# File lib/ooparser/production.rb, line 73 def match( parseState ) # Add a production to the parse state, and then match each item in the # production. Any failing item causes the whole production to fail. parseState.production_match( self ) do |ppstate| @items.each do |item| ppstate.do_skip throw :fail unless item.match( ppstate ) end # If there's an associated action, run it and return its return # value if self.action return self.action.call( ppstate.varset ) else return ppstate.varset end end end
Parse the production’s pattern into an Array of OOParser::Items. Each item specifies a parse behavior of the production.
# File lib/ooparser/production.rb, line 101 def parse_pattern( pattern ) scnr = StringScanner::new( pattern.to_s ) scnr.skip( /\s+/ ) items = [] # Scan to the end of the string, creating OOParser::Item objects for # each (space-delimited) item until scnr.eos? # Subrule if scnr.scan( /<(\w+)>/ ) items << OOParser::Item::create( 'subrule', scnr[1] ) # Literal else scnr.scan( /\S+/ ) items << OOParser::Item::create( 'terminal', scnr.matched ) end scnr.skip( /\s+/ ) end return items end