mol_key 0.0.403 → 0.0.405

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