@@ -4,93 +4,93 @@ import { Static, Runtime, Build } from '@sinclair/parsebox'
44import { ParseJson } from './json/index.ts'
55import { ParseEbnf } from './ebnf/index.ts'
66
7- // ------------------------------------------------------------------
8- //
9- // Example: Ebnf | Interpreted
10- //
11- // ------------------------------------------------------------------
12- const Ebnf = ParseEbnf ( `
13-
14- Operand ::= Ident ;
15-
16- Factor ::= "(" Expr ")"
17- | Operand ;
18-
19- TermTail ::= ("*" Factor TermTail)
20- | ("/" Factor TermTail)
21- | e ;
22-
23- ExprTail ::= ("+" Term ExprTail)
24- | ("-" Term ExprTail)
25- | e ;
26-
27- Term ::= Factor TermTail ;
28-
29- Expr ::= Term ExprTail ;
30-
31- ` )
32-
33- const Result = Ebnf . Parse ( 'Expr' , `X * (Y + Z)` )
34-
35- console . dir ( Result , { depth : 100 } )
36-
37- // ------------------------------------------------------------------
38- //
39- // Example: Json | Interpreted
40- //
41- // ------------------------------------------------------------------
42- const Json = ParseJson ( `{
43- "x": 1,
44- "y": 2,
45- "z": 3
46- }` )
47-
48- console . log ( Json )
49-
50- // ------------------------------------------------------------------
51- //
52- // Example: Expression | Interpreted
53- //
54- // ------------------------------------------------------------------
55- {
56- type Result = Static . Parse < Expr , 'x * (y + z)' > // hover
57-
58- type BinaryReduce < Left extends unknown , Right extends unknown [ ] > = (
59- Right extends [ infer Operator , infer Right , infer Rest extends unknown [ ] ]
60- ? { left : Left ; operator : Operator ; right : BinaryReduce < Right , Rest > }
61- : Left
62- )
63- interface BinaryMapping extends Static . IMapping {
64- output : this[ 'input' ] extends [ infer Left , infer Right extends unknown [ ] ]
65- ? BinaryReduce < Left , Right >
66- : never
67- }
68- interface FactorMapping extends Static . IMapping {
69- output : (
70- this[ 'input' ] extends [ '(' , infer Expr , ')' ] ? Expr :
71- this[ 'input' ] extends [ infer Operand ] ? Operand :
72- never
73- )
74- }
75- type Operand = Static . Ident
76- type Factor = Static . Union < [
77- Static . Tuple < [ Static . Const < '(' > , Expr , Static . Const < ')' > ] > ,
78- Static . Tuple < [ Operand ] >
79- ] , FactorMapping >
80-
81- type TermTail = Static . Union < [
82- Static . Tuple < [ Static . Const < '*' > , Factor , TermTail ] > ,
83- Static . Tuple < [ Static . Const < '/' > , Factor , TermTail ] > ,
84- Static . Tuple < [ ] >
85- ] >
86- type ExprTail = Static . Union < [
87- Static . Tuple < [ Static . Const < '+' > , Term , ExprTail ] > ,
88- Static . Tuple < [ Static . Const < '-' > , Term , ExprTail ] > ,
89- Static . Tuple < [ ] >
90- ] >
91- type Term = Static . Tuple < [ Factor , TermTail ] , BinaryMapping >
92- type Expr = Static . Tuple < [ Term , ExprTail ] , BinaryMapping >
93- }
7+ // // ------------------------------------------------------------------
8+ // //
9+ // // Example: Ebnf | Interpreted
10+ // //
11+ // // ------------------------------------------------------------------
12+ // const Ebnf = ParseEbnf(`
13+
14+ // Operand ::= Ident ;
15+
16+ // Factor ::= "(" Expr ")"
17+ // | Operand ;
18+
19+ // TermTail ::= ("*" Factor TermTail)
20+ // | ("/" Factor TermTail)
21+ // | e ;
22+
23+ // ExprTail ::= ("+" Term ExprTail)
24+ // | ("-" Term ExprTail)
25+ // | e ;
26+
27+ // Term ::= Factor TermTail ;
28+
29+ // Expr ::= Term ExprTail ;
30+
31+ // `)
32+
33+ // const Result = Ebnf.Parse('Expr', `X * (Y + Z)`)
34+
35+ // console.dir(Result, { depth: 100 })
36+
37+ // // ------------------------------------------------------------------
38+ // //
39+ // // Example: Json | Interpreted
40+ // //
41+ // // ------------------------------------------------------------------
42+ // const Json = ParseJson(`{
43+ // "x": 1,
44+ // "y": 2,
45+ // "z": 3
46+ // }`)
47+
48+ // console.log(Json)
49+
50+ // // ------------------------------------------------------------------
51+ // //
52+ // // Example: Expression | Interpreted
53+ // //
54+ // // ------------------------------------------------------------------
55+ // {
56+ // type Result = Static.Parse<Expr, 'x * (y + z)'> // hover
57+
58+ // type BinaryReduce<Left extends unknown, Right extends unknown[]> = (
59+ // Right extends [infer Operator, infer Right, infer Rest extends unknown[]]
60+ // ? { left: Left; operator: Operator; right: BinaryReduce<Right, Rest> }
61+ // : Left
62+ // )
63+ // interface BinaryMapping extends Static.IMapping {
64+ // output: this['input'] extends [infer Left, infer Right extends unknown[]]
65+ // ? BinaryReduce<Left, Right>
66+ // : never
67+ // }
68+ // interface FactorMapping extends Static.IMapping {
69+ // output: (
70+ // this['input'] extends ['(', infer Expr, ')'] ? Expr :
71+ // this['input'] extends [infer Operand] ? Operand :
72+ // never
73+ // )
74+ // }
75+ // type Operand = Static.Ident
76+ // type Factor = Static.Union<[
77+ // Static.Tuple<[Static.Const<'('>, Expr, Static.Const<')'>]>,
78+ // Static.Tuple<[Operand]>
79+ // ], FactorMapping>
80+
81+ // type TermTail = Static.Union<[
82+ // Static.Tuple<[Static.Const<'*'>, Factor, TermTail]>,
83+ // Static.Tuple<[Static.Const<'/'>, Factor, TermTail]>,
84+ // Static.Tuple<[]>
85+ // ]>
86+ // type ExprTail = Static.Union<[
87+ // Static.Tuple<[Static.Const<'+'>, Term, ExprTail]>,
88+ // Static.Tuple<[Static.Const<'-'>, Term, ExprTail]>,
89+ // Static.Tuple<[]>
90+ // ]>
91+ // type Term = Static.Tuple<[Factor, TermTail], BinaryMapping>
92+ // type Expr = Static.Tuple<[Term, ExprTail], BinaryMapping>
93+ // }
9494// ------------------------------------------------------------------
9595//
9696// Example: Compiled Parser
@@ -116,9 +116,7 @@ console.log(Json)
116116 ] )
117117 } )
118118 const project = Build . Project ( ListModule , {
119- mappingPath : './mapping' ,
120- mappingImports : [ ] ,
121- parserImports : [ ]
119+ mappingPath : './mapping.ts' ,
122120 } )
123121 console . log ( project . parser ) // parser file content
124122 console . log ( project . mapping ) // semantic mapping file content
0 commit comments