class Shoes::Highlighter::Syntax::Tokenizer

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.

Constants

EOL

Attributes

chunk[R]

The current chunk of text being accumulated

group[R]

The current group being processed by the tokenizer

Private Class Methods

delegate(sym) click to toggle source

A convenience for delegating method calls to the scanner.

# File lib/shoes/highlighter/common.rb, line 96
def self.delegate(sym)
  define_method(sym) { |*a| @text.__send__(sym, *a) }
end

Public Instance Methods

finish() click to toggle source

Finish tokenizing. This flushes the buffer, yielding any remaining text to the client.

# File lib/shoes/highlighter/common.rb, line 55
def finish
  start_group nil
  teardown
end
option(opt) click to toggle source

Get the value of the specified option.

# File lib/shoes/highlighter/common.rb, line 87
def option(opt)
  @options ? @options[opt] : nil
end
set(opts = {}) click to toggle source

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.

# File lib/shoes/highlighter/common.rb, line 82
def set(opts = {})
  (@options ||= {}).update opts
end
setup() click to toggle source

Subclasses may override this method to provide implementation-specific setup logic.

# File lib/shoes/highlighter/common.rb, line 50
def setup
end
start(text, &block) click to toggle source

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.

# File lib/shoes/highlighter/common.rb, line 40
def start(text, &block)
  @chunk = ""
  @group = :normal
  @callback = block
  @text = StringScanner.new(text)
  setup
end
step() click to toggle source

Subclasses must implement this method, which is called for each iteration of the tokenization process. This method may extract multiple tokens.

# File lib/shoes/highlighter/common.rb, line 67
def step
  fail NotImplementedError, "subclasses must implement #step"
end
teardown() click to toggle source

Subclasses may override this method to provide implementation-specific teardown logic.

# File lib/shoes/highlighter/common.rb, line 62
def teardown
end
tokenize(text, &block) click to toggle source

Begins tokenizing the given text, calling step until the text has been exhausted.

# File lib/shoes/highlighter/common.rb, line 73
def tokenize(text, &block)
  start text, &block
  step until @text.eos?
  finish
end

Private Instance Methods

append(data) click to toggle source

Append the given data to the currently active chunk.

# File lib/shoes/highlighter/common.rb, line 118
def append(data)
  @chunk << data
end
end_region(gr, data = nil) click to toggle source
# File lib/shoes/highlighter/common.rb, line 141
def end_region(gr, data = nil)
  flush_chunk
  @group = gr
  @callback.call(Token.new(data || "", @group, :region_close))
end
flush_chunk() click to toggle source
# File lib/shoes/highlighter/common.rb, line 147
def flush_chunk
  @callback.call(Token.new(@chunk, @group)) unless @chunk.empty?
  @chunk = ""
end
start_group(gr, data = nil) click to toggle source

Request that a new group be started. If the current group is the same as the group being requested, a new group will not be created. If a new group is created and the current chunk is not empty, the chunk’s contents will be yielded to the client as a token, and then cleared.

After the new group is started, if data is non-nil it will be appended to the chunk.

# File lib/shoes/highlighter/common.rb, line 129
def start_group(gr, data = nil)
  flush_chunk if gr != @group
  @group = gr
  @chunk << data if data
end
start_region(gr, data = nil) click to toggle source
# File lib/shoes/highlighter/common.rb, line 135
def start_region(gr, data = nil)
  flush_chunk
  @group = gr
  @callback.call(Token.new(data || "", @group, :region_open))
end
subgroup(n) click to toggle source

Access the n-th subgroup from the most recent match.

# File lib/shoes/highlighter/common.rb, line 113
def subgroup(n)
  @text[n]
end
subtokenize(syntax, text) click to toggle source
# File lib/shoes/highlighter/common.rb, line 152
def subtokenize(syntax, text)
  tokenizer = Syntax.load(syntax)
  tokenizer.set @options if @options
  flush_chunk
  tokenizer.tokenize(text, &@callback)
end