Class OOParser::Rule
In: lib/ooparser/rule.rb  (CVS)
Parent: Object

OOParser::Rules encapsulates the name and production/s for a given rule of its containing parser’s grammar. It can express itself as code to be installed as a method of the parser.

Methods

add_production   as_proc   new  

Attributes

name  [R]  The name of the rule as a Symbol
productions  [R]  The array of productions that make up the rule, in order of precedence.

Public Class methods

Create a new OOParser::Rule object with the given name and productions.

[Source]

# File lib/ooparser/rule.rb, line 32
    def initialize( name, productions=[] )
        @name = name.to_sym
        @productions = productions
    end

Public Instance methods

Add a production to the Rule for the given pattern and (optional) action.

[Source]

# File lib/ooparser/rule.rb, line 52
    def add_production( pattern, &action )
        @productions.push( OOParser::Production::new(pattern, &action) )
    end

Return a Proc containing logic that will parse this rule in the context of a containing parser. The proc should accept one argument, which will be a OOParser::ParseState method.

[Source]

# File lib/ooparser/rule.rb, line 60
    def as_proc( parser )

        # If there are no productions, the rule acts as a terminal that matches
        # its name.
        productions = [OOParser::Production::new( self.name )] if
            @productions.empty?
        productions ||= @productions

        parser.log.debug "Productions: %p" % [productions]

        lambda {|target|
            unless target.is_a?( OOParser::ParseState )
                state = OOParser::ParseState::new( parser, self, target )
            else
                state = target
            end

            # Add a rule to the parse state and then try all the productions in
            # this rule until one succeeds
            state.rule_match( self ) do |pstate|
                rval = nil

                productions.each do |production|
                    rval = production.match( pstate )
                    break if rval
                end

                rval
            end
        }
    end

[Validate]