Skip to content

Commit da560ac

Browse files
committed
updates
1 parent 533f2a8 commit da560ac

File tree

3 files changed

+94
-95
lines changed

3 files changed

+94
-95
lines changed

example/index.ts

Lines changed: 88 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -4,93 +4,93 @@ import { Static, Runtime, Build } from '@sinclair/parsebox'
44
import { ParseJson } from './json/index.ts'
55
import { 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

src/build/options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export interface Options {
3939
/** Default Compiler Options */
4040
export function DefaultOptions(): Options {
4141
return {
42-
mappingPath: './mapping',
42+
mappingPath: './mapping.ts',
4343
mappingImports: [],
4444
parserImports: [],
4545
}

src/build/project.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,20 @@ export interface ProjectResult {
8585
parser: string
8686
mapping: string
8787
}
88-
export function Project(module: Runtime.Module, options: Options = DefaultOptions()): ProjectResult {
89-
const types = Types(module, options)
88+
export function Project(module: Runtime.Module, options: Partial<Options> = {}): ProjectResult {
89+
const options_ = { ...DefaultOptions(), ...options }
90+
const types = Types(module, options_)
9091
// ----------------------------------------------------------------
9192
// File: Parser
9293
// ----------------------------------------------------------------
93-
const parserHeader = [types.header.imports, ...options.parserImports, types.header.importSemantics].join('\n')
94+
const parserHeader = [types.header.imports, ...options_.parserImports, types.header.importSemantics].join('\n')
9495
const parserTypes = types.types.reduce((result, type) => [result, type.type].join('\n'), '')
9596
const parserFuncs = types.types.reduce((result, type) => [result, type.func].join('\n'), '')
9697
const parser = [parserHeader, parserTypes, types.header.ifExpression, parserFuncs].join('\n')
9798
// ----------------------------------------------------------------
9899
// File: Mapping
99100
// ----------------------------------------------------------------
100-
const semanticsHeader = [...options.mappingImports].join('\n')
101+
const semanticsHeader = [...options_.mappingImports].join('\n')
101102
const semanticsBody = types.types.reduce((result, type) => [result, type.comment, type.typeMap, type.funcMap].join('\n'), '')
102103
const semantics = [semanticsHeader, semanticsBody].join('\n')
103104
return { parser, mapping: semantics }

0 commit comments

Comments
 (0)