Class: Syntax::Tokenizer
- Inherits:
-
Object
- Object
- Syntax::Tokenizer
- Defined in:
- lib/syntax/common.rb
Overview
The base class of all tokenizers. It sets up the scanner and manages the looping until all tokens have been extracted. It also provides convenience methods to make sure adjacent tokens of identical groups are returned as a single token.
Class Method Summary collapse
-
.delegate(sym) ⇒ Object
A convenience for delegating method calls to the scanner.
Instance Method Summary collapse
-
#finish ⇒ Object
Finish tokenizing.
-
#setup ⇒ Object
Subclasses may override this method to provide implementation-specific setup logic.
-
#start(text, &block) ⇒ Object
Start tokenizing.
-
#step ⇒ Object
Subclasses must implement this method, which is called for each iteration of the tokenization process.
-
#teardown ⇒ Object
Subclasses may override this method to provide implementation-specific teardown logic.
-
#tokenize(text, &block) ⇒ Object
Begins tokenizing the given text, calling #step until the text has been exhausted.
Class Method Details
.delegate(sym) ⇒ Object
A convenience for delegating method calls to the scanner.
73 74 75 |
# File 'lib/syntax/common.rb', line 73 def self.delegate( sym ) define_method( sym ) { |*a| @text.__send__( sym, *a ) } end |
Instance Method Details
#finish ⇒ Object
Finish tokenizing. This flushes the buffer, yielding any remaining text to the client.
46 47 48 49 |
# File 'lib/syntax/common.rb', line 46 def finish start_group nil teardown end |
#setup ⇒ Object
Subclasses may override this method to provide implementation-specific setup logic.
41 42 |
# File 'lib/syntax/common.rb', line 41 def setup end |
#start(text, &block) ⇒ Object
Start tokenizing. This sets up the state in preparation for tokenization, such as creating a new scanner for the text and saving the callback block. The block will be invoked for each token extracted.
31 32 33 34 35 36 37 |
# File 'lib/syntax/common.rb', line 31 def start( text, &block ) @chunk = "" @group = :normal @callback = block @text = StringScanner.new( text ) setup end |
#step ⇒ Object
Subclasses must implement this method, which is called for each iteration of the tokenization process. This method may extract multiple tokens.
58 59 60 |
# File 'lib/syntax/common.rb', line 58 def step raise NotImplementedError, "subclasses must implement #step" end |
#teardown ⇒ Object
Subclasses may override this method to provide implementation-specific teardown logic.
53 54 |
# File 'lib/syntax/common.rb', line 53 def teardown end |
#tokenize(text, &block) ⇒ Object
Begins tokenizing the given text, calling #step until the text has been exhausted.
64 65 66 67 68 |
# File 'lib/syntax/common.rb', line 64 def tokenize( text, &block ) start text, &block step until @text.eos? finish end |