-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.go
More file actions
128 lines (119 loc) · 2.74 KB
/
Copy pathtemplate.go
File metadata and controls
128 lines (119 loc) · 2.74 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
package languages
import "bytes"
const goTemplateRule = len(rules) - 1
type templateStream struct {
active, brace, close, found bool
quote byte
escaped, comment, dot bool
previous byte
head [len("template")]byte
headSize int
headDone, named, signature bool
offset uint64
}
func (s *templateStream) write(data []byte, sourceAt func(int) uint64) {
for i := 0; i < len(data) && !s.found; i++ {
b := data[i]
if s.active {
s.action(b)
continue
}
if s.brace && b == '{' {
s.active, s.brace = true, false
continue
}
s.brace = false
at := bytes.IndexByte(data[i:], '{')
if at < 0 {
return
}
i += at
*s = templateStream{brace: true, offset: sourceAt(i)}
}
}
func (s *templateStream) action(b byte) {
defer func() { s.previous = b }()
switch {
case s.comment:
if s.previous == '*' && b == '/' {
s.comment = false
}
case s.quote != 0:
s.quoted(b)
case s.close:
s.found, s.active = b == '}' && s.signature, false
case b == '{':
s.active = false
case b == '}':
s.close = true
default:
s.close = false
s.syntax(b)
}
}
func (s *templateStream) quoted(b byte) {
switch {
case s.escaped:
s.escaped = false
case b == '\\' && s.quote != '`':
s.escaped = true
case b == s.quote:
s.quote = 0
s.signature = s.signature || s.named
}
}
func (s *templateStream) syntax(b byte) {
if s.previous == '/' && b == '*' {
s.comment = true
return
}
if s.dot && (b >= 'A' && b <= 'Z' || b >= 'a' && b <= 'z' || b == '_') {
s.signature = true
}
s.dot = b == '.' && (s.previous == 0 || space(s.previous) || s.previous == '(')
if b == '"' || b == '`' || b == '\'' {
s.quote = b
s.named = s.headDone && (b == '"' || b == '`') && namedTemplateAction(s.head[:s.headSize])
return
}
if s.headDone {
return
}
if space(b) || b == '-' && s.headSize == 0 {
s.headDone = s.headSize > 0
return
}
if s.headSize == len(s.head) {
s.headDone = true
s.head[0] = 0
return
}
s.head[s.headSize] = b
s.headSize++
}
func namedTemplateAction(word []byte) bool {
return bytes.Equal(word, []byte("define")) || bytes.Equal(word, []byte("template")) || bytes.Equal(word, []byte("block"))
}
func (s *templateStream) finish(a *Analysis) {
if !s.found {
return
}
markup := false
var source Set
for _, match := range a.Signals[:a.Count] {
r := match.rule()
if r.weight == declaredWeight {
return
}
if r.languages == NewSet(HTML) || r.languages == NewSet(XML) {
markup = true
} else {
source = source.Union(r.languages)
}
}
if !source.Empty() && (!markup || source.Intersect(jsFamily) != source) {
return
}
a.Signals[a.Count] = Match{Rule: uint16(goTemplateRule), Offset: s.offset}
a.Count++
}