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.
- package/LICENSE +21 -661
- package/README.md +3 -2
- package/bnf/data/module.f.d.ts +16 -6
- package/bnf/data/module.f.js +115 -23
- package/bnf/data/test.f.d.ts +4 -0
- package/bnf/data/test.f.js +389 -14
- package/bnf/module.f.d.ts +5 -4
- package/bnf/module.f.js +1 -1
- package/bnf/testlib.f.js +1 -1
- package/ci/module.f.d.ts +3 -0
- package/ci/module.f.js +169 -0
- package/ci/module.js +3 -0
- package/crypto/hmac/module.f.d.ts +5 -4
- package/crypto/hmac/module.f.js +9 -18
- package/crypto/hmac/test.f.d.ts +1 -0
- package/crypto/hmac/test.f.js +16 -8
- package/crypto/prime_field/module.f.d.ts +1 -1
- package/crypto/prime_field/module.f.js +4 -3
- package/crypto/prime_field/test.f.js +13 -13
- package/crypto/rfc6979/module.f.d.ts +15 -0
- package/crypto/rfc6979/module.f.js +98 -0
- package/crypto/rfc6979/test.f.d.ts +10 -0
- package/crypto/rfc6979/test.f.js +490 -0
- package/crypto/secp/module.f.d.ts +4 -4
- package/crypto/secp/module.f.js +1 -1
- package/crypto/secp/test.f.js +8 -8
- package/crypto/sha2/module.f.d.ts +11 -5
- package/crypto/sha2/module.f.js +4 -3
- package/crypto/sha2/test.f.d.ts +4 -1
- package/crypto/sha2/test.f.js +41 -31
- package/crypto/sign/module.f.d.ts +1 -1
- package/crypto/sign/module.f.js +3 -2
- package/dev/tf/all.test.js +9 -1
- package/djs/ast/module.f.d.ts +3 -3
- package/djs/ast/test.f.js +7 -8
- package/djs/parser/module.f.d.ts +3 -3
- package/djs/parser/module.f.js +4 -4
- package/djs/parser/test.f.js +76 -77
- package/djs/serializer/module.f.d.ts +8 -8
- package/djs/serializer/module.f.js +4 -7
- package/djs/serializer/test.f.js +8 -9
- package/djs/tokenizer/module.f.d.ts +2 -2
- package/djs/tokenizer/module.f.js +3 -5
- package/djs/tokenizer/test.f.js +8 -10
- package/djs/transpiler/module.f.d.ts +3 -3
- package/djs/transpiler/module.f.js +2 -0
- package/fsc/bnf.f.d.ts +1 -1
- package/fsc/bnf.f.js +39 -51
- package/fsc/json.f.d.ts +1 -1
- package/fsc/json.f.js +56 -81
- package/fsc/test.f.d.ts +5 -0
- package/fsc/test.f.js +69 -7
- package/fsm/module.f.js +3 -3
- package/fsm/test.f.js +21 -25
- package/html/module.f.js +17 -4
- package/html/test.f.d.ts +7 -0
- package/html/test.f.js +37 -0
- package/issues/031-json.f.d.ts +1 -0
- package/js/tokenizer/module.f.d.ts +4 -4
- package/js/tokenizer/module.f.js +12 -17
- package/js/tokenizer/test.f.js +9 -11
- package/json/module.f.d.ts +6 -6
- package/json/module.f.js +5 -10
- package/json/parser/module.f.d.ts +4 -4
- package/json/parser/module.f.js +7 -4
- package/json/parser/test.f.js +47 -49
- package/json/serializer/module.f.d.ts +6 -6
- package/json/serializer/module.f.js +3 -2
- package/json/serializer/test.f.js +13 -13
- package/json/test.f.js +13 -15
- package/json/tokenizer/module.f.d.ts +4 -4
- package/json/tokenizer/module.f.js +6 -7
- package/json/tokenizer/test.f.js +7 -9
- package/nanvm-lib/tests/vm/test.f.d.ts +25 -0
- package/nanvm-lib/tests/vm/test.f.js +105 -0
- package/package.json +8 -8
- package/text/ascii/test.f.js +2 -2
- package/text/module.f.d.ts +3 -2
- package/text/module.f.js +2 -2
- package/text/test.f.js +3 -3
- package/text/utf16/test.f.js +2 -2
- package/text/utf8/test.f.js +2 -2
- package/types/array/test.f.js +2 -2
- package/types/bigint/module.f.d.ts +6 -3
- package/types/bigint/module.f.js +12 -11
- package/types/bigint/test.f.d.ts +2 -0
- package/types/bigint/test.f.js +21 -2
- package/types/bit_vec/module.f.d.ts +66 -34
- package/types/bit_vec/module.f.js +97 -32
- package/types/bit_vec/test.f.d.ts +7 -0
- package/types/bit_vec/test.f.js +283 -62
- package/types/btree/find/test.f.js +9 -8
- package/types/btree/remove/test.f.js +4 -4
- package/types/btree/set/test.f.js +4 -4
- package/types/btree/test.f.js +7 -7
- package/types/byte_set/test.f.js +2 -2
- package/types/function/compare/module.f.d.ts +15 -1
- package/types/function/compare/module.f.js +1 -1
- package/types/function/compare/test.f.js +37 -4
- package/types/list/test.f.js +93 -93
- package/types/monoid/module.f.d.ts +4 -4
- package/types/monoid/module.f.js +3 -3
- package/types/monoid/test.f.js +3 -3
- package/types/nominal/module.f.d.ts +5 -0
- package/types/nominal/module.f.js +4 -0
- package/types/nominal/test.f.d.ts +5 -0
- package/types/nominal/test.f.js +53 -0
- package/types/number/module.f.js +2 -2
- package/types/range_map/test.f.js +21 -21
- package/types/sorted_list/test.f.js +10 -10
- package/types/sorted_set/test.f.js +14 -14
- package/types/string/module.f.js +2 -2
- package/types/string_set/module.f.js +3 -3
- package/bnf/djs/module.f.d.ts +0 -77
- package/bnf/djs/module.f.js +0 -207
- package/bnf/djs/test.f.d.ts +0 -8
- package/bnf/djs/test.f.js +0 -277
- package/bnf/func/module.f.d.ts +0 -148
- package/bnf/func/module.f.js +0 -132
- package/bnf/func/test.f.d.ts +0 -12
- package/bnf/func/test.f.js +0 -171
- package/bnf/func/testlib.f.d.ts +0 -25
- package/bnf/func/testlib.f.js +0 -150
- /package/{issues/31-json.f.d.ts → ci/module.d.ts} +0 -0
- /package/issues/{31-json.f.js → 031-json.f.js} +0 -0
package/bnf/djs/test.f.js
DELETED
|
@@ -1,277 +0,0 @@
|
|
|
1
|
-
import { cp, range, remove, set, str } from "../func/module.f.js";
|
|
2
|
-
import { parser, toRuleMap } from "./module.f.js";
|
|
3
|
-
import { classic } from "../func/testlib.f.js";
|
|
4
|
-
import * as j from "../../json/module.f.js";
|
|
5
|
-
import { sort } from "../../types/object/module.f.js";
|
|
6
|
-
import { stringToCodePointList } from "../../text/utf16/module.f.js";
|
|
7
|
-
import { toArray } from "../../types/list/module.f.js";
|
|
8
|
-
const stringify = j.stringify(sort);
|
|
9
|
-
const classicTest = () => {
|
|
10
|
-
const map = {
|
|
11
|
-
json: [
|
|
12
|
-
['element']
|
|
13
|
-
],
|
|
14
|
-
value: [
|
|
15
|
-
['object'],
|
|
16
|
-
['array'],
|
|
17
|
-
['string'],
|
|
18
|
-
['number'],
|
|
19
|
-
str('true'),
|
|
20
|
-
str('false'),
|
|
21
|
-
str('null'),
|
|
22
|
-
],
|
|
23
|
-
object: [
|
|
24
|
-
[cp('{'), 'ws', cp('}')],
|
|
25
|
-
[cp('{'), 'members', cp('}')],
|
|
26
|
-
],
|
|
27
|
-
members: [
|
|
28
|
-
['member'],
|
|
29
|
-
['member', cp(','), 'members'],
|
|
30
|
-
],
|
|
31
|
-
member: [
|
|
32
|
-
['ws', 'string', 'ws', cp(':'), 'element'],
|
|
33
|
-
],
|
|
34
|
-
array: [
|
|
35
|
-
[cp('['), 'ws', cp(']')],
|
|
36
|
-
[cp('['), 'elements', cp(']')],
|
|
37
|
-
],
|
|
38
|
-
elements: [
|
|
39
|
-
['element'],
|
|
40
|
-
['element', cp(','), 'elements'],
|
|
41
|
-
],
|
|
42
|
-
element: [
|
|
43
|
-
['ws', 'value', 'ws'],
|
|
44
|
-
],
|
|
45
|
-
string: [
|
|
46
|
-
[cp('"'), 'characters', cp('"')],
|
|
47
|
-
],
|
|
48
|
-
characters: [
|
|
49
|
-
[],
|
|
50
|
-
['character', 'characters'],
|
|
51
|
-
],
|
|
52
|
-
character: [
|
|
53
|
-
...remove([0x20, 0x10FFFF], [cp('"'), cp('\\')]),
|
|
54
|
-
[cp('\\'), 'escape'], // 92
|
|
55
|
-
],
|
|
56
|
-
escape: [
|
|
57
|
-
str('"'),
|
|
58
|
-
str('\\'),
|
|
59
|
-
str('/'),
|
|
60
|
-
str('b'),
|
|
61
|
-
str('f'),
|
|
62
|
-
str('n'),
|
|
63
|
-
str('r'),
|
|
64
|
-
str('t'),
|
|
65
|
-
[cp('u'), 'hex', 'hex', 'hex', 'hex'],
|
|
66
|
-
],
|
|
67
|
-
hex: [
|
|
68
|
-
['digit'],
|
|
69
|
-
[range('AF')], // A-F
|
|
70
|
-
[range('af')], // a-f
|
|
71
|
-
],
|
|
72
|
-
number: [
|
|
73
|
-
['integer', 'fraction', 'exponent'],
|
|
74
|
-
],
|
|
75
|
-
integer: [
|
|
76
|
-
['digit'],
|
|
77
|
-
['onenine', 'digits'],
|
|
78
|
-
[cp('-'), 'digit'],
|
|
79
|
-
[cp('-'), 'onenine', 'digits'],
|
|
80
|
-
],
|
|
81
|
-
digits: [
|
|
82
|
-
['digit'],
|
|
83
|
-
['digit', 'digits'],
|
|
84
|
-
],
|
|
85
|
-
digit: [
|
|
86
|
-
[cp('0')],
|
|
87
|
-
['onenine'],
|
|
88
|
-
],
|
|
89
|
-
onenine: [
|
|
90
|
-
[range('19')],
|
|
91
|
-
],
|
|
92
|
-
fraction: [
|
|
93
|
-
[],
|
|
94
|
-
[cp('.'), 'digits'],
|
|
95
|
-
],
|
|
96
|
-
exponent: [
|
|
97
|
-
[],
|
|
98
|
-
[cp('E'), 'sign', 'digits'],
|
|
99
|
-
[cp('e'), 'sign', 'digits'],
|
|
100
|
-
],
|
|
101
|
-
sign: [
|
|
102
|
-
[],
|
|
103
|
-
[cp('+')],
|
|
104
|
-
[cp('-')],
|
|
105
|
-
],
|
|
106
|
-
ws: [
|
|
107
|
-
[],
|
|
108
|
-
[cp(' '), 'ws'],
|
|
109
|
-
[cp('\n'), 'ws'],
|
|
110
|
-
[cp('\r'), 'ws'],
|
|
111
|
-
[cp('\t'), 'ws'],
|
|
112
|
-
],
|
|
113
|
-
};
|
|
114
|
-
const result = map;
|
|
115
|
-
return result;
|
|
116
|
-
};
|
|
117
|
-
const repeat0Name = (v) => `${v}Repeat0`;
|
|
118
|
-
const repeat0Body = (v) => [
|
|
119
|
-
[],
|
|
120
|
-
[v, repeat0Name(v)]
|
|
121
|
-
];
|
|
122
|
-
const repeat0 = (v) => {
|
|
123
|
-
const name = repeat0Name(v);
|
|
124
|
-
const body = repeat0Body(v);
|
|
125
|
-
return { [name]: body };
|
|
126
|
-
};
|
|
127
|
-
const deterministic = () => {
|
|
128
|
-
const map = {
|
|
129
|
-
json: [
|
|
130
|
-
['wsRepeat0', 'element']
|
|
131
|
-
],
|
|
132
|
-
value: [
|
|
133
|
-
[cp('{'), 'wsRepeat0', 'object', cp('}')],
|
|
134
|
-
[cp('['), 'wsRepeat0', 'array', cp(']')],
|
|
135
|
-
['string'],
|
|
136
|
-
['number'],
|
|
137
|
-
str('true'),
|
|
138
|
-
str('false'),
|
|
139
|
-
str('null'),
|
|
140
|
-
],
|
|
141
|
-
object: [
|
|
142
|
-
[],
|
|
143
|
-
['member', 'memberTailRepeat0'],
|
|
144
|
-
],
|
|
145
|
-
memberTail: [
|
|
146
|
-
[cp(','), 'wsRepeat0', 'member']
|
|
147
|
-
],
|
|
148
|
-
...repeat0('memberTail'),
|
|
149
|
-
member: [
|
|
150
|
-
['string', 'wsRepeat0', cp(':'), 'wsRepeat0', 'element'],
|
|
151
|
-
],
|
|
152
|
-
array: [
|
|
153
|
-
[],
|
|
154
|
-
['element', 'elements'],
|
|
155
|
-
],
|
|
156
|
-
elements: [
|
|
157
|
-
[],
|
|
158
|
-
[cp(','), 'wsRepeat0', 'element', 'elements'],
|
|
159
|
-
],
|
|
160
|
-
element: [
|
|
161
|
-
['value', 'wsRepeat0'],
|
|
162
|
-
],
|
|
163
|
-
string: [
|
|
164
|
-
[cp('"'), 'characterRepeat0', cp('"')],
|
|
165
|
-
],
|
|
166
|
-
...repeat0('character'),
|
|
167
|
-
character: [
|
|
168
|
-
...remove([0x20, 0x10FFFF], [cp('"'), cp('\\')]),
|
|
169
|
-
[cp('\\'), 'escape'], // 92
|
|
170
|
-
],
|
|
171
|
-
escape: [
|
|
172
|
-
...set('"\\/bfnrt'),
|
|
173
|
-
[cp('u'), 'hex', 'hex', 'hex', 'hex'],
|
|
174
|
-
],
|
|
175
|
-
hex: [
|
|
176
|
-
['digit'],
|
|
177
|
-
[range('AF')],
|
|
178
|
-
[range('af')],
|
|
179
|
-
],
|
|
180
|
-
numberSign: [
|
|
181
|
-
[],
|
|
182
|
-
[cp('-')]
|
|
183
|
-
],
|
|
184
|
-
number: [
|
|
185
|
-
['numberSign', 'integer', 'fraction', 'exponent'],
|
|
186
|
-
],
|
|
187
|
-
integer: [
|
|
188
|
-
[cp('0')],
|
|
189
|
-
['onenine', 'digitRepeat0'],
|
|
190
|
-
],
|
|
191
|
-
...repeat0('digit'),
|
|
192
|
-
digit: [
|
|
193
|
-
[cp('0')],
|
|
194
|
-
['onenine'],
|
|
195
|
-
],
|
|
196
|
-
onenine: [
|
|
197
|
-
[range('19')],
|
|
198
|
-
],
|
|
199
|
-
fraction: [
|
|
200
|
-
[],
|
|
201
|
-
[cp('.'), 'digit', 'digitRepeat0'],
|
|
202
|
-
],
|
|
203
|
-
e: set('Ee'),
|
|
204
|
-
exponent: [
|
|
205
|
-
[],
|
|
206
|
-
['e', 'sign', 'digit', 'digitRepeat0'],
|
|
207
|
-
],
|
|
208
|
-
sign: [
|
|
209
|
-
[],
|
|
210
|
-
[cp('+')],
|
|
211
|
-
[cp('-')],
|
|
212
|
-
],
|
|
213
|
-
ws: set(' \n\r\t'),
|
|
214
|
-
...repeat0('ws'),
|
|
215
|
-
};
|
|
216
|
-
const _map = map;
|
|
217
|
-
return _map;
|
|
218
|
-
};
|
|
219
|
-
export default {
|
|
220
|
-
example: {
|
|
221
|
-
module: () => {
|
|
222
|
-
// Define a simple grammar
|
|
223
|
-
const grammar = () => [
|
|
224
|
-
[range('AZ')], // 'A-Z'
|
|
225
|
-
[range('az'), grammar], // 'a-z' followed by more grammar
|
|
226
|
-
];
|
|
227
|
-
const ruleMap = toRuleMap(grammar);
|
|
228
|
-
const parse = parser(ruleMap);
|
|
229
|
-
// Parse an input
|
|
230
|
-
const input = toArray(stringToCodePointList('abcdefgA'));
|
|
231
|
-
const result = parse(grammar.name, input);
|
|
232
|
-
if (result === null) {
|
|
233
|
-
throw result;
|
|
234
|
-
}
|
|
235
|
-
const [, b] = result;
|
|
236
|
-
if (b?.length !== 0) {
|
|
237
|
-
throw b;
|
|
238
|
-
}
|
|
239
|
-
},
|
|
240
|
-
},
|
|
241
|
-
classic: () => {
|
|
242
|
-
const c = classic();
|
|
243
|
-
const json = stringify(toRuleMap(c.json));
|
|
244
|
-
const jsonE = stringify(classicTest());
|
|
245
|
-
if (json !== jsonE) {
|
|
246
|
-
//console.error(json)
|
|
247
|
-
//console.error(jsonE)
|
|
248
|
-
throw [json, jsonE];
|
|
249
|
-
}
|
|
250
|
-
},
|
|
251
|
-
map: () => {
|
|
252
|
-
const f = parser(deterministic());
|
|
253
|
-
// console.error(stringify(x))
|
|
254
|
-
//
|
|
255
|
-
const isSuccess = (s) => s?.length === 0;
|
|
256
|
-
const expect = (s, success) => {
|
|
257
|
-
const [a, r] = f('json', toArray(stringToCodePointList(s)));
|
|
258
|
-
if (isSuccess(r) !== success) {
|
|
259
|
-
throw r;
|
|
260
|
-
}
|
|
261
|
-
};
|
|
262
|
-
//
|
|
263
|
-
expect(' true ', true);
|
|
264
|
-
expect(' tr2ue ', false);
|
|
265
|
-
expect(' true" ', false);
|
|
266
|
-
expect(' "Hello" ', true);
|
|
267
|
-
expect(' "Hello ', false);
|
|
268
|
-
expect(' "Hello\\n\\r\\"" ', true);
|
|
269
|
-
expect(' -56.7e+5 ', true);
|
|
270
|
-
expect(' h-56.7e+5 ', false);
|
|
271
|
-
expect(' -56.7e+5 3', false);
|
|
272
|
-
expect(' [ 12, false, "a"] ', true);
|
|
273
|
-
expect(' [ 12, false2, "a"] ', false);
|
|
274
|
-
expect(' { "q": [ 12, false, [{}], "a"] } ', true);
|
|
275
|
-
expect(' { "q": [ 12, false, [}], "a"] } ', false);
|
|
276
|
-
}
|
|
277
|
-
};
|
package/bnf/func/module.f.d.ts
DELETED
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Types for defining language grammar using Backus-Naur Form (BNF).
|
|
3
|
-
*
|
|
4
|
-
* Utilities for serializing and deserializing BNF grammar
|
|
5
|
-
* and creating a simple LL(1) parser.
|
|
6
|
-
*
|
|
7
|
-
* See [Backus-Naur form](https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_form).
|
|
8
|
-
*
|
|
9
|
-
* @module
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
*
|
|
13
|
-
* ```ts
|
|
14
|
-
* import type { Rule } from './module.f.ts'
|
|
15
|
-
*
|
|
16
|
-
* // Matches 'A-Z', 'a-z', and '0-9'
|
|
17
|
-
* const grammar: Rule = () => [
|
|
18
|
-
* [[65, 90]],
|
|
19
|
-
* [[97, 122]],
|
|
20
|
-
* [[48, 57]],
|
|
21
|
-
* ]
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
24
|
-
import { type CodePoint } from '../../text/utf16/module.f.ts';
|
|
25
|
-
/**
|
|
26
|
-
* Represents a terminal range as a pair of Unicode code points.
|
|
27
|
-
* Typically used to define character ranges.
|
|
28
|
-
*
|
|
29
|
-
* @example
|
|
30
|
-
*
|
|
31
|
-
* ```ts
|
|
32
|
-
* const alpha: TerminalRange = [65, 90] // Matches 'A-Z'
|
|
33
|
-
* ```
|
|
34
|
-
*/
|
|
35
|
-
export type TerminalRange = readonly [CodePoint, CodePoint];
|
|
36
|
-
/**
|
|
37
|
-
* Represents a sequence of rules that must match in order.
|
|
38
|
-
*
|
|
39
|
-
* @example
|
|
40
|
-
*
|
|
41
|
-
* ```ts
|
|
42
|
-
* const alpha: TerminalRange = [65, 90] // Matches 'A-Z'
|
|
43
|
-
* const id2: Sequence = [alpha, alpha] // Matches two uppercase letters
|
|
44
|
-
* ```
|
|
45
|
-
*/
|
|
46
|
-
export type Sequence = readonly (TerminalRange | Rule)[];
|
|
47
|
-
/**
|
|
48
|
-
* Represents a logical "or" operation between multiple sequences.
|
|
49
|
-
* Allows defining alternatives within the grammar.
|
|
50
|
-
*
|
|
51
|
-
* @example
|
|
52
|
-
*
|
|
53
|
-
* ```ts
|
|
54
|
-
* const alpha: TerminalRange = [65, 90] // Matches 'A-Z'
|
|
55
|
-
* const id2: Sequence = [alpha, alpha] // Matches two uppercase letters
|
|
56
|
-
* const digit: TerminalRange = [48, 57] // Matches '0-9'
|
|
57
|
-
* // Matches two uppercase letters or one digit
|
|
58
|
-
* const id2OrDigit: Or = [
|
|
59
|
-
* id2,
|
|
60
|
-
* [digit],
|
|
61
|
-
* ]
|
|
62
|
-
* ```
|
|
63
|
-
*/
|
|
64
|
-
export type Or = readonly Sequence[];
|
|
65
|
-
/**
|
|
66
|
-
* Represents a lazy grammar rule for recursive definitions.
|
|
67
|
-
*
|
|
68
|
-
* @example
|
|
69
|
-
*
|
|
70
|
-
* ```ts
|
|
71
|
-
* const alpha: TerminalRange = [65, 90] // Matches 'A-Z'
|
|
72
|
-
* // zero or more alpha symbols
|
|
73
|
-
* const alpha0x: Rule = () => [
|
|
74
|
-
* [], // Empty
|
|
75
|
-
* [alpha, alpha0x] // Recursive
|
|
76
|
-
* ]
|
|
77
|
-
* const id: Sequence = [alpha, alpha0x]
|
|
78
|
-
* ```
|
|
79
|
-
*/
|
|
80
|
-
export type Rule = () => Or;
|
|
81
|
-
/**
|
|
82
|
-
* Converts a string to an array of terminal ranges where each character is a separate range.
|
|
83
|
-
*
|
|
84
|
-
* @param s - The input string.
|
|
85
|
-
* @returns An array of terminal ranges representing each character in the string.
|
|
86
|
-
*
|
|
87
|
-
* @example
|
|
88
|
-
*
|
|
89
|
-
* ```ts
|
|
90
|
-
* const ranges = str('abc') // [[97, 97], [98, 98], [99, 99]]
|
|
91
|
-
* ```
|
|
92
|
-
*/
|
|
93
|
-
export declare const str: (s: string) => readonly TerminalRange[];
|
|
94
|
-
/**
|
|
95
|
-
* Converts a single character string to a terminal range.
|
|
96
|
-
*
|
|
97
|
-
* @param a - The input character string.
|
|
98
|
-
* @returns A terminal range representing the character.
|
|
99
|
-
*
|
|
100
|
-
* @example
|
|
101
|
-
* ```ts
|
|
102
|
-
* const range = cp('A'); // [65, 65]
|
|
103
|
-
* ```
|
|
104
|
-
*/
|
|
105
|
-
export declare const cp: (a: string) => TerminalRange;
|
|
106
|
-
/**
|
|
107
|
-
* Converts a two-character string into a terminal range.
|
|
108
|
-
*
|
|
109
|
-
* @param ab - The input string of two characters.
|
|
110
|
-
* @returns A terminal range representing the two characters.
|
|
111
|
-
*
|
|
112
|
-
* @throws {number} Throws an error if the input string does not have exactly two code points.
|
|
113
|
-
*
|
|
114
|
-
* @example
|
|
115
|
-
* ```ts
|
|
116
|
-
* const result = range('AZ'); // [65, 90]
|
|
117
|
-
* ```
|
|
118
|
-
*/
|
|
119
|
-
export declare const range: (ab: string) => TerminalRange;
|
|
120
|
-
type RangeSet = readonly TerminalRange[];
|
|
121
|
-
/**
|
|
122
|
-
* A set of terminal ranges compatible with the `Or` rule.
|
|
123
|
-
*/
|
|
124
|
-
export type OrRangeSet = readonly (readonly [TerminalRange])[];
|
|
125
|
-
/**
|
|
126
|
-
* Convert a sequence of character into `OrRangeSet`
|
|
127
|
-
*
|
|
128
|
-
* @param s a set of code points
|
|
129
|
-
* @returns A set compatible with `Or`
|
|
130
|
-
*/
|
|
131
|
-
export declare const set: (s: string) => OrRangeSet;
|
|
132
|
-
/**
|
|
133
|
-
* Removes a terminal range from a set of ranges.
|
|
134
|
-
*
|
|
135
|
-
* @param range the original range.
|
|
136
|
-
* @param removeSet the set of ranges to be removed.
|
|
137
|
-
* @returns The resulting set of ranges after removal.
|
|
138
|
-
*
|
|
139
|
-
* @example
|
|
140
|
-
*
|
|
141
|
-
* ```ts
|
|
142
|
-
* const result = remove([65, 90], [cp('C'), cp('W')]) // [A..Z] w/o C and W
|
|
143
|
-
* ```
|
|
144
|
-
*/
|
|
145
|
-
export declare const remove: (range: TerminalRange, removeSet: RangeSet) => OrRangeSet;
|
|
146
|
-
export declare const repeat0: (rule: Rule) => Rule;
|
|
147
|
-
export declare const join0: (rule: Rule, separator: Rule) => Rule;
|
|
148
|
-
export {};
|
package/bnf/func/module.f.js
DELETED
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Types for defining language grammar using Backus-Naur Form (BNF).
|
|
3
|
-
*
|
|
4
|
-
* Utilities for serializing and deserializing BNF grammar
|
|
5
|
-
* and creating a simple LL(1) parser.
|
|
6
|
-
*
|
|
7
|
-
* See [Backus-Naur form](https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_form).
|
|
8
|
-
*
|
|
9
|
-
* @module
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
*
|
|
13
|
-
* ```ts
|
|
14
|
-
* import type { Rule } from './module.f.ts'
|
|
15
|
-
*
|
|
16
|
-
* // Matches 'A-Z', 'a-z', and '0-9'
|
|
17
|
-
* const grammar: Rule = () => [
|
|
18
|
-
* [[65, 90]],
|
|
19
|
-
* [[97, 122]],
|
|
20
|
-
* [[48, 57]],
|
|
21
|
-
* ]
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
24
|
-
import { stringToCodePointList } from "../../text/utf16/module.f.js";
|
|
25
|
-
import { map, toArray } from "../../types/list/module.f.js";
|
|
26
|
-
import { one } from "../../types/range/module.f.js";
|
|
27
|
-
const toTerminalRangeMap = map(one);
|
|
28
|
-
/**
|
|
29
|
-
* Converts a string to an array of terminal ranges where each character is a separate range.
|
|
30
|
-
*
|
|
31
|
-
* @param s - The input string.
|
|
32
|
-
* @returns An array of terminal ranges representing each character in the string.
|
|
33
|
-
*
|
|
34
|
-
* @example
|
|
35
|
-
*
|
|
36
|
-
* ```ts
|
|
37
|
-
* const ranges = str('abc') // [[97, 97], [98, 98], [99, 99]]
|
|
38
|
-
* ```
|
|
39
|
-
*/
|
|
40
|
-
export const str = (s) => toArray(toTerminalRangeMap(stringToCodePointList(s)));
|
|
41
|
-
/**
|
|
42
|
-
* Converts a single character string to a terminal range.
|
|
43
|
-
*
|
|
44
|
-
* @param a - The input character string.
|
|
45
|
-
* @returns A terminal range representing the character.
|
|
46
|
-
*
|
|
47
|
-
* @example
|
|
48
|
-
* ```ts
|
|
49
|
-
* const range = cp('A'); // [65, 65]
|
|
50
|
-
* ```
|
|
51
|
-
*/
|
|
52
|
-
export const cp = (a) => one(a.codePointAt(0));
|
|
53
|
-
/**
|
|
54
|
-
* Converts a two-character string into a terminal range.
|
|
55
|
-
*
|
|
56
|
-
* @param ab - The input string of two characters.
|
|
57
|
-
* @returns A terminal range representing the two characters.
|
|
58
|
-
*
|
|
59
|
-
* @throws {number} Throws an error if the input string does not have exactly two code points.
|
|
60
|
-
*
|
|
61
|
-
* @example
|
|
62
|
-
* ```ts
|
|
63
|
-
* const result = range('AZ'); // [65, 90]
|
|
64
|
-
* ```
|
|
65
|
-
*/
|
|
66
|
-
export const range = (ab) => {
|
|
67
|
-
const a = toArray(stringToCodePointList(ab));
|
|
68
|
-
if (a.length !== 2) {
|
|
69
|
-
throw a.length;
|
|
70
|
-
}
|
|
71
|
-
// deno-lint-ignore no-explicit-any
|
|
72
|
-
return a;
|
|
73
|
-
};
|
|
74
|
-
const toOr = (r) => r.map(v => [v]);
|
|
75
|
-
/**
|
|
76
|
-
* Convert a sequence of character into `OrRangeSet`
|
|
77
|
-
*
|
|
78
|
-
* @param s a set of code points
|
|
79
|
-
* @returns A set compatible with `Or`
|
|
80
|
-
*/
|
|
81
|
-
export const set = (s) => toOr(str(s));
|
|
82
|
-
const removeOne = (set, [a, b]) => {
|
|
83
|
-
let result = [];
|
|
84
|
-
for (const [a0, b0] of set) {
|
|
85
|
-
if (a0 < a) {
|
|
86
|
-
// [a0
|
|
87
|
-
// ]a
|
|
88
|
-
result = [...result, [a0, Math.min(b0, a - 1)]];
|
|
89
|
-
}
|
|
90
|
-
if (b < b0) {
|
|
91
|
-
// b0]
|
|
92
|
-
// b[
|
|
93
|
-
result = [...result, [Math.max(b + 1, a0), b0]];
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
return result;
|
|
97
|
-
};
|
|
98
|
-
/**
|
|
99
|
-
* Removes a terminal range from a set of ranges.
|
|
100
|
-
*
|
|
101
|
-
* @param range the original range.
|
|
102
|
-
* @param removeSet the set of ranges to be removed.
|
|
103
|
-
* @returns The resulting set of ranges after removal.
|
|
104
|
-
*
|
|
105
|
-
* @example
|
|
106
|
-
*
|
|
107
|
-
* ```ts
|
|
108
|
-
* const result = remove([65, 90], [cp('C'), cp('W')]) // [A..Z] w/o C and W
|
|
109
|
-
* ```
|
|
110
|
-
*/
|
|
111
|
-
export const remove = (range, removeSet) => {
|
|
112
|
-
let result = [range];
|
|
113
|
-
for (const r of removeSet) {
|
|
114
|
-
result = removeOne(result, r);
|
|
115
|
-
}
|
|
116
|
-
return toOr(result);
|
|
117
|
-
};
|
|
118
|
-
export const repeat0 = (rule) => {
|
|
119
|
-
const result = () => [
|
|
120
|
-
[],
|
|
121
|
-
[rule, result],
|
|
122
|
-
];
|
|
123
|
-
return result;
|
|
124
|
-
};
|
|
125
|
-
export const join0 = (rule, separator) => {
|
|
126
|
-
const tail = repeat0(() => [[separator, rule]]);
|
|
127
|
-
const result = () => [
|
|
128
|
-
[],
|
|
129
|
-
[rule, tail],
|
|
130
|
-
];
|
|
131
|
-
return result;
|
|
132
|
-
};
|
package/bnf/func/test.f.d.ts
DELETED