class RLTK::Lexer::Environment

All actions passed to LexerCore.rule are evaluated inside an instance of the Environment class or its subclass (which must have the same name). This class provides functions for manipulating lexer state and flags.

Attributes

flags[R]

@return [Array<Symbol>] Flags currently set in this environment.

match[RW]

@return [Match] Match object generated by a rule’s regular expression.

Public Class Methods

new(start_state, match = nil) click to toggle source

Instantiates a new Environment object.

@param [Symbol] start_state Lexer’s start state. @param [Match] match Match object for matching text.

# File lib/rltk/lexer.rb, line 261
def initialize(start_state, match = nil)
        @state      = [start_state]
        @match      = match
        @flags      = Array.new
end

Public Instance Methods

clear_flags() click to toggle source

Unsets all flags in the current environment.

@return [void]

# File lib/rltk/lexer.rb, line 340
def clear_flags
        @flags = Array.new

        nil
end
pop_state() click to toggle source

Pops a state from the state stack.

@return [void]

# File lib/rltk/lexer.rb, line 282
def pop_state
        @state.pop

        nil
end
push_state(state) click to toggle source

Pushes a new state onto the state stack.

@return [void]

# File lib/rltk/lexer.rb, line 291
def push_state(state)
        @state << state

        nil
end
rule_exec(match, txt, &block) click to toggle source

This function will instance_exec a block for a rule after setting the match value.

@param [Match] match Match object for matching text. @param [String] txt Text of matching string. @param [Proc] block Block for matched rule.

# File lib/rltk/lexer.rb, line 273
def rule_exec(match, txt, &block)
        self.match = match

        self.instance_exec(txt, &block)
end
set_flag(flag) click to toggle source

Sets a flag in the current environment.

@param [Symbol] flag Flag to set as enabled.

@return [void]

# File lib/rltk/lexer.rb, line 318
def set_flag(flag)
        if not @flags.include?(flag)
                @flags << flag
        end

        nil
end
set_state(state) click to toggle source

Sets the value on the top of the state stack.

@param [Symbol] state New state for the lexing environment.

@return [void]

# File lib/rltk/lexer.rb, line 302
def set_state(state)
        @state[-1] = state

        nil
end
state() click to toggle source

@return [Symbol] Current state of the lexing environment.

# File lib/rltk/lexer.rb, line 309
def state
        @state.last
end
unset_flag(flag) click to toggle source

Unsets a flag in the current environment.

@param [Symbol] flag Flag to unset.

@return [void]

# File lib/rltk/lexer.rb, line 331
def unset_flag(flag)
        @flags.delete(flag)

        nil
end