class CodeRay::Tokens
around to encode it more than once, send it to other algorithms…
You can serialize it to a JSON string and store it in a database, pass it
Tokens gives you the power to handle pre-scanned code very easily:
CodeRay.encoder(:html).encode_tokens(tokens)
tokens.html
tokens.encode(:html)
tokens = CodeRay.scan(‘price = 2.59’, :ruby).tokens
Ruby object that can be send to an Encoder later:
Tokens can be used to save the output of a Scanners in a simple
]
:end_group, :string
’“‘, :delimiter,
’a string’, :content,
‘”’, :delimiter,
:begin_group, :string,
[
The Ruby scanner, for example, splits “a string” into:
token actions, for example :begin_group and :end_group.
Some scanners also yield sub-tokens, represented by special
…, ‘$^’, :error, …
…, ‘3.1415926’, :float, …
…, ‘# It looks like this’, :comment, …
It looks like this:
* the token kind (a Symbol representing the type of the token)
a token action (begin_group, end_group, begin_line, end_line)
* the token text (the original source of the token in a String) or
A token itself is not a special object, just two elements in an Array:
a Scanner. It’s actually just an Array with a few helper methods.
The Tokens class represents a list of tokens returned from
def begin_group kind; push :begin_group, kind end
def begin_group kind; push :begin_group, kind end
def begin_line kind; push :begin_line, kind end
def begin_line kind; push :begin_line, kind end
def count
def count size / 2 end
def encode encoder, options = {}
* an Encoder object
* a plugin name like :html oder 'statistic'
encoder can be
Encode the tokens using encoder.
def encode encoder, options = {} encoder = Encoders[encoder].new options if encoder.respond_to? :to_sym encoder.encode_tokens self, options end
def end_group kind; push :end_group, kind end
def end_group kind; push :end_group, kind end
def end_line kind; push :end_line, kind end
def end_line kind; push :end_line, kind end
def method_missing meth, options = {}
For example, if you call +tokens.html+, the HTML encoder
Redirects unknown methods to encoder calls.
def method_missing meth, options = {} encode meth, options rescue PluginHost::PluginNotFound super end
def split_into_parts *sizes
This method is used by @Scanner#tokenize@ when called with an Array
betweem them.
part closes all opened tokens. This is useful to insert tokens
the text size specified by the parameter. In addition, each
The result will be an Array of Tokens objects. The parts have
Split the tokens into parts of the given +sizes+.
def split_into_parts *sizes return Array.new(sizes.size) { Tokens.new } if size == 2 && first == '' parts = [] opened = [] content = nil part = Tokens.new part_size = 0 size = sizes.first i = 0 for item in self case content when nil content = item when String if size && part_size + content.size > size # token must be cut if part_size < size # some part of the token goes into this part content = content.dup # content may no be safe to change part << content.slice!(0, size - part_size) << item end # close all open groups and lines... closing = opened.reverse.flatten.map do |content_or_kind| case content_or_kind when :begin_group :end_group when :begin_line :end_line else content_or_kind end end part.concat closing begin parts << part part = Tokens.new size = sizes[i += 1] end until size.nil? || size > 0 # ...and open them again. part.concat opened.flatten part_size = 0 redo unless content.empty? else part << content << item part_size += content.size end content = nil when Symbol case content when :begin_group, :begin_line opened << [content, item] when :end_group, :end_line opened.pop else raise ArgumentError, 'Unknown token action: %p, kind = %p' % [content, item] end part << content << item content = nil else raise ArgumentError, 'Token input junk: %p, kind = %p' % [content, item] end end parts << part parts << Tokens.new while parts.size < sizes.size parts end
def to_s
def to_s encode CodeRay::Encoders::Encoder.new end