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.esm.js CHANGED
@@ -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["ELSE_WITHOUT_IF"] = 9008] = "ELSE_WITHOUT_IF";
54
- ParseErrorCode[ParseErrorCode["EMPTY_IF_BODY"] = 9009] = "EMPTY_IF_BODY";
55
- ParseErrorCode[ParseErrorCode["EMPTY_FOR_BODY"] = 9010] = "EMPTY_FOR_BODY";
56
- ParseErrorCode[ParseErrorCode["MISSING_FOR_COLLECTION"] = 9011] = "MISSING_FOR_COLLECTION";
57
- ParseErrorCode[ParseErrorCode["MISSING_FOR_SEMICOLON"] = 9012] = "MISSING_FOR_SEMICOLON";
58
- ParseErrorCode[ParseErrorCode["MISSING_FOR_ITEM"] = 9013] = "MISSING_FOR_ITEM";
59
- ParseErrorCode[ParseErrorCode["PIPE_IN_WRONG_CONTEXT"] = 9014] = "PIPE_IN_WRONG_CONTEXT";
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 {
@@ -588,8 +597,30 @@ class Tokenizer {
588
597
  this.setToken(TokenType.Dedent, '');
589
598
  return;
590
599
  }
591
- let realValue = value === 'null' ? null : value === 'undefined' ? undefined : value === 'false' ? false : value === 'true' ? true : value;
592
- this.setToken(TokenType.Identifier, realValue);
600
+ let realValue, tokenType;
601
+ switch (value) {
602
+ case 'null':
603
+ realValue = null;
604
+ tokenType = TokenType.Null;
605
+ break;
606
+ case 'undefined':
607
+ realValue = undefined;
608
+ tokenType = TokenType.Undefined;
609
+ break;
610
+ case 'false':
611
+ realValue = false;
612
+ tokenType = TokenType.Boolean;
613
+ break;
614
+ case 'true':
615
+ realValue = true;
616
+ tokenType = TokenType.Boolean;
617
+ break;
618
+ default:
619
+ realValue = value;
620
+ tokenType = TokenType.Identifier;
621
+ break;
622
+ }
623
+ this.setToken(tokenType, realValue);
593
624
  }
594
625
  str(char) {
595
626
  const startOffset = this.preI,
@@ -615,7 +646,7 @@ class Tokenizer {
615
646
  }
616
647
  value += nextC;
617
648
  }
618
- this.setToken(TokenType.Identifier, value);
649
+ this.setToken(TokenType.String, value);
619
650
  }
620
651
  number(char) {
621
652
  let value = char;
@@ -628,7 +659,7 @@ class Tokenizer {
628
659
  value += nextC;
629
660
  this.next();
630
661
  }
631
- this.setToken(TokenType.Identifier, Number(value));
662
+ this.setToken(TokenType.Number, Number(value));
632
663
  }
