-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlexer.go
159 lines (133 loc) · 2.61 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
package template
import (
"fmt"
"regexp"
"strings"
)
var yyLexDebug = false //|| true
type exprLex struct {
text string
curLine int
err error
result iAstNode
}
type simpleToken struct {
token string
value int
}
type reToken struct {
re string
value int
}
type compiledReToken struct {
re *regexp.Regexp
value int
}
var simpleTokens = []simpleToken{
{"TEMPLATE", TEMPLATE},
{"WRAPPER", WRAPPER},
{"PROCESS", PROCESS},
{"CONTENT", CONTENT_MARKER},
{"IMPORT", IMPORT},
{"VARS", VARS},
{"ELSE", ELSE},
{"FOR", FOR},
{"END", END},
{"USE", USE},
{"NOT", NOT},
{"AND", AND},
{"OR", OR},
{"IF", IF},
{"IN", IN},
{":=", ASSIGNMENT},
{"==", EQ},
{"!=", NE},
{">=", GE},
{"<=", LE},
{"&&", AND},
{"||", OR},
{"++", INC},
{"--", DEC},
{"!", NOT},
}
var reTokens = []reToken{
{`^(?:\")(?:[^\\\"]*(?:\\.[^\\\"]*)*)(?:\")`, STRING},
{"^(?:`)(?:[^\\`]*(?:\\.[^\\`]*)*)(?:`)", STRING},
{`^-?\d+(?:[.,]\d+)?`, NUMBER},
{`^[a-zA-Z_][a-zA-Z0-9\_]*`, IDENTIFIER},
{`(?s)^/\*.+?\*/`, COMMENT},
}
var compiledReTokens = getCompiledReTokens()
func getCompiledReTokens() []compiledReToken {
result := make([]compiledReToken, len(reTokens))
var err error
for i := range reTokens {
result[i].re, err = regexp.Compile(reTokens[i].re)
if err != nil {
panic(err)
}
result[i].value = reTokens[i].value
}
return result
}
func (x *exprLex) Lex(yylval *yySymType) int {
L1:
for {
if len(x.text) == 0 {
return 0
}
for _, token := range simpleTokens {
if strings.HasPrefix(x.text, token.token) {
x.text = x.text[len(token.token):]
yylval.string = token.token
if yyLexDebug {
fmt.Println("SIMPLE TOKEN ", tokenName(token.value))
}
return token.value
}
}
for _, token := range compiledReTokens {
if m := token.re.FindString(x.text); m != "" {
x.text = x.text[len(m):]
if token.value == COMMENT {
continue L1
}
yylval.string = string(m)
x.curLine += strings.Count(yylval.string, "\n")
if yyLexDebug {
fmt.Println("TOKEN ", tokenName(token.value), yylval.string)
}
return token.value
}
}
c := x.text[0]
x.text = x.text[1:]
switch c {
case '\n':
x.curLine++
continue
case ' ', '\t', '\r':
continue
default:
if yyLexDebug {
fmt.Println("TOKEN ", string(c))
}
return int(c)
}
}
return 0
}
func (x *exprLex) Error(s string) {
x.err = fmt.Errorf("%s at line %d", s, x.curLine+1)
}
func tokenName(c int) string {
if c > yyPrivate {
c -= yyPrivate - 1
}
if c >= 0 && c < len(yyToknames) {
if yyToknames[c] != "" {
return yyToknames[c]
}
}
return fmt.Sprintf("tok-%v", c)
}