From 32f7b9c2ed4686da1bca57f4c806f18cd5496b5c Mon Sep 17 00:00:00 2001 From: sinclair Date: Sun, 17 Nov 2024 23:54:48 +0900 Subject: [PATCH] Example: Json --- example/json/index.ts | 2 + example/json/json.ts | 36 ++++++++++ example/json/runtime.ts | 150 ++++++++++++++++++++++++++++++++++++++++ example/json/static.ts | 16 ++--- 4 files changed, 193 insertions(+), 11 deletions(-) create mode 100644 example/json/json.ts create mode 100644 example/json/runtime.ts diff --git a/example/json/index.ts b/example/json/index.ts index 789d0d6..1f593f3 100644 --- a/example/json/index.ts +++ b/example/json/index.ts @@ -26,4 +26,6 @@ THE SOFTWARE. ---------------------------------------------------------------------------*/ +export * from './json' +export * from './runtime' export * from './static' \ No newline at end of file diff --git a/example/json/json.ts b/example/json/json.ts new file mode 100644 index 0000000..e954c56 --- /dev/null +++ b/example/json/json.ts @@ -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 */ +export function ParseJson(value: S): Static.Parse[0] { + return Module.Parse('Json', value)[0] +} \ No newline at end of file diff --git a/example/json/runtime.ts b/example/json/runtime.ts new file mode 100644 index 0000000..490ce16 --- /dev/null +++ b/example/json/runtime.ts @@ -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[]) { + return propertiesList.reduce((result, properties) => { + return {...result, ...properties } + }, {}) +} +function ObjectMapping(values: unknown[]) { + return ObjectReduce(values[1] as Record[]) +} +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 +}) diff --git a/example/json/static.ts b/example/json/static.ts index ad80291..2400903 100644 --- a/example/json/static.ts +++ b/example/json/static.ts @@ -28,13 +28,6 @@ THE SOFTWARE. import { Static } from '@sinclair/parsebox' -// Usage -type Result = Static.Parse[0] - // ----------------------------------------------------------------------- // Json // ----------------------------------------------------------------------- @@ -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 // -----------------------------------------------------------------------