-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1f623d
commit 4ff50dc
Showing
10 changed files
with
491 additions
and
254 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,25 @@ | ||
import { Static } from '@sinclair/parsebox' | ||
import { Static, Runtime } from '@sinclair/parsebox' | ||
import { Parse } from './typebox' | ||
|
||
// ------------------------------------------------------------------ | ||
// Example: TypeBox | ||
// ------------------------------------------------------------------ | ||
// "[" [ element { "," element } [ "," ] ] "]" | ||
|
||
const T = Parse(`{ | ||
x: number, | ||
y: number, | ||
z: number | ||
}`) | ||
const Element = Runtime.Number() | ||
|
||
console.log(T) | ||
|
||
// ------------------------------------------------------------------ | ||
// Example: Expression Parser | ||
// ------------------------------------------------------------------ | ||
|
||
type Result = Static.Parse<Expr, 'x * (y + z)'> // hover | ||
|
||
// prettier-ignore | ||
type BinaryReduce<Left extends unknown, Right extends unknown[]> = ( | ||
Right extends [infer Operator, infer Right, infer Rest extends unknown[]] | ||
? { left: Left; operator: Operator; right: BinaryReduce<Right, Rest> } | ||
: Left | ||
) | ||
|
||
// prettier-ignore | ||
interface BinaryMapping extends Static.IMapping { | ||
output: this['input'] extends [infer Left, infer Right extends unknown[]] | ||
? BinaryReduce<Left, Right> | ||
: never | ||
const ElementsMapping = (_0: unknown, _1: [',', unknown][], _2: [','] | []) => { | ||
return [_0, ..._1.map((value) => value[1])] as const | ||
} | ||
|
||
// prettier-ignore | ||
interface FactorMapping extends Static.IMapping { | ||
output: ( | ||
this['input'] extends ['(', infer Expr, ')'] ? Expr : | ||
this['input'] extends [infer Operand] ? Operand : | ||
never | ||
) | ||
} | ||
|
||
type Operand = Static.Ident | ||
// elements ::= element { "," element } [ "," ] | ||
|
||
// prettier-ignore | ||
type Factor = Static.Union<[ | ||
Static.Tuple<[Static.Const<'('>, Expr, Static.Const<')'>]>, | ||
Static.Tuple<[Operand]> | ||
], FactorMapping> | ||
const Elements = Runtime.Tuple([Element, Runtime.Array(Runtime.Tuple([Runtime.Const(','), Element])), Runtime.Optional(Runtime.Const(','))], (values) => ElementsMapping(...values)) | ||
|
||
// prettier-ignore | ||
type TermTail = Static.Union<[ | ||
Static.Tuple<[Static.Const<'*'>, Factor, TermTail]>, | ||
Static.Tuple<[Static.Const<'/'>, Factor, TermTail]>, | ||
Static.Tuple<[]> | ||
]> | ||
// array ::= "[" [ elements ] "]" | ||
|
||
// prettier-ignore | ||
type ExprTail = Static.Union<[ | ||
Static.Tuple<[Static.Const<'+'>, Term, ExprTail]>, | ||
Static.Tuple<[Static.Const<'-'>, Term, ExprTail]>, | ||
Static.Tuple<[]> | ||
]> | ||
const ArrayMapping = (_0: '[', _1: unknown[], _2: ']') => { | ||
return _1 | ||
} | ||
const Array = Runtime.Tuple([Runtime.Const('['), Runtime.Optional(Elements), Runtime.Const(']')], (values) => ArrayMapping(...values)) | ||
|
||
// prettier-ignore | ||
type Term = Static.Tuple<[Factor, TermTail], BinaryMapping> | ||
const R = Runtime.Parse(Array, '[1, 2, 3]') | ||
|
||
// prettier-ignore | ||
type Expr = Static.Tuple<[Term, ExprTail], BinaryMapping> | ||
console.dir(R, { depth: 100 }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.