1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
# -*- coding: utf-8 -*- #
# frozen_string_literal: true
module Rouge
class Token
class << self
attr_reader :name
attr_reader :parent
attr_reader :shortname
def cache
@cache ||= {}
end
def sub_tokens
@sub_tokens ||= {}
end
def [](qualname)
return qualname unless qualname.is_a? ::String
Token.cache[qualname]
end
def inspect
"<Token #{qualname}>"
end
def matches?(other)
other.token_chain.include? self
end
def token_chain
@token_chain ||= ancestors.take_while { |x| x != Token }.reverse
end
def qualname
@qualname ||= token_chain.map(&:name).join('.')
end
def register!
Token.cache[self.qualname] = self
parent.sub_tokens[self.name] = self
end
def make_token(name, shortname, &b)
parent = self
Class.new(parent) do
@parent = parent
@name = name
@shortname = shortname
register!
class_eval(&b) if b
end
end
def token(name, shortname, &b)
tok = make_token(name, shortname, &b)
const_set(name, tok)
end
def each_token(&b)
Token.cache.each do |(_, t)|
b.call(t)
end
end
end
module Tokens
def self.token(name, shortname, &b)
tok = Token.make_token(name, shortname, &b)
const_set(name, tok)
end
# XXX IMPORTANT XXX
# For compatibility, this list must be kept in sync with
# pygments.token.STANDARD_TYPES
# please see https://github.com/jneen/rouge/wiki/List-of-tokens
token :Text, '' do
token :Whitespace, 'w'
end
token :Escape, 'esc'
token :Error, 'err'
token :Other, 'x'
token :Keyword, 'k' do
token :Constant, 'kc'
token :Declaration, 'kd'
token :Namespace, 'kn'
token :Pseudo, 'kp'
token :Reserved, 'kr'
token :Type, 'kt'
token :Variable, 'kv'
end
token :Name, 'n' do
token :Attribute, 'na'
token :Builtin, 'nb' do
token :Pseudo, 'bp'
end
token :Class, 'nc'
token :Constant, 'no'
token :Decorator, 'nd'
token :Entity, 'ni'
token :Exception, 'ne'
token :Function, 'nf' do
token :Magic, 'fm'
end
token :Property, 'py'
token :Label, 'nl'
token :Namespace, 'nn'
token :Other, 'nx'
token :Tag, 'nt'
token :Variable, 'nv' do
token :Class, 'vc'
token :Global, 'vg'
token :Instance, 'vi'
token :Magic, 'vm'
end
end
token :Literal, 'l' do
token :Date, 'ld'
token :String, 's' do
token :Affix, 'sa'
token :Backtick, 'sb'
token :Char, 'sc'
token :Delimiter, 'dl'
token :Doc, 'sd'
token :Double, 's2'
token :Escape, 'se'
token :Heredoc, 'sh'
token :Interpol, 'si'
token :Other, 'sx'
token :Regex, 'sr'
token :Single, 's1'
token :Symbol, 'ss'
end
token :Number, 'm' do
token :Bin, 'mb'
token :Float, 'mf'
token :Hex, 'mh'
token :Integer, 'mi' do
token :Long, 'il'
end
token :Oct, 'mo'
token :Other, 'mx'
end
end
token :Operator, 'o' do
token :Word, 'ow'
end
token :Punctuation, 'p' do
token :Indicator, 'pi'
end
token :Comment, 'c' do
token :Hashbang, 'ch'
token :Doc, 'cd'
token :Multiline, 'cm'
token :Preproc, 'cp'
token :PreprocFile, 'cpf'
token :Single, 'c1'
token :Special, 'cs'
end
token :Generic, 'g' do
token :Deleted, 'gd'
token :Emph, 'ge'
token :EmphStrong, 'ges'
token :Error, 'gr'
token :Heading, 'gh'
token :Inserted, 'gi'
token :Lineno, 'gl'
token :Output, 'go'
token :Prompt, 'gp'
token :Strong, 'gs'
token :Subheading, 'gu'
token :Traceback, 'gt'
end
# convenience
Num = Literal::Number
Str = Literal::String
end
end
end
|