Skip to content

Commit

Permalink
Example: Json
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Nov 17, 2024
1 parent 9c8ecb2 commit 10d6a79
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 11 deletions.
2 changes: 2 additions & 0 deletions example/json/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

export * from './json'
export * from './runtime'
export * from './static'
36 changes: 36 additions & 0 deletions example/json/json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*--------------------------------------------------------------------------
@sinclair/parsebox
The MIT License (MIT)
Copyright (c) 2024 Haydn Paterson (sinclair) (haydn.developer@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
---------------------------------------------------------------------------*/

import { Static } from '@sinclair/parsebox'
import { Module } from './runtime'
import { Json } from './static'

/** Parses a Json string (orders of magnitude slower than JSON.parse()) */
export function ParseJson<S extends string>(value: S): Static.Parse<Json, S>[0] {
return Module.Parse('Json', value)[0]
}
150 changes: 150 additions & 0 deletions example/json/runtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*--------------------------------------------------------------------------
@sinclair/parsebox
The MIT License (MIT)
Copyright (c) 2024 Haydn Paterson (sinclair) (haydn.developer@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
---------------------------------------------------------------------------*/

import { Runtime } from '@sinclair/parsebox'

// -----------------------------------------------------------------------
// Json
// -----------------------------------------------------------------------
const Json = Runtime.Union([
Runtime.Ref('Number'),
Runtime.Ref('Boolean'),
Runtime.Ref('String'),
Runtime.Ref('Null'),
Runtime.Ref('Object'),
Runtime.Ref('Array')
])
// -----------------------------------------------------------------------
// Number
// -----------------------------------------------------------------------
function NumberMapping(value: string) {
return parseFloat(value)
}
const Number = Runtime.Number(NumberMapping)
// -----------------------------------------------------------------------
// String
// -----------------------------------------------------------------------
function StringMapping(value: string) {
return value
}
const String = Runtime.String(['"'], StringMapping)
// -----------------------------------------------------------------------
// Boolean
// -----------------------------------------------------------------------
function BooleanMapping(value: string) {
return value === 'true'
}
const Boolean = Runtime.Union([
Runtime.Const('true'),
Runtime.Const('false'),
], BooleanMapping)

// -----------------------------------------------------------------------
// Null
// -----------------------------------------------------------------------
function NullMapping(_value: string) {
return null
}
const Null = Runtime.Const('null', NullMapping)
// -----------------------------------------------------------------------
// Property
// -----------------------------------------------------------------------
const Key = Runtime.Union([Runtime.String(['"'])])

function PropertyMapping(values: unknown[]) {
return { [values[0] as string]: values[2] }
}
const Property = Runtime.Tuple([Runtime.Ref('Key'), Runtime.Const(':'), Runtime.Ref('Json')], PropertyMapping)

// -----------------------------------------------------------------------
// Properties
// -----------------------------------------------------------------------
function PropertiesMapping(values: unknown[]) {
return (
values.length === 3 ? [values[0], ...values[2] as unknown[]] :
values.length === 1 ? [values[0]] :
[]
)
}
const Properties = Runtime.Union([
Runtime.Tuple([Runtime.Ref('Property'), Runtime.Const(','), Runtime.Ref('Properties')]),
Runtime.Tuple([Runtime.Ref('Property')]),
Runtime.Tuple([])
], PropertiesMapping)
// -----------------------------------------------------------------------
// Object
// -----------------------------------------------------------------------
function ObjectReduce(propertiesList: Record<PropertyKey, unknown>[]) {
return propertiesList.reduce((result, properties) => {
return {...result, ...properties }
}, {})
}
function ObjectMapping(values: unknown[]) {
return ObjectReduce(values[1] as Record<PropertyKey, unknown>[])
}
const _Object = Runtime.Tuple([
Runtime.Const('{'),
Runtime.Ref('Properties'),
Runtime.Const('}')
], ObjectMapping)
// -----------------------------------------------------------------------
// Elemments
// -----------------------------------------------------------------------
function ElementsMapping(values: unknown[]) {
return (
values.length === 3 ? [values[0], ...values[2] as unknown[]] :
values.length === 1 ? [values[0]] :
[]
)
}
const Elements = Runtime.Union([
Runtime.Tuple([Runtime.Ref('Json'), Runtime.Const(','), Runtime.Ref('Elements')]),
Runtime.Tuple([Runtime.Ref('Json')]),
Runtime.Tuple([])
], ElementsMapping)
// -----------------------------------------------------------------------
// Array
// -----------------------------------------------------------------------
function ArrayMapping(values: unknown[]) {
return values[1]
}
const Array = Runtime.Tuple([Runtime.Const('['), Elements, Runtime.Const(']')], ArrayMapping)

export const Module = new Runtime.Module({
Number,
Boolean,
String,
Null,
Key,
Property,
Properties,
Object: _Object,
Elements,
Array,
Json
})
16 changes: 5 additions & 11 deletions example/json/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@ THE SOFTWARE.

import { Static } from '@sinclair/parsebox'

// Usage
type Result = Static.Parse<Json, `{
"x": 1,
"y": 2,
"z": 3
}`>[0]

// -----------------------------------------------------------------------
// Json
// -----------------------------------------------------------------------
Expand Down Expand Up @@ -79,17 +72,18 @@ interface NullMapping extends Static.IMapping {
}
type Null = Static.Const<'null', NullMapping>
// -----------------------------------------------------------------------
// Key
// -----------------------------------------------------------------------
type Key = Static.Union<[Static.String<['"']>]>
// -----------------------------------------------------------------------
// Property
// -----------------------------------------------------------------------
type Key = Static.Union<[ /* Static.Ident, */ Static.String<['"']>]>

type Property = Static.Tuple<[Key, Static.Const<':'>, Json], PropertyMapping>

interface PropertyMapping extends Static.IMapping {
output: this['input'] extends [infer Key extends string, ':', infer Value extends unknown]
? { [_ in Key]: Value }
: never
}
type Property = Static.Tuple<[Key, Static.Const<':'>, Json], PropertyMapping>
// -----------------------------------------------------------------------
// Properties
// -----------------------------------------------------------------------
Expand Down

0 comments on commit 10d6a79

Please sign in to comment.