mol_compare_deep 0.0.413 → 0.0.415

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/node.test.js CHANGED
@@ -603,7 +603,8 @@ var $;
603
603
  return function $mol_log3_logger(event) {
604
604
  if (!event.time)
605
605
  event = { time: new Date().toISOString(), ...event };
606
- const tree = this.$mol_tree.fromJSON(event).clone({ type });
606
+ let tree = this.$mol_tree2_from_json(event);
607
+ tree = tree.struct(type, tree.kids);
607
608
  let str = color(tree.toString());
608
609
  this.console[level](str);
609
610
  const self = this;
@@ -878,248 +879,219 @@ var $;
878
879
  "use strict";
879
880
  var $;
880
881
  (function ($) {
881
- $.$mol_tree_convert = Symbol('$mol_tree_convert');
882
- class $mol_tree extends $mol_object2 {
883
- type;
884
- data;
885
- sub;
886
- baseUri;
882
+ class $mol_span extends $mol_object2 {
883
+ uri;
884
+ source;
887
885
  row;
888
886
  col;
889
887
  length;
890
- constructor(config = {}) {
888
+ constructor(uri, source, row, col, length) {
891
889
  super();
892
- this.type = config.type || '';
893
- if (config.value !== undefined) {
894
- var sub = $mol_tree.values(config.value);
895
- if (config.type || sub.length > 1) {
896
- this.sub = [...sub, ...(config.sub || [])];
897
- this.data = config.data || '';
898
- }
899
- else {
900
- this.data = sub[0].data;
901
- this.sub = config.sub || [];
902
- }
903
- }
904
- else {
905
- this.data = config.data || '';
906
- this.sub = config.sub || [];
907
- }
908
- this.baseUri = config.baseUri || '';
909
- this.row = config.row || 0;
910
- this.col = config.col || 0;
911
- this.length = config.length || 0;
912
- }
913
- static values(str, baseUri) {
914
- return str.split('\n').map((data, index) => new $mol_tree({
915
- data: data,
916
- baseUri: baseUri,
917
- row: index + 1,
918
- length: data.length,
919
- }));
890
+ this.uri = uri;
891
+ this.source = source;
892
+ this.row = row;
893
+ this.col = col;
894
+ this.length = length;
895
+ this[Symbol.toStringTag] = `${this.uri}#${this.row}:${this.col}/${this.length}`;
920
896
  }
921
- clone(config = {}) {
922
- return new $mol_tree({
923
- type: ('type' in config) ? config.type : this.type,
924
- data: ('data' in config) ? config.data : this.data,
925
- sub: ('sub' in config) ? config.sub : this.sub,
926
- baseUri: ('baseUri' in config) ? config.baseUri : this.baseUri,
927
- row: ('row' in config) ? config.row : this.row,
928
- col: ('col' in config) ? config.col : this.col,
929
- length: ('length' in config) ? config.length : this.length,
930
- value: config.value
931
- });
897
+ static unknown = $mol_span.begin('?');
898
+ static begin(uri, source = '') {
899
+ return new $mol_span(uri, source, 1, 1, 0);
900
+ }
901
+ static end(uri, source) {
902
+ return new $mol_span(uri, source, 1, source.length + 1, length);
932
903
  }
933
- make(config) {
934
- return new $mol_tree({
935
- baseUri: this.baseUri,
904
+ static entire(uri, source) {
905
+ return new $mol_span(uri, source, 1, 1, source.length);
906
+ }
907
+ toString() {
908
+ return this[Symbol.toStringTag];
909
+ }
910
+ toJSON() {
911
+ return {
912
+ uri: this.uri,
936
913
  row: this.row,
937
914
  col: this.col,
938
- length: this.length,
939
- ...config,
940
- });
915
+ length: this.length
916
+ };
941
917
  }
942
- make_data(value, sub) {
943
- return this.make({ value, sub });
944
- }
945
- make_struct(type, sub) {
946
- return this.make({ type, sub });
947
- }
948
- static fromString(str, baseUri) {
949
- var root = new $mol_tree({ baseUri: baseUri });
950
- var stack = [root];
951
- var row = 0;
952
- var prefix = str.replace(/^\n?(\t*)[\s\S]*/, '$1');
953
- var lines = str.replace(new RegExp('^\\t{0,' + prefix.length + '}', 'mg'), '').split('\n');
954
- lines.forEach(line => {
955
- ++row;
956
- var chunks = /^(\t*)((?:[^\n\t\\ ]+ *)*)(\\[^\n]*)?(.*?)(?:$|\n)/m.exec(line);
957
- if (!chunks || chunks[4])
958
- return this.$.$mol_fail(new Error(`Syntax error at ${baseUri}:${row}\n${line}`));
959
- var indent = chunks[1];
960
- var path = chunks[2];
961
- var data = chunks[3];
962
- var deep = indent.length;
963
- var types = path ? path.replace(/ $/, '').split(/ +/) : [];
964
- if (stack.length <= deep)
965
- return this.$.$mol_fail(new Error(`Too many tabs at ${baseUri}:${row}\n${line}`));
966
- stack.length = deep + 1;
967
- var parent = stack[deep];
968
- let col = deep;
969
- types.forEach(type => {
970
- if (!type)
971
- return this.$.$mol_fail(new Error(`Unexpected space symbol ${baseUri}:${row}\n${line}`));
972
- var next = new $mol_tree({ type, baseUri, row, col, length: type.length });
973
- const parent_sub = parent.sub;
974
- parent_sub.push(next);
975
- parent = next;
976
- col += type.length + 1;
977
- });
978
- if (data) {
979
- var next = new $mol_tree({ data: data.substring(1), baseUri, row, col, length: data.length });
980
- const parent_sub = parent.sub;
981
- parent_sub.push(next);
982
- parent = next;
983
- }
984
- stack.push(parent);
985
- });
986
- return root;
987
- }
988
- static fromJSON(json, baseUri = '') {
989
- switch (true) {
990
- case typeof json === 'boolean':
991
- case typeof json === 'number':
992
- case json === null:
993
- return new $mol_tree({
994
- type: String(json),
995
- baseUri: baseUri
996
- });
997
- case typeof json === 'string':
998
- return new $mol_tree({
999
- value: json,
1000
- baseUri: baseUri
1001
- });
1002
- case Array.isArray(json):
1003
- return new $mol_tree({
1004
- type: "/",
1005
- sub: json.map(json => $mol_tree.fromJSON(json, baseUri))
1006
- });
1007
- case json instanceof Date:
1008
- return new $mol_tree({
1009
- value: json.toISOString(),
1010
- baseUri: baseUri
1011
- });
1012
- default:
1013
- if (typeof json[$.$mol_tree_convert] === 'function') {
1014
- return json[$.$mol_tree_convert]();
1015
- }
1016
- if (typeof json.toJSON === 'function') {
1017
- return $mol_tree.fromJSON(json.toJSON());
1018
- }
1019
- if (json instanceof Error) {
1020
- const { name, message, stack } = json;
1021
- json = { ...json, name, message, stack };
1022
- }
1023
- var sub = [];
1024
- for (var key in json) {
1025
- if (json[key] === undefined)
1026
- continue;
1027
- const subsub = $mol_tree.fromJSON(json[key], baseUri);
1028
- if (/^[^\n\t\\ ]+$/.test(key)) {
1029
- var child = new $mol_tree({
1030
- type: key,
1031
- baseUri: baseUri,
1032
- sub: [subsub],
1033
- });
1034
- }
1035
- else {
1036
- var child = new $mol_tree({
1037
- value: key,
1038
- baseUri: baseUri,
1039
- sub: [subsub],
1040
- });
1041
- }
1042
- sub.push(child);
1043
- }
1044
- return new $mol_tree({
1045
- type: "*",
1046
- sub: sub,
1047
- baseUri: baseUri
1048
- });
1049
- }
918
+ error(message, Class = Error) {
919
+ return new Class(`${message}${this}`);
920
+ }
921
+ span(row, col, length) {
922
+ return new $mol_span(this.uri, this.source, row, col, length);
1050
923
  }
1051
- get uri() {
1052
- return this.baseUri + '#' + this.row + ':' + this.col;
924
+ after(length = 0) {
925
+ return new $mol_span(this.uri, this.source, this.row, this.col + this.length, length);
926
+ }
927
+ slice(begin, end = -1) {
928
+ let len = this.length;
929
+ if (begin < 0)
930
+ begin += len;
931
+ if (end < 0)
932
+ end += len;
933
+ if (begin < 0 || begin > len)
934
+ this.$.$mol_fail(`Begin value '${begin}' out of range ${this}`);
935
+ if (end < 0 || end > len)
936
+ this.$.$mol_fail(`End value '${end}' out of range ${this}`);
937
+ if (end < begin)
938
+ this.$.$mol_fail(`End value '${end}' can't be less than begin value ${this}`);
939
+ return this.span(this.row, this.col + begin, end - begin);
940
+ }
941
+ }
942
+ $.$mol_span = $mol_span;
943
+ })($ || ($ = {}));
944
+ //mol/span/span.ts
945
+ ;
946
+ "use strict";
947
+ var $;
948
+ (function ($_1) {
949
+ $mol_test({
950
+ 'span for same uri'($) {
951
+ const span = new $mol_span('test.ts', '', 1, 3, 4);
952
+ const child = span.span(4, 5, 8);
953
+ $mol_assert_equal(child.uri, 'test.ts');
954
+ $mol_assert_equal(child.row, 4);
955
+ $mol_assert_equal(child.col, 5);
956
+ $mol_assert_equal(child.length, 8);
957
+ },
958
+ 'span after of given position'($) {
959
+ const span = new $mol_span('test.ts', '', 1, 3, 4);
960
+ const child = span.after(11);
961
+ $mol_assert_equal(child.uri, 'test.ts');
962
+ $mol_assert_equal(child.row, 1);
963
+ $mol_assert_equal(child.col, 7);
964
+ $mol_assert_equal(child.length, 11);
965
+ },
966
+ 'slice span - regular'($) {
967
+ const span = new $mol_span('test.ts', '', 1, 3, 5);
968
+ const child = span.slice(1, 4);
969
+ $mol_assert_equal(child.row, 1);
970
+ $mol_assert_equal(child.col, 4);
971
+ $mol_assert_equal(child.length, 3);
972
+ const child2 = span.slice(2, 2);
973
+ $mol_assert_equal(child2.col, 5);
974
+ $mol_assert_equal(child2.length, 0);
975
+ },
976
+ 'slice span - negative'($) {
977
+ const span = new $mol_span('test.ts', '', 1, 3, 5);
978
+ const child = span.slice(-3, -1);
979
+ $mol_assert_equal(child.row, 1);
980
+ $mol_assert_equal(child.col, 5);
981
+ $mol_assert_equal(child.length, 2);
982
+ },
983
+ 'slice span - out of range'($) {
984
+ const span = new $mol_span('test.ts', '', 1, 3, 5);
985
+ $mol_assert_fail(() => span.slice(-1, 3));
986
+ $mol_assert_fail(() => span.slice(1, 6));
987
+ $mol_assert_fail(() => span.slice(1, 10));
988
+ },
989
+ 'error handling'($) {
990
+ const span = new $mol_span('test.ts', '', 1, 3, 4);
991
+ const error = span.error('Some error\n');
992
+ $mol_assert_equal(error.message, 'Some error\ntest.ts#1:3/4');
1053
993
  }
1054
- toString(prefix = '') {
1055
- var output = '';
1056
- if (this.type.length) {
994
+ });
995
+ })($ || ($ = {}));
996
+ //mol/span/span.test.ts
997
+ ;
998
+ "use strict";
999
+ var $;
1000
+ (function ($) {
1001
+ function $mol_tree2_to_string(tree) {
1002
+ let output = [];
1003
+ function dump(tree, prefix = '') {
1004
+ if (tree.type.length) {
1057
1005
  if (!prefix.length) {
1058
1006
  prefix = "\t";
1059
1007
  }
1060
- output += this.type;
1061
- if (this.sub.length == 1) {
1062
- return output + ' ' + this.sub[0].toString(prefix);
1008
+ output.push(tree.type);
1009
+ if (tree.kids.length == 1) {
1010
+ output.push(' ');
1011
+ dump(tree.kids[0], prefix);
1012
+ return;
1063
1013
  }
1064
- output += "\n";
1014
+ output.push("\n");
1065
1015
  }
1066
- else if (this.data.length || prefix.length) {
1067
- output += "\\" + this.data + "\n";
1016
+ else if (tree.value.length || prefix.length) {
1017
+ output.push("\\" + tree.value + "\n");
1068
1018
  }
1069
- for (var child of this.sub) {
1070
- output += prefix;
1071
- output += child.toString(prefix + "\t");
1019
+ for (const kid of tree.kids) {
1020
+ output.push(prefix);
1021
+ dump(kid, prefix + "\t");
1072
1022
  }
1073
- return output;
1074
1023
  }
1075
- toJSON() {
1076
- if (!this.type)
1077
- return this.value;
1078
- if (this.type === 'true')
1079
- return true;
1080
- if (this.type === 'false')
1081
- return false;
1082
- if (this.type === 'null')
1083
- return null;
1084
- if (this.type === '*') {
1085
- var obj = {};
1086
- for (var child of this.sub) {
1087
- if (child.type === '-')
1088
- continue;
1089
- var key = child.type || child.clone({ sub: child.sub.slice(0, child.sub.length - 1) }).value;
1090
- var val = child.sub[child.sub.length - 1].toJSON();
1091
- if (val !== undefined)
1092
- obj[key] = val;
1093
- }
1094
- return obj;
1095
- }
1096
- if (this.type === '/') {
1097
- var res = [];
1098
- this.sub.forEach(child => {
1099
- if (child.type === '-')
1100
- return;
1101
- var val = child.toJSON();
1102
- if (val !== undefined)
1103
- res.push(val);
1024
+ dump(tree);
1025
+ return output.join('');
1026
+ }
1027
+ $.$mol_tree2_to_string = $mol_tree2_to_string;
1028
+ })($ || ($ = {}));
1029
+ //mol/tree2/to/string/string.ts
1030
+ ;
1031
+ "use strict";
1032
+ var $;
1033
+ (function ($) {
1034
+ class $mol_tree2 extends Object {
1035
+ type;
1036
+ value;
1037
+ kids;
1038
+ span;
1039
+ constructor(type, value, kids, span) {
1040
+ super();
1041
+ this.type = type;
1042
+ this.value = value;
1043
+ this.kids = kids;
1044
+ this.span = span;
1045
+ this[Symbol.toStringTag] = type || '\\' + value;
1046
+ }
1047
+ static list(kids, span = $mol_span.unknown) {
1048
+ return new $mol_tree2('', '', kids, span);
1049
+ }
1050
+ list(kids) {
1051
+ return $mol_tree2.list(kids, this.span);
1052
+ }
1053
+ static data(value, kids = [], span = $mol_span.unknown) {
1054
+ const chunks = value.split('\n');
1055
+ if (chunks.length > 1) {
1056
+ let kid_span = span.span(span.row, span.col, 0);
1057
+ const data = chunks.map(chunk => {
1058
+ kid_span = kid_span.after(chunk.length);
1059
+ return new $mol_tree2('', chunk, [], kid_span);
1104
1060
  });
1105
- return res;
1061
+ kids = [...data, ...kids];
1062
+ value = '';
1106
1063
  }
1107
- if (this.type === 'time') {
1108
- return new Date(this.value);
1064
+ return new $mol_tree2('', value, kids, span);
1065
+ }
1066
+ data(value, kids = []) {
1067
+ return $mol_tree2.data(value, kids, this.span);
1068
+ }
1069
+ static struct(type, kids = [], span = $mol_span.unknown) {
1070
+ if (/[ \n\t\\]/.test(type)) {
1071
+ $$.$mol_fail(span.error(`Wrong type ${JSON.stringify(type)}`));
1109
1072
  }
1110
- const numb = Number(this.type);
1111
- if (!Number.isNaN(numb) || this.type === 'NaN')
1112
- return numb;
1113
- throw new Error(`Unknown type (${this.type}) at ${this.uri}`);
1073
+ return new $mol_tree2(type, '', kids, span);
1074
+ }
1075
+ struct(type, kids = []) {
1076
+ return $mol_tree2.struct(type, kids, this.span);
1077
+ }
1078
+ clone(kids, span = this.span) {
1079
+ return new $mol_tree2(this.type, this.value, kids, span);
1114
1080
  }
1115
- get value() {
1081
+ text() {
1116
1082
  var values = [];
1117
- for (var child of this.sub) {
1118
- if (child.type)
1083
+ for (var kid of this.kids) {
1084
+ if (kid.type)
1119
1085
  continue;
1120
- values.push(child.value);
1086
+ values.push(kid.value);
1121
1087
  }
1122
- return this.data + values.join("\n");
1088
+ return this.value + values.join('\n');
1089
+ }
1090
+ static fromString(str, uri = 'unknown') {
1091
+ return $$.$mol_tree2_from_string(str, uri);
1092
+ }
1093
+ toString() {
1094
+ return $$.$mol_tree2_to_string(this);
1123
1095
  }
1124
1096
  insert(value, ...path) {
1125
1097
  if (path.length === 0)
@@ -1127,143 +1099,375 @@ var $;
1127
1099
  const type = path[0];
1128
1100
  if (typeof type === 'string') {
1129
1101
  let replaced = false;
1130
- const sub = this.sub.map((item, index) => {
1102
+ const sub = this.kids.map((item, index) => {
1131
1103
  if (item.type !== type)
1132
1104
  return item;
1133
1105
  replaced = true;
1134
1106
  return item.insert(value, ...path.slice(1));
1135
- });
1136
- if (!replaced)
1137
- sub.push(new $mol_tree({ type }).insert(value, ...path.slice(1)));
1138
- return this.clone({ sub });
1107
+ }).filter(Boolean);
1108
+ if (!replaced && value) {
1109
+ sub.push(this.struct(type, []).insert(value, ...path.slice(1)));
1110
+ }
1111
+ return this.clone(sub);
1139
1112
  }
1140
1113
  else if (typeof type === 'number') {
1141
- const sub = this.sub.slice();
1142
- sub[type] = (sub[type] || new $mol_tree).insert(value, ...path.slice(1));
1143
- return this.clone({ sub });
1114
+ const sub = this.kids.slice();
1115
+ sub[type] = (sub[type] || this.list([]))
1116
+ .insert(value, ...path.slice(1));
1117
+ return this.clone(sub.filter(Boolean));
1144
1118
  }
1145
1119
  else {
1146
- return this.clone({ sub: ((this.sub.length === 0) ? [new $mol_tree()] : this.sub).map(item => item.insert(value, ...path.slice(1))) });
1120
+ const kids = ((this.kids.length === 0) ? [this.list([])] : this.kids)
1121
+ .map(item => item.insert(value, ...path.slice(1)))
1122
+ .filter(Boolean);
1123
+ return this.clone(kids);
1147
1124
  }
1148
1125
  }
1149
1126
  select(...path) {
1150
- var next = [this];
1151
- for (var type of path) {
1127
+ let next = [this];
1128
+ for (const type of path) {
1152
1129
  if (!next.length)
1153
1130
  break;
1154
- var prev = next;
1131
+ const prev = next;
1155
1132
  next = [];
1156
1133
  for (var item of prev) {
1157
1134
  switch (typeof (type)) {
1158
1135
  case 'string':
1159
- for (var child of item.sub) {
1160
- if (!type || (child.type == type)) {
1136
+ for (var child of item.kids) {
1137
+ if (child.type == type) {
1161
1138
  next.push(child);
1162
1139
  }
1163
1140
  }
1164
1141
  break;
1165
1142
  case 'number':
1166
- if (type < item.sub.length)
1167
- next.push(item.sub[type]);
1143
+ if (type < item.kids.length)
1144
+ next.push(item.kids[type]);
1168
1145
  break;
1169
- default: next.push(...item.sub);
1146
+ default: next.push(...item.kids);
1170
1147
  }
1171
1148
  }
1172
1149
  }
1173
- return new $mol_tree({ sub: next });
1150
+ return this.list(next);
1174
1151
  }
1175
1152
  filter(path, value) {
1176
- var sub = this.sub.filter(function (item) {
1153
+ const sub = this.kids.filter(item => {
1177
1154
  var found = item.select(...path);
1178
- if (value == null) {
1179
- return Boolean(found.sub.length);
1155
+ if (value === undefined) {
1156
+ return Boolean(found.kids.length);
1180
1157
  }
1181
1158
  else {
1182
- return found.sub.some(child => child.value == value);
1159
+ return found.kids.some(child => child.value == value);
1183
1160
  }
1184
1161
  });
1185
- return new $mol_tree({ sub: sub });
1186
- }
1187
- transform(visit, stack = []) {
1188
- const sub_stack = [this, ...stack];
1189
- return visit(sub_stack, () => this.sub.map(node => node.transform(visit, sub_stack)).filter(n => n));
1190
- }
1191
- hack(context) {
1192
- const sub = [].concat(...this.sub.map(child => {
1193
- const handle = context[child.type] || context[''];
1194
- if (!handle)
1195
- $mol_fail(child.error('Handler not defined'));
1196
- return handle(child, context);
1162
+ return this.clone(sub);
1163
+ }
1164
+ hack(belt, context = {}) {
1165
+ return [].concat(...this.kids.map(child => {
1166
+ let handle = belt[child.type] || belt[''];
1167
+ if (!handle || handle === Object.prototype[child.type]) {
1168
+ handle = (input, belt, context) => [
1169
+ input.clone(input.hack(belt, context), context.span)
1170
+ ];
1171
+ }
1172
+ try {
1173
+ return handle(child, belt, context);
1174
+ }
1175
+ catch (error) {
1176
+ error.message += `\n${child.clone([])}${child.span}`;
1177
+ $mol_fail_hidden(error);
1178
+ }
1197
1179
  }));
1198
- return this.clone({ sub });
1199
1180
  }
1200
- error(message) {
1201
- return new Error(`${message}:\n${this} ${this.baseUri}:${this.row}:${this.col}`);
1181
+ error(message, Class = Error) {
1182
+ return this.span.error(`${message}\n${this.clone([])}`, Class);
1183
+ }
1184
+ }
1185
+ $.$mol_tree2 = $mol_tree2;
1186
+ class $mol_tree2_empty extends $mol_tree2 {
1187
+ constructor() {
1188
+ super('', '', [], $mol_span.unknown);
1202
1189
  }
1203
1190
  }
1204
- $.$mol_tree = $mol_tree;
1191
+ $.$mol_tree2_empty = $mol_tree2_empty;
1205
1192
  })($ || ($ = {}));
1206
- //mol/tree/tree.ts
1193
+ //mol/tree2/tree2.ts
1207
1194
  ;
1208
1195
  "use strict";
1209
1196
  var $;
1210
1197
  (function ($_1) {
1211
1198
  $mol_test({
1212
- 'tree parsing'() {
1213
- $mol_assert_equal($mol_tree.fromString("foo\nbar\n").sub.length, 2);
1214
- $mol_assert_equal($mol_tree.fromString("foo\nbar\n").sub[1].type, "bar");
1215
- $mol_assert_equal($mol_tree.fromString("foo\n\n\n").sub.length, 1);
1216
- $mol_assert_equal($mol_tree.fromString("=foo\n\\bar\n").sub.length, 2);
1217
- $mol_assert_equal($mol_tree.fromString("=foo\n\\bar\n").sub[1].data, "bar");
1218
- $mol_assert_equal($mol_tree.fromString("foo bar \\pol").sub[0].sub[0].sub[0].data, "pol");
1219
- $mol_assert_equal($mol_tree.fromString("foo bar\n\t\\pol\n\t\\men").sub[0].sub[0].sub[1].data, "men");
1220
- $mol_assert_equal($mol_tree.fromString('foo bar \\text\n').toString(), 'foo bar \\text\n');
1199
+ 'inserting'($) {
1200
+ $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
1201
+ .insert($mol_tree2.struct('x'), 'a', 'b', 'c')
1202
+ .toString(), 'a b x\n');
1203
+ $mol_assert_equal($.$mol_tree2_from_string('a b\n')
1204
+ .insert($mol_tree2.struct('x'), 'a', 'b', 'c', 'd')
1205
+ .toString(), 'a b c x\n');
1206
+ $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
1207
+ .insert($mol_tree2.struct('x'), 0, 0, 0)
1208
+ .toString(), 'a b x\n');
1209
+ $mol_assert_equal($.$mol_tree2_from_string('a b\n')
1210
+ .insert($mol_tree2.struct('x'), 0, 0, 0, 0)
1211
+ .toString(), 'a b \\\n\tx\n');
1212
+ $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
1213
+ .insert($mol_tree2.struct('x'), null, null, null)
1214
+ .toString(), 'a b x\n');
1215
+ $mol_assert_equal($.$mol_tree2_from_string('a b\n')
1216
+ .insert($mol_tree2.struct('x'), null, null, null, null)
1217
+ .toString(), 'a b \\\n\tx\n');
1221
1218
  },
1222
- 'inserting'() {
1223
- $mol_assert_equal($mol_tree.fromString('a b c d').insert(new $mol_tree, 'a', 'b', 'c').toString(), 'a b \\\n');
1224
- $mol_assert_equal($mol_tree.fromString('a b').insert(new $mol_tree, 'a', 'b', 'c', 'd').toString(), 'a b c \\\n');
1225
- $mol_assert_equal($mol_tree.fromString('a b c d').insert(new $mol_tree, 0, 0, 0).toString(), 'a b \\\n');
1226
- $mol_assert_equal($mol_tree.fromString('a b').insert(new $mol_tree, 0, 0, 0, 0).toString(), 'a b \\\n\t\\\n');
1227
- $mol_assert_equal($mol_tree.fromString('a b c d').insert(new $mol_tree, null, null, null).toString(), 'a b \\\n');
1228
- $mol_assert_equal($mol_tree.fromString('a b').insert(new $mol_tree, null, null, null, null).toString(), 'a b \\\n\t\\\n');
1219
+ 'deleting'($) {
1220
+ $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
1221
+ .insert(null, 'a', 'b', 'c')
1222
+ .toString(), 'a b\n');
1223
+ $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
1224
+ .insert(null, 0, 0, 0)
1225
+ .toString(), 'a b\n');
1229
1226
  },
1230
- 'fromJSON'() {
1231
- $mol_assert_equal($mol_tree.fromJSON([]).toString(), '/\n');
1232
- $mol_assert_equal($mol_tree.fromJSON([false, true]).toString(), '/\n\tfalse\n\ttrue\n');
1233
- $mol_assert_equal($mol_tree.fromJSON([0, 1, 2.3]).toString(), '/\n\t0\n\t1\n\t2.3\n');
1234
- $mol_assert_equal($mol_tree.fromJSON(['', 'foo', 'bar\nbaz']).toString(), '/\n\t\\\n\t\\foo\n\t\\\n\t\t\\bar\n\t\t\\baz\n');
1235
- $mol_assert_equal($mol_tree.fromJSON({ 'foo': false, 'bar\nbaz': 'lol' }).toString(), '*\n\tfoo false\n\t\\\n\t\t\\bar\n\t\t\\baz\n\t\t\\lol\n');
1227
+ 'hack'($) {
1228
+ const res = $.$mol_tree2_from_string(`foo bar xxx\n`)
1229
+ .hack({
1230
+ 'bar': (input, belt) => [input.struct('777', input.hack(belt))],
1231
+ });
1232
+ $mol_assert_equal(res.toString(), 'foo 777 xxx\n');
1236
1233
  },
1237
- 'toJSON'() {
1238
- $mol_assert_equal(JSON.stringify($mol_tree.fromString('/\n').sub[0]), '[]');
1239
- $mol_assert_equal(JSON.stringify($mol_tree.fromString('/\n\tfalse\n\ttrue\n').sub[0]), '[false,true]');
1240
- $mol_assert_equal(JSON.stringify($mol_tree.fromString('/\n\t0\n\t1\n\t2.3\n').sub[0]), '[0,1,2.3]');
1241
- $mol_assert_equal(JSON.stringify($mol_tree.fromString('/\n\t\\\n\t\\foo\n\t\\\n\t\t\\bar\n\t\t\\baz\n').sub[0]), '["","foo","bar\\nbaz"]');
1242
- $mol_assert_equal(JSON.stringify($mol_tree.fromString('*\n\tfoo false\n\t\\\n\t\t\\bar\n\t\t\\baz\n\t\t\\lol\n').sub[0]), '{"foo":false,"bar\\nbaz":"lol"}');
1234
+ });
1235
+ })($ || ($ = {}));
1236
+ //mol/tree2/tree2.test.ts
1237
+ ;
1238
+ "use strict";
1239
+ var $;
1240
+ (function ($) {
1241
+ class $mol_error_syntax extends SyntaxError {
1242
+ reason;
1243
+ line;
1244
+ span;
1245
+ constructor(reason, line, span) {
1246
+ super(`${reason}\n${span}\n${line.substring(0, span.col - 1).replace(/\S/g, ' ')}${''.padEnd(span.length, '!')}\n${line}`);
1247
+ this.reason = reason;
1248
+ this.line = line;
1249
+ this.span = span;
1250
+ }
1251
+ }
1252
+ $.$mol_error_syntax = $mol_error_syntax;
1253
+ })($ || ($ = {}));
1254
+ //mol/error/syntax/syntax.ts
1255
+ ;
1256
+ "use strict";
1257
+ var $;
1258
+ (function ($) {
1259
+ function $mol_tree2_from_string(str, uri = '?') {
1260
+ const span = $mol_span.entire(uri, str);
1261
+ var root = $mol_tree2.list([], span);
1262
+ var stack = [root];
1263
+ var pos = 0, row = 0, min_indent = 0;
1264
+ while (str.length > pos) {
1265
+ var indent = 0;
1266
+ var line_start = pos;
1267
+ row++;
1268
+ while (str.length > pos && str[pos] == '\t') {
1269
+ indent++;
1270
+ pos++;
1271
+ }
1272
+ if (!root.kids.length) {
1273
+ min_indent = indent;
1274
+ }
1275
+ indent -= min_indent;
1276
+ if (indent < 0 || indent >= stack.length) {
1277
+ const sp = span.span(row, 1, pos - line_start);
1278
+ while (str.length > pos && str[pos] != '\n') {
1279
+ pos++;
1280
+ }
1281
+ if (indent < 0) {
1282
+ if (str.length > pos) {
1283
+ this.$mol_fail(new this.$mol_error_syntax(`Too few tabs`, str.substring(line_start, pos), sp));
1284
+ }
1285
+ }
1286
+ else {
1287
+ this.$mol_fail(new this.$mol_error_syntax(`Too many tabs`, str.substring(line_start, pos), sp));
1288
+ }
1289
+ }
1290
+ stack.length = indent + 1;
1291
+ var parent = stack[indent];
1292
+ while (str.length > pos && str[pos] != '\\' && str[pos] != '\n') {
1293
+ var error_start = pos;
1294
+ while (str.length > pos && (str[pos] == ' ' || str[pos] == '\t')) {
1295
+ pos++;
1296
+ }
1297
+ if (pos > error_start) {
1298
+ let line_end = str.indexOf('\n', pos);
1299
+ if (line_end === -1)
1300
+ line_end = str.length;
1301
+ const sp = span.span(row, error_start - line_start, pos - error_start + 1);
1302
+ this.$mol_fail(new this.$mol_error_syntax(`Wrong nodes separator`, str.substring(line_start, line_end), sp));
1303
+ }
1304
+ var type_start = pos;
1305
+ while (str.length > pos &&
1306
+ str[pos] != '\\' &&
1307
+ str[pos] != ' ' &&
1308
+ str[pos] != '\t' &&
1309
+ str[pos] != '\n') {
1310
+ pos++;
1311
+ }
1312
+ if (pos > type_start) {
1313
+ let next = new $mol_tree2(str.slice(type_start, pos), '', [], span.span(row, type_start - line_start + 1, pos - type_start));
1314
+ const parent_kids = parent.kids;
1315
+ parent_kids.push(next);
1316
+ parent = next;
1317
+ }
1318
+ if (str.length > pos && str[pos] == ' ') {
1319
+ pos++;
1320
+ }
1321
+ }
1322
+ if (str.length > pos && str[pos] == '\\') {
1323
+ var data_start = pos;
1324
+ while (str.length > pos && str[pos] != '\n') {
1325
+ pos++;
1326
+ }
1327
+ let next = new $mol_tree2('', str.slice(data_start + 1, pos), [], span.span(row, data_start - line_start + 2, pos - data_start - 1));
1328
+ const parent_kids = parent.kids;
1329
+ parent_kids.push(next);
1330
+ parent = next;
1331
+ }
1332
+ if (str.length === pos && stack.length > 0) {
1333
+ const sp = span.span(row, pos - line_start + 1, 1);
1334
+ this.$mol_fail(new this.$mol_error_syntax(`Undexpected EOF, LF required`, str.substring(line_start, str.length), sp));
1335
+ }
1336
+ stack.push(parent);
1337
+ pos++;
1338
+ }
1339
+ return root;
1340
+ }
1341
+ $.$mol_tree2_from_string = $mol_tree2_from_string;
1342
+ })($ || ($ = {}));
1343
+ //mol/tree2/from/string/string.ts
1344
+ ;
1345
+ "use strict";
1346
+ var $;
1347
+ (function ($_1) {
1348
+ $mol_test({
1349
+ 'tree parsing'($) {
1350
+ $mol_assert_equal($.$mol_tree2_from_string("foo\nbar\n").kids.length, 2);
1351
+ $mol_assert_equal($.$mol_tree2_from_string("foo\nbar\n").kids[1].type, "bar");
1352
+ $mol_assert_equal($.$mol_tree2_from_string("foo\n\n\n").kids.length, 1);
1353
+ $mol_assert_equal($.$mol_tree2_from_string("=foo\n\\bar\n").kids.length, 2);
1354
+ $mol_assert_equal($.$mol_tree2_from_string("=foo\n\\bar\n").kids[1].value, "bar");
1355
+ $mol_assert_equal($.$mol_tree2_from_string("foo bar \\pol\n").kids[0].kids[0].kids[0].value, "pol");
1356
+ $mol_assert_equal($.$mol_tree2_from_string("foo bar\n\t\\pol\n\t\\men\n").kids[0].kids[0].kids[1].value, "men");
1357
+ $mol_assert_equal($.$mol_tree2_from_string('foo bar \\text\n').toString(), 'foo bar \\text\n');
1243
1358
  },
1244
- 'hack'() {
1245
- const res = $mol_tree.fromString(`foo bar xxx`).hack({
1246
- '': (tree, context) => [tree.hack(context)],
1247
- 'bar': (tree, context) => [tree.hack(context).clone({ type: '777' })],
1248
- });
1249
- $mol_assert_equal(res.toString(), new $mol_tree({ type: 'foo 777 xxx' }).toString());
1359
+ 'Too many tabs'($) {
1360
+ const tree = `
1361
+ foo
1362
+ bar
1363
+ `;
1364
+ $mol_assert_fail(() => {
1365
+ $.$mol_tree2_from_string(tree, 'test');
1366
+ }, 'Too many tabs\ntest#3:1/6\n!!!!!!\n\t\t\t\t\t\tbar');
1367
+ },
1368
+ 'Too few tabs'($) {
1369
+ const tree = `
1370
+ foo
1371
+ bar
1372
+ `;
1373
+ $mol_assert_fail(() => {
1374
+ $.$mol_tree2_from_string(tree, 'test');
1375
+ }, 'Too few tabs\ntest#3:1/4\n!!!!\n\t\t\t\tbar');
1376
+ },
1377
+ 'Wrong nodes separator'($) {
1378
+ const tree = `foo bar\n`;
1379
+ $mol_assert_fail(() => {
1380
+ $.$mol_tree2_from_string(tree, 'test');
1381
+ }, 'Wrong nodes separator\ntest#1:4/2\n !!\nfoo bar');
1250
1382
  },
1251
- 'errors handling'($) {
1383
+ 'Undexpected EOF, LF required'($) {
1384
+ const tree = ` foo`;
1385
+ $mol_assert_fail(() => {
1386
+ $.$mol_tree2_from_string(tree, 'test');
1387
+ }, 'Undexpected EOF, LF required\ntest#1:5/1\n !\n foo');
1388
+ },
1389
+ 'Errors skip and collect'($) {
1390
+ const tree = `foo bar`;
1252
1391
  const errors = [];
1253
- class Tree extends $mol_tree {
1254
- static $ = $.$mol_ambient({
1255
- $mol_fail: error => errors.push(error.message)
1256
- });
1392
+ const $$ = $.$mol_ambient({
1393
+ $mol_fail: (error) => {
1394
+ errors.push(error.message);
1395
+ return null;
1396
+ }
1397
+ });
1398
+ const res = $$.$mol_tree2_from_string(tree, 'test');
1399
+ $mol_assert_like(errors, [
1400
+ 'Wrong nodes separator\ntest#1:4/2\n !!\nfoo bar',
1401
+ 'Undexpected EOF, LF required\ntest#1:9/1\n !\nfoo bar',
1402
+ ]);
1403
+ $mol_assert_equal(res.toString(), 'foo bar\n');
1404
+ },
1405
+ });
1406
+ })($ || ($ = {}));
1407
+ //mol/tree2/from/string/string.test.ts
1408
+ ;
1409
+ "use strict";
1410
+ var $;
1411
+ (function ($) {
1412
+ function $mol_tree2_from_json(json, span = $mol_span.unknown) {
1413
+ if (typeof json === 'boolean' || typeof json === 'number' || json === null) {
1414
+ return new $mol_tree2(String(json), '', [], span);
1415
+ }
1416
+ if (typeof json === 'string') {
1417
+ return $mol_tree2.data(json, [], span);
1418
+ }
1419
+ if (Array.isArray(json)) {
1420
+ const sub = json.map(json => $mol_tree2_from_json(json, span));
1421
+ return new $mol_tree2('/', '', sub, span);
1422
+ }
1423
+ if (ArrayBuffer.isView(json)) {
1424
+ const buf = new Uint8Array(json.buffer, json.byteOffset, json.byteLength);
1425
+ return $mol_tree2.data(String.fromCharCode(...buf), [], span);
1426
+ }
1427
+ if (json instanceof Date) {
1428
+ return new $mol_tree2('', json.toISOString(), [], span);
1429
+ }
1430
+ if (typeof json.toJSON === 'function') {
1431
+ return $mol_tree2_from_json(json.toJSON());
1432
+ }
1433
+ if (json instanceof Error) {
1434
+ const { name, message, stack } = json;
1435
+ json = { ...json, name, message, stack };
1436
+ }
1437
+ const sub = [];
1438
+ for (var key in json) {
1439
+ const val = json[key];
1440
+ if (val === undefined)
1441
+ continue;
1442
+ const subsub = $mol_tree2_from_json(val, span);
1443
+ if (/^[^\n\t\\ ]+$/.test(key)) {
1444
+ sub.push(new $mol_tree2(key, '', [subsub], span));
1445
+ }
1446
+ else {
1447
+ sub.push($mol_tree2.data(key, [subsub], span));
1257
1448
  }
1258
- Tree.fromString(`
1259
- \t \tfoo
1260
- bar \\data
1261
- `, 'test');
1262
- $mol_assert_like(errors, ['Syntax error at test:2\n \tfoo']);
1449
+ }
1450
+ return new $mol_tree2('*', '', sub, span);
1451
+ }
1452
+ $.$mol_tree2_from_json = $mol_tree2_from_json;
1453
+ })($ || ($ = {}));
1454
+ //mol/tree2/from/json/json.ts
1455
+ ;
1456
+ "use strict";
1457
+ var $;
1458
+ (function ($) {
1459
+ $mol_test({
1460
+ 'fromJSON'() {
1461
+ $mol_assert_equal($mol_tree2_from_json([]).toString(), '/\n');
1462
+ $mol_assert_equal($mol_tree2_from_json([false, true]).toString(), '/\n\tfalse\n\ttrue\n');
1463
+ $mol_assert_equal($mol_tree2_from_json([0, 1, 2.3]).toString(), '/\n\t0\n\t1\n\t2.3\n');
1464
+ $mol_assert_equal($mol_tree2_from_json(new Uint16Array([1, 10, 256])).toString(), '\\\x01\x00\n\\\x00\x00\x01\n');
1465
+ $mol_assert_equal($mol_tree2_from_json(['', 'foo', 'bar\nbaz']).toString(), '/\n\t\\\n\t\\foo\n\t\\\n\t\t\\bar\n\t\t\\baz\n');
1466
+ $mol_assert_equal($mol_tree2_from_json({ 'foo': false, 'bar\nbaz': 'lol' }).toString(), '*\n\tfoo false\n\t\\\n\t\t\\bar\n\t\t\\baz\n\t\t\\lol\n');
1263
1467
  },
1264
1468
  });
1265
1469
  })($ || ($ = {}));
1266
- //mol/tree/tree.test.ts
1470
+ //mol/tree2/from/json/json.test.ts
1267
1471
  ;
1268
1472
  "use strict";
1269
1473
  var $;