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/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 [
|
|
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).
|
package/bnf/data/module.f.d.ts
CHANGED
|
@@ -11,27 +11,37 @@ export type Rule = Variant | Sequence | TerminalRange;
|
|
|
11
11
|
/** The full grammar */
|
|
12
12
|
export type RuleSet = Readonly<Record<string, Rule>>;
|
|
13
13
|
type EmptyTag = string | true | undefined;
|
|
14
|
+
type EmptyTagEntry = string | boolean;
|
|
14
15
|
type DispatchRule = {
|
|
15
16
|
readonly emptyTag: EmptyTag;
|
|
16
17
|
readonly rangeMap: Dispatch;
|
|
17
18
|
};
|
|
18
19
|
type Dispatch = RangeMapArray<DispatchResult>;
|
|
19
20
|
type DispatchResult = DispatchRuleCollection | null;
|
|
21
|
+
type DispatchRuleOrName = DispatchRule | string;
|
|
20
22
|
type DispatchRuleCollection = {
|
|
21
23
|
readonly tag: string | undefined;
|
|
22
|
-
readonly rules:
|
|
24
|
+
readonly rules: DispatchRuleOrName[];
|
|
23
25
|
};
|
|
24
26
|
type DispatchMap = {
|
|
25
27
|
readonly [id in string]: DispatchRule;
|
|
26
28
|
};
|
|
29
|
+
type EmptyTagMap = {
|
|
30
|
+
readonly [id in string]: EmptyTagEntry;
|
|
31
|
+
};
|
|
32
|
+
export type DescentMatchRule = (r: Rule, s: readonly CodePoint[], idx: number) => MatchResult;
|
|
27
33
|
/**
|
|
28
34
|
* Represents a parsed Abstract Syntax Tree (AST) sequence.
|
|
29
35
|
*/
|
|
30
36
|
export type AstSequence = readonly (AstRule | CodePoint)[];
|
|
37
|
+
type AstTag = string | true | undefined;
|
|
31
38
|
/**
|
|
32
39
|
* Represents a parsed AST rule, consisting of a rule name and its parsed sequence.
|
|
33
40
|
*/
|
|
34
|
-
|
|
41
|
+
type AstRule = {
|
|
42
|
+
readonly tag: AstTag;
|
|
43
|
+
readonly sequence: AstSequence;
|
|
44
|
+
};
|
|
35
45
|
/**
|
|
36
46
|
* Represents the remaining input after a match attempt, or `null` if no match is possible.
|
|
37
47
|
*/
|
|
@@ -39,15 +49,15 @@ export type Remainder = readonly CodePoint[] | null;
|
|
|
39
49
|
/**
|
|
40
50
|
* Represents the result of a match operation, including the parsed AST rule and the remainder of the input.
|
|
41
51
|
*/
|
|
42
|
-
export type MatchResult = readonly [AstRule, Remainder];
|
|
52
|
+
export type MatchResult = readonly [AstRule, boolean, Remainder];
|
|
43
53
|
/**
|
|
44
54
|
* Represents an LL(1) parser function for matching input against grammar rules.
|
|
45
55
|
*/
|
|
46
56
|
export type Match = (name: string, s: readonly CodePoint[]) => MatchResult;
|
|
57
|
+
export type MatchRule = (dr: DispatchRule, s: readonly CodePoint[]) => MatchResult;
|
|
47
58
|
export declare const toData: (fr: FRule) => readonly [RuleSet, string];
|
|
48
59
|
export declare const dispatchMap: (ruleSet: RuleSet) => DispatchMap;
|
|
60
|
+
export declare const createEmptyTagMap: (data: readonly [RuleSet, string]) => EmptyTagMap;
|
|
49
61
|
export declare const parser: (fr: FRule) => Match;
|
|
62
|
+
export declare const parserRuleSet: (ruleSet: RuleSet) => Match;
|
|
50
63
|
export {};
|
|
51
|
-
/**
|
|
52
|
-
* Either `{ variantItem: id }` or `id`.
|
|
53
|
-
*/
|
package/bnf/data/module.f.js
CHANGED
|
@@ -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,
|
|
94
|
+
const addRuleToDispatch = (dr, name) => {
|
|
95
95
|
if (dr === null)
|
|
96
96
|
return null;
|
|
97
|
-
return { tag: dr.tag, rules: [...dr.rules,
|
|
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
|
-
|
|
120
|
-
|
|
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
|
-
|
|
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,106 @@ 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
|
};
|
|
164
|
+
const emptyTagMapAdd = (ruleSet) => (map) => (name) => {
|
|
165
|
+
if (name in map) {
|
|
166
|
+
return [ruleSet, map, map[name]];
|
|
167
|
+
}
|
|
168
|
+
const rule = ruleSet[name];
|
|
169
|
+
if (typeof rule === 'number') {
|
|
170
|
+
return [ruleSet, { ...map, [name]: false }, false];
|
|
171
|
+
}
|
|
172
|
+
else if (rule instanceof Array) {
|
|
173
|
+
map = { ...map, [name]: true };
|
|
174
|
+
let emptyTag = rule.length == 0;
|
|
175
|
+
for (const item of rule) {
|
|
176
|
+
const [, newMap, itemEmptyTag] = emptyTagMapAdd(ruleSet)(map)(item);
|
|
177
|
+
map = newMap;
|
|
178
|
+
if (emptyTag === false) {
|
|
179
|
+
emptyTag = itemEmptyTag !== false;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return [ruleSet, { ...map, [name]: emptyTag }, emptyTag];
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
map = { ...map, [name]: true };
|
|
186
|
+
const entries = Object.entries(rule);
|
|
187
|
+
let emptyTag = false;
|
|
188
|
+
for (const [tag, item] of entries) {
|
|
189
|
+
const [, newMap, itemEmptyTag] = emptyTagMapAdd(ruleSet)(map)(item);
|
|
190
|
+
map = newMap;
|
|
191
|
+
if (itemEmptyTag !== false) {
|
|
192
|
+
emptyTag = tag;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return [ruleSet, { ...map, [name]: emptyTag }, emptyTag];
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
export const createEmptyTagMap = (data) => {
|
|
199
|
+
return emptyTagMapAdd(data[0])({})(data[1])[1];
|
|
200
|
+
};
|
|
201
|
+
// export const parserDescent = (fr: FRule): Match => {
|
|
202
|
+
// const data = toData(fr)
|
|
203
|
+
// const getEmptyTag = (rule: Rule): EmptyTag => {
|
|
204
|
+
// return todo()
|
|
205
|
+
// }
|
|
206
|
+
// const f: DescentMatchRule = (r, cp, idx): MatchResult => {
|
|
207
|
+
// const mrSuccess = (tag: AstTag, sequence: AstSequence, r: Remainder): MatchResult => [{tag, sequence}, true, r]
|
|
208
|
+
// const mrFail = (tag: AstTag, sequence: AstSequence, r: Remainder): MatchResult => [{tag, sequence}, false, r]
|
|
209
|
+
// if (idx >= cp.length) {
|
|
210
|
+
// const emptyTag = getEmptyTag(r)
|
|
211
|
+
// return mrSuccess(emptyTag, [], emptyTag === undefined ? null : cp)
|
|
212
|
+
// }
|
|
213
|
+
// return todo()
|
|
214
|
+
// }
|
|
215
|
+
// const match: Match = (name, cp): MatchResult => {
|
|
216
|
+
// return f(data[0][name], cp, 0)
|
|
217
|
+
// }
|
|
218
|
+
// return match
|
|
219
|
+
// }
|
|
157
220
|
export const parser = (fr) => {
|
|
158
221
|
const data = toData(fr);
|
|
159
|
-
return
|
|
222
|
+
return parserRuleSet(data[0]);
|
|
223
|
+
};
|
|
224
|
+
export const parserRuleSet = (ruleSet) => {
|
|
225
|
+
const map = dispatchMap(ruleSet);
|
|
226
|
+
const f = (rule, cp) => {
|
|
227
|
+
const mrSuccess = (tag, sequence, r) => [{ tag, sequence }, true, r];
|
|
228
|
+
const mrFail = (tag, sequence, r) => [{ tag, sequence }, false, r];
|
|
229
|
+
const { emptyTag, rangeMap } = rule;
|
|
230
|
+
if (cp.length === 0) {
|
|
231
|
+
return mrSuccess(emptyTag, [], emptyTag === undefined ? null : cp);
|
|
232
|
+
}
|
|
233
|
+
const cp0 = cp[0];
|
|
234
|
+
const dr = dispatchOp.get(cp0)(rangeMap);
|
|
235
|
+
if (dr === null) {
|
|
236
|
+
if (emptyTag === undefined) {
|
|
237
|
+
return mrFail(emptyTag, [], cp);
|
|
238
|
+
}
|
|
239
|
+
return mrSuccess(emptyTag, [], cp);
|
|
240
|
+
}
|
|
241
|
+
let seq = [cp0];
|
|
242
|
+
let r = cp;
|
|
243
|
+
const [_, ...restCp] = cp;
|
|
244
|
+
r = restCp;
|
|
245
|
+
const { tag, rules } = dr;
|
|
246
|
+
for (const i of rules) {
|
|
247
|
+
const rule = typeof i === 'string' ? map[i] : i;
|
|
248
|
+
const res = f(rule, r);
|
|
249
|
+
const [astRule, success, newR] = res;
|
|
250
|
+
if (success === false) {
|
|
251
|
+
return res;
|
|
252
|
+
}
|
|
253
|
+
seq = [...seq, astRule];
|
|
254
|
+
if (newR === null) {
|
|
255
|
+
return mrSuccess(tag, seq, null);
|
|
256
|
+
}
|
|
257
|
+
r = newR;
|
|
258
|
+
}
|
|
259
|
+
return mrSuccess(tag, seq, r);
|
|
260
|
+
};
|
|
261
|
+
return (name, cp) => f(map[name], cp);
|
|
160
262
|
};
|
|
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
|
-
*/
|
package/bnf/data/test.f.d.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
declare const _default: {
|
|
2
2
|
toData: (() => void)[];
|
|
3
|
+
emptyTags: (() => void)[];
|
|
3
4
|
variantTest: () => void;
|
|
4
5
|
dispatch: (() => void)[];
|
|
6
|
+
parser: (() => void)[];
|
|
7
|
+
repeat: (() => void)[];
|
|
8
|
+
repeatParser: (() => void)[];
|
|
5
9
|
example: () => void;
|
|
6
10
|
};
|
|
7
11
|
export default _default;
|