functionalscript 0.7.0 → 0.8.1

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.
Files changed (125) hide show
  1. package/LICENSE +21 -661
  2. package/README.md +3 -2
  3. package/bnf/data/module.f.d.ts +16 -6
  4. package/bnf/data/module.f.js +115 -23
  5. package/bnf/data/test.f.d.ts +4 -0
  6. package/bnf/data/test.f.js +389 -14
  7. package/bnf/module.f.d.ts +5 -4
  8. package/bnf/module.f.js +1 -1
  9. package/bnf/testlib.f.js +1 -1
  10. package/ci/module.f.d.ts +3 -0
  11. package/ci/module.f.js +169 -0
  12. package/ci/module.js +3 -0
  13. package/crypto/hmac/module.f.d.ts +5 -4
  14. package/crypto/hmac/module.f.js +9 -18
  15. package/crypto/hmac/test.f.d.ts +1 -0
  16. package/crypto/hmac/test.f.js +16 -8
  17. package/crypto/prime_field/module.f.d.ts +1 -1
  18. package/crypto/prime_field/module.f.js +4 -3
  19. package/crypto/prime_field/test.f.js +13 -13
  20. package/crypto/rfc6979/module.f.d.ts +15 -0
  21. package/crypto/rfc6979/module.f.js +98 -0
  22. package/crypto/rfc6979/test.f.d.ts +10 -0
  23. package/crypto/rfc6979/test.f.js +490 -0
  24. package/crypto/secp/module.f.d.ts +4 -4
  25. package/crypto/secp/module.f.js +1 -1
  26. package/crypto/secp/test.f.js +8 -8
  27. package/crypto/sha2/module.f.d.ts +11 -5
  28. package/crypto/sha2/module.f.js +4 -3
  29. package/crypto/sha2/test.f.d.ts +4 -1
  30. package/crypto/sha2/test.f.js +41 -31
  31. package/crypto/sign/module.f.d.ts +1 -1
  32. package/crypto/sign/module.f.js +3 -2
  33. package/dev/tf/all.test.js +9 -1
  34. package/djs/ast/module.f.d.ts +3 -3
  35. package/djs/ast/test.f.js +7 -8
  36. package/djs/parser/module.f.d.ts +3 -3
  37. package/djs/parser/module.f.js +4 -4
  38. package/djs/parser/test.f.js +76 -77
  39. package/djs/serializer/module.f.d.ts +8 -8
  40. package/djs/serializer/module.f.js +4 -7
  41. package/djs/serializer/test.f.js +8 -9
  42. package/djs/tokenizer/module.f.d.ts +2 -2
  43. package/djs/tokenizer/module.f.js +3 -5
  44. package/djs/tokenizer/test.f.js +8 -10
  45. package/djs/transpiler/module.f.d.ts +3 -3
  46. package/djs/transpiler/module.f.js +2 -0
  47. package/fsc/bnf.f.d.ts +1 -1
  48. package/fsc/bnf.f.js +39 -51
  49. package/fsc/json.f.d.ts +1 -1
  50. package/fsc/json.f.js +56 -81
  51. package/fsc/test.f.d.ts +5 -0
  52. package/fsc/test.f.js +69 -7
  53. package/fsm/module.f.js +3 -3
  54. package/fsm/test.f.js +21 -25
  55. package/html/module.f.js +17 -4
  56. package/html/test.f.d.ts +7 -0
  57. package/html/test.f.js +37 -0
  58. package/issues/031-json.f.d.ts +1 -0
  59. package/js/tokenizer/module.f.d.ts +4 -4
  60. package/js/tokenizer/module.f.js +12 -17
  61. package/js/tokenizer/test.f.js +9 -11
  62. package/json/module.f.d.ts +6 -6
  63. package/json/module.f.js +5 -10
  64. package/json/parser/module.f.d.ts +4 -4
  65. package/json/parser/module.f.js +7 -4
  66. package/json/parser/test.f.js +47 -49
  67. package/json/serializer/module.f.d.ts +6 -6
  68. package/json/serializer/module.f.js +3 -2
  69. package/json/serializer/test.f.js +13 -13
  70. package/json/test.f.js +13 -15
  71. package/json/tokenizer/module.f.d.ts +4 -4
  72. package/json/tokenizer/module.f.js +6 -7
  73. package/json/tokenizer/test.f.js +7 -9
  74. package/nanvm-lib/tests/vm/test.f.d.ts +25 -0
  75. package/nanvm-lib/tests/vm/test.f.js +105 -0
  76. package/package.json +8 -8
  77. package/text/ascii/test.f.js +2 -2
  78. package/text/module.f.d.ts +3 -2
  79. package/text/module.f.js +2 -2
  80. package/text/test.f.js +3 -3
  81. package/text/utf16/test.f.js +2 -2
  82. package/text/utf8/test.f.js +2 -2
  83. package/types/array/test.f.js +2 -2
  84. package/types/bigint/module.f.d.ts +6 -3
  85. package/types/bigint/module.f.js +12 -11
  86. package/types/bigint/test.f.d.ts +2 -0
  87. package/types/bigint/test.f.js +21 -2
  88. package/types/bit_vec/module.f.d.ts +66 -34
  89. package/types/bit_vec/module.f.js +97 -32
  90. package/types/bit_vec/test.f.d.ts +7 -0
  91. package/types/bit_vec/test.f.js +283 -62
  92. package/types/btree/find/test.f.js +9 -8
  93. package/types/btree/remove/test.f.js +4 -4
  94. package/types/btree/set/test.f.js +4 -4
  95. package/types/btree/test.f.js +7 -7
  96. package/types/byte_set/test.f.js +2 -2
  97. package/types/function/compare/module.f.d.ts +15 -1
  98. package/types/function/compare/module.f.js +1 -1
  99. package/types/function/compare/test.f.js +37 -4
  100. package/types/list/test.f.js +93 -93
  101. package/types/monoid/module.f.d.ts +4 -4
  102. package/types/monoid/module.f.js +3 -3
  103. package/types/monoid/test.f.js +3 -3
  104. package/types/nominal/module.f.d.ts +5 -0
  105. package/types/nominal/module.f.js +4 -0
  106. package/types/nominal/test.f.d.ts +5 -0
  107. package/types/nominal/test.f.js +53 -0
  108. package/types/number/module.f.js +2 -2
  109. package/types/range_map/test.f.js +21 -21
  110. package/types/sorted_list/test.f.js +10 -10
  111. package/types/sorted_set/test.f.js +14 -14
  112. package/types/string/module.f.js +2 -2
  113. package/types/string_set/module.f.js +3 -3
  114. package/bnf/djs/module.f.d.ts +0 -77
  115. package/bnf/djs/module.f.js +0 -207
  116. package/bnf/djs/test.f.d.ts +0 -8
  117. package/bnf/djs/test.f.js +0 -277
  118. package/bnf/func/module.f.d.ts +0 -148
  119. package/bnf/func/module.f.js +0 -132
  120. package/bnf/func/test.f.d.ts +0 -12
  121. package/bnf/func/test.f.js +0 -171
  122. package/bnf/func/testlib.f.d.ts +0 -25
  123. package/bnf/func/testlib.f.js +0 -150
  124. /package/{issues/31-json.f.d.ts → ci/module.d.ts} +0 -0
  125. /package/issues/{31-json.f.js → 031-json.f.js} +0 -0
