-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlex.go
249 lines (224 loc) · 5.05 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
244
245
246
247
248
249
package main
import (
"strings"
"unicode"
"unicode/utf8"
)
// TokenType represents the type of token.
type TokenType int
const (
TokenText TokenType = iota // Contains plain text of html.
TokenTemplate // Contains template actions.
TokenHTML // Contains HTML tags.
)
// Token represents a token in the input text.
type Token struct {
Type TokenType
Subtype TokenSubtype // If the token is a TokenTemplate, this holds the keyword.
Value string
}
// TokenSubtype describe the deeper type of a token, like what kind of template action or if that html is an open tag or not.
type TokenSubtype int
const (
Pipe TokenSubtype = iota // Pipe is anything in {{ that is not a keyword
Block
Define
Template
Break
Continue
Else
If
Range
With
End
TagOpen
TagClose
)
// Container returns true if the TokenSubType is a container type.
func Container(s TokenSubtype) bool {
switch s {
case Block:
fallthrough
case Define:
fallthrough
// case Else:
// fallthrough
case If:
fallthrough
case Range:
fallthrough
case With:
return true
case TagOpen:
return true
}
return false
}
var Subtypes = map[string]TokenSubtype{
"block": Block,
"define": Define,
"break": Break,
"continue": Continue,
"template": Template,
"else": Else, // 'else with' and 'else if' fall in this category
"end": End,
"if": If,
"range": Range,
"with": With,
}
// Lexer holds the state of the lexer.
type Lexer struct {
input string
start int
pos int
width int
tokens []Token
}
// NewLexer creates a new lexer for the given input.
func NewLexer(input string) *Lexer { return &Lexer{input: input} }
// backup steps back one rune.
func (l *Lexer) backup() { l.pos -= l.width }
// Lex runs the lexer and returns the tokens.
func (l *Lexer) Lex() []Token {
l.lexText()
tokens := l.tokens
return tokens
}
// next returns the next rune in the input.
func (l *Lexer) next() rune {
if l.pos >= len(l.input) {
l.width = 0
return -1
}
r, w := utf8.DecodeRuneInString(l.input[l.pos:])
l.width = w
l.pos += l.width
return r
}
// emit adds a token to the token list.
func (l *Lexer) emit(t TokenType) {
value := l.input[l.start:l.pos]
// some cleanup TokenTemplates, {{<space>thing is reduced to {{thing, execpt for {{-, then it is {{- thing.
// Same for the end, internal whitespace is reduced to a single space. After this we are left with {{-<space>
// (anything else is reject by the go parser) or {{<space>thing, the later is reduced to {{thing. And again also
// at the end.
subtype := Pipe
if t == TokenTemplate {
value = strings.Join(strings.Fields(value), " ")
if strings.HasPrefix(value, "{{ ") {
value = "{{" + strings.TrimPrefix(value, "{{ ")
}
if strings.HasSuffix(value, " }}") {
value = strings.TrimSuffix(value, " }}") + "}}"
}
// beginning is now {{ or {{-, check if we can extract the subtype
Loop:
for s := range Subtypes {
switch {
case strings.HasPrefix(value, "{{"+s):
subtype = Subtypes[s]
break Loop
case strings.HasPrefix(value, "{{- "+s):
subtype = Subtypes[s]
break Loop
}
}
}
defer func() { l.start = l.pos }()
if t == TokenText || t == TokenHTML {
// If the token start with spaces and when trimmed is empty, we skip this token.
if trimmed := strings.TrimLeftFunc(value, unicode.IsSpace); len(trimmed) == 0 {
return
}
// If the token start with a newline we trimleft the value. We do add it to the list.
if strings.HasPrefix(value, "\n") {
value = strings.TrimLeftFunc(value, unicode.IsSpace)
}
// If the remainder contains 1 newline, we trim the whitespace at the end too.
if strings.Count(value, "\n") == 1 {
value = strings.TrimRightFunc(value, unicode.IsSpace)
}
}
if t == TokenHTML {
switch {
case strings.HasPrefix(value, "</"):
subtype = TagClose
case strings.HasSuffix(value, "/>"):
// none
case strings.HasPrefix(value, "<"):
subtype = TagOpen
}
}
l.tokens = append(l.tokens, Token{Type: t, Value: value, Subtype: subtype})
}
// lexText scans plain text until it encounters a template or html tag.
func (l *Lexer) lexText() {
for {
r := l.next()
if r == -1 {
break
}
if r == '{' {
r2 := l.next()
if r2 == '{' {
l.backup()
l.backup()
if l.pos > l.start {
l.emit(TokenText)
}
l.lexTemplate()
continue
}
l.backup()
}
if r == '<' {
r2 := l.next()
if r2 == '/' || unicode.IsLetter(r2) {
l.backup()
l.backup()
if l.pos > l.start {
l.emit(TokenText)
}
l.lexHTML()
continue
}
l.backup()
}
}
if l.pos > l.start {
l.emit(TokenText)
}
}
// lexTemplate scans a template tag.
func (l *Lexer) lexTemplate() {
l.next() // consume '{'
l.next() // consume '{'
for {
r := l.next()
if r == -1 {
break
}
if r == '}' {
r2 := l.next()
if r2 == '}' {
l.emit(TokenTemplate)
break
}
l.backup()
}
}
}
// lexHTML scans an HTML tag.
func (l *Lexer) lexHTML() {
l.next() // consume '<'
for {
r := l.next()
if r == -1 {
break
}
if r == '>' {
l.emit(TokenHTML)
break
}
}
}