-
Notifications
You must be signed in to change notification settings - Fork 2
/
lexer.go
209 lines (175 loc) · 4.8 KB
/
lexer.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
package dockerlang
import (
"fmt"
"io"
"text/scanner"
"unicode"
)
func (c *Compterpreter) Lex() error {
// starting off from the beginning of the source file we will
// always first advance to the first character.
c.Advance()
for {
token, err := c.GetNextToken()
// gracefully catch end of file
switch {
case err == io.EOF:
return nil
case err == nil:
c.Tokens = append(c.Tokens, token)
continue
case err != nil:
return err
}
}
}
func (c *Compterpreter) GetNextToken() (Token, error) {
var err error
// we must clear the CurrentToken each time we get the next token!
c.CurrentToken = Token{}
// we are looping since there are characters we may want to ignore
// for example, whitespace or something.
for {
switch {
case c.IsWhitespace(c.CurrentChar):
// igfnore non-linebreak whitespace
err = c.TokenizeWhitespace(c.CurrentChar)
switch {
case err == io.EOF:
return c.CurrentToken, err
case err == TrivialWhitespaceError:
continue
}
case c.IsOperator(c.CurrentChar):
// something
err = c.TokenizeOperator(c.CurrentChar)
case c.IsNumber(c.CurrentChar):
// get full multidigit number token
err = c.TokenizeNumber(c.CurrentChar)
case c.IsIdentifierFirstSymbol(c.CurrentChar):
// is it a keyword?
// is it a function/variable identifier?
err = c.TokenizeIdentifier(c.CurrentChar)
case c.IsPunctuation(c.CurrentChar):
err = c.TokenizePunctuation(c.CurrentChar)
default:
// we've encountered something very unexpected!
// i'd like to panic, but i'm gunna keep my kewl
return Token{}, fmt.Errorf(
"sry, but ive NO IDEA wut this char is: %s. Try typing another one(?)",
string(c.CurrentChar),
)
}
// if we ever get here, we have gotten the next token
// and the CurrentChar should be pointing to the next
// character we want to start tokenizing!
break
}
// currently just returning the CurrentToken (what we just tokenized) and
// assuming that the caller is appending the token to an array of seen tokens
return c.CurrentToken, err
}
func (c *Compterpreter) IsWhitespace(r rune) bool {
return unicode.IsSpace(r)
}
func (c *Compterpreter) IsNumber(r rune) bool {
return unicode.IsDigit(r)
}
func (c *Compterpreter) IsIdentifierFirstSymbol(r rune) bool {
return VALID_IDENTIFIER_FIRST_SYMBOL.MatchString(string(r))
}
func (c *Compterpreter) IsIdentifier(r rune) bool {
return VALID_IDENTIFIER_SYMBOL.MatchString(string(r))
}
func (c *Compterpreter) IsOperator(r rune) bool {
for _, symbol := range c.Symbols.Operators {
if string(r) == symbol {
return true
}
}
return false
}
func (c *Compterpreter) IsPunctuation(r rune) bool {
for _, symbol := range c.Symbols.Punctuation {
if string(r) == symbol {
return true
}
}
return false
}
func (c *Compterpreter) TokenizeWhitespace(r rune) error {
if r == '\n' {
c.CurrentToken.Value = string(r)
c.CurrentToken.Type = PUNCTUATION
if err := c.Advance(); err != nil {
return err
}
return nil
}
if err := c.Advance(); err != nil {
return err
}
return TrivialWhitespaceError
}
func (c *Compterpreter) TokenizeNumber(r rune) error {
c.CurrentToken.Type = INT
c.CurrentToken.Value = c.CurrentToken.Value + string(r)
// check to see if we need to include the next character in the
// current token
if err := c.Advance(); err != nil {
return err
}
if c.IsNumber(c.CurrentChar) {
c.TokenizeNumber(c.CurrentChar)
}
return nil
}
func (c *Compterpreter) TokenizeIdentifier(r rune) error {
c.CurrentToken.Type = IDENTIFIER
c.CurrentToken.Value = c.CurrentToken.Value + string(r)
// check to see if we need to include the next character in the
// current token
if err := c.Advance(); err != nil {
return err
}
if c.IsIdentifier(c.CurrentChar) {
c.TokenizeIdentifier(c.CurrentChar)
}
// at this point, we have our current token, but we want to
// check whether it is a keyword of an identifier
for _, keyword := range c.Symbols.Keywords {
if c.CurrentToken.Value == keyword {
c.CurrentToken.Type = KEYWORD
}
}
return nil
}
func (c *Compterpreter) TokenizeOperator(r rune) error {
c.CurrentToken.Type = OPERATOR
c.CurrentToken.Value = c.CurrentToken.Value + string(r)
if err := c.Advance(); err != nil {
return err
}
// TODO: for a bright future which containts multi-symbol operators
//if c.IsOperator(c.CurrentChar) {
// // check if the proposed multi-symbol operator is valid
// // if it's not, it's two operators next to each other
// c.TokenizeOperator(c.CurrentChar)
//}
return nil
}
func (c *Compterpreter) TokenizePunctuation(r rune) error {
c.CurrentToken.Type = PUNCTUATION
c.CurrentToken.Value = string(r)
if err := c.Advance(); err != nil {
return err
}
return nil
}
func (c *Compterpreter) Advance() error {
c.CurrentChar = c.Scanner.Next()
if c.CurrentChar == scanner.EOF {
return io.EOF
}
return nil
}