-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
124 lines (112 loc) · 2.14 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
package main
import (
"fmt"
)
// TokenType is used for representing different tokens
type TokenType int
const (
tEOF TokenType = iota // end of file
tError // error has occurred
tComment // comment
tInt // integer
tFloat // floating point
tIdent // identifier
tString // string
tNewline // new line
tIllegal
keywordBeginning
LINE
SCALE
MOVE
ROTATE
XAXIS
YAXIS
ZAXIS
SAVE
DISPLAY
CIRCLE
HERMITE
BEZIER
BOX
CLEAR
SPHERE
TORUS
PUSH
POP
VARY
BASENAME
FRAMES
SET
SETKNOBS
MESH
LIGHT
AMBIENT
CONSTANTS
keywordEnd
)
var tokens = map[TokenType]string{
tEOF: "EOF",
tError: "ERROR",
tComment: "COMMENT",
tInt: "INT",
tFloat: "FLOAT",
tIdent: "IDENTIFIER",
tString: "STRING",
tIllegal: "ILLEGAL",
tNewline: "NEWLINE",
LINE: "line",
SCALE: "scale",
MOVE: "move",
ROTATE: "rotate",
XAXIS: "x",
YAXIS: "y",
ZAXIS: "z",
SAVE: "save",
DISPLAY: "display",
CIRCLE: "circle",
HERMITE: "hermite",
BEZIER: "bezier",
BOX: "box",
CLEAR: "clear",
SPHERE: "sphere",
TORUS: "torus",
PUSH: "push",
POP: "pop",
VARY: "vary",
BASENAME: "basename",
FRAMES: "frames",
SET: "set",
SETKNOBS: "setknobs",
MESH: "mesh",
LIGHT: "light",
AMBIENT: "ambient",
CONSTANTS: "constants",
}
var keywords map[string]TokenType
func init() {
keywords = make(map[string]TokenType)
for i := keywordBeginning; i < keywordEnd; i++ {
keywords[tokens[i]] = i
}
}
// Token is a lexical token
type Token struct {
tt TokenType // type of token
value string // value of token
}
func (tt TokenType) String() string {
if s, isToken := tokens[tt]; isToken {
return s
}
return fmt.Sprint("token(", int(tt), ")")
}
func (t Token) String() string {
return fmt.Sprintf("{%s %s}", t.tt, t.value)
}
// LookupIdent returns the corresponding token type for an identifier
func LookupIdent(ident string) TokenType {
if tok, isKeyword := keywords[ident]; isKeyword {
return tok
}
return tIllegal
}