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
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).
@@ -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: DispatchRule[];
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
- export type AstRule = readonly [string, AstSequence];
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
- */
@@ -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,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 todo();
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
- */
@@ -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;