functionalscript 0.3.12 → 0.3.14

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/bnf/module.f.d.ts DELETED
@@ -1,135 +0,0 @@
1
- /**
2
- * Types for defining language grammars using Backus-Naur Form (BNF).
3
- *
4
- * @module
5
- *
6
- * @description
7
- *
8
- * The primary utility of this module is to define grammars for text processing,
9
- * parsing, and lexing in a structured and reusable way.
10
- *
11
- * See [Backus-Naur form](https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_form).
12
- *
13
- * @example
14
- *
15
- * ```js
16
- * import { firstSet, type Rule, type Set } from './module.f.ts'
17
- *
18
- * const grammar: Rule = [
19
- * { or: [[65, 90], [97, 122], [48, 57]] }, // Matches 'A-Z', 'a-z', and '0-9'
20
- * ];
21
- *
22
- * const s = firstSet(grammar)
23
- * if (s.empty) { throw s }
24
- * if (setOp.get('0'.codePointAt(0))(s.map) !== true) { throw s }
25
- * if (setOp.get('h'.codePointAt(0))(s.map) !== true) { throw s }
26
- * if (setOp.get('$'.codePointAt(0))(s.map) !== false) { throw s }
27
- * ```
28
- */
29
- import { type CodePoint } from '../text/utf16/module.f.ts';
30
- import { type RangeMapOp, type RangeMapArray } from '../types/range_map/module.f.ts';
31
- /**
32
- * Represents a terminal range as a pair of Unicode code points.
33
- * Typically used to define character ranges.
34
- *
35
- * @example
36
- *
37
- * ```ts
38
- * const alpha: TerminalRange = [65, 90] // Matches 'A-Z'
39
- * ```
40
- */
41
- export type TerminalRange = readonly [CodePoint, CodePoint];
42
- /**
43
- * Represents a sequence of rules that must match in order.
44
- *
45
- * @example
46
- *
47
- * ```ts
48
- * const alpha: TerminalRange = [65, 90] // Matches 'A-Z'
49
- * const id2: Sequence = [alpha, alpha] // Matches two uppercase letters
50
- * ```
51
- */
52
- export type Sequence = readonly Rule[];
53
- /**
54
- * Represents a logical "or" operation between multiple sequences.
55
- * Allows defining alternatives within the grammar.
56
- *
57
- * @example
58
- *
59
- * ```ts
60
- * const alpha: TerminalRange = [65, 90] // Matches 'A-Z'
61
- * const id2: Sequence = [alpha, alpha] // Matches two uppercase letters
62
- * const digit: TerminalRange = [48, 57] // Matches '0-9'
63
- * // Matches two uppercase letters or one digit
64
- * const id2OrDigit: Or = { or: [
65
- * id2,
66
- * digit,
67
- * ] }
68
- * ```
69
- */
70
- export type Or = {
71
- readonly or: Sequence;
72
- };
73
- /**
74
- * Represents a grammar rule, which can be:
75
- * - A sequence of rules (`Sequence`)
76
- * - An "or" operation (`Or`)
77
- * - A terminal range (`TerminalRange`)
78
- * - A string (representing a literal sequence of characters)
79
- *
80
- * @remarks The rule can't have recursions.
81
- *
82
- * @example
83
- *
84
- * ```ts
85
- * // Matches 'true', 'false'
86
- * const bool: DataRule = { or: [
87
- * 'true',
88
- * 'false',
89
- * ] }
90
- */
91
- export type DataRule = Or | Sequence | TerminalRange | string;
92
- /**
93
- * Represents a lazy grammar rule for recursive definitions.
94
- *
95
- * @example
96
- *
97
- * ```ts
98
- * const alpha: TerminalRange = [65, 90] // Matches 'A-Z'
99
- * // Matches a string of uppercase letters
100
- * const id: LazyRule = () => [alpha, { or: [
101
- * [], // Empty
102
- * id // Recursive
103
- * ] }]
104
- * ```
105
- */
106
- export type LazyRule = () => DataRule;
107
- /**
108
- * Represents a rule in the grammar.
109
- */
110
- export type Rule = DataRule | LazyRule;
111
- export declare const toTerminalRangeSequence: (s: string) => readonly TerminalRange[];
112
- /**
113
- * A set that represents possible code points.
114
- */
115
- export type CpSet = {
116
- /**
117
- * Whether a grammar rule allows an empty sequence.
118
- */
119
- readonly empty: boolean;
120
- /**
121
- * The range map representing a set of possible code points.
122
- */
123
- readonly map: RangeMapArray<boolean>;
124
- };
125
- /**
126
- * Operations on code point sets.
127
- */
128
- export declare const setOp: RangeMapOp<boolean>;
129
- /**
130
- * Processes a `Rule` and converts it into a `Set` of possible code points at the start of the rule.
131
- *
132
- * @param rule - The grammar rule to process.
133
- * @returns A set representing the first possible code points in the grammar rule.
134
- */
135
- export declare const firstSet: (rule: Rule) => CpSet;
package/bnf/module.f.js DELETED
@@ -1,142 +0,0 @@
1
- /**
2
- * Types for defining language grammars using Backus-Naur Form (BNF).
3
- *
4
- * @module
5
- *
6
- * @description
7
- *
8
- * The primary utility of this module is to define grammars for text processing,
9
- * parsing, and lexing in a structured and reusable way.
10
- *
11
- * See [Backus-Naur form](https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_form).
12
- *
13
- * @example
14
- *
15
- * ```js
16
- * import { firstSet, type Rule, type Set } from './module.f.ts'
17
- *
18
- * const grammar: Rule = [
19
- * { or: [[65, 90], [97, 122], [48, 57]] }, // Matches 'A-Z', 'a-z', and '0-9'
20
- * ];
21
- *
22
- * const s = firstSet(grammar)
23
- * if (s.empty) { throw s }
24
- * if (setOp.get('0'.codePointAt(0))(s.map) !== true) { throw s }
25
- * if (setOp.get('h'.codePointAt(0))(s.map) !== true) { throw s }
26
- * if (setOp.get('$'.codePointAt(0))(s.map) !== false) { throw s }
27
- * ```
28
- */
29
- import { stringToCodePointList } from "../text/utf16/module.f.js";
30
- import { map, toArray } from "../types/list/module.f.js";
31
- import { rangeMap } from "../types/range_map/module.f.js";
32
- const toTerminalRangeMap = map((cp) => [cp, cp]);
33
- export const toTerminalRangeSequence = (s) => toArray(toTerminalRangeMap(stringToCodePointList(s)));
34
- /**
35
- * Operations on code point sets.
36
- */
37
- export const setOp = rangeMap({
38
- union: a => b => a || b,
39
- equal: a => b => a === b,
40
- def: false
41
- });
42
- const { merge, fromRange } = setOp;
43
- const rangeToSet = (r) => ({ empty: false, map: fromRange(r)(true) });
44
- const isTerminalRange = (rule) => typeof rule[0] === 'number';
45
- const passSet = { empty: true, map: [] };
46
- /**
47
- * Processes a `Rule` and converts it into a `Set` of possible code points at the start of the rule.
48
- *
49
- * @param rule - The grammar rule to process.
50
- * @returns A set representing the first possible code points in the grammar rule.
51
- */
52
- export const firstSet = (rule) => {
53
- if (typeof rule === 'function') {
54
- rule = rule();
55
- }
56
- if (typeof rule === 'string') {
57
- const first = toTerminalRangeSequence(rule).at(0);
58
- if (first === undefined) {
59
- return passSet;
60
- }
61
- return rangeToSet(first);
62
- }
63
- if (rule instanceof Array) {
64
- if (isTerminalRange(rule)) {
65
- return rangeToSet(rule);
66
- }
67
- let result = [];
68
- for (const r of rule) {
69
- const { empty, map } = firstSet(r);
70
- result = toArray(merge(result)(map));
71
- if (!empty) {
72
- return { empty: false, map: result };
73
- }
74
- }
75
- return { empty: true, map: result };
76
- }
77
- let empty = false;
78
- let map = [];
79
- for (const r of rule.or) {
80
- const { empty: rEmpty, map: rMap } = firstSet(r);
81
- map = toArray(merge(map)(rMap));
82
- empty ||= rEmpty;
83
- }
84
- return { empty, map };
85
- };
86
- // type MatchMap = {
87
- // empty: boolean
88
- // map: RangeMapArray<MatchMap>
89
- // }
90
- // const defMatchMap: MatchMap = { empty: false, map: [] }
91
- // const unionMap = (a: MatchMap) => (b: MatchMap) =>
92
- // ({ empty: a.empty || b.empty, map: toArray(matchMapOp.merge(a.map)(b.map)) })
93
- // /**
94
- // * Operations on code point map.
95
- // */
96
- // const matchMapOp: RangeMapOp<MatchMap> = rangeMap({
97
- // union: unionMap,
98
- // equal: a => b => a === b,
99
- // def: defMatchMap
100
- // })
101
- // const { merge: mergeMap, fromRange: fromRangeMap } = matchMapOp
102
- // const pass: MatchMap = { empty: true, map: [] }
103
- // const rangeToMatchMap = (r: TerminalRange, p: MatchMap): MatchMap => ({ empty: false, map: fromRangeMap(r)(p) })
104
- // /**
105
- // * Processes a `Rule` and converts it into a `Set` of possible code points at the start of the rule.
106
- // *
107
- // * @param rule - The grammar rule to process.
108
- // * @returns A set representing the first possible code points in the grammar rule.
109
- // */
110
- // export const matchMap = (rule: Rule): MatchMap => {
111
- // if (typeof rule === 'function') {
112
- // rule = rule()
113
- // }
114
- // if (typeof rule === 'string') {
115
- // const a = toTerminalRangeSequence(rule)
116
- // let result = pass
117
- // for (const r of a) {
118
- // result = rangeToMatchMap(r, result)
119
- // }
120
- // return result
121
- // }
122
- // if (rule instanceof Array) {
123
- // if (isTerminalRange(rule)) {
124
- // return rangeToMatchMap(rule, pass)
125
- // }
126
- // let result = pass
127
- // for (const r of rule.toReversed()) {
128
- // todo()
129
- // /*
130
- // const rMap = matchMap(r)
131
- // result = unionMap(result)(rMap)
132
- // */
133
- // }
134
- // return result
135
- // }
136
- // let result = pass
137
- // for (const r of rule.or) {
138
- // const rMap = matchMap(r)
139
- // result = unionMap(result)(rMap)
140
- // }
141
- // return result
142
- // }
package/bnf/test.f.d.ts DELETED
@@ -1,65 +0,0 @@
1
- declare const _default: {
2
- example: {
3
- module: () => void;
4
- types: () => void;
5
- };
6
- classic: () => {
7
- ws: () => void;
8
- sign: () => void;
9
- digits: () => void;
10
- exponent: () => void;
11
- fraction: () => void;
12
- onenine: () => void;
13
- digit: () => void;
14
- string: () => void;
15
- member: () => void;
16
- members: () => void;
17
- object: () => void;
18
- array: () => void;
19
- integer: () => void;
20
- number: () => void;
21
- value: () => void;
22
- element: () => void;
23
- elements: () => void;
24
- json: () => void;
25
- hex: () => void;
26
- escape: () => void;
27
- character: () => void;
28
- characters: () => void;
29
- };
30
- deterministic: () => {
31
- ws: () => void;
32
- sign: () => void;
33
- digits1: () => void;
34
- digits: () => void;
35
- exponent: () => void;
36
- fraction: () => void;
37
- onenine: () => void;
38
- digit: () => void;
39
- string: () => void;
40
- member1: () => void;
41
- member: () => void;
42
- members2: () => void;
43
- members1: () => void;
44
- members: () => void;
45
- object1: () => void;
46
- object: () => void;
47
- array1: () => void;
48
- array: () => void;
49
- integer1: () => void;
50
- integer: () => void;
51
- number: () => void;
52
- value: () => void;
53
- element1: () => void;
54
- element: () => void;
55
- elements2: () => void;
56
- elements1: () => void;
57
- elements: () => void;
58
- json: () => void;
59
- hex: () => void;
60
- escape: () => void;
61
- character: () => void;
62
- characters: () => void;
63
- };
64
- };
65
- export default _default;