-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
61 lines (51 loc) · 1.16 KB
/
Copy pathparser.go
File metadata and controls
61 lines (51 loc) · 1.16 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
package main
type Token struct {
Type TokenType
Content []byte
}
type TokenType byte
const (
Text TokenType = iota
Variable
)
type parsingState byte
const (
OutsideVar parsingState = iota
InsideVar
EscapingBracket
)
func tokens(content []byte) []Token {
tokens := make([]Token, 0)
state := OutsideVar
oldIdx := 0
for i, c := range content {
switch {
case state == OutsideVar && c == '{':
state = InsideVar
tokens = append(tokens, Token{Text, content[oldIdx:i]})
case state == OutsideVar && c == '\\':
state = EscapingBracket
tokens = append(tokens, Token{Text, content[oldIdx:i]})
case state == EscapingBracket && c == '{':
state = OutsideVar
case state == EscapingBracket:
state = OutsideVar
continue
case state == InsideVar && c == '}':
state = OutsideVar
tokens = append(tokens, Token{Variable, content[oldIdx+1 : i]})
oldIdx = i + 1
continue
case state == InsideVar && c == '\n':
state = OutsideVar
tokens = append(tokens, Token{Text, content[oldIdx:i]})
default:
continue
}
oldIdx = i
}
if oldIdx < len(content) {
tokens = append(tokens, Token{Text, content[oldIdx:]})
}
return tokens
}