-
Notifications
You must be signed in to change notification settings - Fork 2
/
token.go
87 lines (76 loc) · 1.12 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
package tlps
import "fmt"
// TokenType is type of type
type TokenType int
const (
// Single-character tokens
LeftParenTT TokenType = iota
RightParenTT
LeftBraceTT
RightBraceTT
CommaTT
DotTT
MinusTT
NewlineTT
PlusTT
SemicolonTT
ColonTT
SlashTT
StarTT
// One or two chacacter tokens
BangTT
BangEqualTT
BangBangTT
EqualTT
EqualEqualTT
GreaterTT
GreaterEqualTT
LessTT
LessEqualTT
// Literal
IdentifierTT
StringTT
NumberTT
// keywords
AndTT
ClassTT
ElseTT
ElseifTT
FalseTT
FunTT
ForTT
IfTT
IncludeTT
NilTT
OrTT
PassTT
ReturnTT
SuperTT
ThisTT
TrueTT
VarTT
WhileTT
EOFTT
)
// Token is struct of token
type Token struct {
Type TokenType
Lexeme string
Literal interface{}
Line int
}
// TokenList is slice of Token
type TokenList []*Token
// NewToken is constructor of Token
func NewToken(tt TokenType, lexeme string, literal interface{}, line int) *Token {
return &Token{
Type: tt,
Lexeme: lexeme,
Literal: literal,
Line: line,
}
}
// String stringfy Token
func (t *Token) String() string {
return fmt.Sprintf("%v\t%v\t%v\t%v", t.Type, t.Lexeme, t.Literal, t.Line)
}