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

Variable set for matches against productions. The return value for each item in the production (if it matches) is placed in the varset with an index and one or more optional names. The varset is the argument to a matching production’s action after it matches completely, or the return value from a production with no action.

The index operator can be used to fetch items either by numerical index or by a symbolic name derived from the appearance of the item in the associated production. For example, given the following productions:

  "[ <sectionname> ] : <sectionvalue>+"

the following keys would be valid (assuming a match had been made):

1:’[‘
2:(whatever the ‘sectionname’ rule returned)
3:’]’
4:’:’
5:(the return of the first match of the ‘sectionvalue’ rule)
6:(the return of the second match of the ‘sectionvalue’ rule)
7:etc., etc.
sectionname:(whatever the ‘sectionname’ rule returned)
sectionvalues:(all of the returns from the ‘sectionvalue’ rule matches)
sectionvalue_1:(the return of the first match of the ‘sectionvalue’ rule)
first_sectionvalue:(the return of the first match of the ‘sectionvalue’ rule)
sectionvalue_2:(the return of the second match of the ‘sectionvalue’ rule)
second_sectionvalue:(the return of the second match of the ‘sectionvalue’ rule)

Methods

[]   add   new  

Attributes

indexed  [R]  The match values in raw match order.
named  [R]  The match value by name, keyed with Symbols

Public Class methods

Create a new varset

[Source]

# File lib/ooparser/parsestate.rb, line 81
        def initialize
            @indexed = [nil]
            @named = {}
        end

Public Instance methods

Element reference — returns the element at index, which can be either a Fixnum or a name as either a Symbol or a String.

[Source]

# File lib/ooparser/parsestate.rb, line 100
        def []( index )
            case index
            when 0
                return @indexed[ 1 .. @indexed.length - 1 ]

            when Numeric
                return @indexed[ Integer(index) ]

            when String, Symbol
                return @named[ index.to_sym ]

            else
                raise TypeError, "%s as %s index" %
                    [index.class.name, self.class.name]
            end
        end

Add a new match result for the given item to the receiver.

[Source]

# File lib/ooparser/parsestate.rb, line 119
        def add( result, item )
            @indexed << result

            # Add the result to each name variant 
            # :TODO: Do the complex ones later
            for name in item.varnames
                @named[ name.to_sym ] = result
            end
        end

[Validate]