-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoken.go
102 lines (87 loc) · 1.8 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
// Package token contains the definition of the lexical tokens of the
// TypeScript programming language.
package token
import "strconv"
// Kind is the set of lexical tokens of the TypeScript programming
// language.
type Kind int
const (
// Special tokens.
Illegal Kind = iota
EOF
// Comments.
Comment // // or /* */ at the beginning of a line
LineComment // // or /* */ at the end of a line
// Identifiers and literals.
Ident // main, const, extends, etc.
Number // 12345
String // "abc"
// Operators.
Or // |
Assign // =
Minus // -
// Delimiters and punctuation.
LParen // (
RParen // )
LBrack // [
RBrack // ]
LBrace // {
RBrace // }
LAngle // <
RAngle // >
Comma // ,
Dot // .
Colon // :
Semicolon // ;
Question // ?
)
var tokens = [...]string{
// Special tokens.
Illegal: "Illegal",
EOF: "EOF",
// Comments.
Comment: "Comment",
LineComment: "LineComment",
// Identifiers and literals.
Ident: "Ident",
Number: "Number",
String: "String",
// Operators.
Or: "|",
Assign: "=",
Minus: "-",
// Delimiters and punctuation.
LParen: "(",
RParen: ")",
LBrack: "[",
RBrack: "]",
LBrace: "{",
RBrace: "}",
LAngle: "<",
RAngle: ">",
Comma: ",",
Dot: ".",
Colon: ":",
Semicolon: ";",
Question: "?",
}
func (k Kind) String() string {
if 0 <= k && k < Kind(len(tokens)) {
return tokens[k]
}
return "token(" + strconv.Itoa(int(k)) + ")"
}
func (k Kind) IsLiteral() bool {
switch k {
case Ident, Number, String, Comment, LineComment:
return true
default:
return false
}
}
// Token represents a lexical token of the TypeScript programming language.
// Identifiers and literals are accompanied by the corresponding text.
type Token struct {
Kind Kind
Text string
}