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