Class: RLTK::Lexer

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

Overview

The Lexer class may be sub-classed to produce new lexers. These lexers have a lot of features, and are described in the main documentation.

Direct Known Subclasses

RLTK::Lexers::Calculator, RLTK::Lexers::EBNF

Defined Under Namespace

Classes: Environment, LexerCore, Rule

Class Method Summary (collapse)

Class Method Details

+ (Object) inherited(klass)

Called when the Lexer class is sub-classed, this method adds a LexerCore to the new class, and installs some needed class and instance methods.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rltk/lexer.rb', line 44

def Lexer.inherited(klass)
	klass.class_exec do
		@core = LexerCore.new
		
		# Returns this class's LexerCore object.
		def self.core
			@core
		end
		
		# Lexes the given string using a newly instantiated
		# environment.
		def self.lex(str)
			@core.lex(str, self::Environment.new(@core.start_state))
		end
		
		# Lexes the contents of the given file using a newly
		# instantiated environment.
		def self.lex_file(file_name)
			@core.lex_file(file_name, self::Environment.new(@core.start_state))
		end
		
		# Routes method calls to the new subclass to the LexerCore
		# object.
		def self.method_missing(method, *args, &proc)
			@core.send(method, *args, &proc)
		end
		
		# Instantiates a new lexer and creates an environment to be
		# used for subsequent calls.
		def initialize
			@env = self.class::Environment.new(self.class.core.start_state)
		end
		
		# Returns the environment used by an instantiated lexer.
		def env
			@env
		end
		
		# Lexes a string using the encapsulated environment.
		def lex(string)
			self.class.core.lex(string, @env)
		end
		
		# Lexes a file using the encapsulated environment.
		def lex_file(file_name)
			self.class.core.lex_file(file_name, @env)
		end
	end
end