This repository has been archived by the owner on Jun 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
lexfile.go
487 lines (400 loc) · 9.4 KB
/
lexfile.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package main
import (
"bytes"
"fmt"
goast "go/ast"
goparser "go/parser"
goprinter "go/printer"
gotoken "go/token"
"io"
"regexp"
"strings"
)
type LexFile struct {
packageName string
prologue string
actionInline string
startConditions map[string]LexStartCondition
rules []LexRule
epilogue string
}
type LexStartCondition struct {
num int
excl bool
}
type LexRule struct {
startConditions []string
pattern string
trailingPattern string
code string
}
func NewLexFile() *LexFile {
return &LexFile{startConditions: make(map[string]LexStartCondition),
rules: make([]LexRule, 0, 15)}
}
type outf struct {
io.Writer
}
func (w outf) W(s string) {
w.Write([]byte(s))
}
func (w outf) Wf(s string, args ...interface{}) {
w.W(fmt.Sprintf(s, args...))
}
func (lf *LexFile) WriteGo(out io.Writer) {
w := outf{out}
w.Wf("// Generated by golex\npackage %s\n\n", lf.packageName)
w.W(IMPORTS)
w.W(lf.prologue)
w.W(PROLOGUE)
for k, v := range lf.startConditions {
w.Wf("var %s yystartcondition = %d\n", k, v.num)
}
w.W(`var yystartconditionexclmap = map[yystartcondition]bool{`)
for k, v := range lf.startConditions {
w.Wf("%s: %v, ", k, v.excl)
}
w.W("}\n")
w.W(`var yyrules []yyrule = []yyrule{`)
for _, v := range lf.rules {
// fmt.Fprintf(os.Stdout, "compiling __%s__\n", v.pattern)
// m := regexp.MustCompile(v.pattern).FindStringSubmatch("")
// if m != nil {
// fmt.Fprintf(os.Stdout, "WARNING: pattern /%s/ matches empty string \"\"\n", v.pattern)
// }
w.Wf("{regexp.MustCompile(%s), ", quoteRegexp(v.pattern))
if tp := v.trailingPattern; tp != "" {
w.Wf("regexp.MustCompile(%s), ", quoteRegexp(tp))
} else {
w.W("nil, ")
}
w.W("[]yystartcondition{")
for _, sc := range v.startConditions {
if sc == "*" {
w.W("-1, ")
} else {
w.Wf("%s, ", sc)
}
}
w.W("}, ")
if v.pattern[0] == '^' {
w.W("true, ")
} else {
w.W("false, ")
}
w.W(codeToAction(v.code))
w.W("}, ")
}
w.W("}\n")
w.W("func yyactioninline(BEGIN func(yystartcondition)) {" + lf.actionInline + "}\n")
w.W(lf.epilogue)
}
type codeToActionVisitor struct{}
func (ctav *codeToActionVisitor) Visit(node goast.Node) goast.Visitor {
exprs, ok := node.(*goast.ExprStmt)
if ok {
// Transform ECHO, REJECT to yyECHO(), yyREJECT().
rid, rok := exprs.X.(*goast.Ident)
if rok && (rid.Name == "ECHO" || rid.Name == "REJECT") {
rid.Name = "yy" + rid.Name
exprs.X = &goast.CallExpr{Fun: exprs.X,
Args: nil}
}
// Transform BEGIN(...) into yyBEGIN(...).
rcall, rok := exprs.X.(*goast.CallExpr)
if rok {
rident, rok := rcall.Fun.(*goast.Ident)
if rok && rident.Name == "BEGIN" {
rident.Name = "yyBEGIN"
}
}
return ctav
}
// Transform 'return 1' into 'return yyactionreturn{1, yyRT_USER_RETURN}'. Take special
// effort not to touch existing 'return yyactionreturn{...}' statements.
retstmt, ok := node.(*goast.ReturnStmt)
if ok {
if len(retstmt.Results) == 1 {
r := retstmt.Results[0]
_, ok := r.(*goast.CompositeLit)
if !ok {
// Wrap it.
retstmt.Results[0] = &goast.CompositeLit{Type: &goast.Ident{Name: "yyactionreturn"},
Elts: []goast.Expr{r, &goast.Ident{Name: "yyRT_USER_RETURN"}}}
}
}
}
return ctav
}
func codeToAction(code string) string {
fs := gotoken.NewFileSet()
newCode := `
func() (yyar yyactionreturn) {
defer func() {
if r := recover(); r != nil {
if r != "yyREJECT" {
panic(r)
}
yyar.returnType = yyRT_REJECT
}
}()
` + code + `;
return yyactionreturn{0, yyRT_FALLTHROUGH}
}`
expr, _ := goparser.ParseExpr(newCode)
fexp := expr.(*goast.FuncLit)
ctav := &codeToActionVisitor{}
goast.Walk(ctav, fexp)
result := bytes.NewBuffer(make([]byte, 0, len(code)*2))
goprinter.Fprint(result, fs, fexp)
return result.String()
}
func isOctalDigit(d uint8) bool {
return d >= '0' && d <= '7'
}
func isHexDigit(d uint8) bool {
return (d >= '0' && d <= '9') || (d >= 'a' && d <= 'f') || (d >= 'A' && d <= 'F')
}
func formatMetaChar(d int) string {
if d < 32 {
return fmt.Sprintf("\\x%02x", d)
}
return strings.Replace(strings.Replace(regexp.QuoteMeta(string(d)), "\\", "\\\\", -1), "\"", "\\\"", -1)
}
// quoteRegexp prepares a regular expression for insertion into a Go source
// as a string suitable for use as argument to regexp.(Must)?Compile.
func quoteRegexp(re string) (out string) {
out = "\""
skip := 0
for i, c := range re {
if skip > 0 {
skip--
continue
}
switch c {
case '"':
out += "\\\""
case '\\':
if len(re) == i+1 {
// This is the last character.
out += "\\\\"
} else if isOctalDigit(re[i+1]) {
// The next character is an octal digit.
if len(re) >= i+4 && isOctalDigit(re[i+2]) && isOctalDigit(re[i+3]) {
// .. and there two more octal digits beyond that.
var oct int
fmt.Sscanf(re[i+1:i+4], "%o", &oct)
out += formatMetaChar(oct)
skip += 3
} else {
// There's no valid 3-digit octal here ..
if re[i+1] == '0' {
// .. so treat the leading \0 as a NUL.
out += "\\000"
skip++
} else {
// .. so escape the leading \.
out += "\\\\"
}
}
} else if re[i+1] == 'x' || re[i+1] == 'X' {
// The next character ~ [xX].
if len(re) >= i+4 && isHexDigit(re[i+2]) && isHexDigit(re[i+3]) {
// .. and there are two hex digits beyond that.
var hex int
fmt.Sscanf(re[i+2:i+4], "%x", &hex)
out += formatMetaChar(hex)
skip += 3
} else {
// There's no valid hex sequence here.
out += "\\\\"
}
} else if re[i+1] == '\\' {
out += "\\\\\\\\"
skip++
} else {
out += "\\\\"
}
default:
out += string(c)
}
}
out += "\""
return
}
const IMPORTS = `
import (
"bufio"
"io"
"os"
"regexp"
"sort"
)
`
const PROLOGUE = `
var yyin io.Reader = os.Stdin
var yyout io.Writer = os.Stdout
type yyrule struct {
regexp *regexp.Regexp
trailing *regexp.Regexp
startConds []yystartcondition
sol bool
action func() yyactionreturn
}
type yyactionreturn struct {
userReturn int
returnType yyactionreturntype
}
type yyactionreturntype int
const (
yyRT_FALLTHROUGH yyactionreturntype = iota
yyRT_USER_RETURN
yyRT_REJECT
)
var yydata string = ""
var yyorig string
var yyorigidx int
var yytext string = ""
var yytextrepl bool = true
func yymore() {
yytextrepl = false
}
func yyBEGIN(state yystartcondition) {
YY_START = state
}
func yyECHO() {
yyout.Write([]byte(yytext))
}
func yyREJECT() {
panic("yyREJECT")
}
var yylessed int
func yyless(n int) {
yylessed = len(yytext) - n
}
func unput(c uint8) {
yyorig = yyorig[:yyorigidx] + string(c) + yyorig[yyorigidx:]
yydata = yydata[:len(yytext)-yylessed] + string(c) + yydata[len(yytext)-yylessed:]
}
func input() int {
if len(yyorig) <= yyorigidx {
return EOF
}
c := yyorig[yyorigidx]
yyorig = yyorig[:yyorigidx] + yyorig[yyorigidx+1:]
yydata = yydata[:len(yytext)-yylessed] + yydata[len(yytext)-yylessed+1:]
return int(c)
}
var EOF int = -1
type yystartcondition int
var INITIAL yystartcondition = 0
var YY_START yystartcondition = INITIAL
type yylexMatch struct {
index int
matchFunc func() yyactionreturn
sortLen int
advLen int
}
type yylexMatchList []yylexMatch
func (ml yylexMatchList) Len() int {
return len(ml)
}
func (ml yylexMatchList) Less(i, j int) bool {
return ml[i].sortLen > ml[j].sortLen && ml[i].index > ml[j].index
}
func (ml yylexMatchList) Swap(i, j int) {
ml[i], ml[j] = ml[j], ml[i]
}
func yylex() int {
reader := bufio.NewReader(yyin)
for {
line, err := reader.ReadString('\n')
if len(line) == 0 && err == io.EOF {
break
}
yydata += line
}
yyorig = yydata
yyorigidx = 0
yyactioninline(yyBEGIN)
for len(yydata) > 0 {
matches := yylexMatchList(make([]yylexMatch, 0, 6))
excl := yystartconditionexclmap[YY_START]
for i, v := range yyrules {
sol := yyorigidx == 0 || yyorig[yyorigidx-1] == '\n'
if v.sol && !sol {
continue
}
// Check start conditions.
ok := false
// YY_START or '*' must feature in v.startConds
for _, c := range v.startConds {
if c == YY_START || c == -1 {
ok = true
break
}
}
if !excl {
// If v.startConds is empty, this is also acceptable.
if len(v.startConds) == 0 {
ok = true
}
}
if !ok {
continue
}
idxs := v.regexp.FindStringIndex(yydata)
if idxs != nil && idxs[0] == 0 {
// Check the trailing context, if any.
checksOk := true
sortLen := idxs[1]
advLen := idxs[1]
if v.trailing != nil {
tridxs := v.trailing.FindStringIndex(yydata[idxs[1]:])
if tridxs == nil || tridxs[0] != 0 {
checksOk = false
} else {
sortLen += tridxs[1]
}
}
if checksOk {
matches = append(matches, yylexMatch{i, v.action, sortLen, advLen})
}
}
}
if yytextrepl {
yytext = ""
}
sort.Sort(matches)
tryMatch:
if len(matches) == 0 {
yytext += yydata[:1]
yydata = yydata[1:]
yyorigidx += 1
yyout.Write([]byte(yytext))
} else {
m := matches[0]
yytext += yydata[:m.advLen]
yyorigidx += m.advLen
yytextrepl, yylessed = true, 0
ar := m.matchFunc()
if ar.returnType != yyRT_REJECT {
yydata = yydata[m.advLen-yylessed:]
yyorigidx -= yylessed
}
switch ar.returnType {
case yyRT_FALLTHROUGH:
// Do nothing.
case yyRT_USER_RETURN:
return ar.userReturn
case yyRT_REJECT:
matches = matches[1:]
yytext = yytext[:len(yytext)-m.advLen]
yyorigidx -= m.advLen
goto tryMatch
}
}
}
return 0
}
`