functionalscript 0.7.0 → 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).
@@ -17,9 +17,10 @@ type DispatchRule = {
17
17
  };
18
18
  type Dispatch = RangeMapArray<DispatchResult>;
19
19
  type DispatchResult = DispatchRuleCollection | null;
20
+ type DispatchRuleOrName = DispatchRule | string;
20
21
  type DispatchRuleCollection = {
21
22
  readonly tag: string | undefined;
22
- readonly rules: DispatchRule[];
23
+ readonly rules: DispatchRuleOrName[];
23
24
  };
24
25
  type DispatchMap = {
25
26
  readonly [id in string]: DispatchRule;
@@ -28,10 +29,14 @@ type DispatchMap = {
28
29
  * Represents a parsed Abstract Syntax Tree (AST) sequence.
29
30
  */
30
31
  export type AstSequence = readonly (AstRule | CodePoint)[];
32
+ type AstTag = string | true | undefined;
31
33
  /**
32
34
  * Represents a parsed AST rule, consisting of a rule name and its parsed sequence.
33
35
  */
34
- export type AstRule = readonly [string, AstSequence];
36
+ type AstRule = {
37
+ readonly tag: AstTag;
38
+ readonly sequence: AstSequence;
39
+ };
35
40
  /**
36
41
  * Represents the remaining input after a match attempt, or `null` if no match is possible.
37
42
  */
@@ -39,15 +44,14 @@ export type Remainder = readonly CodePoint[] | null;
39
44
  /**
40
45
  * Represents the result of a match operation, including the parsed AST rule and the remainder of the input.
41
46
  */
42
- export type MatchResult = readonly [AstRule, Remainder];
47
+ export type MatchResult = readonly [AstRule, boolean, Remainder];
43
48
  /**
44
49
  * Represents an LL(1) parser function for matching input against grammar rules.
45
50
  */
46
51
  export type Match = (name: string, s: readonly CodePoint[]) => MatchResult;
52
+ export type MatchRule = (dr: DispatchRule, s: readonly CodePoint[]) => MatchResult;
47
53
  export declare const toData: (fr: FRule) => readonly [RuleSet, string];
48
54
  export declare const dispatchMap: (ruleSet: RuleSet) => DispatchMap;
49
55
  export declare const parser: (fr: FRule) => Match;
56
+ export declare const parserRuleSet: (ruleSet: RuleSet) => Match;
50
57
  export {};
51
- /**
52
- * Either `{ variantItem: id }` or `id`.
53
- */
@@ -1,8 +1,8 @@
1
- import { todo } from "../../dev/module.f.js";
2
1
  import { stringToCodePointList } from "../../text/utf16/module.f.js";
3
2
  import { strictEqual } from "../../types/function/operator/module.f.js";
4
3
  import { map, toArray } from "../../types/list/module.f.js";
5
4
  import { rangeMap } from "../../types/range_map/module.f.js";
5
+ import { contains, set } from "../../types/string_set/module.f.js";
6
6
  import { oneEncode, rangeDecode, } from "../module.f.js";
7
7
  const { entries } = Object;
8
8
  const find = (map) => (fr) => {
@@ -91,20 +91,21 @@ const dispatchOp = rangeMap({
91
91
  def: null,
92
92
  });
93
93
  export const dispatchMap = (ruleSet) => {
94
- const addRuleToDispatch = (dr, rule) => {
94
+ const addRuleToDispatch = (dr, name) => {
95
95
  if (dr === null)
96
96
  return null;
97
- return { tag: dr.tag, rules: [...dr.rules, rule] };
97
+ return { tag: dr.tag, rules: [...dr.rules, name] };
98
98
  };
99
99
  const addTagToDispatch = (dr, tag) => {
100
100
  if (dr === null)
101
101
  return null;
102
102
  return { tag, rules: dr.rules };
103
103
  };
104
- const dispatchRule = (dm, name) => {
104
+ const dispatchRule = (dm, name, current) => {
105
105
  if (name in dm) {
106
106
  return dm;
107
107
  }
108
+ const newCurrent = set(name)(current);
108
109
  const rule = ruleSet[name];
109
110
  if (typeof rule === 'number') {
110
111
  const range = rangeDecode(rule);
@@ -116,14 +117,20 @@ export const dispatchMap = (ruleSet) => {
116
117
  let emptyTag = true;
117
118
  let result = [];
118
119
  for (const item of rule) {
119
- dm = dispatchRule(dm, item);
120
- const dr = dm[item];
121
- if (emptyTag === true) {
122
- result = toArray(dispatchOp.merge(result)(dr.rangeMap));
123
- emptyTag = dr.emptyTag !== undefined ? true : undefined;
120
+ if (contains(item)(newCurrent)) {
121
+ result = result.map(x => [addRuleToDispatch(x[0], item), x[1]]);
124
122
  }
125
123
  else {
126
- result = result.map(x => [addRuleToDispatch(x[0], dr), x[1]]);
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
+ }
127
134
  }
128
135
  }
129
136
  const dr = { emptyTag, rangeMap: result };
@@ -134,7 +141,7 @@ export const dispatchMap = (ruleSet) => {
134
141
  let result = [];
135
142
  let emptyTag = undefined;
136
143
  for (const [tag, item] of entries) {
137
- dm = dispatchRule(dm, item);
144
+ dm = dispatchRule(dm, item, newCurrent);
138
145
  const dr = dm[item];
139
146
  if (dr.emptyTag !== undefined) {
140
147
  emptyTag = tag;
@@ -150,21 +157,53 @@ export const dispatchMap = (ruleSet) => {
150
157
  };
151
158
  let result = {};
152
159
  for (const k in ruleSet) {
153
- result = dispatchRule(result, k);
160
+ result = dispatchRule(result, k, null);
154
161
  }
155
162
  return result;
156
163
  };
157
164
  export const parser = (fr) => {
158
165
  const data = toData(fr);
159
- return todo();
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;
160
209
  };
161
- /**
162
- * Either `{ variantItem: id }` or `id`.
163
- */
164
- /*
165
- type DispatchRule = SingleProperty<> | string
166
-
167
- type Dispatch = RangeMapArray<DispatchRule>
168
-
169
- type DispatchMap = { readonly[id in string]: Dispatch }
170
- */
@@ -2,6 +2,9 @@ declare const _default: {
2
2
  toData: (() => void)[];
3
3
  variantTest: () => void;
4
4
  dispatch: (() => void)[];
5
+ parser: (() => void)[];
6
+ repeat: (() => void)[];
7
+ repeatParser: (() => void)[];
5
8
  example: () => void;
6
9
  };
7
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 { dispatchMap, 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,15 +53,44 @@ 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
  },
@@ -68,7 +100,7 @@ export default {
68
100
  const data = toData(terminalRangeRule);
69
101
  const dm = dispatchMap(data[0]);
70
102
  const result = JSON.stringify(dm);
71
- if (result != '{"":{"rangeMap":[[null,64],[{"rules":[]},70]]}}') {
103
+ if (result !== '{"":{"rangeMap":[[null,64],[{"rules":[]},70]]}}') {
72
104
  throw result;
73
105
  }
74
106
  },
@@ -77,7 +109,18 @@ export default {
77
109
  const data = toData(stringRule);
78
110
  const dm = dispatchMap(data[0]);
79
111
  const result = JSON.stringify(dm);
80
- if (result != '{"0":{"rangeMap":[[null,64],[{"rules":[]},65]]},"1":{"rangeMap":[[null,65],[{"rules":[]},66]]},"":{"rangeMap":[[null,64],[{"rules":[{"rangeMap":[[null,65],[{"rules":[]},66]]}]},65]]}}') {
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]]}}') {
81
124
  throw result;
82
125
  }
83
126
  },
@@ -86,7 +129,7 @@ export default {
86
129
  const data = toData(emptyRule);
87
130
  const dm = dispatchMap(data[0]);
88
131
  const result = JSON.stringify(dm);
89
- if (result != '{"":{"emptyTag":true,"rangeMap":[]}}') {
132
+ if (result !== '{"":{"emptyTag":true,"rangeMap":[]}}') {
90
133
  throw result;
91
134
  }
92
135
  },
@@ -95,7 +138,7 @@ export default {
95
138
  const data = toData(variantRule);
96
139
  const dm = dispatchMap(data[0]);
97
140
  const result = JSON.stringify(dm);
98
- 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]]}}') {
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]]}}') {
99
142
  throw result;
100
143
  }
101
144
  },
