Repository navigation
Expand file tree
/
Copy pathscan.go
More file actions
223 lines (197 loc) · 4.4 KB
/
Copy pathscan.go
File metadata and controls
223 lines (197 loc) · 4.4 KB
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
// Package jinja implements a Jinja2-compatible template engine.
package jinja
import "fmt"
// segKind identifies the type of a scanned template segment.
type segKind int
const (
segText segKind = iota // Raw text between tags.
segComment // {# ... #}
segExpr // {{ ... }}
segStmt // {% ... %}
)
// segment represents a single scanned piece of a template.
type segment struct {
kind segKind
text string // Content between delimiters (tags) or raw text.
trimLeft bool // {%- / {{- / {#- was used.
trimRight bool // -%} / -}} / -#} was used.
line int // 1-based line number where the segment starts.
col int // 1-based column number where the segment starts.
}
// scan splits source into a flat slice of segments representing the raw text,
// comments, expressions, and statements that make up a Jinja template.
func scan(source string) ([]segment, error) {
var segs []segment
// Position tracking.
line := 1
col := 1
i := 0
textStart := 0
textLine := 1
textCol := 1
for i < len(source) {
// Look for a two-character tag opener starting with '{'.
if source[i] != '{' || i+1 >= len(source) {
if source[i] == '\n' {
line++
col = 1
} else {
col++
}
i++
continue
}
next := source[i+1]
var kind segKind
var closer string
switch next {
case '{':
kind = segExpr
closer = "}}"
case '%':
kind = segStmt
closer = "%}"
case '#':
kind = segComment
closer = "#}"
default:
if source[i] == '\n' {
line++
col = 1
} else {
col++
}
i++
continue
}
// Emit any accumulated text before this tag.
if i > textStart {
segs = append(segs, segment{
kind: segText,
text: source[textStart:i],
line: textLine,
col: textCol,
})
}
tagLine := line
tagCol := col
// Skip past the two-character opener.
i += 2
col += 2
// Check for the trim flag immediately after the opener.
trimLeft := false
if i < len(source) && source[i] == '-' {
trimLeft = true
i++
col++
}
contentStart := i
// Scan forward for the matching closer, respecting quoted strings.
trimRight := false
found := false
for i < len(source) {
ch := source[i]
// Handle quoted strings so that closers inside them are ignored.
// Comments are raw text until #}, so don't treat quotes specially there.
if kind != segComment && (ch == '\'' || ch == '"') {
quote := ch
if ch == '\n' {
line++
col = 1
} else {
col++
}
i++
for i < len(source) && source[i] != quote {
if source[i] == '\\' && i+1 < len(source) {
// Skip escaped character.
if source[i+1] == '\n' {
line++
col = 1
} else {
col++
}
i++
}
if source[i] == '\n' {
line++
col = 1
} else {
col++
}
i++
}
if i < len(source) {
// Skip closing quote.
col++
i++
}
continue
}
// Check for trim-right flag followed by the closer.
if ch == '-' && i+2 < len(source) && source[i+1] == closer[0] && source[i+2] == closer[1] {
trimRight = true
contentEnd := i
i += 3
col += 3
segs = append(segs, segment{
kind: kind,
text: source[contentStart:contentEnd],
trimLeft: trimLeft,
trimRight: trimRight,
line: tagLine,
col: tagCol,
})
found = true
break
}
// Check for the two-character closer.
if i+1 < len(source) && ch == closer[0] && source[i+1] == closer[1] {
contentEnd := i
i += 2
col += 2
segs = append(segs, segment{
kind: kind,
text: source[contentStart:contentEnd],
trimLeft: trimLeft,
trimRight: trimRight,
line: tagLine,
col: tagCol,
})
found = true
break
}
if ch == '\n' {
line++
col = 1
} else {
col++
}
i++
}
if !found {
tagKind := "expression"
switch kind {
case segStmt:
tagKind = "statement"
case segComment:
tagKind = "comment"
}
return nil, fmt.Errorf("unclosed %s tag at line %d, col %d", tagKind, tagLine, tagCol)
}
// Next text segment starts here.
textStart = i
textLine = line
textCol = col
}
// Emit any trailing text.
if textStart < len(source) {
segs = append(segs, segment{
kind: segText,
text: source[textStart:],
line: textLine,
col: textCol,
})
}
return segs, nil
}