-
Notifications
You must be signed in to change notification settings - Fork 71
/
token.go
801 lines (697 loc) · 17.9 KB
/
token.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
// Copyright (c) 2018, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package token defines a complete set of all lexical tokens for any kind of
// language! It is based on the alecthomas/chroma / pygments lexical tokens
// plus all the more detailed tokens needed for actually parsing languages
package token
//go:generate core generate
import (
"fmt"
"cogentcore.org/core/icons"
)
// Tokens is a complete set of lexical tokens that encompasses all programming and text
// markup languages. It includes everything in alecthomas/chroma (pygments) and
// everything needed for Go, C, C++, Python, etc.
//
// There are categories and sub-categories, and methods to get those from a given
// element. The first category is 'None'.
//
// See http://pygments.org/docs/tokens/ for more docs on the different categories
//
// Anything missing should be added via a pull request etc
type Tokens int32 //enums:enum
// CatMap is the map into the category level for each token
var CatMap map[Tokens]Tokens
// SubCatMap is the map into the sub-category level for each token
var SubCatMap map[Tokens]Tokens
func init() {
InitCatMap()
InitSubCatMap()
}
// Cat returns the category that a given token lives in, using CatMap
func (tk Tokens) Cat() Tokens {
return CatMap[tk]
}
// SubCat returns the sub-category that a given token lives in, using SubCatMap
func (tk Tokens) SubCat() Tokens {
return SubCatMap[tk]
}
// IsCat returns true if this is a category-level token
func (tk Tokens) IsCat() bool {
return tk.Cat() == tk
}
// IsSubCat returns true if this is a sub-category-level token
func (tk Tokens) IsSubCat() bool {
return tk.SubCat() == tk
}
// InCat returns true if this is in same category as given token
func (tk Tokens) InCat(other Tokens) bool {
return tk.Cat() == other.Cat()
}
// InCat returns true if this is in same sub-category as given token
func (tk Tokens) InSubCat(other Tokens) bool {
return tk.SubCat() == other.SubCat()
}
// IsCode returns true if this token is in Keyword, Name, Operator, or Punctuation categs.
// these are recognized code (program) elements that can usefully be distinguished from
// other forms of raw text (e.g., for spell checking)
func (tk Tokens) IsCode() bool {
return tk.InCat(Keyword) || tk.InCat(Name) || tk.InCat(Operator) || tk.InCat(Punctuation)
}
// IsKeyword returns true if this in the Keyword category
func (tk Tokens) IsKeyword() bool {
return tk.Cat() == Keyword
}
// Parent returns the closest parent-level of this token (subcat or cat)
func (tk Tokens) Parent() Tokens {
if tk.IsSubCat() {
return tk.Cat()
}
return tk.SubCat()
}
// Match returns true if the two tokens match, in a category / subcategory sensitive manner:
// if receiver token is a category, then it matches other token if it is the same category
// and likewise for subcategory
func (tk Tokens) Match(otk Tokens) bool {
if tk == otk {
return true
}
if tk.IsCat() && otk.Cat() == tk {
return true
}
return tk.IsSubCat() && otk.SubCat() == tk
}
// IsPunctGpLeft returns true if token is a PunctGpL token -- left paren, brace, bracket
func (tk Tokens) IsPunctGpLeft() bool {
return (tk == PunctGpLParen || tk == PunctGpLBrack || tk == PunctGpLBrace)
}
// IsPunctGpRight returns true if token is a PunctGpR token -- right paren, brace, bracket
func (tk Tokens) IsPunctGpRight() bool {
return (tk == PunctGpRParen || tk == PunctGpRBrack || tk == PunctGpRBrace)
}
// PunctGpMatch returns the matching token for given PunctGp token
func (tk Tokens) PunctGpMatch() Tokens {
switch tk {
case PunctGpLParen:
return PunctGpRParen
case PunctGpRParen:
return PunctGpLParen
case PunctGpLBrack:
return PunctGpRBrack
case PunctGpRBrack:
return PunctGpLBrack
case PunctGpLBrace:
return PunctGpRBrace
case PunctGpRBrace:
return PunctGpLBrace
}
return None
}
// IsAmbigUnaryOp returns true if this token is an operator that could either be
// a Unary or Binary operator -- need special matching for this.
// includes * and & which are used for address operations in C-like languages
func (tk Tokens) IsAmbigUnaryOp() bool {
return (tk == OpMathSub || tk == OpMathMul || tk == OpBitAnd || tk == OpMathAdd || tk == OpBitXor)
}
// IsUnaryOp returns true if this token is an operator that is typically used as
// a Unary operator: - + & * ! ^ ! <-
func (tk Tokens) IsUnaryOp() bool {
return (tk == OpMathSub || tk == OpMathMul || tk == OpBitAnd || tk == OpMathAdd ||
tk == OpBitXor || tk == OpLogNot || tk == OpAsgnArrow)
}
// CombineRepeats are token types where repeated tokens of the same type should
// be combined together -- literals, comments, text
func (tk Tokens) CombineRepeats() bool {
cat := tk.Cat()
return (cat == Literal || cat == Comment || cat == Text || cat == Name)
}
// StyleName returns the abbreviated 2-3 letter style name of the tag
func (tk Tokens) StyleName() string {
return Names[tk]
}
// ClassName returns the . prefixed CSS classname of the tag style
// for styling, a CSS property should exist with this name
func (tk Tokens) ClassName() string {
return "." + tk.StyleName()
}
/////////////////////////////////////////////////////////////////////////////
// KeyToken -- keyword + token
// KeyToken combines a token and an optional keyword name for Keyword token types
// if Tok is in Keyword category, then Key string can be used to check for same keyword.
// Also has a Depth for matching against a particular nesting depth
type KeyToken struct {
Token Tokens
Key string
Depth int
}
var KeyTokenZero = KeyToken{}
func (kt KeyToken) String() string {
ds := ""
if kt.Depth != 0 {
ds = fmt.Sprintf("+%d:", kt.Depth)
}
if kt.Key != "" {
return ds + kt.Token.String() + ": " + kt.Key
}
return ds + kt.Token.String()
}
// Equal compares equality of two tokens including keywords if token is in Keyword category.
// See also Match for version that uses category / subcategory matching
func (kt KeyToken) Equal(okt KeyToken) bool {
if kt.Token.IsKeyword() && kt.Key != "" {
return kt.Token == okt.Token && kt.Key == okt.Key
}
return kt.Token == okt.Token
}
// Match compares equality of two tokens including keywords if token is in Keyword category.
// returns true if the two tokens match, in a category / subcategory sensitive manner:
// if receiver token is a category, then it matches other token if it is the same category
// and likewise for subcategory
func (kt KeyToken) Match(okt KeyToken) bool {
if kt.Token.IsKeyword() && kt.Key != "" {
return kt.Token.Match(okt.Token) && kt.Key == okt.Key
}
return kt.Token.Match(okt.Token)
}
// MatchDepth compares equality of two tokens including depth -- see Match for other matching
// criteria
func (kt KeyToken) MatchDepth(okt KeyToken) bool {
if kt.Depth != okt.Depth {
return false
}
return kt.Match(okt)
}
// StringKey encodes token into a string for optimized string-based map key lookup
func (kt KeyToken) StringKey() string {
tstr := string([]byte{byte(kt.Token)})
if kt.Token.IsKeyword() {
return tstr + kt.Key
} else {
return tstr
}
}
// KeyTokenList is a list (slice) of KeyTokens
type KeyTokenList []KeyToken
// Match returns true if given keytoken matches any of the items on the list
func (kl KeyTokenList) Match(okt KeyToken) bool {
for _, kt := range kl {
if kt.Match(okt) {
return true
}
}
return false
}
// IconName returns the appropriate icon name for the type of lexical item this is
func (tk Tokens) IconName() string {
switch {
case tk.SubCat() == NameVar:
return string(icons.Variable)
case tk == NameConstant || tk == NameEnum || tk == NameEnumMember:
return string(icons.Constant)
case tk == NameField:
return string(icons.Field)
case tk.SubCat() == NameType:
return string(icons.Type)
case tk == NameMethod:
return string(icons.Method)
case tk.SubCat() == NameFunction:
return string(icons.Function)
}
return ""
}
/////////////////////////////////////////////////////////////////////////////
// Tokens
// The list of tokens
const (
// None is the nil token value -- for non-terminal cases or TBD
None Tokens = iota
// Error is an input that could not be tokenized due to syntax error etc
Error
// EOF is end of file
EOF
// EOL is end of line (typically implicit -- used for rule matching)
EOL
// EOS is end of statement -- a key meta-token -- in C it is ;, in Go it is either ; or EOL
EOS
// Background is for syntax highlight styles based on these tokens
Background
// Cat: Keywords (actual keyword is just the string)
Keyword
KeywordConstant
KeywordDeclaration
KeywordNamespace // incl package, import
KeywordPseudo
KeywordReserved
KeywordType
// Cat: Names.
Name
NameBuiltin // e.g., true, false -- builtin values..
NameBuiltinPseudo // e.g., this, self
NameOther
NamePseudo
// SubCat: Type names
NameType
NameClass
NameStruct
NameField
NameInterface
NameConstant
NameEnum
NameEnumMember
NameArray // includes slice etc
NameMap
NameObject
NameTypeParam // for generics, templates
// SubCat: Function names
NameFunction
NameDecorator // function-like wrappers in python
NameFunctionMagic // e.g., __init__ in python
NameMethod
NameOperator
NameConstructor // includes destructor..
NameException
NameLabel // e.g., goto label
NameEvent // for LSP -- not really sure what it is..
// SubCat: Scoping names
NameScope
NameNamespace
NameModule
NamePackage
NameLibrary
// SubCat: NameVar -- variable names
NameVar
NameVarAnonymous
NameVarClass
NameVarGlobal
NameVarInstance
NameVarMagic
NameVarParam
// SubCat: Value -- data-like elements
NameValue
NameTag // e.g., HTML tag
NameProperty
NameAttribute // e.g., HTML attr
NameEntity // special entities. (e.g. in HTML). seems like other..
// Cat: Literals.
Literal
LiteralDate
LiteralOther
LiteralBool
// SubCat: Literal Strings.
LitStr
LitStrAffix // unicode specifiers etc
LitStrAtom
LitStrBacktick
LitStrBoolean
LitStrChar
LitStrDelimiter
LitStrDoc // doc-specific strings where syntactically noted
LitStrDouble
LitStrEscape // esc sequences within strings
LitStrHeredoc // in ruby, perl
LitStrInterpol // interpolated parts of strings in #{foo} in Ruby
LitStrName
LitStrOther
LitStrRegex
LitStrSingle
LitStrSymbol
LitStrFile // filename
// SubCat: Literal Numbers.
LitNum
LitNumBin
LitNumFloat
LitNumHex
LitNumInteger
LitNumIntegerLong
LitNumOct
LitNumImag
// Cat: Operators.
Operator
OperatorWord
// SubCat: Math operators
OpMath
OpMathAdd // +
OpMathSub // -
OpMathMul // *
OpMathDiv // /
OpMathRem // %
// SubCat: Bitwise operators
OpBit
OpBitAnd // &
OpBitOr // |
OpBitNot // ~
OpBitXor // ^
OpBitShiftLeft // <<
OpBitShiftRight // >>
OpBitAndNot // &^
// SubCat: Assign operators
OpAsgn
OpAsgnAssign // =
OpAsgnInc // ++
OpAsgnDec // --
OpAsgnArrow // <-
OpAsgnDefine // :=
// SubCat: Math Assign operators
OpMathAsgn
OpMathAsgnAdd // +=
OpMathAsgnSub // -=
OpMathAsgnMul // *=
OpMathAsgnDiv // /=
OpMathAsgnRem // %=
// SubCat: Bitwise Assign operators
OpBitAsgn
OpBitAsgnAnd // &=
OpBitAsgnOr // |=
OpBitAsgnXor // ^=
OpBitAsgnShiftLeft // <<=
OpBitAsgnShiftRight // >>=
OpBitAsgnAndNot // &^=
// SubCat: Logical operators
OpLog
OpLogAnd // &&
OpLogOr // ||
OpLogNot // !
// SubCat: Relational operators
OpRel
OpRelEqual // ==
OpRelNotEqual // !=
OpRelLess // <
OpRelGreater // >
OpRelLtEq // <=
OpRelGtEq // >=
// SubCat: List operators
OpList
OpListEllipsis // ...
// Cat: Punctuation.
Punctuation
// SubCat: Grouping punctuation
PunctGp
PunctGpLParen // (
PunctGpRParen // )
PunctGpLBrack // [
PunctGpRBrack // ]
PunctGpLBrace // {
PunctGpRBrace // }
// SubCat: Separator punctuation
PunctSep
PunctSepComma // ,
PunctSepPeriod // .
PunctSepSemicolon // ;
PunctSepColon // :
// SubCat: String punctuation
PunctStr
PunctStrDblQuote // "
PunctStrQuote // '
PunctStrBacktick // `
PunctStrEsc // \
// Cat: Comments.
Comment
CommentHashbang
CommentMultiline
CommentSingle
CommentSpecial
// SubCat: Preprocessor "comments".
CommentPreproc
CommentPreprocFile
// Cat: Text.
Text
TextWhitespace
TextSymbol
TextPunctuation
TextSpellErr
// SubCat: TextStyle (corresponds to Generic in chroma / pygments) todo: look in font deco for more
TextStyle
TextStyleDeleted // strike-through
TextStyleEmph // italics
TextStyleError
TextStyleHeading
TextStyleInserted
TextStyleOutput
TextStylePrompt
TextStyleStrong // bold
TextStyleSubheading
TextStyleTraceback
TextStyleUnderline
TextStyleLink
)
// Categories
var Cats = []Tokens{
None,
Keyword,
Name,
Literal,
Operator,
Punctuation,
Comment,
Text,
TokensN,
}
// Sub-Categories
var SubCats = []Tokens{
None,
Keyword,
Name,
NameType,
NameFunction,
NameScope,
NameVar,
NameValue,
Literal,
LitStr,
LitNum,
Operator,
OpMath,
OpBit,
OpAsgn,
OpMathAsgn,
OpBitAsgn,
OpLog,
OpRel,
OpList,
Punctuation,
PunctGp,
PunctSep,
PunctStr,
Comment,
CommentPreproc,
Text,
TextStyle,
TokensN,
}
// InitCatMap initializes the CatMap
func InitCatMap() {
if CatMap != nil {
return
}
CatMap = make(map[Tokens]Tokens, TokensN)
for tk := None; tk < TokensN; tk++ {
for c := 1; c < len(Cats); c++ {
if tk < Cats[c] {
CatMap[tk] = Cats[c-1]
break
}
}
}
}
// InitSubCatMap initializes the SubCatMap
func InitSubCatMap() {
if SubCatMap != nil {
return
}
SubCatMap = make(map[Tokens]Tokens, TokensN)
for tk := None; tk < TokensN; tk++ {
for c := 1; c < len(SubCats); c++ {
if tk < SubCats[c] {
SubCatMap[tk] = SubCats[c-1]
break
}
}
}
}
// OpPunctMap provides a lookup of operators and punctuation tokens by their usual
// string representation
var OpPunctMap = map[string]Tokens{
"+": OpMathAdd,
"-": OpMathSub,
"*": OpMathMul,
"/": OpMathDiv,
"%": OpMathRem,
"&": OpBitAnd,
"|": OpBitOr,
"~": OpBitNot,
"^": OpBitXor,
"<<": OpBitShiftLeft,
">>": OpBitShiftRight,
"&^": OpBitAndNot,
"=": OpAsgnAssign,
"++": OpAsgnInc,
"--": OpAsgnDec,
"<-": OpAsgnArrow,
":=": OpAsgnDefine,
"+=": OpMathAsgnAdd,
"-=": OpMathAsgnSub,
"*=": OpMathAsgnMul,
"/=": OpMathAsgnDiv,
"%=": OpMathAsgnRem,
"&=": OpBitAsgnAnd,
"|=": OpBitAsgnOr,
"^=": OpBitAsgnXor,
"<<=": OpBitAsgnShiftLeft,
">>=": OpBitAsgnShiftRight,
"&^=": OpBitAsgnAndNot,
"&&": OpLogAnd,
"||": OpLogOr,
"!": OpLogNot,
"==": OpRelEqual,
"!=": OpRelNotEqual,
"<": OpRelLess,
">": OpRelGreater,
"<=": OpRelLtEq,
">=": OpRelGtEq,
"...": OpListEllipsis,
"(": PunctGpLParen,
")": PunctGpRParen,
"[": PunctGpLBrack,
"]": PunctGpRBrack,
"{": PunctGpLBrace,
"}": PunctGpRBrace,
",": PunctSepComma,
".": PunctSepPeriod,
";": PunctSepSemicolon,
":": PunctSepColon,
"\"": PunctStrDblQuote,
"'": PunctStrQuote,
"`": PunctStrBacktick,
"\\": PunctStrEsc,
}
// Names are the short tag names for each token, used e.g., for syntax highlighting
// These are based on alecthomas/chroma / pygments
var Names = map[Tokens]string{
None: "",
Error: "err",
EOF: "EOF",
EOL: "EOL",
EOS: "EOS",
Background: "bg",
Keyword: "k",
KeywordConstant: "kc",
KeywordDeclaration: "kd",
KeywordNamespace: "kn",
KeywordPseudo: "kp",
KeywordReserved: "kr",
KeywordType: "kt",
Name: "n",
NameBuiltin: "nb",
NameBuiltinPseudo: "bp",
NameOther: "nx",
NamePseudo: "pu",
NameType: "nt",
NameClass: "nc",
NameStruct: "ns",
NameField: "nfl",
NameInterface: "nti",
NameConstant: "no",
NameEnum: "nen",
NameEnumMember: "nem",
NameArray: "nr",
NameMap: "nm",
NameObject: "nj",
NameTypeParam: "ntp",
NameFunction: "nf",
NameDecorator: "nd",
NameFunctionMagic: "fm",
NameMethod: "mt",
NameOperator: "np",
NameConstructor: "cr",
NameException: "ne",
NameLabel: "nl",
NameEvent: "ev",
NameScope: "nsc",
NameNamespace: "nn",
NameModule: "md",
NamePackage: "pk",
NameLibrary: "lb",
NameVar: "nv",
NameVarAnonymous: "ay",
NameVarClass: "vc",
NameVarGlobal: "vg",
NameVarInstance: "vi",
NameVarMagic: "vm",
NameVarParam: "vp",
NameValue: "vl",
NameTag: "ng",
NameProperty: "py",
NameAttribute: "na",
NameEntity: "ni",
Literal: "l",
LiteralDate: "ld",
LiteralOther: "lo",
LiteralBool: "bo",
LitStr: "s",
LitStrAffix: "sa",
LitStrAtom: "st",
LitStrBacktick: "sb",
LitStrBoolean: "so",
LitStrChar: "sc",
LitStrDelimiter: "dl",
LitStrDoc: "sd",
LitStrDouble: "s2",
LitStrEscape: "se",
LitStrHeredoc: "sh",
LitStrInterpol: "si",
LitStrName: "sn",
LitStrOther: "sx",
LitStrRegex: "sr",
LitStrSingle: "s1",
LitStrSymbol: "ss",
LitStrFile: "fl",
LitNum: "m",
LitNumBin: "mb",
LitNumFloat: "mf",
LitNumHex: "mh",
LitNumInteger: "mi",
LitNumIntegerLong: "il",
LitNumOct: "mo",
LitNumImag: "mj",
Operator: "o",
OperatorWord: "ow",
// don't really need these -- only have at sub-categ level
OpMath: "om",
OpBit: "ob",
OpAsgn: "oa",
OpMathAsgn: "pa",
OpBitAsgn: "ba",
OpLog: "ol",
OpRel: "or",
OpList: "oi",
Punctuation: "p",
PunctGp: "pg",
PunctSep: "ps",
PunctStr: "pr",
Comment: "c",
CommentHashbang: "ch",
CommentMultiline: "cm",
CommentSingle: "c1",
CommentSpecial: "cs",
CommentPreproc: "cp",
CommentPreprocFile: "cpf",
Text: "",
TextWhitespace: "w",
TextSymbol: "ts",
TextPunctuation: "tp",
TextSpellErr: "te",
TextStyle: "g",
TextStyleDeleted: "gd",
TextStyleEmph: "ge",
TextStyleError: "gr",
TextStyleHeading: "gh",
TextStyleInserted: "gi",
TextStyleOutput: "go",
TextStylePrompt: "gp",
TextStyleStrong: "gs",
TextStyleSubheading: "gu",
TextStyleTraceback: "gt",
TextStyleUnderline: "gl",
TextStyleLink: "ga",
}