-
Notifications
You must be signed in to change notification settings - Fork 3
/
blocks.go
245 lines (220 loc) · 9.63 KB
/
blocks.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
package zui
import (
"errors"
"slices"
"strings"
"golang.org/x/net/html"
)
type BlockKind int
const (
_ BlockKind = iota
BlockIfStart
BlockIfElseIf
BlockIfElse
BlockIfEnd
BlockEachStart
BlockEachEnd
BlockAwaitStart
BlockAwaitThen
BlockAwaitCatch
BlockAwaitEnd
)
type blockFnStackItem struct {
kind BlockKind
fnName string
deps []string
namePrevParent string
nameSelfParent string
tmpTag string
}
func (me *zui2js) blocknessCheck(src string) (BlockKind, string, error) {
switch {
case !(strings.HasPrefix(src, "#") || strings.HasPrefix(src, ":") || strings.HasPrefix(src, "/")):
return 0, src, nil
case strings.HasPrefix(src, "#if "):
return BlockIfStart, src[len("#if "):], nil
case strings.HasPrefix(src, ":else if "):
return BlockIfElseIf, src[len(":else if "):], nil
case strTrim(src) == ":else":
return BlockIfElse, "", nil
case strTrim(src) == "/if":
return BlockIfEnd, "", nil
case strings.HasPrefix(src, "#each "):
src_js := src[len("#each "):]
lhs, rhs, ok := strings.Cut(src_js, " as ")
if !ok {
return 0, "", errors.New(me.zuiFilePath + ": expected 'as' in `{" + src + "}`")
}
rhs, id, _ := strings.Cut(rhs, "(")
id = strTrim(strings.TrimSuffix(strTrim(id), ")"))
name, idx, _ := strings.Cut(rhs, ",")
if lhs, name, idx = strTrim(lhs), strTrim(name), strTrim(idx); lhs == "" || name == "" {
return 0, "", errors.New(me.zuiFilePath + ": expected identifiers around 'as' in `{" + src + "}`")
}
src_js = "[" + lhs + "," + name + "," + ıf(idx == "", "null", idx) + "," + ıf(id == "", "null", id) + "]"
return BlockEachStart, src_js, nil
case strTrim(src) == "/each":
return BlockEachEnd, "", nil
case strings.HasPrefix(src, "#await "):
return BlockAwaitStart, src[len("#await "):], nil
case strings.HasPrefix(src, ":then "):
return BlockAwaitThen, src[len(":then "):], nil
case strings.HasPrefix(src, ":catch "):
return BlockAwaitCatch, src[len(":catch "):], nil
case strTrim(src) == "/await":
return BlockAwaitEnd, "", nil
}
return 0, "", errors.New(me.zuiFilePath + ": unrecognized block syntax in `{" + src + "}`")
}
func (me *zui2js) blockFnStackHasDep(dep string) bool {
return slices.ContainsFunc(me.blockFnStack, func(it *blockFnStackItem) bool {
return slices.Contains(it.deps, dep)
})
}
func (me *zui2js) blockFragmentEmitJS(jsSrc string, part *htmlTextAndExprsSplitItem, parentNode *html.Node, parentNodeVarName *string) error {
const pref = "\n "
switch part.jsBlockKind {
case BlockAwaitStart:
it := blockFnStackItem{kind: BlockIfStart, tmpTag: jsSrc, fnName: me.nextFnName(), namePrevParent: *parentNodeVarName, deps: part.jsTopLevelRefs}
*parentNodeVarName = me.nextElName()
it.nameSelfParent = *parentNodeVarName
me.WriteString(pref + "const " + it.nameSelfParent + " = newE('zui-wait');")
me.WriteString(pref + "const n_" + it.nameSelfParent + " = [];")
me.WriteString(pref + "const " + it.fnName + " = (async () => { //startWait")
me.blockFnStack = append(me.blockFnStack, &it)
case BlockAwaitThen:
if len(me.blockFnStack) == 0 || me.blockFnStack[len(me.blockFnStack)-1].kind != BlockIfStart {
return errors.New(me.zuiFilePath + ": unmatched `{" + jsSrc + "}`")
}
it := me.blockFnStack[len(me.blockFnStack)-1]
me.WriteString(pref + " " + it.nameSelfParent + ".replaceChildren(...n_" + it.nameSelfParent + ");")
me.WriteString(pref + " n_" + it.nameSelfParent + ".splice(0);")
me.WriteString(pref + " try {")
me.WriteString(pref + " const " + jsSrc + " = await " + it.tmpTag + ";")
case BlockAwaitCatch:
if len(me.blockFnStack) == 0 || me.blockFnStack[len(me.blockFnStack)-1].kind != BlockIfStart {
return errors.New(me.zuiFilePath + ": unmatched `{" + jsSrc + "}`")
}
it := me.blockFnStack[len(me.blockFnStack)-1]
me.WriteString(pref + " " + it.nameSelfParent + ".replaceChildren(...n_" + it.nameSelfParent + ");")
me.WriteString(pref + " n_" + it.nameSelfParent + ".splice(0);")
me.WriteString(pref + " } catch (" + jsSrc + ") {")
case BlockAwaitEnd:
if len(me.blockFnStack) == 0 || me.blockFnStack[len(me.blockFnStack)-1].kind != BlockIfStart {
return errors.New(me.zuiFilePath + ": unmatched `" + jsSrc + "`")
}
it := me.blockFnStack[len(me.blockFnStack)-1]
*parentNodeVarName = it.namePrevParent
me.blockFnStack = me.blockFnStack[:len(me.blockFnStack)-1]
me.WriteString(pref + " " + it.nameSelfParent + ".replaceChildren(...n_" + it.nameSelfParent + ");")
me.WriteString(pref + " n_" + it.nameSelfParent + ".splice(0);")
me.WriteString(pref + " }")
me.WriteString(pref + "}).bind(this); //endWait")
me.WriteString(pref + it.fnName + "();")
for _, dep := range it.deps {
if !me.blockFnStackHasDep(dep) {
me.WriteString(pref + "this.zuiSub(" + strQ(dep) + ", " + it.fnName + ");")
}
}
me.WriteString(pref + "n_" + it.namePrevParent + ".push(" + it.nameSelfParent + ");")
case BlockIfStart: // {#if ...}
it := blockFnStackItem{kind: BlockIfStart, fnName: me.nextFnName(), namePrevParent: *parentNodeVarName, deps: part.jsTopLevelRefs}
*parentNodeVarName = me.nextElName()
it.nameSelfParent = *parentNodeVarName
me.WriteString(pref + "const " + it.nameSelfParent + " = newE('zui-cond');")
me.WriteString(pref + "const n_" + it.nameSelfParent + " = [];")
me.WriteString(pref + "const " + it.fnName + " = (() => { //startCond")
me.WriteString(pref + " if (" + jsSrc + ") {")
me.blockFnStack = append(me.blockFnStack, &it)
case BlockIfElseIf: // {:else if ...}
if len(me.blockFnStack) == 0 || me.blockFnStack[len(me.blockFnStack)-1].kind != BlockIfStart {
return errors.New(me.zuiFilePath + ": unmatched `{" + jsSrc + "}`")
}
it := me.blockFnStack[len(me.blockFnStack)-1]
for _, dep := range part.jsTopLevelRefs {
if !slices.Contains(it.deps, dep) {
it.deps = append(it.deps, dep)
}
}
me.WriteString(pref + " } else if (" + jsSrc + ") {")
case BlockIfElse: // {:else}
if len(me.blockFnStack) == 0 || me.blockFnStack[len(me.blockFnStack)-1].kind != BlockIfStart {
return errors.New(me.zuiFilePath + ": unmatched `" + jsSrc + "`")
}
me.WriteString(pref + " } else {")
case BlockIfEnd: // {/if}
if len(me.blockFnStack) == 0 || me.blockFnStack[len(me.blockFnStack)-1].kind != BlockIfStart {
return errors.New(me.zuiFilePath + ": unmatched `" + jsSrc + "`")
}
it := me.blockFnStack[len(me.blockFnStack)-1]
*parentNodeVarName = it.namePrevParent
me.blockFnStack = me.blockFnStack[:len(me.blockFnStack)-1]
me.WriteString(pref + " }")
me.WriteString(pref + " " + it.nameSelfParent + ".replaceChildren(...n_" + it.nameSelfParent + ");")
me.WriteString(pref + " n_" + it.nameSelfParent + ".splice(0);")
me.WriteString(pref + "}).bind(this); //endCond")
me.WriteString(pref + it.fnName + "();")
for _, dep := range it.deps {
if !me.blockFnStackHasDep(dep) {
me.WriteString(pref + "this.zuiSub(" + strQ(dep) + ", " + it.fnName + ");")
}
}
me.WriteString(pref + "n_" + it.namePrevParent + ".push(" + it.nameSelfParent + ");")
case BlockEachStart: // {#each ...}
names := strings.Split(jsSrc[:len(jsSrc)-1][1:], ",")
assert(len(names) == 4)
for i, name := range names {
names[i] = strTrim(name)
}
loop_arr, loop_item, loop_idx, _ /* loop_id */ := names[0], names[1], names[2], names[3]
has_own_elem := (1 != htmlNumTagNodes(parentNode))
it := blockFnStackItem{kind: BlockEachStart, fnName: me.nextFnName(), namePrevParent: *parentNodeVarName, deps: part.jsTopLevelRefs}
if !has_own_elem {
it.nameSelfParent = *parentNodeVarName
} else {
*parentNodeVarName = me.nextElName()
it.nameSelfParent = *parentNodeVarName
me.WriteString(pref + "const " + it.nameSelfParent + " = newE('zui-loop');")
me.WriteString(pref + "const n_" + it.nameSelfParent + " = [];")
}
me.WriteString(pref + "const " + it.fnName + " = (() => { //startLoop")
assert(jsSrc[0] == '[' && jsSrc[len(jsSrc)-1] == ']')
if loop_idx != "null" {
me.WriteString(pref + " let " + loop_idx + " = -1;")
}
me.WriteString(pref + " n_" + it.nameSelfParent + ".splice(0);")
me.WriteString(pref + " for (const " + loop_item + " of " + loop_arr + ") {")
if loop_idx != "null" {
me.WriteString(pref + " " + loop_idx + "++;")
}
me.blockFnStack = append(me.blockFnStack, &it)
case BlockEachEnd: // {/each}
if len(me.blockFnStack) == 0 || me.blockFnStack[len(me.blockFnStack)-1].kind != BlockEachStart {
return errors.New(me.zuiFilePath + ": unmatched `{" + jsSrc + "}`")
}
it := me.blockFnStack[len(me.blockFnStack)-1]
*parentNodeVarName = it.namePrevParent
me.blockFnStack = me.blockFnStack[:len(me.blockFnStack)-1]
me.WriteString(pref + " }")
me.WriteString(pref + " if (n_" + it.nameSelfParent + ".length != " + it.nameSelfParent + ".childNodes.length)")
me.WriteString(pref + " " + it.nameSelfParent + ".replaceChildren(...n_" + it.nameSelfParent + ");")
me.WriteString(pref + " else")
me.WriteString(pref + " for (let i = 0; i < n_" + it.nameSelfParent + ".length; i++) {")
me.WriteString(pref + " if (!n_" + it.nameSelfParent + "[i].isEqualNode(" + it.nameSelfParent + ".childNodes[i]))")
me.WriteString(pref + " " + it.nameSelfParent + ".replaceChild(n_" + it.nameSelfParent + "[i], " + it.nameSelfParent + ".childNodes[i]);")
me.WriteString(pref + " }")
me.WriteString(pref + "}).bind(this); //endLoop")
me.WriteString(pref + it.fnName + "();")
for _, dep := range it.deps {
if !me.blockFnStackHasDep(dep) {
me.WriteString(pref + "this.zuiSub(" + strQ(dep) + ", " + it.fnName + ");")
}
}
if it.namePrevParent != it.nameSelfParent {
me.WriteString(pref + "n_" + it.namePrevParent + ".push(" + it.nameSelfParent + ");")
}
default:
panic("TODO: " + itoa(int(part.jsBlockKind)))
}
return nil
}