633
664
  eof() {
634
665
  this.setToken(TokenType.Eof, 'End Of File');
@@ -907,7 +938,7 @@ var NodeType = function (NodeType) {
907
938
  let _initProto;
908
939
  class Compiler {
909
940
  static {
910
- 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);
941
+ 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);
911
942
  _initProto = _applyDecs$e[0];
912
943
  }
913
944
  errors = (_initProto(this), []);
@@ -915,7 +946,10 @@ class Compiler {
915
946
  this.tokenizer = tokenizer;
916
947
  this.hooks = hooks;
917
948
  }
918
- addError(code, message, loc) {
949
+ addError(code, message, loc, node) {
950
+ if (node) {
951
+ node.hasError = true;
952
+ }
919
953
  this.errors.push({
920
954
  code,
921
955
  message,
@@ -1018,7 +1052,7 @@ class Compiler {
1018
1052
  parseElementNode(node) {
1019
1053
  const tagToken = this.tokenizer.token;
1020
1054
  if (!(tagToken.type & TokenType.Identifier)) {
1021
- this.addError(ParseErrorCode.INVALID_TAG_NAME, `无效的标签名,期望标识符但得到 "${tagToken.value}"`, tagToken.loc ?? this.tokenizer.emptyLoc());
1055
+ this.addError(ParseErrorCode.INVALID_TAG_NAME, `无效的标签名,期望标识符但得到 "${tagToken.value}"`, tagToken.loc ?? this.tokenizer.emptyLoc(), node);
1022
1056
  while (!(this.tokenizer.token.type & TokenType.NewLine) && !this.tokenizer.isEof()) {
1023
1057
  this.tokenizer.nextToken();
1024
1058
  }
@@ -1038,7 +1072,7 @@ class Compiler {
1038
1072
  parseConditionalNode(node) {
1039
1073
  const keyword = this.tokenizer.token.value;
1040
1074
  this.tokenizer.condExp();
1041
- const condition = this.parsePropertyValue();
1075
+ const condition = this.parseJsExp();
1042
1076
  this.tokenizer.nextToken();
1043
1077
  this.tokenizer.nextToken();
1044
1078
  node.type = keyword === 'if' ? NodeType.If : keyword === 'else' ? NodeType.Else : NodeType.Fail;
@@ -1051,22 +1085,22 @@ class Compiler {
1051
1085
  parseLoopNode(node) {
1052
1086
  const forLoc = this.tokenizer.token.loc ?? this.tokenizer.emptyLoc();
1053
1087
  this.tokenizer.nextToken();
1054
- const collection = this.parsePropertyValue();
1088
+ const collection = this.parseJsExp();
1055
1089
  if (!collection.value && collection.value !== 0) {
1056
- this.addError(ParseErrorCode.MISSING_FOR_COLLECTION, '"for" 缺少集合表达式', forLoc);
1090
+ this.addError(ParseErrorCode.MISSING_FOR_COLLECTION, '"for" 缺少集合表达式', forLoc, node);
1057
1091
  }
1058
1092
  const semicolonToken = this.tokenizer.nextToken();
1059
1093
  if (!(semicolonToken.type & TokenType.Semicolon)) {
1060
- this.addError(ParseErrorCode.MISSING_FOR_SEMICOLON, '"for" 语法:for <集合>; <item> [index][; key],缺少第一个 ";"', semicolonToken.loc ?? this.tokenizer.emptyLoc());
1094
+ this.addError(ParseErrorCode.MISSING_FOR_SEMICOLON, '"for" 语法:for <集合>; <item> [index][; key],缺少第一个 ";"', semicolonToken.loc ?? this.tokenizer.emptyLoc(), node);
1061
1095
  }
1062
1096
  const itemToken = this.tokenizer.nextToken();
1063
1097
  const isDestruct = itemToken.type === TokenType.InsertionExp;
1064
1098
  if (isDestruct) {
1065
1099
  itemToken.value = '{' + itemToken.value + '}';
1066
1100
  }
1067
- const item = this.parsePropertyValue();
1101
+ const item = this.parseJsExp();
1068
1102
  if (!item.value && item.value !== 0) {
1069
- this.addError(ParseErrorCode.MISSING_FOR_ITEM, '"for" 缺少 item 变量名', itemToken.loc ?? this.tokenizer.emptyLoc());
1103
+ this.addError(ParseErrorCode.MISSING_FOR_ITEM, '"for" 缺少 item 变量名', itemToken.loc ?? this.tokenizer.emptyLoc(), node);
1070
1104
  }
1071
1105
  let char = this.tokenizer.peekChar(),
1072
1106
  key,
@@ -1075,16 +1109,16 @@ class Compiler {
1075
1109
  this.tokenizer.nextToken();
1076
1110
  if (this.tokenizer.peekChar() !== '\n') {
1077
1111
  this.tokenizer.jsExp();
1078
- key = this.parsePropertyValue();
1112
+ key = this.parseJsExp();
1079
1113
  }
1080
1114
  } else if (char === '\n') ; else {
1081
1115
  this.tokenizer.nextToken();
1082
- index = this.parsePropertyValue();
1116
+ index = this.parseJsExp();
1083
1117
  if (this.tokenizer.peekChar() === ';') {
1084
1118
  this.tokenizer.nextToken();
1085
1119
  if (this.tokenizer.peekChar() !== '\n') {
1086
1120
  this.tokenizer.jsExp();
1087
- key = this.parsePropertyValue();
1121
+ key = this.parseJsExp();
1088
1122
  }
1089
1123
  }
1090
1124
  }
@@ -1118,23 +1152,41 @@ class Compiler {
1118
1152
  attributeList() {
1119
1153
  const props = [];
1120
1154
  while (!(this.tokenizer.token.type & TokenType.NewLine) && !(this.tokenizer.token.type & TokenType.Pipe) && !this.tokenizer.isEof()) {
1121
- props.push(this.parseProperty());
1155
+ const prop = this.parseProperty();
1156
+ if (prop) {
1157
+ props.push(prop);
1158
+ }
1122
1159
  }
1123
1160
  return props;
1124
1161
  }
1125
1162
  parseProperty(node) {
1126
1163
  node.type = NodeType.Property;
1127
- node.key = this.parsePropertyKey();
1128
- const token = this.tokenizer.nextToken();
1129
- if (token.value === '=') {
1130
- this.tokenizer.nextToken();
1131
- node.value = this.parsePropertyValue();
1164
+ if (this.tokenizer.token.type !== TokenType.Identifier) {
1165
+ this.addError(ParseErrorCode.INVALID_PROP_KEY, `属性名 "${this.tokenizer.token.value}" 不合法`, this.tokenizer.token.loc ?? this.tokenizer.emptyLoc(), node);
1132
1166
  this.tokenizer.nextToken();
1133
- } else {
1134
- this.addError(ParseErrorCode.MISSING_ASSIGN, `属性 "${node.key.key}" 缺少 "=" 赋值符号`, node.key.loc ?? this.tokenizer.emptyLoc());
1167
+ return null;
1135
1168
  }
1169
+ node.key = this.parsePropertyKey();
1170
+ const token = this.tokenizer.nextToken();
1171
+ if (token.value !== '=') {
1172
+ this.addError(ParseErrorCode.MISSING_ASSIGN, `属性 "${node.key.key}" 缺少 "=" 赋值符号`, node.key.loc ?? this.tokenizer.emptyLoc(), node);
1173
+ node.loc.start = node.key.loc.start;
1174
+ node.loc.end = node.key.loc.end;
1175
+ node.loc.source = this.tokenizer.code.slice(node.loc.start.offset, node.loc.end.offset);
1176
+ return node;
1177
+ }
1178
+ const valueToken = this.tokenizer.nextToken();
1179
+ if ((valueToken.type & ValueTokenType) === 0) {
1180
+ this.addError(ParseErrorCode.MISSING_PROP_ASSIGNMENT, `属性值不合法, "${valueToken.value}" 不合法`, valueToken.loc ?? this.tokenizer.emptyLoc(), node);
1181
+ node.loc.start = node.key.loc.start;
1182
+ node.loc.end = node.key.loc.end;
1183
+ node.loc.source = this.tokenizer.code.slice(node.loc.start.offset, node.loc.end.offset);
1184
+ return node;
1185
+ }
1186
+ node.value = this.parsePropertyValue();
1187
+ this.tokenizer.nextToken();
1136
1188
  node.loc.start = node.key.loc.start;
1137
- node.loc.end = node.value ? node.value.loc.end : node.key.loc.end;
1189
+ node.loc.end = node.value.loc.end;
1138
1190
  node.loc.source = this.tokenizer.code.slice(node.loc.start.offset, node.loc.end.offset);
1139
1191
  return node;
1140
1192
  }
@@ -1143,7 +1195,7 @@ class Compiler {
1143
1195
  node.key = this.tokenizer.token.value;
1144
1196
  return node;
1145
1197
  }
1146
- parsePropertyValue(node) {
1198
+ parseJsExp(node) {
1147
1199
  const _this$tokenizer$_hook3 = this.tokenizer._hook({}),
1148
1200
  _this$tokenizer$_hook4 = _slicedToArray(_this$tokenizer$_hook3, 2),
1149
1201
  hookType = _this$tokenizer$_hook4[0],
@@ -1152,7 +1204,7 @@ class Compiler {
1152
1204
  node.value = value;
1153
1205
  return node;
1154
1206
  }
1155
- parseName(node) {
1207
+ parsePropertyValue(node) {
1156
1208
  const _this$tokenizer$_hook5 = this.tokenizer._hook({}),
1157
1209
  _this$tokenizer$_hook6 = _slicedToArray(_this$tokenizer$_hook5, 2),
1158
1210
  hookType = _this$tokenizer$_hook6[0],
@@ -1161,6 +1213,15 @@ class Compiler {
1161
1213
  node.value = value;
1162
1214
  return node;
1163
1215
  }
1216
+ parseName(node) {
1217
+ const _this$tokenizer$_hook7 = this.tokenizer._hook({}),
1218
+ _this$tokenizer$_hook8 = _slicedToArray(_this$tokenizer$_hook7, 2),
1219
+ hookType = _this$tokenizer$_hook8[0],
1220
+ value = _this$tokenizer$_hook8[1];
1221
+ node.type = hookType === 'dynamic' ? NodeType.DynamicValue : NodeType.StaticValue;
1222
+ node.value = value;
1223
+ return node;
1224
+ }
1164
1225
  }
1165
1226
  function NodeLoc(target, context) {
1166
1227
  return function (_node) {