@@ -105,10 +148,276 @@ export default {
105
148
  const data = toData(variantRule);
106
149
  const dm = dispatchMap(data[0]);
107
150
  const result = JSON.stringify(dm);
108
- if (result != '{"0":{"emptyTag":true,"rangeMap":[]},"1":{"rangeMap":[[null,64],[{"rules":[]},65]]},"":{"emptyTag":"e","rangeMap":[[null,64],[{"tag":"a","rules":[]},65]]}}') {
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]]}}') {
109
165
  throw result;
110
166
  }
111
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
+ }
112
421
  ],
113
422
  example: () => {
114
423
  const grammar = {
package/bnf/module.f.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import { type Array2 } from '../types/array/module.f.ts';
2
2
  /**
3
- * A range of symbols (48 bits)
3
+ * A range of symbols. Two 24-bit numbers are stored in one JS number (48 bits).
4
+ *
4
5
  * For example: 0xBBBBBB_EEEEEE
5
- * - 0xBBBBBB is the first symbol
6
- * - 0xEEEEEE is the last symbol
6
+ * - 0xBBBBBB is the first symbol (24 bits)
7
+ * - 0xEEEEEE is the last symbol (24 bits)
7
8
  */
8
9
  export type TerminalRange = number;
9
10
  /** A sequence of rules. */
@@ -34,8 +35,8 @@ export declare const remove: (range: TerminalRange, removeSet: RangeVariant) =>
34
35
  export type None = readonly [];
35
36
  export declare const none: None;
36
37
  export type Option<S> = {
37
- none: None;
38
38
  some: S;
39
+ none: None;
39
40
  };
40
41
  export declare const option: <S extends Rule>(some: S) => Option<S>;
41
42
  export type Repeat0Plus<T> = () => Option<readonly [T, Repeat0Plus<T>]>;
package/bnf/module.f.js CHANGED
@@ -69,8 +69,8 @@ export const remove = (range, removeSet) => {
69
69
  };
70
70
  export const none = [];
71
71
  export const option = (some) => ({
72
- none,
73
72
  some,
73
+ none,
74
74
  });
75
75
  /**
76
76
  * Repeat zero or more times.