bobe 0.0.37 → 0.0.38
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 +95 -34
- package/dist/bobe.cjs.js.map +1 -1
- package/dist/bobe.compiler.cjs.js +95 -34
- package/dist/bobe.compiler.cjs.js.map +1 -1
- package/dist/bobe.compiler.esm.js +95 -34
- package/dist/bobe.compiler.esm.js.map +1 -1
- package/dist/bobe.esm.js +95 -34
- package/dist/bobe.esm.js.map +1 -1
- package/dist/index.d.ts +20 -8
- package/dist/index.umd.js +95 -34
- package/dist/index.umd.js.map +1 -1
- package/package.json +3 -3
|
@@ -13,8 +13,15 @@ let TokenType = function (TokenType) {
|
|
|
13
13
|
TokenType[TokenType["InsertionExp"] = 128] = "InsertionExp";
|
|
14
14
|
TokenType[TokenType["Semicolon"] = 256] = "Semicolon";
|
|
15
15
|
TokenType[TokenType["StaticInsExp"] = 512] = "StaticInsExp";
|
|
16
|
+
TokenType[TokenType["String"] = 1024] = "String";
|
|
17
|
+
TokenType[TokenType["Number"] = 2048] = "Number";
|
|
18
|
+
TokenType[TokenType["Boolean"] = 4096] = "Boolean";
|
|
19
|
+
TokenType[TokenType["Null"] = 8192] = "Null";
|
|
20
|
+
TokenType[TokenType["Undefined"] = 16384] = "Undefined";
|
|
16
21
|
return TokenType;
|
|
17
22
|
}({});
|
|
23
|
+
const BaseTokenType = TokenType.String | TokenType.Number | TokenType.Boolean | TokenType.Null | TokenType.Undefined;
|
|
24
|
+
const ValueTokenType = BaseTokenType | TokenType.InsertionExp | TokenType.StaticInsExp;
|
|
18
25
|
let FakeType = function (FakeType) {
|
|
19
26
|
FakeType[FakeType["If"] = 1] = "If";
|
|
20
27
|
FakeType[FakeType["Fail"] = 2] = "Fail";
|
|
@@ -50,13 +57,15 @@ let ParseErrorCode = function (ParseErrorCode) {
|
|
|
50
57
|
ParseErrorCode[ParseErrorCode["INDENT_MISMATCH"] = 9005] = "INDENT_MISMATCH";
|
|
51
58
|
ParseErrorCode[ParseErrorCode["MISSING_ASSIGN"] = 9006] = "MISSING_ASSIGN";
|
|
52
59
|
ParseErrorCode[ParseErrorCode["INVALID_TAG_NAME"] = 9007] = "INVALID_TAG_NAME";
|
|
53
|
-
ParseErrorCode[ParseErrorCode["
|
|
54
|
-
ParseErrorCode[ParseErrorCode["
|
|
55
|
-
ParseErrorCode[ParseErrorCode["
|
|
56
|
-
ParseErrorCode[ParseErrorCode["
|
|
57
|
-
ParseErrorCode[ParseErrorCode["
|
|
58
|
-
ParseErrorCode[ParseErrorCode["
|
|
59
|
-
ParseErrorCode[ParseErrorCode["
|
|
60
|
+
ParseErrorCode[ParseErrorCode["INVALID_PROP_KEY"] = 9008] = "INVALID_PROP_KEY";
|
|
61
|
+
ParseErrorCode[ParseErrorCode["ELSE_WITHOUT_IF"] = 9009] = "ELSE_WITHOUT_IF";
|
|
62
|
+
ParseErrorCode[ParseErrorCode["EMPTY_IF_BODY"] = 9010] = "EMPTY_IF_BODY";
|
|
63
|
+
ParseErrorCode[ParseErrorCode["EMPTY_FOR_BODY"] = 9011] = "EMPTY_FOR_BODY";
|
|
64
|
+
ParseErrorCode[ParseErrorCode["MISSING_FOR_COLLECTION"] = 9012] = "MISSING_FOR_COLLECTION";
|
|
65
|
+
ParseErrorCode[ParseErrorCode["MISSING_FOR_SEMICOLON"] = 9013] = "MISSING_FOR_SEMICOLON";
|
|
66
|
+
ParseErrorCode[ParseErrorCode["MISSING_FOR_ITEM"] = 9014] = "MISSING_FOR_ITEM";
|
|
67
|
+
ParseErrorCode[ParseErrorCode["MISSING_PROP_ASSIGNMENT"] = 9015] = "MISSING_PROP_ASSIGNMENT";
|
|
68
|
+
ParseErrorCode[ParseErrorCode["PIPE_IN_WRONG_CONTEXT"] = 9016] = "PIPE_IN_WRONG_CONTEXT";
|
|
60
69
|
return ParseErrorCode;
|
|
61
70
|
}({});
|
|
62
71
|
class ParseSyntaxError extends SyntaxError {
|
|
@@ -631,8 +640,30 @@ class Tokenizer {
|
|
|
631
640
|
this.setToken(TokenType.Dedent, '');
|
|
632
641
|
return;
|
|
633
642
|
}
|
|
634
|
-
let realValue
|
|
635
|
-
|
|
643
|
+
let realValue, tokenType;
|
|
644
|
+
switch (value) {
|
|
645
|
+
case 'null':
|
|
646
|
+
realValue = null;
|
|
647
|
+
tokenType = TokenType.Null;
|
|
648
|
+
break;
|
|
649
|
+
case 'undefined':
|
|
650
|
+
realValue = undefined;
|
|
651
|
+
tokenType = TokenType.Undefined;
|
|
652
|
+
break;
|
|
653
|
+
case 'false':
|
|
654
|
+
realValue = false;
|
|
655
|
+
tokenType = TokenType.Boolean;
|
|
656
|
+
break;
|
|
657
|
+
case 'true':
|
|
658
|
+
realValue = true;
|
|
659
|
+
tokenType = TokenType.Boolean;
|
|
660
|
+
break;
|
|
661
|
+
default:
|
|
662
|
+
realValue = value;
|
|
663
|
+
tokenType = TokenType.Identifier;
|
|
664
|
+
break;
|
|
665
|
+
}
|
|
666
|
+
this.setToken(tokenType, realValue);
|
|
636
667
|
}
|
|
637
668
|
str(char) {
|
|
638
669
|
const startOffset = this.preI,
|
|
@@ -658,7 +689,7 @@ class Tokenizer {
|
|
|
658
689
|
}
|
|
659
690
|
value += nextC;
|
|
660
691
|
}
|
|
661
|
-
this.setToken(TokenType.
|
|
692
|
+
this.setToken(TokenType.String, value);
|
|
662
693
|
}
|
|
663
694
|
number(char) {
|
|
664
695
|
let value = char;
|
|
@@ -671,7 +702,7 @@ class Tokenizer {
|
|
|
671
702
|
value += nextC;
|
|
672
703
|
this.next();
|
|
673
704
|
}
|
|
674
|
-
this.setToken(TokenType.
|
|
705
|
+
this.setToken(TokenType.Number, Number(value));
|
|
675
706
|
}
|
|
676
707
|
eof() {
|
|
677
708
|
this.setToken(TokenType.Eof, 'End Of File');
|
|
@@ -950,7 +981,7 @@ var NodeType = function (NodeType) {
|
|
|
950
981
|
let _initProto;
|
|
951
982
|
class Compiler {
|
|
952
983
|
static {
|
|
953
|
-
var _applyDecs$e = _slicedToArray(_applyDecs2311(this, [], [[NodeHook, 2, "parseProgram"], [[NodeHook, NodeLoc], 2, "parseComponentNode"], [[NodeHook, NodeLoc], 2, "parseElementNode"], [[NodeHook, NodeLoc], 2, "parseConditionalNode"], [[NodeHook, NodeLoc], 2, "parseLoopNode"], [NodeHook, 2, "parseProperty"], [[NodeHook, TokenLoc], 2, "parsePropertyKey"], [[NodeHook, TokenLoc], 2, "parsePropertyValue"], [[NodeHook, TokenLoc], 2, "parseName"]]).e, 1);
|
|
984
|
+
var _applyDecs$e = _slicedToArray(_applyDecs2311(this, [], [[NodeHook, 2, "parseProgram"], [[NodeHook, NodeLoc], 2, "parseComponentNode"], [[NodeHook, NodeLoc], 2, "parseElementNode"], [[NodeHook, NodeLoc], 2, "parseConditionalNode"], [[NodeHook, NodeLoc], 2, "parseLoopNode"], [NodeHook, 2, "parseProperty"], [[NodeHook, TokenLoc], 2, "parsePropertyKey"], [[NodeHook, TokenLoc], 2, "parseJsExp"], [[NodeHook, TokenLoc], 2, "parsePropertyValue"], [[NodeHook, TokenLoc], 2, "parseName"]]).e, 1);
|
|
954
985
|
_initProto = _applyDecs$e[0];
|
|
955
986
|
}
|
|
956
987
|
errors = (_initProto(this), []);
|
|
@@ -958,7 +989,10 @@ class Compiler {
|
|
|
958
989
|
this.tokenizer = tokenizer;
|
|
959
990
|
this.hooks = hooks;
|
|
960
991
|
}
|
|
961
|
-
addError(code, message, loc) {
|
|
992
|
+
addError(code, message, loc, node) {
|
|
993
|
+
if (node) {
|
|
994
|
+
node.hasError = true;
|
|
995
|
+
}
|
|
962
996
|
this.errors.push({
|
|
963
997
|
code,
|
|
964
998
|
message,
|
|
@@ -1061,7 +1095,7 @@ class Compiler {
|
|
|
1061
1095
|
parseElementNode(node) {
|
|
1062
1096
|
const tagToken = this.tokenizer.token;
|
|
1063
1097
|
if (!(tagToken.type & TokenType.Identifier)) {
|
|
1064
|
-
this.addError(ParseErrorCode.INVALID_TAG_NAME, `无效的标签名,期望标识符但得到 "${tagToken.value}"`, tagToken.loc ?? this.tokenizer.emptyLoc());
|
|
1098
|
+
this.addError(ParseErrorCode.INVALID_TAG_NAME, `无效的标签名,期望标识符但得到 "${tagToken.value}"`, tagToken.loc ?? this.tokenizer.emptyLoc(), node);
|
|
1065
1099
|
while (!(this.tokenizer.token.type & TokenType.NewLine) && !this.tokenizer.isEof()) {
|
|
1066
1100
|
this.tokenizer.nextToken();
|
|
1067
1101
|
}
|
|
@@ -1081,7 +1115,7 @@ class Compiler {
|
|
|
1081
1115
|
parseConditionalNode(node) {
|
|
1082
1116
|
const keyword = this.tokenizer.token.value;
|
|
1083
1117
|
this.tokenizer.condExp();
|
|
1084
|
-
const condition = this.
|
|
1118
|
+
const condition = this.parseJsExp();
|
|
1085
1119
|
this.tokenizer.nextToken();
|
|
1086
1120
|
this.tokenizer.nextToken();
|
|
1087
1121
|
node.type = keyword === 'if' ? NodeType.If : keyword === 'else' ? NodeType.Else : NodeType.Fail;
|
|
@@ -1094,22 +1128,22 @@ class Compiler {
|
|
|
1094
1128
|
parseLoopNode(node) {
|
|
1095
1129
|
const forLoc = this.tokenizer.token.loc ?? this.tokenizer.emptyLoc();
|
|
1096
1130
|
this.tokenizer.nextToken();
|
|
1097
|
-
const collection = this.
|
|
1131
|
+
const collection = this.parseJsExp();
|
|
1098
1132
|
if (!collection.value && collection.value !== 0) {
|
|
1099
|
-
this.addError(ParseErrorCode.MISSING_FOR_COLLECTION, '"for" 缺少集合表达式', forLoc);
|
|
1133
|
+
this.addError(ParseErrorCode.MISSING_FOR_COLLECTION, '"for" 缺少集合表达式', forLoc, node);
|
|
1100
1134
|
}
|
|
1101
1135
|
const semicolonToken = this.tokenizer.nextToken();
|
|
1102
1136
|
if (!(semicolonToken.type & TokenType.Semicolon)) {
|
|
1103
|
-
this.addError(ParseErrorCode.MISSING_FOR_SEMICOLON, '"for" 语法:for <集合>; <item> [index][; key],缺少第一个 ";"', semicolonToken.loc ?? this.tokenizer.emptyLoc());
|
|
1137
|
+
this.addError(ParseErrorCode.MISSING_FOR_SEMICOLON, '"for" 语法:for <集合>; <item> [index][; key],缺少第一个 ";"', semicolonToken.loc ?? this.tokenizer.emptyLoc(), node);
|
|
1104
1138
|
}
|
|
1105
1139
|
const itemToken = this.tokenizer.nextToken();
|
|
1106
1140
|
const isDestruct = itemToken.type === TokenType.InsertionExp;
|
|
1107
1141
|
if (isDestruct) {
|
|
1108
1142
|
itemToken.value = '{' + itemToken.value + '}';
|
|
1109
1143
|
}
|
|
1110
|
-
const item = this.
|
|
1144
|
+
const item = this.parseJsExp();
|
|
1111
1145
|
if (!item.value && item.value !== 0) {
|
|
1112
|
-
this.addError(ParseErrorCode.MISSING_FOR_ITEM, '"for" 缺少 item 变量名', itemToken.loc ?? this.tokenizer.emptyLoc());
|
|
1146
|
+
this.addError(ParseErrorCode.MISSING_FOR_ITEM, '"for" 缺少 item 变量名', itemToken.loc ?? this.tokenizer.emptyLoc(), node);
|
|
1113
1147
|
}
|
|
1114
1148
|
let char = this.tokenizer.peekChar(),
|
|
1115
1149
|
key,
|
|
@@ -1118,16 +1152,16 @@ class Compiler {
|
|
|
1118
1152
|
this.tokenizer.nextToken();
|
|
1119
1153
|
if (this.tokenizer.peekChar() !== '\n') {
|
|
1120
1154
|
this.tokenizer.jsExp();
|
|
1121
|
-
key = this.
|
|
1155
|
+
key = this.parseJsExp();
|
|
1122
1156
|
}
|
|
1123
1157
|
} else if (char === '\n') ; else {
|
|
1124
1158
|
this.tokenizer.nextToken();
|
|
1125
|
-
index = this.
|
|
1159
|
+
index = this.parseJsExp();
|
|
1126
1160
|
if (this.tokenizer.peekChar() === ';') {
|
|
1127
1161
|
this.tokenizer.nextToken();
|
|
1128
1162
|
if (this.tokenizer.peekChar() !== '\n') {
|
|
1129
1163
|
this.tokenizer.jsExp();
|
|
1130
|
-
key = this.
|
|
1164
|
+
key = this.parseJsExp();
|
|
1131
1165
|
}
|
|
1132
1166
|
}
|
|
1133
1167
|
}
|
|
@@ -1161,23 +1195,41 @@ class Compiler {
|
|
|
1161
1195
|
attributeList() {
|
|
1162
1196
|
const props = [];
|
|
1163
1197
|
while (!(this.tokenizer.token.type & TokenType.NewLine) && !(this.tokenizer.token.type & TokenType.Pipe) && !this.tokenizer.isEof()) {
|
|
1164
|
-
|
|
1198
|
+
const prop = this.parseProperty();
|
|
1199
|
+
if (prop) {
|
|
1200
|
+
props.push(prop);
|
|
1201
|
+
}
|
|
1165
1202
|
}
|
|
1166
1203
|
return props;
|
|
1167
1204
|
}
|
|
1168
1205
|
parseProperty(node) {
|
|
1169
1206
|
node.type = NodeType.Property;
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
if (token.value === '=') {
|
|
1173
|
-
this.tokenizer.nextToken();
|
|
1174
|
-
node.value = this.parsePropertyValue();
|
|
1207
|
+
if (this.tokenizer.token.type !== TokenType.Identifier) {
|
|
1208
|
+
this.addError(ParseErrorCode.INVALID_PROP_KEY, `属性名 "${this.tokenizer.token.value}" 不合法`, this.tokenizer.token.loc ?? this.tokenizer.emptyLoc(), node);
|
|
1175
1209
|
this.tokenizer.nextToken();
|
|
1176
|
-
|
|
1177
|
-
this.addError(ParseErrorCode.MISSING_ASSIGN, `属性 "${node.key.key}" 缺少 "=" 赋值符号`, node.key.loc ?? this.tokenizer.emptyLoc());
|
|
1210
|
+
return null;
|
|
1178
1211
|
}
|
|
1212
|
+
node.key = this.parsePropertyKey();
|
|
1213
|
+
const token = this.tokenizer.nextToken();
|
|
1214
|
+
if (token.value !== '=') {
|
|
1215
|
+
this.addError(ParseErrorCode.MISSING_ASSIGN, `属性 "${node.key.key}" 缺少 "=" 赋值符号`, node.key.loc ?? this.tokenizer.emptyLoc(), node);
|
|
1216
|
+
node.loc.start = node.key.loc.start;
|
|
1217
|
+
node.loc.end = node.key.loc.end;
|
|
1218
|
+
node.loc.source = this.tokenizer.code.slice(node.loc.start.offset, node.loc.end.offset);
|
|
1219
|
+
return node;
|
|
1220
|
+
}
|
|
1221
|
+
const valueToken = this.tokenizer.nextToken();
|
|
1222
|
+
if ((valueToken.type & ValueTokenType) === 0) {
|
|
1223
|
+
this.addError(ParseErrorCode.MISSING_PROP_ASSIGNMENT, `属性值不合法, "${valueToken.value}" 不合法`, valueToken.loc ?? this.tokenizer.emptyLoc(), node);
|
|
1224
|
+
node.loc.start = node.key.loc.start;
|
|
1225
|
+
node.loc.end = node.key.loc.end;
|
|
1226
|
+
node.loc.source = this.tokenizer.code.slice(node.loc.start.offset, node.loc.end.offset);
|
|
1227
|
+
return node;
|
|
1228
|
+
}
|
|
1229
|
+
node.value = this.parsePropertyValue();
|
|
1230
|
+
this.tokenizer.nextToken();
|
|
1179
1231
|
node.loc.start = node.key.loc.start;
|
|
1180
|
-
node.loc.end = node.value
|
|
1232
|
+
node.loc.end = node.value.loc.end;
|
|
1181
1233
|
node.loc.source = this.tokenizer.code.slice(node.loc.start.offset, node.loc.end.offset);
|
|
1182
1234
|
return node;
|
|
1183
1235
|
}
|
|
@@ -1186,7 +1238,7 @@ class Compiler {
|
|
|
1186
1238
|
node.key = this.tokenizer.token.value;
|
|
1187
1239
|
return node;
|
|
1188
1240
|
}
|
|
1189
|
-
|
|
1241
|
+
parseJsExp(node) {
|
|
1190
1242
|
const _this$tokenizer$_hook3 = this.tokenizer._hook({}),
|
|
1191
1243
|
_this$tokenizer$_hook4 = _slicedToArray(_this$tokenizer$_hook3, 2),
|
|
1192
1244
|
hookType = _this$tokenizer$_hook4[0],
|
|
@@ -1195,7 +1247,7 @@ class Compiler {
|
|
|
1195
1247
|
node.value = value;
|
|
1196
1248
|
return node;
|
|
1197
1249
|
}
|
|
1198
|
-
|
|
1250
|
+
parsePropertyValue(node) {
|
|
1199
1251
|
const _this$tokenizer$_hook5 = this.tokenizer._hook({}),
|
|
1200
1252
|
_this$tokenizer$_hook6 = _slicedToArray(_this$tokenizer$_hook5, 2),
|
|
1201
1253
|
hookType = _this$tokenizer$_hook6[0],
|
|
@@ -1204,6 +1256,15 @@ class Compiler {
|
|
|
1204
1256
|
node.value = value;
|
|
1205
1257
|
return node;
|
|
1206
1258
|
}
|
|
1259
|
+
parseName(node) {
|
|
1260
|
+
const _this$tokenizer$_hook7 = this.tokenizer._hook({}),
|
|
1261
|
+
_this$tokenizer$_hook8 = _slicedToArray(_this$tokenizer$_hook7, 2),
|
|
1262
|
+
hookType = _this$tokenizer$_hook8[0],
|
|
1263
|
+
value = _this$tokenizer$_hook8[1];
|
|
1264
|
+
node.type = hookType === 'dynamic' ? NodeType.DynamicValue : NodeType.StaticValue;
|
|
1265
|
+
node.value = value;
|
|
1266
|
+
return node;
|
|
1267
|
+
}
|
|
1207
1268
|
}
|
|
1208
1269
|
function NodeLoc(target, context) {
|
|
1209
1270
|
return function (_node) {
|