functionalscript 0.6.11 → 0.8.0

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/README.md CHANGED
@@ -19,7 +19,7 @@ Learn more about
19
19
  - [Purely Functional Programming in JavaScript](https://blog.bitsrc.io/purely-functional-programming-in-javascript-91114b1b2dff?sk=5f7132e56902f38fcf4c6164bfa681ed),
20
20
  - [FunctionalScript and I/O](https://medium.com/@sergeyshandar/functionalscript-5cf817345376?sk=30b32189a81d1a2dad16c2244f32328d).
21
21
 
22
- This repository is a [monorepo](https://en.wikipedia.org/wiki/Monorepo) and distributed under [AGPL-3.0](https://www.gnu.org/licenses/agpl-3.0.en.html#license-text). Let us know if you need another license by sending an [email](mailto:sergey.oss@proton.me).
22
+ This repository is a [monorepo](https://en.wikipedia.org/wiki/Monorepo) and distributed under [MIT](LICENSE).
23
23
 
24
24
  ## Getting Started
25
25
 
@@ -75,4 +75,5 @@ In FunctionalScript:
75
75
  ## Sponsors
76
76
 
77
77
  - [KirillOsenkov](https://github.com/KirillOsenkov),
78
- - [antkmsft](https://github.com/antkmsft).
78
+ - [antkmsft](https://github.com/antkmsft),
79
+ - [Mark Heyman](https://opencollective.com/body-count).
@@ -1,3 +1,5 @@
1
+ import { type CodePoint } from '../../text/utf16/module.f.ts';
2
+ import { type RangeMapArray } from '../../types/range_map/module.f.ts';
1
3
  import { type Rule as FRule } from '../module.f.ts';
2
4
  export type TerminalRange = number;
3
5
  export type Sequence = readonly string[];
@@ -8,7 +10,48 @@ export type Variant = {
8
10
  export type Rule = Variant | Sequence | TerminalRange;
9
11
  /** The full grammar */
10
12
  export type RuleSet = Readonly<Record<string, Rule>>;
11
- export declare const toData: (fr: FRule) => readonly [RuleSet, string];
13
+ type EmptyTag = string | true | undefined;
14
+ type DispatchRule = {
15
+ readonly emptyTag: EmptyTag;
16
+ readonly rangeMap: Dispatch;
17
+ };
18
+ type Dispatch = RangeMapArray<DispatchResult>;
19
+ type DispatchResult = DispatchRuleCollection | null;
20
+ type DispatchRuleOrName = DispatchRule | string;
21
+ type DispatchRuleCollection = {
22
+ readonly tag: string | undefined;
23
+ readonly rules: DispatchRuleOrName[];
24
+ };
25
+ type DispatchMap = {
26
+ readonly [id in string]: DispatchRule;
27
+ };
28
+ /**
29
+ * Represents a parsed Abstract Syntax Tree (AST) sequence.
30
+ */
31
+ export type AstSequence = readonly (AstRule | CodePoint)[];
32
+ type AstTag = string | true | undefined;
33
+ /**
34
+ * Represents a parsed AST rule, consisting of a rule name and its parsed sequence.
35
+ */
36
+ type AstRule = {
37
+ readonly tag: AstTag;
38
+ readonly sequence: AstSequence;
39
+ };
40
+ /**
41
+ * Represents the remaining input after a match attempt, or `null` if no match is possible.
42
+ */
43
+ export type Remainder = readonly CodePoint[] | null;
12
44
  /**
13
- * Either `{ variantItem: id }` or `id`.
45
+ * Represents the result of a match operation, including the parsed AST rule and the remainder of the input.
14
46
  */
47
+ export type MatchResult = readonly [AstRule, boolean, Remainder];
48
+ /**
49
+ * Represents an LL(1) parser function for matching input against grammar rules.
50
+ */
51
+ export type Match = (name: string, s: readonly CodePoint[]) => MatchResult;
52
+ export type MatchRule = (dr: DispatchRule, s: readonly CodePoint[]) => MatchResult;
53
+ export declare const toData: (fr: FRule) => readonly [RuleSet, string];
54
+ export declare const dispatchMap: (ruleSet: RuleSet) => DispatchMap;
55
+ export declare const parser: (fr: FRule) => Match;
56
+ export declare const parserRuleSet: (ruleSet: RuleSet) => Match;
57
+ export {};
@@ -1,6 +1,9 @@
1
1
  import { stringToCodePointList } from "../../text/utf16/module.f.js";
2
+ import { strictEqual } from "../../types/function/operator/module.f.js";
2
3
  import { map, toArray } from "../../types/list/module.f.js";
3
- import { oneEncode, } from "../module.f.js";
4
+ import { rangeMap } from "../../types/range_map/module.f.js";
5
+ import { contains, set } from "../../types/string_set/module.f.js";
6
+ import { oneEncode, rangeDecode, } from "../module.f.js";
4
7
  const { entries } = Object;
5
8
  const find = (map) => (fr) => {
6
9
  for (const [k, v] of entries(map)) {
@@ -74,14 +77,133 @@ export const toData = (fr) => {
74
77
  const [, ruleSet, id] = toDataAdd({})(fr);
75
78
  return [ruleSet, id];
76
79
  };
77
- // type Dispatch = RangeMapArray<string>;
78
- /**
79
- * Either `{ variantItem: id }` or `id`.
80
- */
81
- /*
82
- type DispatchRule = SingleProperty<> | string
83
-
84
- type Dispatch = RangeMapArray<DispatchRule>
85
-
86
- type DispatchMap = { readonly[id in string]: Dispatch }
87
- */
80
+ const dispatchOp = rangeMap({
81
+ union: a => b => {
82
+ if (a === null) {
83
+ return b;
84
+ }
85
+ if (b === null) {
86
+ return a;
87
+ }
88
+ throw ['can not merge [', a, '][', b, ']'];
89
+ },
90
+ equal: strictEqual,
91
+ def: null,
92
+ });
93
+ export const dispatchMap = (ruleSet) => {
94
+ const addRuleToDispatch = (dr, name) => {
95
+ if (dr === null)
96
+ return null;
97
+ return { tag: dr.tag, rules: [...dr.rules, name] };
98
+ };
99
+ const addTagToDispatch = (dr, tag) => {
100
+ if (dr === null)
101
+ return null;
102
+ return { tag, rules: dr.rules };
103
+ };
104
+ const dispatchRule = (dm, name, current) => {
105
+ if (name in dm) {
106
+ return dm;
107
+ }
108
+ const newCurrent = set(name)(current);
109
+ const rule = ruleSet[name];
110
+ if (typeof rule === 'number') {
111
+ const range = rangeDecode(rule);
112
+ const dispatch = dispatchOp.fromRange(range)({ tag: undefined, rules: [] });
113
+ const dr = { emptyTag: undefined, rangeMap: dispatch };
114
+ return { ...dm, [name]: dr };
115
+ }
116
+ else if (rule instanceof Array) {
117
+ let emptyTag = true;
118
+ let result = [];
119
+ for (const item of rule) {
120
+ if (contains(item)(newCurrent)) {
121
+ result = result.map(x => [addRuleToDispatch(x[0], item), x[1]]);
122
+ }
123
+ else {
124
+ dm = dispatchRule(dm, item, newCurrent);
125
+ const dr = dm[item];
126
+ if (emptyTag === true) {
127
+ result = result.map(x => [addRuleToDispatch(x[0], item), x[1]]);
128
+ result = toArray(dispatchOp.merge(result)(dr.rangeMap));
129
+ emptyTag = dr.emptyTag !== undefined ? true : undefined;
130
+ }
131
+ else {
132
+ result = result.map(x => [addRuleToDispatch(x[0], item), x[1]]);
133
+ }
134
+ }
135
+ }
136
+ const dr = { emptyTag, rangeMap: result };
137
+ return { ...dm, [name]: dr };
138
+ }
139
+ else {
140
+ const entries = Object.entries(rule);
141
+ let result = [];
142
+ let emptyTag = undefined;
143
+ for (const [tag, item] of entries) {
144
+ dm = dispatchRule(dm, item, newCurrent);
145
+ const dr = dm[item];
146
+ if (dr.emptyTag !== undefined) {
147
+ emptyTag = tag;
148
+ }
149
+ else {
150
+ const d = dr.rangeMap.map(x => [addTagToDispatch(x[0], tag), x[1]]);
151
+ result = toArray(dispatchOp.merge(result)(d));
152
+ }
153
+ }
154
+ const dr = { emptyTag, rangeMap: result };
155
+ return { ...dm, [name]: dr };
156
+ }
157
+ };
158
+ let result = {};
159
+ for (const k in ruleSet) {
160
+ result = dispatchRule(result, k, null);
161
+ }
162
+ return result;
163
+ };
164
+ export const parser = (fr) => {
165
+ const data = toData(fr);
166
+ return parserRuleSet(data[0]);
167
+ };
168
+ export const parserRuleSet = (ruleSet) => {
169
+ const map = dispatchMap(ruleSet);
170
+ const f = (rule, cp) => {
171
+ const mrSuccess = (tag, sequence, r) => [{ tag, sequence }, true, r];
172
+ const mrFail = (tag, sequence, r) => [{ tag, sequence }, false, r];
173
+ const { emptyTag, rangeMap } = rule;
174
+ if (cp.length === 0) {
175
+ return mrSuccess(emptyTag, [], emptyTag === undefined ? null : cp);
176
+ }
177
+ const cp0 = cp[0];
178
+ const dr = dispatchOp.get(cp0)(rangeMap);
179
+ if (dr === null) {
180
+ if (emptyTag === undefined) {
181
+ return mrFail(emptyTag, [], cp);
182
+ }
183
+ return mrSuccess(emptyTag, [], cp);
184
+ }
185
+ let seq = [cp0];
186
+ let r = cp;
187
+ const [_, ...restCp] = cp;
188
+ r = restCp;
189
+ const { tag, rules } = dr;
190
+ for (const i of rules) {
191
+ const rule = typeof i === 'string' ? map[i] : i;
192
+ const res = f(rule, r);
193
+ const [astRule, success, newR] = res;
194
+ if (success === false) {
195
+ return res;
196
+ }
197
+ seq = [...seq, astRule];
198
+ if (newR === null) {
199
+ return mrSuccess(tag, seq, null);
200
+ }
201
+ r = newR;
202
+ }
203
+ return mrSuccess(tag, seq, r);
204
+ };
205
+ const match = (name, cp) => {
206
+ return f(map[name], cp);
207
+ };
208
+ return match;
209
+ };
@@ -1,6 +1,10 @@
1
1
  declare const _default: {
2
2
  toData: (() => void)[];
3
3
  variantTest: () => void;
4
+ dispatch: (() => void)[];
5
+ parser: (() => void)[];
6
+ repeat: (() => void)[];
7
+ repeatParser: (() => void)[];
4
8
  example: () => void;
5
9
  };
6
10
  export default _default;
@@ -1,8 +1,11 @@
1
1
  import { stringify } from "../../json/module.f.js";
2
+ import { stringToCodePointList } from "../../text/utf16/module.f.js";
3
+ import { identity } from "../../types/function/module.f.js";
4
+ import { toArray } from "../../types/list/module.f.js";
2
5
  import { sort } from "../../types/object/module.f.js";
3
- import { range } from "../module.f.js";
6
+ import { join0Plus, max, option, range, remove, repeat, repeat0Plus, set } from "../module.f.js";
4
7
  import { classic, deterministic } from "../testlib.f.js";
5
- import { toData } from "./module.f.js";
8
+ import { dispatchMap, parser, parserRuleSet, toData } from "./module.f.js";
6
9
  export default {
7
10
  toData: [
8
11
  () => {
@@ -12,28 +15,28 @@ export default {
12
15
  () => {
13
16
  const stringRule = 'true';
14
17
  const result = stringify(sort)(toData(stringRule));
15
- if (result != '[{"":["0","1","2","3"],"0":1946157172,"1":1912602738,"2":1962934389,"3":1694498917},""]') {
18
+ if (result !== '[{"":["0","1","2","3"],"0":1946157172,"1":1912602738,"2":1962934389,"3":1694498917},""]') {
16
19
  throw result;
17
20
  }
18
21
  },
19
22
  () => {
20
23
  const terminalRangeRule = range('AF');
21
24
  const result = stringify(sort)(toData(terminalRangeRule));
22
- if (result != '[{"":1090519110},""]') {
25
+ if (result !== '[{"":1090519110},""]') {
23
26
  throw result;
24
27
  } //1090519110 = 65 * 2^24 + 70
25
28
  },
26
29
  () => {
27
30
  const sequenceRangeRule = [range('AF'), range('af')];
28
31
  const result = stringify(sort)(toData(sequenceRangeRule));
29
- if (result != '[{"":["0","1"],"0":1090519110,"1":1627390054},""]') {
32
+ if (result !== '[{"":["0","1"],"0":1090519110,"1":1627390054},""]') {
30
33
  throw result;
31
34
  }
32
35
  },
33
36
  () => {
34
37
  const lazyRule = () => 'true';
35
38
  const result = stringify(sort)(toData(lazyRule));
36
- if (result != '[{"":1946157172,"0":1912602738,"1":1962934389,"2":1694498917,"lazyRule":["","0","1","2"]},"lazyRule"]') {
39
+ if (result !== '[{"":1946157172,"0":1912602738,"1":1962934389,"2":1694498917,"lazyRule":["","0","1","2"]},"lazyRule"]') {
37
40
  throw result;
38
41
  }
39
42
  },
@@ -41,7 +44,7 @@ export default {
41
44
  const varintRule = { true: 'true', false: 'false' };
42
45
  const result = stringify(sort)(toData(varintRule));
43
46
  const expected = '[{"":{"false":"5","true":"0"},"0":["1","2","3","4"],"1":1946157172,"2":1912602738,"3":1962934389,"4":1694498917,"5":["6","7","8","9","4"],"6":1711276134,"7":1627390049,"8":1811939436,"9":1929379955},""]';
44
- if (result != expected) {
47
+ if (result !== expected) {
45
48
  throw [result, expected];
46
49
  }
47
50
  },
@@ -50,18 +53,372 @@ export default {
50
53
  const lazyRule0 = () => 'false';
51
54
  const result = stringify(sort)(toData([lazyRule, lazyRule0]));
52
55
  const expected = '[{"":["lazyRule","lazyRule0"],"0":1946157172,"1":1912602738,"2":1962934389,"3":1694498917,"4":1711276134,"5":1627390049,"6":1811939436,"7":1929379955,"lazyRule":["0","1","2","3"],"lazyRule0":["4","5","6","7","3"]},""]';
53
- if (result != expected) {
56
+ if (result !== expected) {
54
57
  throw [result, expected];
55
58
  }
56
59
  },
60
+ () => {
61
+ const emptyRule = '';
62
+ const result = stringify(sort)(toData(emptyRule));
63
+ const expected = '[{"":[]},""]';
64
+ if (result !== expected) {
65
+ throw [result, expected];
66
+ }
67
+ },
68
+ () => {
69
+ const optionRule = option('a');
70
+ const result = stringify(identity)(toData(optionRule));
71
+ if (result !== '[{"0":["1"],"1":1627390049,"2":[],"":{"some":"0","none":"2"}},""]') {
72
+ throw result;
73
+ }
74
+ },
75
+ () => {
76
+ const repeatRule = repeat0Plus(option('a'));
77
+ const result = stringify(identity)(toData(repeatRule));
78
+ if (result !== '[{"0":{"some":"1","none":"3"},"1":["2"],"2":1627390049,"3":[],"":["0","r"],"r":{"some":"","none":"3"}},"r"]') {
79
+ throw result;
80
+ }
81
+ },
82
+ () => {
83
+ const repeatRule = repeat0Plus(set(' \n\r\t'));
84
+ const result = stringify(identity)(toData(repeatRule));
85
+ if (result !== '[{"0":{" ":"1","\\n":"2","\\r":"3","\\t":"4"},"1":536870944,"2":167772170,"3":218103821,"4":150994953,"5":[],"":["0","r"],"r":{"some":"","none":"5"}},"r"]') {
86
+ throw result;
87
+ }
88
+ }
57
89
  ],
58
90
  variantTest: () => {
59
91
  const varintRule = { a: 'a', b: 'b' };
60
92
  const result = stringify(sort)(toData(varintRule));
61
- if (result != '[{"":{"a":"0","b":"2"},"0":["1"],"1":1627390049,"2":["3"],"3":1644167266},""]') {
93
+ if (result !== '[{"":{"a":"0","b":"2"},"0":["1"],"1":1627390049,"2":["3"],"3":1644167266},""]') {
62
94
  throw result;
63
95
  }
64
96
  },
97
+ dispatch: [
98
+ () => {
99
+ const terminalRangeRule = range('AF');
100
+ const data = toData(terminalRangeRule);
101
+ const dm = dispatchMap(data[0]);
102
+ const result = JSON.stringify(dm);
103
+ if (result !== '{"":{"rangeMap":[[null,64],[{"rules":[]},70]]}}') {
104
+ throw result;
105
+ }
106
+ },
107
+ () => {
108
+ const stringRule = 'AB';
109
+ const data = toData(stringRule);
110
+ const dm = dispatchMap(data[0]);
111
+ const result = JSON.stringify(dm);
112
+ if (result !== '{"0":{"rangeMap":[[null,64],[{"rules":[]},65]]},"1":{"rangeMap":[[null,65],[{"rules":[]},66]]},"":{"rangeMap":[[null,64],[{"rules":["1"]},65]]}}') {
113
+ throw result;
114
+ }
115
+ },
116
+ () => {
117
+ const a = range('AA');
118
+ const b = range('BB');
119
+ const ab = [a, b];
120
+ const data = toData(ab);
121
+ const dm = dispatchMap(data[0]);
122
+ const result = JSON.stringify(dm);
123
+ if (result !== '{"0":{"rangeMap":[[null,64],[{"rules":[]},65]]},"1":{"rangeMap":[[null,65],[{"rules":[]},66]]},"":{"rangeMap":[[null,64],[{"rules":["1"]},65]]}}') {
124
+ throw result;
125
+ }
126
+ },
127
+ () => {
128
+ const emptyRule = '';
129
+ const data = toData(emptyRule);
130
+ const dm = dispatchMap(data[0]);
131
+ const result = JSON.stringify(dm);
132
+ if (result !== '{"":{"emptyTag":true,"rangeMap":[]}}') {
133
+ throw result;
134
+ }
135
+ },
136
+ () => {
137
+ const variantRule = { 'a': range('AA'), 'b': range('BB') };
138
+ const data = toData(variantRule);
139
+ const dm = dispatchMap(data[0]);
140
+ const result = JSON.stringify(dm);
141
+ if (result !== '{"0":{"rangeMap":[[null,64],[{"rules":[]},65]]},"1":{"rangeMap":[[null,65],[{"rules":[]},66]]},"":{"rangeMap":[[null,64],[{"tag":"a","rules":[]},65],[{"tag":"b","rules":[]},66]]}}') {
142
+ throw result;
143
+ }
144
+ },
145
+ () => {
146
+ const emptyRule = '';
147
+ const variantRule = { 'e': emptyRule, 'a': range('AA') };
148
+ const data = toData(variantRule);
149
+ const dm = dispatchMap(data[0]);
150
+ const result = JSON.stringify(dm);
151
+ if (result !== '{"0":{"emptyTag":true,"rangeMap":[]},"1":{"rangeMap":[[null,64],[{"rules":[]},65]]},"":{"emptyTag":"e","rangeMap":[[null,64],[{"tag":"a","rules":[]},65]]}}') {
152
+ throw result;
153
+ }
154
+ },
155
+ () => {
156
+ const emptyRule = '';
157
+ const minursRule = range('--');
158
+ const optionalMinusRule = { 'none': emptyRule, 'minus': minursRule };
159
+ const digitRule = range('09');
160
+ const numberRule = [optionalMinusRule, digitRule];
161
+ const data = toData(numberRule);
162
+ const dm = dispatchMap(data[0]);
163
+ const result = JSON.stringify(dm);
164
+ if (result !== '{"0":{"emptyTag":"none","rangeMap":[[null,44],[{"tag":"minus","rules":[]},45]]},"1":{"emptyTag":true,"rangeMap":[]},"2":{"rangeMap":[[null,44],[{"rules":[]},45]]},"3":{"rangeMap":[[null,47],[{"rules":[]},57]]},"":{"rangeMap":[[null,44],[{"tag":"minus","rules":["3"]},45],[null,47],[{"rules":[]},57]]}}') {
165
+ throw result;
166
+ }
167
+ },
168
+ () => {
169
+ const emptyRule = '';
170
+ const spaceRule = range(' ');
171
+ const optionalSpaceRule = { 'none': emptyRule, 'space': spaceRule };
172
+ const minusRule = range('--');
173
+ const optionalMinusRule = { 'none': emptyRule, 'minus': minusRule };
174
+ const digitRule = range('09');
175
+ const numberRule = [optionalSpaceRule, optionalMinusRule, digitRule];
176
+ const data = toData(numberRule);
177
+ const dm = dispatchMap(data[0]);
178
+ const result = JSON.stringify(dm);
179
+ if (result !== '{"0":{"emptyTag":"none","rangeMap":[[null,31],[{"tag":"space","rules":[]},32]]},"1":{"emptyTag":true,"rangeMap":[]},"2":{"rangeMap":[[null,31],[{"rules":[]},32]]},"3":{"emptyTag":"none","rangeMap":[[null,44],[{"tag":"minus","rules":[]},45]]},"4":{"rangeMap":[[null,44],[{"rules":[]},45]]},"5":{"rangeMap":[[null,47],[{"rules":[]},57]]},"":{"rangeMap":[[null,31],[{"tag":"space","rules":["3","5"]},32],[null,44],[{"tag":"minus","rules":["5"]},45],[null,47],[{"rules":[]},57]]}}') {
180
+ throw result;
181
+ }
182
+ }
183
+ ],
184
+ parser: [
185
+ () => {
186
+ const emptyRule = '';
187
+ const m = parser(emptyRule);
188
+ const mr = m("", []);
189
+ const result = JSON.stringify(mr);
190
+ if (result !== '[{"tag":true,"sequence":[]},true,[]]') {
191
+ throw result;
192
+ }
193
+ },
194
+ () => {
195
+ const emptyRule = '';
196
+ const m = parser(emptyRule);
197
+ const mr = m("", [65, 70]);
198
+ const result = JSON.stringify(mr);
199
+ if (result !== '[{"tag":true,"sequence":[]},true,[65,70]]') {
200
+ throw result;
201
+ }
202
+ },
203
+ () => {
204
+ const terminalRangeRule = range('AF');
205
+ const m = parser(terminalRangeRule);
206
+ const mr = m("", [65]);
207
+ const result = JSON.stringify(mr);
208
+ if (result !== '[{"sequence":[65]},true,[]]') {
209
+ throw result;
210
+ }
211
+ },
212
+ () => {
213
+ const terminalRangeRule = range('AF');
214
+ const m = parser(terminalRangeRule);
215
+ const mr = m("", [64]);
216
+ const result = JSON.stringify(mr);
217
+ if (result !== '[{"sequence":[]},false,[64]]') {
218
+ throw result;
219
+ }
220
+ },
221
+ () => {
222
+ const variantRule = { 'a': range('AA'), 'b': range('BB') };
223
+ const m = parser(variantRule);
224
+ const mr = m("", [65]);
225
+ const result = JSON.stringify(mr);
226
+ if (result !== '[{"tag":"a","sequence":[65]},true,[]]') {
227
+ throw result;
228
+ }
229
+ },
230
+ () => {
231
+ const variantRule = { 'a': range('AA'), 'b': range('BB') };
232
+ const m = parser(variantRule);
233
+ const mr = m("", [64]);
234
+ const result = JSON.stringify(mr);
235
+ if (result !== '[{"sequence":[]},false,[64]]') {
236
+ throw result;
237
+ }
238
+ },
239
+ () => {
240
+ const emptyRule = '';
241
+ const variantRule = { 'e': emptyRule, 'a': range('AA') };
242
+ const m = parser(variantRule);
243
+ const mr = m("", []);
244
+ const result = JSON.stringify(mr);
245
+ if (result !== '[{"tag":"e","sequence":[]},true,[]]') {
246
+ throw result;
247
+ }
248
+ },
249
+ () => {
250
+ const emptyRule = '';
251
+ const variantRule = { 'e': emptyRule, 'a': range('AA') };
252
+ const m = parser(variantRule);
253
+ const mr = m("", [64]);
254
+ const result = JSON.stringify(mr);
255
+ if (result !== '[{"tag":"e","sequence":[]},true,[64]]') {
256
+ throw result;
257
+ }
258
+ },
259
+ () => {
260
+ const stringRule = 'AB';
261
+ const m = parser(stringRule);
262
+ const mr = m("", [65, 66]);
263
+ const result = JSON.stringify(mr);
264
+ if (result !== '[{"sequence":[65,{"sequence":[66]}]},true,[]]') {
265
+ throw result;
266
+ }
267
+ },
268
+ () => {
269
+ const stringRule = 'AB';
270
+ const m = parser(stringRule);
271
+ const mr = m("", [65, 67]);
272
+ const result = JSON.stringify(mr);
273
+ if (result !== '[{"sequence":[]},false,[67]]') {
274
+ throw result;
275
+ }
276
+ },
277
+ () => {
278
+ const emptyRule = '';
279
+ const minursRule = range('--');
280
+ const optionalMinusRule = { 'none': emptyRule, 'minus': minursRule };
281
+ const digitRule = range('09');
282
+ const numberRule = [optionalMinusRule, digitRule];
283
+ const m = parser(numberRule);
284
+ const mr = m("", [50]);
285
+ const result = JSON.stringify(mr);
286
+ if (result !== '[{"sequence":[50]},true,[]]') {
287
+ throw result;
288
+ }
289
+ },
290
+ () => {
291
+ const emptyRule = '';
292
+ const minusRule = range('--');
293
+ const optionalMinusRule = { 'none': emptyRule, 'minus': minusRule };
294
+ const digitRule = range('09');
295
+ const numberRule = [optionalMinusRule, digitRule];
296
+ const m = parser(numberRule);
297
+ const mr = m("", [45, 50]);
298
+ const result = JSON.stringify(mr);
299
+ if (result !== '[{"tag":"minus","sequence":[45,{"sequence":[50]}]},true,[]]') {
300
+ throw result;
301
+ }
302
+ },
303
+ () => {
304
+ const m = parser(option('a'));
305
+ const isSuccess = (mr) => mr[1] && mr[2]?.length === 0;
306
+ const expect = (s, success) => {
307
+ const mr = m('', toArray(stringToCodePointList(s)));
308
+ if (isSuccess(mr) !== success) {
309
+ throw mr;
310
+ }
311
+ };
312
+ expect('a', true);
313
+ expect('', true);
314
+ expect('aa', false);
315
+ expect('b', false);
316
+ },
317
+ () => {
318
+ const ws = repeat0Plus(set(' \n\r\t'));
319
+ const commaJoin0Plus = ([open, close], a) => [
320
+ open,
321
+ ws,
322
+ join0Plus([a, ws], [',', ws]),
323
+ close,
324
+ ];
325
+ const value = () => ({
326
+ object: commaJoin0Plus('{}', 'a'),
327
+ array: commaJoin0Plus('[]', 'a')
328
+ });
329
+ value.name; //bun will fail if no usage of name found
330
+ const m = parser(value);
331
+ const isSuccess = (mr) => mr[1] && mr[2]?.length === 0;
332
+ const expect = (s, success) => {
333
+ const mr = m('value', toArray(stringToCodePointList(s)));
334
+ if (isSuccess(mr) !== success) {
335
+ throw mr;
336
+ }
337
+ };
338
+ expect('[]', true);
339
+ expect('[a]', true);
340
+ expect('[a, a]', true);
341
+ expect('{a}', true);
342
+ },
343
+ () => {
344
+ const m = parser(deterministic());
345
+ const isSuccess = (mr) => mr[1] && mr[2]?.length === 0;
346
+ const expect = (s, success) => {
347
+ const mr = m('', toArray(stringToCodePointList(s)));
348
+ if (isSuccess(mr) !== success) {
349
+ throw mr;
350
+ }
351
+ };
352
+ expect(' true ', true);
353
+ expect(' tr2ue ', false);
354
+ expect(' true" ', false);
355
+ expect(' "Hello" ', true);
356
+ expect(' "Hello ', false);
357
+ expect(' "Hello\\n\\r\\"" ', true);
358
+ expect(' -56.7e+5 ', true);
359
+ expect(' h-56.7e+5 ', false);
360
+ expect(' -56.7e+5 3', false);
361
+ expect(' [] ', true);
362
+ expect(' {} ', true);
363
+ expect(' [[[]]] ', true);
364
+ expect(' [1] ', true);
365
+ expect(' [ 12, false, "a"] ', true);
366
+ expect(' [ 12, false2, "a"] ', false);
367
+ expect(' { "q": [ 12, false, [{"b" : "c"}], "a"] } ', true);
368
+ expect(' { "q": [ 12, false, [{}], "a"] } ', true);
369
+ expect(' { "q": [ 12, false, [}], "a"] } ', false);
370
+ expect(' [{ "q": [ 12, false, [{}], "a"] }] ', true);
371
+ expect(' [{ "q": [ 12, false, [}], "a"] }] ', false);
372
+ }
373
+ ],
374
+ repeat: [
375
+ () => {
376
+ const repeatData = [{ "": ["ws", "repa"], "ws": [], "repa": ["a", ""], "a": 1090519105 }, ""];
377
+ const dm = dispatchMap(repeatData[0]);
378
+ const result = JSON.stringify(dm);
379
+ if (result !== '{"ws":{"emptyTag":true,"rangeMap":[]},"a":{"rangeMap":[[null,64],[{"rules":[]},65]]},"repa":{"rangeMap":[[null,64],[{"rules":[""]},65]]},"":{"rangeMap":[[null,64],[{"rules":[""]},65]]}}') {
380
+ throw result;
381
+ }
382
+ }
383
+ ],
384
+ repeatParser: [
385
+ () => {
386
+ const repeatData = [{ "": ["ws", "repa"], "ws": [], "repa": ["a", ""], "a": 1090519105 }, ""];
387
+ const m = parserRuleSet(repeatData[0]);
388
+ const mr = m("", []);
389
+ const result = JSON.stringify(mr);
390
+ if (result !== '[{"sequence":[]},true,null]') {
391
+ throw result;
392
+ }
393
+ },
394
+ () => {
395
+ const repeatData = [{ "": ["ws", "repa"], "ws": [], "repa": ["a", ""], "a": 1090519105 }, ""];
396
+ const m = parserRuleSet(repeatData[0]);
397
+ const mr = m("", [65]);
398
+ const result = JSON.stringify(mr);
399
+ if (result !== '[{"sequence":[65,{"sequence":[]}]},true,null]') {
400
+ throw result;
401
+ }
402
+ },
403
+ () => {
404
+ const repeatData = [{ "": ["ws", "repa"], "ws": [], "repa": ["a", ""], "a": 1090519105 }, ""];
405
+ const m = parserRuleSet(repeatData[0]);
406
+ const mr = m("", [65, 65, 65]);
407
+ const result = JSON.stringify(mr);
408
+ if (result !== '[{"sequence":[65,{"sequence":[65,{"sequence":[65,{"sequence":[]}]}]}]},true,null]') {
409
+ throw result;
410
+ }
411
+ },
412
+ () => {
413
+ const repeatData = [{ "": ["ws", "repa"], "ws": [], "repa": ["a", ""], "a": 1090519105 }, ""];
414
+ const m = parserRuleSet(repeatData[0]);
415
+ const mr = m("", [66]);
416
+ const result = JSON.stringify(mr);
417
+ if (result !== '[{"sequence":[]},false,[66]]') {
418
+ throw result;
419
+ }
420
+ }
421
+ ],
65
422
  example: () => {
66
423
  const grammar = {
67
424
  space: 0x000020_000020,