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