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

OOParser - an object-oriented parser class

Methods

grammar   inspect   install_grammar   log   metaclass   new  

Classes and Modules

Class OOParser::Grammar
Class OOParser::Item
Class OOParser::ParseState
Class OOParser::Production
Class OOParser::Rule

Constants

SVNRev = %q$Rev: 5 $   SVN Revision
SVNId = %q$Id: ooparser.rb 5 2005-09-08 07:41:12Z ged $   SVN Id

Attributes

log  [R]  The Logger object for this particular parser instance

Public Class methods

Define a grammar for a new OOParser

[Source]

# File lib/ooparser.rb, line 112
    def self::grammar( &block )
        if block
            self.log.debug "Defining grammar with %p" % block
            @grammar = OOParser::Grammar::new( &block )
        end

        return @grammar
    end

Return the logging handle for the parser class, creating a new one if necessary.

[Source]

# File lib/ooparser.rb, line 101
    def self::log
        if !defined?( @log ) || @log.nil?
            @log = Logger::new( $deferr )
            @log.level = Logger::WARN
        end

        return @log
    end

Create a new ooparser object.

[Source]

# File lib/ooparser.rb, line 128
    def initialize
        @log = self.class.log.clone
        @rules = {}

        self.install_grammar
    end

Public Instance methods

Return a human-readable programmatic representation of the object.

[Source]

# File lib/ooparser.rb, line 145
    def inspect
        return "#<%s:0x%0x @rules=%p>" % [
            self.class.name,
            self.object_id,
            @rules,
        ]
    end

Protected Instance methods

Install an OOParser::Grammar in this class

[Source]

# File lib/ooparser.rb, line 165
    def install_grammar
        grammar = self.class.grammar or raise OOParser::Error,
            "Cannot instantiate %s: no grammar defined" % self.class.name
        if grammar.rules.empty?
            raise OOParser::Error,
                "Cannot instantiate %s: grammar has no rules"
        end

        # Install a singleton method on the parser object for each rule of the
        # grammar
        grammar.rules.each do |name,rule|
            block = rule.as_proc( self )
            @rules[ name.to_sym ] = block

            self.log.debug "Installing rule %p: %s" % [ rule, block ]
            self.metaclass.send( :define_method, name.to_sym, block )
        end

        # Alias the #parse method to the grammar's default rule
        self.metaclass.send( :alias_method, :parse, grammar.default_rule )
    end

Return the instance’s metaclass

[Source]

# File lib/ooparser.rb, line 159
    def metaclass
        (class << self; self; end)
    end

[Validate]