-
Notifications
You must be signed in to change notification settings - Fork 2
/
token.go
161 lines (128 loc) · 2.54 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
package mui
import (
"encoding/json"
"fmt"
)
type TokenType int
const EOF = rune(0)
const (
Identifier = iota
OpenParenthesis
CloseParenthesis
String
Colon
NewLine
Tab
Space
Comma
)
type Token struct {
TokenType TokenType
Value string
Idx int
nextToken *Token
previousToken *Token
}
func (token *Token) ResolveType() string {
var typeStr string
switch token.TokenType {
case Identifier:
typeStr = "identifier"
case OpenParenthesis:
typeStr = "OpenParenthesis"
case CloseParenthesis:
typeStr = "CloseParenthesis"
case String:
typeStr = "String"
case Colon:
typeStr = "Colon"
case NewLine:
typeStr = "NewLine"
case Tab:
typeStr = "Tab"
case Space:
typeStr = "Space"
case Comma:
typeStr = "Comma"
}
return typeStr
}
func (token *Token) Is(tokenType TokenType) bool {
return token.TokenType == tokenType
}
func (token *Token) IsWhiteSpace() bool {
return token.TokenType == NewLine || token.TokenType == Tab || token.TokenType == Space
}
func (token *Token) HasNextToken() bool {
if token == nil {
return false
}
return token.nextToken != nil
}
func (token *Token) HasPreviousToken() bool {
return token.previousToken != nil
}
func (token *Token) NextNonWhitespaceToken() *Token {
cToken := token.nextToken
for cToken != nil {
if !cToken.IsWhiteSpace() {
return cToken
}
if cToken.HasNextToken() {
cToken = cToken.nextToken
}
}
return nil
}
func (token *Token) PreviousNonWhitespaceToken() *Token {
cToken := token.previousToken
for cToken != nil {
if !cToken.IsWhiteSpace() {
return cToken
}
if cToken.HasPreviousToken() {
cToken = cToken.previousToken
}
}
return nil
}
func (token *Token) NextToken() *Token {
return token.nextToken
}
func (token *Token) IsExpectedAfter(expected *Token) bool {
switch expected.TokenType {
case Identifier:
if token.TokenType == Colon || token.TokenType == OpenParenthesis {
return true
}
break
}
return false
}
func (token *Token) AsJSON() string {
res, err := json.Marshal(token)
if err != nil {
fmt.Println(err)
return "{}"
}
return string(res)
}
type TokenList struct {
Tokens []*Token
tokenIdx int
tokenListLen int
}
func CreateTokenList() *TokenList {
var tokens []*Token
return &TokenList{Tokens: tokens}
}
func (tokenList *TokenList) Push(token *Token) {
tokenList.Tokens = append(tokenList.Tokens, token)
tokenList.tokenListLen = len(tokenList.Tokens)
}
func (tokenList *TokenList) HasToken() bool {
if tokenList.tokenIdx == tokenList.tokenListLen {
return false
}
return true
}