Class: RLTK::Lexer::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/rltk/lexer.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_state, match = nil) ⇒ Environment

Instantiates a new Environment object.

Parameters:

  • start_state (Symbol)

    Lexer’s start state.

  • match (Match) (defaults to: nil)

    Match object for matching text.



261
262
263
264
265
# File 'lib/rltk/lexer.rb', line 261

def initialize(start_state, match = nil)
	@state	= [start_state]
	@match	= match
	@flags	= Array.new
end

Instance Attribute Details

#flagsArray<Symbol> (readonly)

Returns Flags currently set in this environment.

Returns:

  • (Array<Symbol>)

    Flags currently set in this environment.



252
253
254
# File 'lib/rltk/lexer.rb', line 252

def flags
  @flags
end

#matchMatch

Returns Match object generated by a rule’s regular expression.

Returns:

  • (Match)

    Match object generated by a rule’s regular expression.



255
256
257
# File 'lib/rltk/lexer.rb', line 255

def match
  @match
end

Instance Method Details

#clear_flagsvoid

This method returns an undefined value.

Unsets all flags in the current environment.



340
341
342
343
344
# File 'lib/rltk/lexer.rb', line 340

def clear_flags
	@flags = Array.new
	
	nil
end

#pop_statevoid

This method returns an undefined value.

Pops a state from the state stack.



282
283
284
285
286
# File 'lib/rltk/lexer.rb', line 282

def pop_state
	@state.pop
	
	nil
end

#push_state(state) ⇒ void

This method returns an undefined value.

Pushes a new state onto the state stack.



291
292
293
294
295
# File 'lib/rltk/lexer.rb', line 291

def push_state(state)
	@state << state
	
	nil
end

#rule_exec(match, txt, &block) ⇒ Object

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

Parameters:

  • match (Match)

    Match object for matching text.

  • txt (String)

    Text of matching string.

  • block (Proc)

    Block for matched rule.



273
274
275
276
277
# 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) ⇒ void

This method returns an undefined value.

Sets a flag in the current environment.

Parameters:

  • flag (Symbol)

    Flag to set as enabled.



318
319
320
321
322
323
324
# File 'lib/rltk/lexer.rb', line 318

def set_flag(flag)
	if not @flags.include?(flag)
		@flags << flag
	end
	
	nil
end

#set_state(state) ⇒ void

This method returns an undefined value.

Sets the value on the top of the state stack.

Parameters:

  • state (Symbol)

    New state for the lexing environment.



302
303
304
305
306
# File 'lib/rltk/lexer.rb', line 302

def set_state(state)
	@state[-1] = state
	
	nil
end

#stateSymbol

Returns Current state of the lexing environment.

Returns:

  • (Symbol)

    Current state of the lexing environment.



309
310
311
# File 'lib/rltk/lexer.rb', line 309

def state
	@state.last
end

#unset_flag(flag) ⇒ void

This method returns an undefined value.

Unsets a flag in the current environment.

Parameters:

  • flag (Symbol)

    Flag to unset.



331
332
333
334
335
# File 'lib/rltk/lexer.rb', line 331

def unset_flag(flag)
	@flags.delete(flag)
	
	nil
end