bobe 0.0.10 → 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bobe.cjs.js +578 -149
- package/dist/bobe.cjs.js.map +1 -1
- package/dist/bobe.esm.js +573 -150
- package/dist/bobe.esm.js.map +1 -1
- package/dist/index.d.ts +96 -99
- package/dist/index.umd.js +578 -149
- package/dist/index.umd.js.map +1 -1
- package/package.json +3 -3
package/dist/bobe.cjs.js
CHANGED
|
@@ -30,27 +30,27 @@ var TerpEvt = /* @__PURE__ */ ((TerpEvt2) => {
|
|
|
30
30
|
return TerpEvt2;
|
|
31
31
|
})(TerpEvt || {});
|
|
32
32
|
|
|
33
|
-
var __defProp = Object.defineProperty;
|
|
34
|
-
var __defProps = Object.defineProperties;
|
|
35
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
36
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
37
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
38
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
39
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
40
|
-
var __spreadValues = (a, b) => {
|
|
33
|
+
var __defProp$1 = Object.defineProperty;
|
|
34
|
+
var __defProps$1 = Object.defineProperties;
|
|
35
|
+
var __getOwnPropDescs$1 = Object.getOwnPropertyDescriptors;
|
|
36
|
+
var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
|
|
37
|
+
var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
|
|
38
|
+
var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
|
|
39
|
+
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
40
|
+
var __spreadValues$1 = (a, b) => {
|
|
41
41
|
for (var prop in b || (b = {}))
|
|
42
|
-
if (__hasOwnProp.call(b, prop))
|
|
43
|
-
__defNormalProp(a, prop, b[prop]);
|
|
44
|
-
if (__getOwnPropSymbols)
|
|
45
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
46
|
-
if (__propIsEnum.call(b, prop))
|
|
47
|
-
__defNormalProp(a, prop, b[prop]);
|
|
42
|
+
if (__hasOwnProp$1.call(b, prop))
|
|
43
|
+
__defNormalProp$1(a, prop, b[prop]);
|
|
44
|
+
if (__getOwnPropSymbols$1)
|
|
45
|
+
for (var prop of __getOwnPropSymbols$1(b)) {
|
|
46
|
+
if (__propIsEnum$1.call(b, prop))
|
|
47
|
+
__defNormalProp$1(a, prop, b[prop]);
|
|
48
48
|
}
|
|
49
49
|
return a;
|
|
50
50
|
};
|
|
51
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
51
|
+
var __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b));
|
|
52
52
|
const tap = new bobeShared.BaseEvent();
|
|
53
|
-
class
|
|
53
|
+
class Interpreter {
|
|
54
54
|
constructor(tokenizer) {
|
|
55
55
|
this.tokenizer = tokenizer;
|
|
56
56
|
/** 模板字符串动态节点的占位符 */
|
|
@@ -59,7 +59,6 @@ class Terp {
|
|
|
59
59
|
this.data = {};
|
|
60
60
|
/** 模板字符串动态节点索引 */
|
|
61
61
|
this.hookI = 0;
|
|
62
|
-
this.stack = [];
|
|
63
62
|
this._hook = (props) => {
|
|
64
63
|
const value = this.tokenizer.token.value;
|
|
65
64
|
const isDynamicHook = this.tokenizer.token.type & TokenType.InsertionExp;
|
|
@@ -67,7 +66,7 @@ class Terp {
|
|
|
67
66
|
const hookType = isDynamicHook ? "dynamic" : isStaticHook ? "static" : void 0;
|
|
68
67
|
if (this.hook && isStaticHook) {
|
|
69
68
|
const hookI = Number(value.slice(this.HookId.length));
|
|
70
|
-
const res = this.hook(__spreadProps(__spreadValues({}, props), {
|
|
69
|
+
const res = this.hook(__spreadProps$1(__spreadValues$1({}, props), {
|
|
71
70
|
HookId: this.HookId,
|
|
72
71
|
i: hookI
|
|
73
72
|
}));
|
|
@@ -83,119 +82,89 @@ class Terp {
|
|
|
83
82
|
// root: any;
|
|
84
83
|
// /** program 挂载的前置节点 */
|
|
85
84
|
// anchor: any;
|
|
86
|
-
program() {
|
|
87
|
-
const
|
|
85
|
+
program(root, before) {
|
|
86
|
+
const componentNode = {
|
|
88
87
|
__logicType: LogicType.Component,
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
directList: []
|
|
88
|
+
realParent: root,
|
|
89
|
+
store: new aoye.Store()
|
|
92
90
|
};
|
|
93
91
|
this.tokenizer.consume();
|
|
92
|
+
let prevSibling = null;
|
|
94
93
|
let current;
|
|
95
|
-
|
|
96
|
-
this.stack = [{ node: root, prevSibling }];
|
|
94
|
+
const stack = [{ node: root, prev: prevSibling }];
|
|
97
95
|
while (1) {
|
|
98
96
|
if (this.tokenizer.isEof()) {
|
|
99
|
-
this.handleInsert(root, current,
|
|
97
|
+
this.handleInsert(root, current, prevSibling, componentNode);
|
|
100
98
|
break;
|
|
101
99
|
}
|
|
102
100
|
const token = this.tokenizer.token;
|
|
103
101
|
if (token.type & TokenType.Indent) {
|
|
104
102
|
this.tokenizer.consume();
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
103
|
+
stack.push({
|
|
104
|
+
node: current,
|
|
105
|
+
prev: prevSibling
|
|
108
106
|
});
|
|
107
|
+
current = this.declaration({ stack, prevSibling });
|
|
109
108
|
prevSibling = null;
|
|
110
|
-
current = this.declaration();
|
|
111
109
|
continue;
|
|
112
110
|
}
|
|
113
111
|
if (current) {
|
|
114
|
-
if (
|
|
115
|
-
const parent =
|
|
112
|
+
if (stack.length > 1) {
|
|
113
|
+
const { node: parent } = stack[stack.length - 1];
|
|
116
114
|
this.handleInsert(parent, current, prevSibling);
|
|
117
115
|
} else {
|
|
118
|
-
|
|
116
|
+
if (!prevSibling) {
|
|
117
|
+
prevSibling = before;
|
|
118
|
+
}
|
|
119
|
+
this.handleInsert(root, current, prevSibling, componentNode);
|
|
119
120
|
}
|
|
120
121
|
}
|
|
121
122
|
if (this.tokenizer.token.type & TokenType.Dedent) {
|
|
122
123
|
this.tokenizer.consume();
|
|
123
|
-
const { node: parent,
|
|
124
|
-
prevSibling =
|
|
124
|
+
const { node: parent, prev } = stack.pop();
|
|
125
|
+
prevSibling = prev;
|
|
125
126
|
current = parent;
|
|
126
127
|
} else {
|
|
127
128
|
prevSibling = current;
|
|
128
|
-
current = this.declaration();
|
|
129
|
+
current = this.declaration({ stack, prevSibling });
|
|
129
130
|
}
|
|
130
131
|
}
|
|
131
|
-
|
|
132
|
+
componentNode.lastInserted = this.lastInserted;
|
|
133
|
+
this.lastInserted = null;
|
|
134
|
+
return componentNode;
|
|
132
135
|
}
|
|
133
136
|
/** 处理
|
|
134
|
-
*
|
|
135
|
-
* 父节点
|
|
136
|
-
* 子节点
|
|
137
|
+
* 是逻辑 是普通
|
|
138
|
+
* 父节点 将子节点加入 directList 调用 insert 方法挨个插入子节点
|
|
139
|
+
* 子节点 仅插入到父逻辑节点 将本节点插入父节点
|
|
140
|
+
* 理论上父节点不能是一个 逻辑节点,遇到if 时 Terp 会重新执行 program 这种情况下,会指定 root 为真实 dom
|
|
137
141
|
*/
|
|
138
|
-
handleInsert(parent, child,
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
const realList = child.realList;
|
|
147
|
-
for (let i = realList.length; i--; ) {
|
|
148
|
-
const item = realList[i];
|
|
149
|
-
insertFn(parent, item, prevSibling);
|
|
142
|
+
handleInsert(parent, child, prev, parentComponent) {
|
|
143
|
+
if (!child.__logicType) {
|
|
144
|
+
if (!prev || !prev.__logicType) {
|
|
145
|
+
this.insertAfter(parent, child, prev);
|
|
146
|
+
} else {
|
|
147
|
+
const before = prev.lastInserted;
|
|
148
|
+
this.insertAfter(parent, child, before);
|
|
149
|
+
prev.realAfter = child;
|
|
150
150
|
}
|
|
151
151
|
} else {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
return;
|
|
169
|
-
}
|
|
170
|
-
if (this.tokenizer.token.type & TokenType.Dedent) {
|
|
171
|
-
this.tokenizer.consume();
|
|
172
|
-
return;
|
|
173
|
-
}
|
|
174
|
-
_node = this.node();
|
|
175
|
-
const insert = parent.__logicType ? this.defaultInsert : this.insert.bind(this);
|
|
176
|
-
parent.__logicType ? this.defaultRemove : this.remove.bind(this);
|
|
177
|
-
if (!_node.__logicType) {
|
|
178
|
-
const realPrev = this.getPrevRealSibling(prevSibling);
|
|
179
|
-
const currItem = insert(parent, _node, realPrev, prevItem);
|
|
180
|
-
prevItem = currItem;
|
|
181
|
-
prevSibling = _node;
|
|
182
|
-
continue;
|
|
183
|
-
}
|
|
184
|
-
if (prevSibling) {
|
|
185
|
-
_node.anchor = prevSibling;
|
|
186
|
-
} else if (parent.__logicType) {
|
|
187
|
-
_node.anchor = parent;
|
|
188
|
-
} else ;
|
|
189
|
-
if (_node.child && _node.condition()) {
|
|
190
|
-
let item = _node.child;
|
|
191
|
-
while (item != null) {
|
|
192
|
-
const { value: child } = item;
|
|
193
|
-
const realPrev = this.getPrevRealSibling(prevSibling);
|
|
194
|
-
const currItem = insert(parent, child, realPrev, prevItem);
|
|
195
|
-
item = item.next;
|
|
196
|
-
prevItem = currItem;
|
|
197
|
-
prevSibling = child;
|
|
198
|
-
}
|
|
152
|
+
const childCmp = child;
|
|
153
|
+
childCmp.realParent = parent;
|
|
154
|
+
if (!prev) {
|
|
155
|
+
const anchor = this.createAnchor();
|
|
156
|
+
this.insertAfter(parent, anchor, prev);
|
|
157
|
+
this.insertAfter(parent, child, anchor);
|
|
158
|
+
childCmp.realBefore = anchor;
|
|
159
|
+
} else if (prev.__logicType) {
|
|
160
|
+
const before = prev.lastInserted;
|
|
161
|
+
const anchor = this.createAnchor();
|
|
162
|
+
this.insertAfter(parent, anchor, before);
|
|
163
|
+
this.insertAfter(parent, child, anchor);
|
|
164
|
+
childCmp.realBefore = anchor;
|
|
165
|
+
prev.realAfter = anchor;
|
|
166
|
+
} else {
|
|
167
|
+
childCmp.realBefore = prev;
|
|
199
168
|
}
|
|
200
169
|
}
|
|
201
170
|
}
|
|
@@ -212,25 +181,12 @@ class Terp {
|
|
|
212
181
|
point = point.anchor;
|
|
213
182
|
}
|
|
214
183
|
}
|
|
215
|
-
/**
|
|
216
|
-
* 单个节点:
|
|
217
|
-
* 由声明部分和(可选的)子节点块组成
|
|
218
|
-
* <node> ::= <declaration> <childrenBlockOpt>
|
|
219
|
-
* */
|
|
220
|
-
node() {
|
|
221
|
-
const _declaration = this.declaration();
|
|
222
|
-
if (_declaration.__logicType & LogicType.If && !_declaration.condition()) {
|
|
223
|
-
return _declaration;
|
|
224
|
-
}
|
|
225
|
-
this.childrenBlockOpt(_declaration);
|
|
226
|
-
return _declaration;
|
|
227
|
-
}
|
|
228
184
|
/**
|
|
229
185
|
* 声明部分:
|
|
230
186
|
* 包含首行定义和(可选的)多行属性扩展
|
|
231
187
|
* <declaration> ::= <tagName=token> <headerLine> <extensionLines>
|
|
232
188
|
* */
|
|
233
|
-
declaration() {
|
|
189
|
+
declaration(ctx) {
|
|
234
190
|
const [hookType, value] = this._hook({});
|
|
235
191
|
let _node;
|
|
236
192
|
if (value === "if") {
|
|
@@ -238,7 +194,7 @@ class Terp {
|
|
|
238
194
|
} else if (hookType) {
|
|
239
195
|
if (hookType === "static") {
|
|
240
196
|
if (typeof value === "function" && value.prototype instanceof aoye.Store) {
|
|
241
|
-
_node = this.componentDeclaration(value);
|
|
197
|
+
_node = this.componentDeclaration(value, ctx);
|
|
242
198
|
} else if (typeof value === "function") {
|
|
243
199
|
_node = this.fragmentDeclaration(value);
|
|
244
200
|
} else {
|
|
@@ -265,8 +221,7 @@ class Terp {
|
|
|
265
221
|
fragmentDeclaration(renderFragment) {
|
|
266
222
|
const fragmentNode = {
|
|
267
223
|
__logicType: LogicType.Fragment,
|
|
268
|
-
|
|
269
|
-
realList: []
|
|
224
|
+
realParent: null
|
|
270
225
|
};
|
|
271
226
|
renderFragment.call(this.data, this.opt, { data: this.data, root: "", anchor: "" });
|
|
272
227
|
return fragmentNode;
|
|
@@ -294,7 +249,7 @@ class Terp {
|
|
|
294
249
|
this.setProp(node, key, value, hookI);
|
|
295
250
|
}
|
|
296
251
|
}
|
|
297
|
-
componentDeclaration(Component) {
|
|
252
|
+
componentDeclaration(Component, ctx) {
|
|
298
253
|
const child = Component.new();
|
|
299
254
|
const prevOnePropParsed = this.onePropParsed;
|
|
300
255
|
this.onePropParsed = (node, key, value, valueIsMapKey, hookI) => {
|
|
@@ -310,9 +265,11 @@ class Terp {
|
|
|
310
265
|
}
|
|
311
266
|
};
|
|
312
267
|
tap.once(TerpEvt.AllAttrGot, () => {
|
|
268
|
+
const parent = ctx.stack[ctx.stack.length - 1].node;
|
|
269
|
+
const prev = ctx.prevSibling;
|
|
313
270
|
this.onePropParsed = prevOnePropParsed;
|
|
314
|
-
const
|
|
315
|
-
tap.emit(TerpEvt.HandledComponentNode,
|
|
271
|
+
const componentNode = child["ui"](this.opt, { data: child }, parent, prev);
|
|
272
|
+
tap.emit(TerpEvt.HandledComponentNode, componentNode);
|
|
316
273
|
});
|
|
317
274
|
return { __logicType: LogicType.Component };
|
|
318
275
|
}
|
|
@@ -322,8 +279,7 @@ class Terp {
|
|
|
322
279
|
const ifNode = {
|
|
323
280
|
__logicType: LogicType.If,
|
|
324
281
|
condition: value,
|
|
325
|
-
|
|
326
|
-
realList: [],
|
|
282
|
+
realParent: null,
|
|
327
283
|
snapshot: this.tokenizer.snapshot(),
|
|
328
284
|
isFirstRender: true,
|
|
329
285
|
watcher: null,
|
|
@@ -402,19 +358,6 @@ class Terp {
|
|
|
402
358
|
this.tokenizer.consume();
|
|
403
359
|
}
|
|
404
360
|
}
|
|
405
|
-
/** 子节点块:
|
|
406
|
-
* 必须被缩进包裹
|
|
407
|
-
* <childrenBlockOpt> ::= INDENT <nodeList>
|
|
408
|
-
* | ε /* 空(表示叶子节点,没有孩子)
|
|
409
|
-
* */
|
|
410
|
-
childrenBlockOpt(parent) {
|
|
411
|
-
if (!(this.tokenizer.token.type & TokenType.Indent)) {
|
|
412
|
-
return [];
|
|
413
|
-
}
|
|
414
|
-
this.tokenizer.consume();
|
|
415
|
-
const list = this.nodeList(parent);
|
|
416
|
-
return list;
|
|
417
|
-
}
|
|
418
361
|
config(opt) {
|
|
419
362
|
Object.assign(this, opt);
|
|
420
363
|
this.opt = opt;
|
|
@@ -422,28 +365,43 @@ class Terp {
|
|
|
422
365
|
createNode(name) {
|
|
423
366
|
return {
|
|
424
367
|
name,
|
|
425
|
-
props: {}
|
|
368
|
+
props: {},
|
|
369
|
+
nextSibling: null
|
|
426
370
|
};
|
|
427
371
|
}
|
|
428
|
-
|
|
429
|
-
return
|
|
372
|
+
nextSib(node) {
|
|
373
|
+
return node.nextSibling;
|
|
430
374
|
}
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
375
|
+
createAnchor() {
|
|
376
|
+
return {
|
|
377
|
+
name: "anchor",
|
|
378
|
+
nextSibling: null
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
insertAfter(parent, node, prev) {
|
|
382
|
+
this.lastInserted = node;
|
|
383
|
+
return this.defaultInsert(parent, node, prev);
|
|
384
|
+
}
|
|
385
|
+
defaultInsert(parent, node, prev) {
|
|
386
|
+
if (prev) {
|
|
387
|
+
const next = prev.nextSibling;
|
|
388
|
+
prev.nextSibling = node;
|
|
389
|
+
node.nextSibling = next;
|
|
436
390
|
} else {
|
|
437
|
-
parent.
|
|
391
|
+
parent.firstChild = node;
|
|
438
392
|
}
|
|
439
393
|
}
|
|
440
|
-
remove(parent, node, prevSibling
|
|
441
|
-
return this.defaultRemove(parent, node, prevSibling
|
|
394
|
+
remove(parent, node, prevSibling) {
|
|
395
|
+
return this.defaultRemove(parent, node, prevSibling);
|
|
442
396
|
}
|
|
443
397
|
// TODO: 默认改成 prevItem
|
|
444
|
-
defaultRemove(parent, node, prevSibling
|
|
445
|
-
|
|
446
|
-
|
|
398
|
+
defaultRemove(parent, node, prevSibling) {
|
|
399
|
+
const next = node.nextSibling;
|
|
400
|
+
if (prevSibling) {
|
|
401
|
+
prevSibling.nextSibling = next;
|
|
402
|
+
}
|
|
403
|
+
if (parent.firstChild === node) {
|
|
404
|
+
parent.firstChild = next;
|
|
447
405
|
}
|
|
448
406
|
}
|
|
449
407
|
setProp(node, key, value, hookI) {
|
|
@@ -463,5 +421,476 @@ class Terp {
|
|
|
463
421
|
}
|
|
464
422
|
}
|
|
465
423
|
|
|
466
|
-
|
|
424
|
+
const _Tokenizer = class _Tokenizer {
|
|
425
|
+
constructor() {
|
|
426
|
+
/** 缩进大小 默认 2 */
|
|
427
|
+
this.TabSize = 2;
|
|
428
|
+
/** 缩进字符 */
|
|
429
|
+
this.Tab = Array.from({ length: this.TabSize }, () => " ").join("");
|
|
430
|
+
/** 匹配标识符 */
|
|
431
|
+
this.IdExp = /[\d\w\/]/;
|
|
432
|
+
/** 回车后需要判断缩进 */
|
|
433
|
+
this.needIndent = false;
|
|
434
|
+
/** 用于跳过第一个节点前的空白字符串,以及生成基础缩进 */
|
|
435
|
+
this.isFirstToken = true;
|
|
436
|
+
/** 记录历史缩进的长度,相对于行首 */
|
|
437
|
+
this.dentStack = [0];
|
|
438
|
+
/** 当前字符 index */
|
|
439
|
+
this.i = 0;
|
|
440
|
+
this.handledTokens = [];
|
|
441
|
+
/**
|
|
442
|
+
* 有些标识符能产生多个 token
|
|
443
|
+
* 例如 dedent
|
|
444
|
+
* parent1
|
|
445
|
+
* child
|
|
446
|
+
* subChild
|
|
447
|
+
* parent2 <- 产生两个 dedent
|
|
448
|
+
*/
|
|
449
|
+
this.waitingTokens = new bobeShared.Queue();
|
|
450
|
+
}
|
|
451
|
+
consume() {
|
|
452
|
+
const token = this.token;
|
|
453
|
+
this.nextToken();
|
|
454
|
+
return token;
|
|
455
|
+
}
|
|
456
|
+
// /** 恢复至某一个现场,进行 token 重算 */
|
|
457
|
+
resume(_snapshot) {
|
|
458
|
+
this.token = void 0;
|
|
459
|
+
this.needIndent = false;
|
|
460
|
+
this.isFirstToken = true;
|
|
461
|
+
this.dentStack = [0];
|
|
462
|
+
Object.assign(this, _snapshot);
|
|
463
|
+
}
|
|
464
|
+
snapshot() {
|
|
465
|
+
return {
|
|
466
|
+
i: this.i,
|
|
467
|
+
waitingTokens: this.waitingTokens.clone()
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
skip() {
|
|
471
|
+
const dentLen = this.dentStack[this.dentStack.length - 1];
|
|
472
|
+
let needIndent = false;
|
|
473
|
+
let skipFragment = ``;
|
|
474
|
+
this.token = void 0;
|
|
475
|
+
while (1) {
|
|
476
|
+
const char = this.char;
|
|
477
|
+
if (char === "\n") {
|
|
478
|
+
needIndent = true;
|
|
479
|
+
skipFragment += char;
|
|
480
|
+
this.next();
|
|
481
|
+
continue;
|
|
482
|
+
}
|
|
483
|
+
if (!needIndent) {
|
|
484
|
+
skipFragment += char;
|
|
485
|
+
this.next();
|
|
486
|
+
continue;
|
|
487
|
+
}
|
|
488
|
+
needIndent = false;
|
|
489
|
+
const { value, isEmptyLine } = this.getDentValue();
|
|
490
|
+
const currLen = value.length;
|
|
491
|
+
if (isEmptyLine) continue;
|
|
492
|
+
if (value.length > dentLen) {
|
|
493
|
+
skipFragment += value;
|
|
494
|
+
} else {
|
|
495
|
+
for (let i = this.dentStack.length - 1; i >= 0; i--) {
|
|
496
|
+
const expLen = this.dentStack[i];
|
|
497
|
+
if (currLen === expLen) break;
|
|
498
|
+
if (currLen > expLen) {
|
|
499
|
+
throw SyntaxError(`\u7F29\u8FDB\u9519\u8BEF\uFF0C\u7F29\u8FDB\u957F\u5EA6\u4E0D\u5339\u914D`);
|
|
500
|
+
}
|
|
501
|
+
this.dentStack.pop();
|
|
502
|
+
if (!this.token) {
|
|
503
|
+
this.setToken(TokenType.Dedent, String(expLen));
|
|
504
|
+
} else {
|
|
505
|
+
this.waitingTokens.push({
|
|
506
|
+
type: TokenType.Dedent,
|
|
507
|
+
typeName: TokenType[TokenType.Dedent],
|
|
508
|
+
value: String(expLen)
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
break;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
if (!this.token) {
|
|
516
|
+
this.nextToken();
|
|
517
|
+
}
|
|
518
|
+
return skipFragment;
|
|
519
|
+
}
|
|
520
|
+
setCode(code) {
|
|
521
|
+
this.code = "\n" + code.trimEnd() + `
|
|
522
|
+
${_Tokenizer.EofId}`;
|
|
523
|
+
}
|
|
524
|
+
tokenize() {
|
|
525
|
+
var _a, _b;
|
|
526
|
+
do {
|
|
527
|
+
this.nextToken();
|
|
528
|
+
console.log("token:", TokenType[(_a = this.token) == null ? void 0 : _a.type], JSON.stringify(((_b = this.token) == null ? void 0 : _b.value) || ""));
|
|
529
|
+
} while (!this.isEof());
|
|
530
|
+
}
|
|
531
|
+
isEof() {
|
|
532
|
+
if (!this.token) return false;
|
|
533
|
+
return this.token.type & TokenType.Identifier && this.token.value === _Tokenizer.EofId;
|
|
534
|
+
}
|
|
535
|
+
get char() {
|
|
536
|
+
return this.code[this.i];
|
|
537
|
+
}
|
|
538
|
+
get prev() {
|
|
539
|
+
return this.code[this.i - 1];
|
|
540
|
+
}
|
|
541
|
+
get after() {
|
|
542
|
+
return this.code[this.i + 1];
|
|
543
|
+
}
|
|
544
|
+
next() {
|
|
545
|
+
const prev = this.code[this.i];
|
|
546
|
+
this.i++;
|
|
547
|
+
const curr = this.code[this.i];
|
|
548
|
+
return [prev, curr];
|
|
549
|
+
}
|
|
550
|
+
setToken(type, value) {
|
|
551
|
+
this.token = {
|
|
552
|
+
type,
|
|
553
|
+
typeName: TokenType[type],
|
|
554
|
+
value
|
|
555
|
+
};
|
|
556
|
+
this.isFirstToken = false;
|
|
557
|
+
}
|
|
558
|
+
testId(value) {
|
|
559
|
+
if (typeof value !== "string") return false;
|
|
560
|
+
return this.IdExp.test(value);
|
|
561
|
+
}
|
|
562
|
+
nextToken() {
|
|
563
|
+
try {
|
|
564
|
+
if (this.isEof()) {
|
|
565
|
+
return this.token;
|
|
566
|
+
}
|
|
567
|
+
this.token = void 0;
|
|
568
|
+
if (this.waitingTokens.len) {
|
|
569
|
+
const item = this.waitingTokens.shift();
|
|
570
|
+
this.setToken(item.type, item.value);
|
|
571
|
+
return this.token;
|
|
572
|
+
}
|
|
573
|
+
outer: while (1) {
|
|
574
|
+
if (this.needIndent) {
|
|
575
|
+
this.dent();
|
|
576
|
+
} else {
|
|
577
|
+
let { char } = this;
|
|
578
|
+
switch (char) {
|
|
579
|
+
case " ":
|
|
580
|
+
case " ":
|
|
581
|
+
break;
|
|
582
|
+
// 找后续所有 newLine
|
|
583
|
+
case "\n":
|
|
584
|
+
this.newLine();
|
|
585
|
+
this.needIndent = true;
|
|
586
|
+
break;
|
|
587
|
+
case "=":
|
|
588
|
+
this.assignment();
|
|
589
|
+
break;
|
|
590
|
+
case "|":
|
|
591
|
+
this.pipe();
|
|
592
|
+
break;
|
|
593
|
+
case "'":
|
|
594
|
+
case '"':
|
|
595
|
+
this.str(char);
|
|
596
|
+
break;
|
|
597
|
+
case "{":
|
|
598
|
+
this.brace();
|
|
599
|
+
break;
|
|
600
|
+
case "$":
|
|
601
|
+
const handled = this.dynamic(char);
|
|
602
|
+
if (handled) break;
|
|
603
|
+
default:
|
|
604
|
+
if (bobeShared.isNum(char)) {
|
|
605
|
+
this.number(char);
|
|
606
|
+
break;
|
|
607
|
+
}
|
|
608
|
+
if (this.testId(char)) {
|
|
609
|
+
this.identifier(char);
|
|
610
|
+
}
|
|
611
|
+
break;
|
|
612
|
+
}
|
|
613
|
+
this.next();
|
|
614
|
+
}
|
|
615
|
+
if (this.token) {
|
|
616
|
+
break;
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
return this.token;
|
|
620
|
+
} catch (error) {
|
|
621
|
+
console.error(error);
|
|
622
|
+
} finally {
|
|
623
|
+
this.handledTokens.push(this.token);
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
assignment() {
|
|
627
|
+
this.setToken(TokenType.Assign, "=");
|
|
628
|
+
}
|
|
629
|
+
pipe() {
|
|
630
|
+
this.setToken(TokenType.Pipe, "|");
|
|
631
|
+
}
|
|
632
|
+
dynamic(char) {
|
|
633
|
+
let nextC = this.after;
|
|
634
|
+
if (nextC !== "{") {
|
|
635
|
+
return false;
|
|
636
|
+
}
|
|
637
|
+
this.next();
|
|
638
|
+
let value = "${";
|
|
639
|
+
let innerBrace = 0;
|
|
640
|
+
while (1) {
|
|
641
|
+
nextC = this.after;
|
|
642
|
+
value += nextC;
|
|
643
|
+
this.next();
|
|
644
|
+
if (nextC === "{") {
|
|
645
|
+
innerBrace++;
|
|
646
|
+
}
|
|
647
|
+
if (nextC === "}") {
|
|
648
|
+
if (!innerBrace) {
|
|
649
|
+
break;
|
|
650
|
+
}
|
|
651
|
+
innerBrace--;
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
this.setToken(TokenType.Identifier, value);
|
|
655
|
+
return true;
|
|
656
|
+
}
|
|
657
|
+
brace() {
|
|
658
|
+
let inComment, inString, count = 0, value = "", backslashCount = 0;
|
|
659
|
+
while (1) {
|
|
660
|
+
const char = this.char;
|
|
661
|
+
const nextChar = this.after;
|
|
662
|
+
if (inComment === "single" && char === "\n") {
|
|
663
|
+
inComment = null;
|
|
664
|
+
} else if (inComment === "multi" && char === "*" && nextChar === "/") {
|
|
665
|
+
inComment = null;
|
|
666
|
+
value += this.next()[0];
|
|
667
|
+
} else if (inString) {
|
|
668
|
+
if (char === inString && backslashCount % 2 === 0) {
|
|
669
|
+
inString = null;
|
|
670
|
+
}
|
|
671
|
+
backslashCount = char === "\\" ? backslashCount + 1 : 0;
|
|
672
|
+
} else {
|
|
673
|
+
if (char === "/" && nextChar === "/") {
|
|
674
|
+
inComment = "single";
|
|
675
|
+
value += this.next()[0];
|
|
676
|
+
} else if (char === "/" && nextChar === "*") {
|
|
677
|
+
inComment = "multi";
|
|
678
|
+
value += this.next()[0];
|
|
679
|
+
} else if (char === "'" || char === '"' || char === "`") {
|
|
680
|
+
inString = char;
|
|
681
|
+
} else if (char === "{") {
|
|
682
|
+
count++;
|
|
683
|
+
} else if (char === "}") {
|
|
684
|
+
count--;
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
if (count === 0 && inString == null && inComment == null) {
|
|
688
|
+
this.setToken(TokenType.InsertionExp, value.slice(1));
|
|
689
|
+
return;
|
|
690
|
+
}
|
|
691
|
+
value += this.next()[0];
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
newLine() {
|
|
695
|
+
let value = "\n";
|
|
696
|
+
let nextC;
|
|
697
|
+
while (1) {
|
|
698
|
+
nextC = this.after;
|
|
699
|
+
if (nextC !== "\n") {
|
|
700
|
+
break;
|
|
701
|
+
}
|
|
702
|
+
value += nextC;
|
|
703
|
+
this.next();
|
|
704
|
+
}
|
|
705
|
+
if (this.isFirstToken) {
|
|
706
|
+
return;
|
|
707
|
+
}
|
|
708
|
+
this.setToken(TokenType.NewLine, value);
|
|
709
|
+
}
|
|
710
|
+
getDentValue() {
|
|
711
|
+
let value = "";
|
|
712
|
+
let nextC;
|
|
713
|
+
let isEmptyLine = false;
|
|
714
|
+
while (1) {
|
|
715
|
+
const nextChar = this.char;
|
|
716
|
+
switch (nextChar) {
|
|
717
|
+
case " ":
|
|
718
|
+
nextC = this.Tab;
|
|
719
|
+
break;
|
|
720
|
+
case " ":
|
|
721
|
+
nextC = " ";
|
|
722
|
+
break;
|
|
723
|
+
case "\n":
|
|
724
|
+
nextC = "\n";
|
|
725
|
+
break;
|
|
726
|
+
default:
|
|
727
|
+
nextC = "";
|
|
728
|
+
break;
|
|
729
|
+
}
|
|
730
|
+
if (nextC === "\n") {
|
|
731
|
+
isEmptyLine = true;
|
|
732
|
+
break;
|
|
733
|
+
}
|
|
734
|
+
if (!nextC) {
|
|
735
|
+
break;
|
|
736
|
+
}
|
|
737
|
+
value += nextC;
|
|
738
|
+
this.next();
|
|
739
|
+
}
|
|
740
|
+
return {
|
|
741
|
+
value,
|
|
742
|
+
isEmptyLine
|
|
743
|
+
};
|
|
744
|
+
}
|
|
745
|
+
dent() {
|
|
746
|
+
const { value, isEmptyLine } = this.getDentValue();
|
|
747
|
+
if (isEmptyLine) {
|
|
748
|
+
this.needIndent = true;
|
|
749
|
+
return;
|
|
750
|
+
}
|
|
751
|
+
this.needIndent = false;
|
|
752
|
+
if (this.isFirstToken) {
|
|
753
|
+
this.dentStack[0] = value.length;
|
|
754
|
+
return;
|
|
755
|
+
}
|
|
756
|
+
let currLen = value.length;
|
|
757
|
+
const indentHasLen = currLen > 0;
|
|
758
|
+
const prevLen = this.dentStack[this.dentStack.length - 1];
|
|
759
|
+
if (currLen > prevLen) {
|
|
760
|
+
this.dentStack.push(currLen);
|
|
761
|
+
this.setToken(TokenType.Indent, String(currLen));
|
|
762
|
+
return indentHasLen;
|
|
763
|
+
}
|
|
764
|
+
if (currLen < prevLen) {
|
|
765
|
+
for (let i = this.dentStack.length - 2; i >= 0; i--) {
|
|
766
|
+
const expLen = this.dentStack[i];
|
|
767
|
+
const prevExpLen = this.dentStack[i + 1];
|
|
768
|
+
if (currLen > expLen && currLen < prevExpLen) {
|
|
769
|
+
throw SyntaxError("\u7F29\u8FDB\u5927\u5C0F\u4E0D\u7EDF\u4E00");
|
|
770
|
+
}
|
|
771
|
+
this.dentStack.pop();
|
|
772
|
+
if (!this.token) {
|
|
773
|
+
this.setToken(TokenType.Dedent, String(expLen));
|
|
774
|
+
} else {
|
|
775
|
+
this.waitingTokens.push({
|
|
776
|
+
type: TokenType.Dedent,
|
|
777
|
+
typeName: TokenType[TokenType.Dedent],
|
|
778
|
+
value: String(expLen)
|
|
779
|
+
});
|
|
780
|
+
}
|
|
781
|
+
if (currLen === expLen) {
|
|
782
|
+
break;
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
return indentHasLen;
|
|
786
|
+
}
|
|
787
|
+
return indentHasLen;
|
|
788
|
+
}
|
|
789
|
+
identifier(char) {
|
|
790
|
+
let value = char;
|
|
791
|
+
let nextC;
|
|
792
|
+
while (1) {
|
|
793
|
+
nextC = this.after;
|
|
794
|
+
if (!this.testId(nextC)) {
|
|
795
|
+
break;
|
|
796
|
+
}
|
|
797
|
+
value += nextC;
|
|
798
|
+
this.next();
|
|
799
|
+
}
|
|
800
|
+
let realValue = value === "null" ? null : value === "undefined" ? void 0 : value === "false" ? false : value === "true" ? true : value;
|
|
801
|
+
this.setToken(TokenType.Identifier, realValue);
|
|
802
|
+
}
|
|
803
|
+
str(char) {
|
|
804
|
+
let value = '"';
|
|
805
|
+
let nextC;
|
|
806
|
+
let continuousBackslashCount = 0;
|
|
807
|
+
while (1) {
|
|
808
|
+
nextC = this.after;
|
|
809
|
+
value += nextC;
|
|
810
|
+
const memoCount = continuousBackslashCount;
|
|
811
|
+
if (nextC === "\\") {
|
|
812
|
+
continuousBackslashCount++;
|
|
813
|
+
} else {
|
|
814
|
+
continuousBackslashCount = 0;
|
|
815
|
+
}
|
|
816
|
+
this.next();
|
|
817
|
+
if (nextC === char && memoCount % 2 === 0) {
|
|
818
|
+
break;
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
this.setToken(TokenType.Identifier, JSON.parse(value.slice(0, -1) + '"'));
|
|
822
|
+
}
|
|
823
|
+
number(char) {
|
|
824
|
+
let value = char;
|
|
825
|
+
let nextC;
|
|
826
|
+
while (1) {
|
|
827
|
+
nextC = this.after;
|
|
828
|
+
if (!bobeShared.isNum(nextC)) {
|
|
829
|
+
break;
|
|
830
|
+
}
|
|
831
|
+
value += nextC;
|
|
832
|
+
this.next();
|
|
833
|
+
}
|
|
834
|
+
this.setToken(TokenType.Identifier, Number(value));
|
|
835
|
+
}
|
|
836
|
+
eof() {
|
|
837
|
+
this.setToken(TokenType.Eof, "End Of File");
|
|
838
|
+
}
|
|
839
|
+
};
|
|
840
|
+
/** Eof 标识符的值 */
|
|
841
|
+
_Tokenizer.EofId = `__EOF__${Date.now()}`;
|
|
842
|
+
let Tokenizer = _Tokenizer;
|
|
843
|
+
|
|
844
|
+
var __defProp = Object.defineProperty;
|
|
845
|
+
var __defProps = Object.defineProperties;
|
|
846
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
847
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
848
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
849
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
850
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
851
|
+
var __spreadValues = (a, b) => {
|
|
852
|
+
for (var prop in b || (b = {}))
|
|
853
|
+
if (__hasOwnProp.call(b, prop))
|
|
854
|
+
__defNormalProp(a, prop, b[prop]);
|
|
855
|
+
if (__getOwnPropSymbols)
|
|
856
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
857
|
+
if (__propIsEnum.call(b, prop))
|
|
858
|
+
__defNormalProp(a, prop, b[prop]);
|
|
859
|
+
}
|
|
860
|
+
return a;
|
|
861
|
+
};
|
|
862
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
863
|
+
function bobe(fragments, ...values) {
|
|
864
|
+
const ui = function ui2(options, valueOpt, root, before) {
|
|
865
|
+
const tokenizer = new Tokenizer();
|
|
866
|
+
const cmp = new Interpreter(tokenizer);
|
|
867
|
+
Object.assign(cmp, valueOpt);
|
|
868
|
+
cmp.config(__spreadProps(__spreadValues({}, options), {
|
|
869
|
+
hook({ i }) {
|
|
870
|
+
return values[i];
|
|
871
|
+
},
|
|
872
|
+
setProp(node, key, value, hookI) {
|
|
873
|
+
node.props[key] = value;
|
|
874
|
+
}
|
|
875
|
+
}));
|
|
876
|
+
cmp.init(Array.from(fragments));
|
|
877
|
+
return cmp.program(root, before);
|
|
878
|
+
};
|
|
879
|
+
return ui;
|
|
880
|
+
}
|
|
881
|
+
function customRender(option) {
|
|
882
|
+
return function render(Ctor, root) {
|
|
883
|
+
const store = Ctor.new();
|
|
884
|
+
return [store["ui"](option, { data: store }, root), store];
|
|
885
|
+
};
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
exports.bobe = bobe;
|
|
889
|
+
exports.customRender = customRender;
|
|
890
|
+
Object.keys(aoye).forEach(function (k) {
|
|
891
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
892
|
+
enumerable: true,
|
|
893
|
+
get: function () { return aoye[k]; }
|
|
894
|
+
});
|
|
895
|
+
});
|
|
467
896
|
//# sourceMappingURL=bobe.cjs.js.map
|