Class: HH::Syntax::Tokenizer
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.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#chunk ⇒ Object
readonly
The current chunk of text being accumulated.
-
#group ⇒ Object
readonly
The current group being processed by the tokenizer.
Instance Method Summary collapse
-
#finish ⇒ Object
Finish tokenizing.
-
#option(opt) ⇒ Object
Get the value of the specified option.
-
#set(opts = {}) ⇒ Object
Specify a set of tokenizer-specific options.
-
#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.
Instance Attribute Details
#chunk ⇒ Object (readonly)
The current chunk of text being accumulated
37 38 39 |
# File 'lib/ext/highlighter/common.rb', line 37 def chunk @chunk end |
#group ⇒ Object (readonly)
The current group being processed by the tokenizer
34 35 36 |
# File 'lib/ext/highlighter/common.rb', line 34 def group @group end |
Instance Method Details
#finish ⇒ Object
Finish tokenizing. This flushes the buffer, yielding any remaining text to the client.
57 58 59 60 |
# File 'lib/ext/highlighter/common.rb', line 57 def finish start_group nil teardown end |
#option(opt) ⇒ Object
Get the value of the specified option.
89 90 91 |
# File 'lib/ext/highlighter/common.rb', line 89 def option(opt) @options ? @options[opt] : nil end |
#set(opts = {}) ⇒ Object
Specify a set of tokenizer-specific options. Each tokenizer may (or may not) publish any options, but if a tokenizer does those options may be used to specify optional behavior.
84 85 86 |
# File 'lib/ext/highlighter/common.rb', line 84 def set( opts={} ) ( @options ||= Hash.new ).update opts end |
#setup ⇒ Object
Subclasses may override this method to provide implementation-specific setup logic.
52 53 |
# File 'lib/ext/highlighter/common.rb', line 52 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.
42 43 44 45 46 47 48 |
# File 'lib/ext/highlighter/common.rb', line 42 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.
69 70 71 |
# File 'lib/ext/highlighter/common.rb', line 69 def step raise NotImplementedError, "subclasses must implement #step" end |
#teardown ⇒ Object
Subclasses may override this method to provide implementation-specific teardown logic.
64 65 |
# File 'lib/ext/highlighter/common.rb', line 64 def teardown end |
#tokenize(text, &block) ⇒ Object
Begins tokenizing the given text, calling #step until the text has been exhausted.
75 76 77 78 79 |
# File 'lib/ext/highlighter/common.rb', line 75 def tokenize( text, &block ) start text, &block step until @text.eos? finish end |