@@ -3,25 +3,23 @@ module CodeRay
3
3
# GZip library for writing and reading token dumps.
4
4
autoload :GZip , coderay_path ( 'helpers' , 'gzip' )
5
5
6
- # = Tokens TODO: Rewrite!
6
+ # The Tokens class represents a list of tokens returned from
7
+ # a Scanner. It's actually just an Array with a few helper methods.
7
8
#
8
- # The Tokens class represents a list of tokens returnd from
9
- # a Scanner.
10
- #
11
- # A token is not a special object, just a two-element Array
9
+ # A token itself is not a special object, just a two-element Array
12
10
# consisting of
13
11
# * the _token_ _text_ (the original source of the token in a String) or
14
12
# a _token_ _action_ (begin_group, end_group, begin_line, end_line)
15
13
# * the _token_ _kind_ (a Symbol representing the type of the token)
16
14
#
17
- # A token looks like this:
15
+ # It looks like this:
18
16
#
19
17
# ['# It looks like this', :comment]
20
18
# ['3.1415926', :float]
21
19
# ['$^', :error]
22
20
#
23
21
# Some scanners also yield sub-tokens, represented by special
24
- # token actions, namely begin_group and end_group.
22
+ # token actions, for example : begin_group and : end_group.
25
23
#
26
24
# The Ruby scanner, for example, splits "a string" into:
27
25
#
@@ -33,23 +31,17 @@ module CodeRay
33
31
# [:end_group, :string]
34
32
# ]
35
33
#
36
- # Tokens is the interface between Scanners and Encoders:
37
- # The input is split and saved into a Tokens object. The Encoder
38
- # then builds the output from this object.
39
- #
40
- # Thus, the syntax below becomes clear:
41
- #
42
- # CodeRay.scan('price = 2.59', :ruby).html
43
- # # the Tokens object is here -------^
34
+ # Tokens can be used to save the output of a Scanners in a simple
35
+ # Ruby object that can be send to an Encoder later:
44
36
#
45
- # See how small it is? ;)
37
+ # tokens = CodeRay.scan('price = 2.59', :ruby).tokens
38
+ # tokens.encode(:html)
39
+ # tokens.html
40
+ # CodeRay.encoder(:html).encode_tokens(tokens)
46
41
#
47
42
# Tokens gives you the power to handle pre-scanned code very easily:
48
- # You can convert it to a webpage, a YAML file, or dump it into a gzip'ed string
49
- # that you put in your DB.
50
- #
51
- # It also allows you to generate tokens directly (without using a scanner),
52
- # to load them from a file, and still use any Encoder that CodeRay provides.
43
+ # You can serialize it to a JSON string and store it in a database, pass it
44
+ # around to encode it more than once, send it to other algorithms...
53
45
class Tokens < Array
54
46
55
47
# The Scanner instance that created the tokens.
@@ -58,8 +50,7 @@ class Tokens < Array
58
50
# Encode the tokens using encoder.
59
51
#
60
52
# encoder can be
61
- # * a symbol like :html oder :statistic
62
- # * an Encoder class
53
+ # * a plugin name like :html oder 'statistic'
63
54
# * an Encoder object
64
55
#
65
56
# options are passed to the encoder.
0 commit comments