This repository has been archived by the owner on Mar 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlex.go
243 lines (222 loc) · 4.92 KB
/
lex.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package arnoldc
import (
"bytes"
"fmt"
"io"
"os"
"strconv"
"strings"
"unicode"
)
//go:generate goyacc -c -cr -o parser.go parser.y
type ArnoldC struct {
input io.ReadSeeker
program Program // Our result object
err error // The last error to come out of the parser
Debug bool // Print debugging messages
// State
scannedInstruction bool
// Where are we in out input?
line int
offset int
currentByte byte
// TODO: Keep the current line in memory so we can add it to errors?
}
func New(input io.ReadSeeker) *ArnoldC {
return &ArnoldC{input: input, line: 1, Debug: false}
}
func (a *ArnoldC) Parse() (*Program, error) {
_ = yyParse(a)
if a.err != nil {
return nil, a.err
}
return &a.program, nil
}
func (a *ArnoldC) log(format string, args ...interface{}) {
if a.Debug {
fmt.Fprintf(os.Stderr, "Lexer: "+format+"\n", args...)
}
}
// Error satisfies yyLexer.
func (a *ArnoldC) Error(s string) {
a.err = fmt.Errorf("%s on line %d at character %d, %q", s, a.line, a.offset, a.currentByte)
}
// Lex satisfies yyLexer.
func (a *ArnoldC) Lex(lval *yySymType) int {
if !a.scannedInstruction {
return a.scanInstruction(lval)
}
return a.scanNormal(lval)
}
// Gets the next byte in the input.
func (a *ArnoldC) next() byte {
// TODO: Optimize this buffer creation.
// TODO: We can also scan like, 10 chars and walk them to eat up space and stuff.
var b []byte = make([]byte, 1)
_, err := a.input.Read(b)
if err != nil {
if err == io.EOF {
a.log("!! EOF")
}
// TODO: Set the non-EOF error contents somewhere?
return 0
}
a.offset++
a.currentByte = b[0]
return b[0]
}
// Seek input backwards one byte.
func (a *ArnoldC) backup() error {
a.log("<- backup")
_, err := a.input.Seek(-1, io.SeekCurrent)
if err == nil {
a.offset--
a.currentByte = 0
}
return err
}
func (a *ArnoldC) scanInstruction(lval *yySymType) int {
a.log("> scanInstruction")
buf := bytes.NewBuffer(nil)
for b := a.next(); ; b = a.next() {
a.log("%q %d", b, b)
switch {
case (unicode.IsUpper(rune(b)) || b == '\'' || b == ',' || unicode.IsSpace(rune(b))) && b != '\n':
buf.WriteByte(b)
default:
lval.str = strings.TrimSpace(buf.String())
if len(lval.str) == 0 {
if b == 0 {
return 0
}
continue
}
a.log("str = %q", lval.str)
if b != 0 {
a.backup()
}
a.scannedInstruction = true
tk, ok := instructions[lval.str]
if !ok {
a.log("!! Unknown Instruction")
// TODO: Store error.
return LexError
}
if '\n' == b {
a.line++
a.offset = 0
}
return tk
}
}
}
func (a *ArnoldC) scanNormal(lval *yySymType) int {
a.log("> scanNormal")
for b := a.next(); b != 0; b = a.next() {
a.log("%q %d", b, b)
switch {
case '\n' == b:
a.line++
a.offset = 0
a.scannedInstruction = false
return a.scanInstruction(lval)
case unicode.IsSpace(rune(b)):
continue
case b == '@':
return a.scanMacro(lval)
case b == '"':
return a.scanString(lval)
case unicode.IsDigit(rune(b)) || b == '-':
a.backup()
return a.scanInteger(lval)
case unicode.IsLetter(rune(b)):
a.backup()
return a.scanVariable(lval)
default:
return int(b)
}
}
return 0
}
func (a *ArnoldC) scanString(lval *yySymType) int {
a.log("> scanString")
buf := bytes.NewBuffer(nil)
for b := a.next(); b != 0; b = a.next() {
a.log("%q %d", b, b)
switch b {
case '"':
lval.str = buf.String()
a.log("str = %q", lval.str)
return String
default:
buf.WriteByte(b)
}
}
return LexError
}
func (a *ArnoldC) scanVariable(lval *yySymType) int {
a.log("> scanVariable")
buf := bytes.NewBuffer(nil)
for b := a.next(); ; b = a.next() {
a.log("%q %d", b, b)
switch {
case unicode.IsLetter(rune(b)) || unicode.IsDigit(rune(b)):
buf.WriteByte(b)
case b == '\n':
a.backup()
fallthrough
default:
lval.str = buf.String()
a.log("str = %q", lval.str)
return Variable
}
}
}
func (a *ArnoldC) scanMacro(lval *yySymType) int {
a.log("> scanMacro")
buf := bytes.NewBuffer(nil)
for b := a.next(); ; b = a.next() {
a.log("%q %d", b, b)
switch {
case unicode.IsLetter(rune(b)) || b == ' ':
buf.WriteByte(b)
case b == '\n':
a.backup()
fallthrough
default:
lval.str = buf.String()
a.log("str = %q", lval.str)
tk, ok := macros[lval.str]
if !ok {
a.log("!! Unknown Macro \"@%s\"", lval.str)
// TODO: Store error.
return LexError
}
return tk
}
}
}
func (a *ArnoldC) scanInteger(lval *yySymType) int {
a.log("> scanInteger")
buf := bytes.NewBuffer(nil)
for b := a.next(); ; b = a.next() {
a.log("%q %d", b, b)
// TODO: - should only be allowed for the first byte
if unicode.IsDigit(rune(b)) || b == '-' {
buf.WriteByte(b)
} else {
if 0 != b {
a.backup()
}
val, err := strconv.Atoi(buf.String())
if err != nil {
a.log("!! Invalid Integer")
// TODO: Store error.
return LexError
}
lval.integer = val
a.log("integer = %d", lval.integer)
return Integer
}
}
}