forked from alecthomas/chroma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
345 lines (308 loc) · 6.97 KB
/
types.go
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package chroma
import (
"encoding/json"
"fmt"
)
//go:generate stringer -type TokenType
// TokenType is the type of token to highlight.
//
// It is also an Emitter, emitting a single token of itself
type TokenType int
func (t *TokenType) MarshalJSON() ([]byte, error) { return json.Marshal(t.String()) }
func (t *TokenType) UnmarshalJSON(data []byte) error {
key := ""
err := json.Unmarshal(data, &key)
if err != nil {
return err
}
for tt, text := range _TokenType_map {
if text == key {
*t = tt
return nil
}
}
return fmt.Errorf("unknown TokenType %q", data)
}
// Set of TokenTypes.
//
// Categories of types are grouped in ranges of 1000, while sub-categories are in ranges of 100. For
// example, the literal category is in the range 3000-3999. The sub-category for literal strings is
// in the range 3100-3199.
// Meta token types.
const (
// Default background style.
Background TokenType = -1 - iota
// Line numbers in output.
LineNumbers
// Line numbers in output when in table.
LineNumbersTable
// Line higlight style.
LineHighlight
// Line numbers table wrapper style.
LineTable
// Line numbers table TD wrapper style.
LineTableTD
// Input that could not be tokenised.
Error
// Other is used by the Delegate lexer to indicate which tokens should be handled by the delegate.
Other
// No highlighting.
None
)
// Keywords.
const (
Keyword TokenType = 1000 + iota
KeywordConstant
KeywordDeclaration
KeywordNamespace
KeywordPseudo
KeywordReserved
KeywordType
)
// Names.
const (
Name TokenType = 2000 + iota
NameAttribute
NameBuiltin
NameBuiltinPseudo
NameClass
NameConstant
NameDecorator
NameEntity
NameException
NameFunction
NameFunctionMagic
NameKeyword
NameLabel
NameNamespace
NameOperator
NameOther
NamePseudo
NameProperty
NameTag
NameVariable
NameVariableAnonymous
NameVariableClass
NameVariableGlobal
NameVariableInstance
NameVariableMagic
)
// Literals.
const (
Literal TokenType = 3000 + iota
LiteralDate
LiteralOther
)
// Strings.
const (
LiteralString TokenType = 3100 + iota
LiteralStringAffix
LiteralStringAtom
LiteralStringBacktick
LiteralStringBoolean
LiteralStringChar
LiteralStringDelimiter
LiteralStringDoc
LiteralStringDouble
LiteralStringEscape
LiteralStringHeredoc
LiteralStringInterpol
LiteralStringName
LiteralStringOther
LiteralStringRegex
LiteralStringSingle
LiteralStringSymbol
)
// Literals.
const (
LiteralNumber TokenType = 3200 + iota
LiteralNumberBin
LiteralNumberFloat
LiteralNumberHex
LiteralNumberInteger
LiteralNumberIntegerLong
LiteralNumberOct
)
// Operators.
const (
Operator TokenType = 4000 + iota
OperatorWord
)
// Punctuation.
const (
Punctuation TokenType = 5000 + iota
)
// Comments.
const (
Comment TokenType = 6000 + iota
CommentHashbang
CommentMultiline
CommentSingle
CommentSpecial
)
// Preprocessor "comments".
const (
CommentPreproc TokenType = 6100 + iota
CommentPreprocFile
)
// Generic tokens.
const (
Generic TokenType = 7000 + iota
GenericDeleted
GenericEmph
GenericError
GenericHeading
GenericInserted
GenericOutput
GenericPrompt
GenericStrong
GenericSubheading
GenericTraceback
GenericUnderline
)
// Text.
const (
Text TokenType = 8000 + iota
TextWhitespace
TextSymbol
TextPunctuation
)
// Aliases.
const (
Whitespace = TextWhitespace
Date = LiteralDate
String = LiteralString
StringAffix = LiteralStringAffix
StringBacktick = LiteralStringBacktick
StringChar = LiteralStringChar
StringDelimiter = LiteralStringDelimiter
StringDoc = LiteralStringDoc
StringDouble = LiteralStringDouble
StringEscape = LiteralStringEscape
StringHeredoc = LiteralStringHeredoc
StringInterpol = LiteralStringInterpol
StringOther = LiteralStringOther
StringRegex = LiteralStringRegex
StringSingle = LiteralStringSingle
StringSymbol = LiteralStringSymbol
Number = LiteralNumber
NumberBin = LiteralNumberBin
NumberFloat = LiteralNumberFloat
NumberHex = LiteralNumberHex
NumberInteger = LiteralNumberInteger
NumberIntegerLong = LiteralNumberIntegerLong
NumberOct = LiteralNumberOct
)
var (
StandardTypes = map[TokenType]string{
Background: "chroma",
LineNumbers: "ln",
LineNumbersTable: "lnt",
LineHighlight: "hl",
LineTable: "lntable",
LineTableTD: "lntd",
Text: "",
Whitespace: "w",
Error: "err",
Other: "x",
// I have no idea what this is used for...
// Escape: "esc",
Keyword: "k",
KeywordConstant: "kc",
KeywordDeclaration: "kd",
KeywordNamespace: "kn",
KeywordPseudo: "kp",
KeywordReserved: "kr",
KeywordType: "kt",
Name: "n",
NameAttribute: "na",
NameBuiltin: "nb",
NameBuiltinPseudo: "bp",
NameClass: "nc",
NameConstant: "no",
NameDecorator: "nd",
NameEntity: "ni",
NameException: "ne",
NameFunction: "nf",
NameFunctionMagic: "fm",
NameProperty: "py",
NameLabel: "nl",
NameNamespace: "nn",
NameOther: "nx",
NameTag: "nt",
NameVariable: "nv",
NameVariableClass: "vc",
NameVariableGlobal: "vg",
NameVariableInstance: "vi",
NameVariableMagic: "vm",
Literal: "l",
LiteralDate: "ld",
String: "s",
StringAffix: "sa",
StringBacktick: "sb",
StringChar: "sc",
StringDelimiter: "dl",
StringDoc: "sd",
StringDouble: "s2",
StringEscape: "se",
StringHeredoc: "sh",
StringInterpol: "si",
StringOther: "sx",
StringRegex: "sr",
StringSingle: "s1",
StringSymbol: "ss",
Number: "m",
NumberBin: "mb",
NumberFloat: "mf",
NumberHex: "mh",
NumberInteger: "mi",
NumberIntegerLong: "il",
NumberOct: "mo",
Operator: "o",
OperatorWord: "ow",
Punctuation: "p",
Comment: "c",
CommentHashbang: "ch",
CommentMultiline: "cm",
CommentPreproc: "cp",
CommentPreprocFile: "cpf",
CommentSingle: "c1",
CommentSpecial: "cs",
Generic: "g",
GenericDeleted: "gd",
GenericEmph: "ge",
GenericError: "gr",
GenericHeading: "gh",
GenericInserted: "gi",
GenericOutput: "go",
GenericPrompt: "gp",
GenericStrong: "gs",
GenericSubheading: "gu",
GenericTraceback: "gt",
GenericUnderline: "gl",
}
)
func (t TokenType) Parent() TokenType {
if t%100 != 0 {
return t / 100 * 100
}
if t%1000 != 0 {
return t / 1000 * 1000
}
return 0
}
func (t TokenType) Category() TokenType {
return t / 1000 * 1000
}
func (t TokenType) SubCategory() TokenType {
return t / 100 * 100
}
func (t TokenType) InCategory(other TokenType) bool {
return t/1000 == other/1000
}
func (t TokenType) InSubCategory(other TokenType) bool {
return t/100 == other/100
}
func (t TokenType) Emit(groups []string, lexer Lexer) Iterator {
return Literator(&Token{Type: t, Value: groups[0]})
}