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.



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

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.



242
243
244
# File 'lib/rltk/lexer.rb', line 242

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.



245
246
247
# File 'lib/rltk/lexer.rb', line 245

def match
  @match
end

Instance Method Details

#clear_flagsvoid

This method returns an undefined value.

Unsets all flags in the current environment.



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

def clear_flags
	@flags = Array.new
	
	nil
end

#pop_statevoid

This method returns an undefined value.

Pops a state from the state stack.



272
273
274
275
276
# File 'lib/rltk/lexer.rb', line 272

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.



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

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.



263
264
265
266
267
# File 'lib/rltk/lexer.rb', line 263

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.



308
309
310
311
312
313
314
# File 'lib/rltk/lexer.rb', line 308

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.



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

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.



299
300
301
# File 'lib/rltk/lexer.rb', line 299

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.



321
322
323
324
325
# File 'lib/rltk/lexer.rb', line 321

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