@@ -1,14 +1,14 @@
1
1
  import { get, merge, fromRange, rangeMap } from "./module.f.js";
2
- import { unsafeCmp } from "../function/compare/module.f.js";
3
- import * as json from "../../json/module.f.js";
2
+ import { stringify } from "../../json/module.f.js";
4
3
  import { sort } from "../object/module.f.js";
5
4
  import { union } from "../sorted_set/module.f.js";
6
5
  import { equal, toArray } from "../list/module.f.js";
7
- import * as operator from "../function/operator/module.f.js";
8
- const stringify = json.stringify(sort);
6
+ import { strictEqual } from "../function/operator/module.f.js";
7
+ import { cmp } from "../string/module.f.js";
8
+ const str = stringify(sort);
9
9
  const op = {
10
- union: union(unsafeCmp),
11
- equal: equal(operator.strictEqual),
10
+ union: union(cmp),
11
+ equal: equal(strictEqual),
12
12
  def: []
13
13
  };
14
14
  export default {
@@ -56,7 +56,7 @@ export default {
56
56
  const a = [[['a'], 1], [['b'], 2]];
57
57
  const b = null;
58
58
  const merged = merge(op)(a)(b);
59
- const result = stringify(toArray(merged));
59
+ const result = str(toArray(merged));
60
60
  if (result !== '[[["a"],1],[["b"],2]]') {
61
61
  throw result;
62
62
  }
@@ -65,7 +65,7 @@ export default {
65
65
  const a = null;
66
66
  const b = [[['a'], 1], [['b'], 2]];
67
67
  const merged = merge(op)(a)(b);
68
- const result = stringify(toArray(merged));
68
+ const result = str(toArray(merged));
69
69
  if (result !== '[[["a"],1],[["b"],2]]') {
70
70
  throw result;
71
71
  }
@@ -74,7 +74,7 @@ export default {
74
74
  const a = [[['a'], 1], [['b'], 2]];
75
75
  const b = [[['a'], 1], [['b'], 2]];
76
76
  const merged = merge(op)(a)(b);
77
- const result = stringify(toArray(merged));
77
+ const result = str(toArray(merged));
78
78
  if (result !== '[[["a"],1],[["b"],2]]') {
79
79
  throw result;
80
80
  }
@@ -83,7 +83,7 @@ export default {
83
83
  const a = [[['a'], 1], [['c'], 3]];
84
84
  const b = [[['b'], 2], [['d'], 4]];
85
85
  const merged = merge(op)(a)(b);
86
- const result = stringify(toArray(merged));
86
+ const result = str(toArray(merged));
87
87
  if (result !== '[[["a","b"],1],[["b","c"],2],[["c","d"],3],[["d"],4]]') {
88
88
  throw result;
89
89
  }
@@ -92,7 +92,7 @@ export default {
92
92
  const a = [[['a'], 1], [['d'], 4]];
93
93
  const b = [[['b'], 2], [['c'], 3]];
94
94
  const merged = merge(op)(a)(b);
95
- const result = stringify(toArray(merged));
95
+ const result = str(toArray(merged));
96
96
  if (result !== '[[["a","b"],1],[["b","d"],2],[["c","d"],3],[["d"],4]]') {
97
97
  throw result;
98
98
  }
@@ -101,7 +101,7 @@ export default {
101
101
  const a = [[['a'], 1], [['b'], 2]];
102
102
  const b = [[['b'], 1], [['a'], 2]];
103
103
  const merged = merge(op)(a)(b);
104
- const result = stringify(toArray(merged));
104
+ const result = str(toArray(merged));
105
105
  if (result !== '[[["a","b"],2]]') {
106
106
  throw result;
107
107
  }
@@ -110,7 +110,7 @@ export default {
110
110
  const a = [[['a'], 1], [['b'], 2], [['a'], 3]];
111
111
  const b = [[['a'], 5]];
112
112
  const merged = merge(op)(a)(b);
113
- const result = stringify(toArray(merged));
113
+ const result = str(toArray(merged));
114
114
  if (result !== '[[["a"],1],[["a","b"],2],[["a"],5]]') {
115
115
  throw result;
116
116
  }
@@ -122,56 +122,56 @@ export default {
122
122
  return [
123
123
  () => {
124
124
  const rm = [[['a'], 10], [['b'], 20], [['c'], 30]];
125
- const result = stringify(at(5)(rm));
125
+ const result = str(at(5)(rm));
126
126
  if (result !== '["a"]') {
127
127
  throw result;
128
128
  }
129
129
  },
130
130
  () => {
131
131
  const rm = [[['a'], 10], [['b'], 20], [['c'], 30]];
132
- const result = stringify(at(10)(rm));
132
+ const result = str(at(10)(rm));
133
133
  if (result !== '["a"]') {
134
134
  throw result;
135
135
  }
136
136
  },
137
137
  () => {
138
138
  const rm = [[['a'], 10], [['b'], 20], [['c'], 30]];
139
- const result = stringify(at(15)(rm));
139
+ const result = str(at(15)(rm));
140
140
  if (result !== '["b"]') {
141
141
  throw result;
142
142
  }
143
143
  },
144
144
  () => {
145
145
  const rm = [[['a'], 10], [['b'], 20], [['c'], 30]];
146
- const result = stringify(at(20)(rm));
146
+ const result = str(at(20)(rm));
147
147
  if (result !== '["b"]') {
148
148
  throw result;
149
149
  }
150
150
  },
151
151
  () => {
152
152
  const rm = [[['a'], 10], [['b'], 20], [['c'], 30]];
153
- const result = stringify(at(25)(rm));
153
+ const result = str(at(25)(rm));
154
154
  if (result !== '["c"]') {
155
155
  throw result;
156
156
  }
157
157
  },
158
158
  () => {
159
159
  const rm = [[['a'], 10], [['b'], 20], [['c'], 30]];
160
- const result = stringify(at(30)(rm));
160
+ const result = str(at(30)(rm));
161
161
  if (result !== '["c"]') {
162
162
  throw result;
163
163
  }
164
164
  },
165
165
  () => {
166
166
  const rm = [[['a'], 10], [['b'], 20], [['c'], 30]];
167
- const result = stringify(at(35)(rm));
167
+ const result = str(at(35)(rm));
168
168
  if (result !== '[]') {
169
169
  throw result;
170
170
  }
171
171
  },
172
172
  () => {
173
173
  const rm = [];
174
- const result = stringify(at(10)(rm));
174
+ const result = str(at(10)(rm));
175
175
  if (result !== '[]') {
176
176
  throw result;
177
177
  }
@@ -1,21 +1,21 @@
1
1
  import { find, merge } from "./module.f.js";
2
- import { unsafeCmp } from "../function/compare/module.f.js";
3
- import * as json from "../../json/module.f.js";
2
+ import { stringify } from "../../json/module.f.js";
4
3
  import { sort } from "../object/module.f.js";
5
4
  import { toArray, countdown, length } from "../list/module.f.js";
6
5
  import { flip } from "../function/module.f.js";
7
- const stringify = json.stringify(sort);
8
- const reverseCmp = flip(unsafeCmp);
6
+ import { cmp } from "../number/module.f.js";
7
+ const str = stringify(sort);
8
+ const reverseCmp = flip(cmp);
9
9
  export default {
10
10
  sortedMergre: [
11
11
  () => {
12
- const result = stringify(toArray(merge(unsafeCmp)([2, 3, 4])([1, 3, 5])));
12
+ const result = str(toArray(merge(cmp)([2, 3, 4])([1, 3, 5])));
13
13
  if (result !== '[1,2,3,4,5]') {
14
14
  throw result;
15
15
  }
16
16
  },
17
17
  () => {
18
- const result = stringify(toArray(merge(unsafeCmp)([1, 2, 3])([])));
18
+ const result = str(toArray(merge(cmp)([1, 2, 3])([])));
19
19
  if (result !== '[1,2,3]') {
20
20
  throw result;
21
21
  }
@@ -32,25 +32,25 @@ export default {
32
32
  ],
33
33
  find: [
34
34
  () => {
35
- const result = find(unsafeCmp)(0)([0, 10, 20, 30, 40, 50, 60, 70, 80, 90]);
35
+ const result = find(cmp)(0)([0, 10, 20, 30, 40, 50, 60, 70, 80, 90]);
36
36
  if (result !== 0) {
37
37
  throw result;
38
38
  }
39
39
  },
40
40
  () => {
41
- const result = find(unsafeCmp)(3)([0, 10, 20, 30, 40, 50, 60, 70, 80, 90]);
41
+ const result = find(cmp)(3)([0, 10, 20, 30, 40, 50, 60, 70, 80, 90]);
42
42
  if (result !== null) {
43
43
  throw result;
44
44
  }
45
45
  },
46
46
  () => {
47
- const result = find(unsafeCmp)(77)([0, 10, 20, 30, 40, 50, 60, 70, 80, 90]);
47
+ const result = find(cmp)(77)([0, 10, 20, 30, 40, 50, 60, 70, 80, 90]);
48
48
  if (result !== null) {
49
49
  throw result;
50
50
  }
51
51
  },
52
52
  () => {
53
- const result = find(unsafeCmp)(80)([0, 10, 20, 30, 40, 50, 60, 70, 80, 90]);
53
+ const result = find(cmp)(80)([0, 10, 20, 30, 40, 50, 60, 70, 80, 90]);
54
54
  if (result !== 80) {
55
55
  throw result;
56
56
  }
@@ -1,22 +1,22 @@
1
1
  import { has, intersect, union } from "./module.f.js";
2
- import { unsafeCmp } from "../function/compare/module.f.js";
3
- import * as json from "../../json/module.f.js";
2
+ import { stringify } from "../../json/module.f.js";
4
3
  import { sort } from "../object/module.f.js";
5
4
  import { toArray, countdown, length } from "../list/module.f.js";
6
5
  import { flip } from "../function/module.f.js";
7
- const stringify = a => json.stringify(sort)(a);
8
- const reverseCmp = flip(unsafeCmp);
6
+ import { cmp } from "../number/module.f.js";
7
+ const str = a => stringify(sort)(a);
8
+ const reverseCmp = flip(cmp);
9
9
  export default {
10
10
  example: () => {
11
11
  const cmp = (a) => (b) => a < b ? -1 : a > b ? 1 : 0;
12
12
  const setA = [1, 3, 5];
13
13
  const setB = [3, 4, 5];
14
14
  const unionSet = union(cmp)(setA)(setB); // [1, 3, 4, 5]
15
- if (stringify(unionSet) !== '[1,3,4,5]') {
15
+ if (str(unionSet) !== '[1,3,4,5]') {
16
16
  throw 0;
17
17
  }
18
18
  const intersectionSet = intersect(cmp)(setA)(setB); // [3, 5]
19
- if (stringify(intersectionSet) !== '[3,5]') {
19
+ if (str(intersectionSet) !== '[3,5]') {
20
20
  throw 1;
21
21
  }
22
22
  if (!has(cmp)(3)(setA)) {
@@ -28,13 +28,13 @@ export default {
28
28
  },
29
29
  union: [
30
30
  () => {
31
- const result = stringify(toArray(union(unsafeCmp)([2, 3, 4])([1, 3, 5])));
31
+ const result = str(toArray(union(cmp)([2, 3, 4])([1, 3, 5])));
32
32
  if (result !== '[1,2,3,4,5]') {
33
33
  throw result;
34
34
  }
35
35
  },
36
36
  () => {
37
- const result = stringify(toArray(union(unsafeCmp)([1, 2, 3])([])));
37
+ const result = str(toArray(union(cmp)([1, 2, 3])([])));
38
38
  if (result !== '[1,2,3]') {
39
39
  throw result;
40
40
  }
@@ -51,13 +51,13 @@ export default {
51
51
  ],
52
52
  intersect: [
53
53
  () => {
54
- const result = stringify(toArray(intersect(unsafeCmp)([2, 3, 4])([1, 3, 5])));
54
+ const result = str(toArray(intersect(cmp)([2, 3, 4])([1, 3, 5])));
55
55
  if (result !== '[3]') {
56
56
  throw result;
57
57
  }
58
58
  },
59
59
  () => {
60
- const result = stringify(toArray(intersect(unsafeCmp)([1, 2, 3])([])));
60
+ const result = str(toArray(intersect(cmp)([1, 2, 3])([])));
61
61
  if (result !== '[]') {
62
62
  throw result;
63
63
  }
@@ -65,25 +65,25 @@ export default {
65
65
  ],
66
66
  has: [
67
67
  () => {
68
- const result = has(unsafeCmp)(0)([0, 10, 20, 30, 40, 50, 60, 70, 80, 90]);
68
+ const result = has(cmp)(0)([0, 10, 20, 30, 40, 50, 60, 70, 80, 90]);
69
69
  if (!result) {
70
70
  throw result;
71
71
  }
72
72
  },
73
73
  () => {
74
- const result = has(unsafeCmp)(3)([0, 10, 20, 30, 40, 50, 60, 70, 80, 90]);
74
+ const result = has(cmp)(3)([0, 10, 20, 30, 40, 50, 60, 70, 80, 90]);
75
75
  if (result) {
76
76
  throw result;
77
77
  }
78
78
  },
79
79
  () => {
80
- const result = has(unsafeCmp)(77)([0, 10, 20, 30, 40, 50, 60, 70, 80, 90]);
80
+ const result = has(cmp)(77)([0, 10, 20, 30, 40, 50, 60, 70, 80, 90]);
81
81
  if (result) {
82
82
  throw result;
83
83
  }
84
84
  },
85
85
  () => {
86
- const result = has(unsafeCmp)(80)([0, 10, 20, 30, 40, 50, 60, 70, 80, 90]);
86
+ const result = has(cmp)(80)([0, 10, 20, 30, 40, 50, 60, 70, 80, 90]);
87
87
  if (!result) {
88
88
  throw result;
89
89
  }
@@ -17,10 +17,10 @@
17
17
  */
18
18
  import { reduce as listReduce, repeat as listRepeat } from "../list/module.f.js";
19
19
  import { compose } from "../function/module.f.js";
20
- import { unsafeCmp } from "../function/compare/module.f.js";
20
+ import { cmp as uCmp } from "../function/compare/module.f.js";
21
21
  import { join as joinOp, concat as concatOp } from "../function/operator/module.f.js";
22
22
  const reduce = o => listReduce(o)('');
23
23
  export const join = compose(joinOp)(reduce);
24
24
  export const concat = reduce(concatOp);
25
25
  export const repeat = v => compose(listRepeat(v))(concat);
26
- export const cmp = unsafeCmp;
26
+ export const cmp = uCmp;
@@ -19,15 +19,15 @@
19
19
  * if (contains('banana')(mySet)) { throw '4' }
20
20
  * ```
21
21
  */
22
- import * as btree from "../btree/module.f.js";
22
+ import { empty as btEmpty, values as btValues } from "../btree/module.f.js";
23
23
  import { find, isFound } from "../btree/find/module.f.js";
24
24
  import { remove as btreeRemove } from "../btree/remove/module.f.js";
25
25
  import { set as btreeSet } from "../btree/set/module.f.js";
26
26
  import { cmp } from "../string/module.f.js";
27
27
  import { fold } from "../list/module.f.js";
28
28
  import { compose } from "../function/module.f.js";
29
- export const values = btree.values;
30
- export const empty = btree.empty;
29
+ export const values = btValues;
30
+ export const empty = btEmpty;
31
31
  export const contains = value => {
32
32
  const f = find(cmp(value));
33
33
  return s => s !== null && isFound(f(s).first);
@@ -1,77 +0,0 @@
1
- /**
2
- * Rules for serializing and deserializing the BNF grammar.
3
- *
4
- * @module
5
- *
6
- * @example
7
- *
8
- * ```ts
9
- * // Define a simple grammar
10
- * const grammar: Rule = () => [
11
- * [range('AZ')], // 'A-Z'
12
- * [range('az'), grammar], // 'a-z' followed by more grammar
13
- * ]
14
- *
15
- * const parse = parser(toRuleMap(grammar))
16
- *
17
- * // Parse an input
18
- * const input = toArray(stringToCodePointList('abcdefgA'))
19
- * const result = parse(grammar.name, input)
20
- * if (result === null) { throw result }
21
- * const [, b] = result
22
- * if (b?.length !== 0) { throw b }
23
- * ```
24
- */
25
- import type { CodePoint } from '../../text/utf16/module.f.ts';
26
- import type { TerminalRange, Rule as FRule } from '../func/module.f.ts';
27
- /**
28
- * Represents a sequence of grammar rules, including terminal ranges or rule identifiers.
29
- * @template Id - The type for rule identifiers.
30
- */
31
- export type Sequence<Id> = readonly (TerminalRange | Id)[];
32
- /**
33
- * Represents a grammar rule as a collection of sequences.
34
- * @template Id - The type for rule identifiers.
35
- */
36
- export type Rule<Id> = readonly Sequence<Id>[];
37
- /**
38
- * Represents a map of grammar rules, where each rule is identified by a unique string identifier.
39
- * @template Id - The type for rule identifiers.
40
- */
41
- export type RuleMap<Id extends string> = {
42
- readonly [k in Id]: Rule<Id>;
43
- };
44
- /**
45
- * Transforming functional rule to a serializable rule map.
46
- *
47
- * @param src a functional rule.
48
- * @returns a serializable rule map.
49
- */
50
- export declare const toRuleMap: (src: FRule) => RuleMap<string>;
51
- /**
52
- * Represents a parsed Abstract Syntax Tree (AST) sequence.
53
- */
54
- export type AstSequence = readonly (AstRule | CodePoint)[];
55
- /**
56
- * Represents a parsed AST rule, consisting of a rule name and its parsed sequence.
57
- */
58
- export type AstRule = readonly [string, AstSequence];
59
- /**
60
- * Represents the remaining input after a match attempt, or `null` if no match is possible.
61
- */
62
- export type Remainder = readonly CodePoint[] | null;
63
- /**
64
- * Represents the result of a match operation, including the parsed AST rule and the remainder of the input.
65
- */
66
- export type MatchResult = readonly [AstRule, Remainder];
67
- /**
68
- * Represents an LL(1) parser function for matching input against grammar rules.
69
- */
70
- export type Match = (name: string, s: readonly CodePoint[]) => MatchResult;
71
- /**
72
- * Creates a simple LL1 parser for the given map.
73
- *
74
- * @param map a prepared rule map.
75
- * @returns a parser.
76
- */
77
- export declare const parser: (rm: RuleMap<string>) => Match;
@@ -1,207 +0,0 @@
1
- /**
2
- * Rules for serializing and deserializing the BNF grammar.
3
- *
4
- * @module
5
- *
6
- * @example
7
- *
8
- * ```ts
9
- * // Define a simple grammar
10
- * const grammar: Rule = () => [
11
- * [range('AZ')], // 'A-Z'
12
- * [range('az'), grammar], // 'a-z' followed by more grammar
13
- * ]
14
- *
15
- * const parse = parser(toRuleMap(grammar))
16
- *
17
- * // Parse an input
18
- * const input = toArray(stringToCodePointList('abcdefgA'))
19
- * const result = parse(grammar.name, input)
20
- * if (result === null) { throw result }
21
- * const [, b] = result
22
- * if (b?.length !== 0) { throw b }
23
- * ```
24
- */
25
- import { empty as emptyArray } from "../../types/array/module.f.js";
26
- import { strictEqual } from "../../types/function/operator/module.f.js";
27
- import { toArray } from "../../types/list/module.f.js";
28
- import { contains } from "../../types/range/module.f.js";
29
- import { rangeMap } from "../../types/range_map/module.f.js";
30
- const findName = (map, rule) => {
31
- const { name } = rule;
32
- let result = name;
33
- {
34
- let i = 0;
35
- while (result in map) {
36
- result = name + i;
37
- ++i;
38
- }
39
- }
40
- return result;
41
- };
42
- /**
43
- * Add a new rule to the temporary map.
44
- */
45
- const tmpAdd = ({ queue, result }) => (src) => {
46
- // find a name for the item.
47
- const name = findName(result, src);
48
- return [{
49
- // add the item to a queue under the name.
50
- queue: [...queue, [src, name]],
51
- // add the name to the result and fill the rule later.
52
- result: { ...result, [name]: emptyArray },
53
- }, name];
54
- };
55
- const tmpNew = tmpAdd({
56
- queue: emptyArray,
57
- result: {},
58
- });
59
- const tmpItem = (tmp, src) => {
60
- const found = tmp.queue.find(([f]) => f === src);
61
- return found !== undefined ? [tmp, found[1]] : tmpAdd(tmp)(src);
62
- };
63
- /**
64
- * Transforming functional rule to a serializable rule map.
65
- *
66
- * @param src a functional rule.
67
- * @returns a serializable rule map.
68
- */
69
- export const toRuleMap = (src) => {
70
- let [tmp] = tmpNew(src);
71
- let i = 0;
72
- do {
73
- const [srcOr, name] = tmp.queue[i];
74
- let rule = emptyArray;
75
- // iterate all sequences of the `Or` rule.
76
- for (const srcSeq of srcOr()) {
77
- let s = emptyArray;
78
- // iterate all items of the sequence.
79
- for (const srcItem of srcSeq) {
80
- let item;
81
- if (srcItem instanceof Array) {
82
- item = srcItem;
83
- }
84
- else {
85
- [tmp, item] = tmpItem(tmp, srcItem);
86
- }
87
- s = [...s, item];
88
- }
89
- rule = [...rule, s];
90
- }
91
- // fix the rule in the result.
92
- tmp = {
93
- queue: tmp.queue,
94
- result: { ...tmp.result, [name]: rule },
95
- };
96
- ++i;
97
- } while (i !== tmp.queue.length);
98
- return tmp.result;
99
- };
100
- const dispatchOp = rangeMap({
101
- union: a => b => {
102
- if (a === null) {
103
- return b;
104
- }
105
- if (b === null) {
106
- return a;
107
- }
108
- throw ['can not merge [', a, '][', b, ']'];
109
- },
110
- equal: strictEqual,
111
- def: null,
112
- });
113
- /**
114
- * Creates a dispatch map for LL1 parser.
115
- *
116
- * @param ruleMap a serializable rule map.
117
- * @returns A dispatch map
118
- */
119
- const dispatchMap = (ruleMap) => {
120
- const dispatchSequence = (dm, sequence) => {
121
- let empty = true;
122
- let result = [];
123
- for (const item of sequence) {
124
- if (typeof item === 'string') {
125
- dm = dispatchRule(dm, item);
126
- const [e, dispatch] = dm[item];
127
- result = toArray(dispatchOp.merge(result)(dispatch.map(x => [x[0] === null ? null : sequence, x[1]])));
128
- if (e) {
129
- continue;
130
- }
131
- }
132
- else {
133
- const dispatch = dispatchOp.fromRange(item)(sequence);
134
- result = toArray(dispatchOp.merge(result)(dispatch));
135
- }
136
- empty = false;
137
- break;
138
- }
139
- return [dm, [empty, result]];
140
- };
141
- const dispatchRule = (dm, name) => {
142
- if (name in dm) {
143
- return dm;
144
- }
145
- let empty = false;
146
- let dispatch = [];
147
- for (const sequence of ruleMap[name]) {
148
- const [newDm, [e, d]] = dispatchSequence(dm, sequence);
149
- dm = newDm;
150
- empty ||= e;
151
- dispatch = toArray(dispatchOp.merge(dispatch)(d));
152
- }
153
- return { ...dm, [name]: [empty, dispatch] };
154
- };
155
- let result = {};
156
- for (const k in ruleMap) {
157
- result = dispatchRule(result, k);
158
- }
159
- // TODO: validate all sequences if they are deterministic
160
- return result;
161
- };
162
- /**
163
- * Creates a simple LL1 parser for the given map.
164
- *
165
- * @param map a prepared rule map.
166
- * @returns a parser.
167
- */
168
- export const parser = (rm) => {
169
- const map = dispatchMap(rm);
170
- const f = (name, s) => {
171
- const mr = (a, r) => [[name, a], r];
172
- const mre = (a) => mr(a, null);
173
- const [empty, sequence] = map[name];
174
- if (s.length === 0) {
175
- return mr([], empty ? s : null);
176
- }
177
- const cp = s[0];
178
- const i = dispatchOp.get(cp)(sequence);
179
- if (i === null) {
180
- return mr([], empty ? s : null);
181
- }
182
- let a = [];
183
- let si = s;
184
- for (const c of i) {
185
- if (typeof c === 'string') {
186
- // c is a name
187
- const [astRule, newSi] = f(c, si);
188
- a = [...a, astRule];
189
- if (newSi === null) {
190
- return mre(a);
191
- }
192
- si = newSi;
193
- }
194
- else {
195
- // c is TerminalRange
196
- const [first, ...newSi] = si;
197
- if (first === undefined || !contains(c)(first)) {
198
- return mre(a);
199
- }
200
- a = [...a, first];
201
- si = newSi;
202
- }
203
- }
204
- return mr(a, si);
205
- };
206
- return f;
207
- };
@@ -1,8 +0,0 @@
1
- declare const _default: {
2
- example: {
3
- module: () => void;
4
- };
5
- classic: () => void;
6
- map: () => void;
7
- };
8
- export default _default;