|
| 1 | +/*-------------------------------------------------------------------------- |
| 2 | +
|
| 3 | +ParseBox |
| 4 | +
|
| 5 | +The MIT License (MIT) |
| 6 | +
|
| 7 | +Copyright (c) 2024-2025 Haydn Paterson |
| 8 | +
|
| 9 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +of this software and associated documentation files (the "Software"), to deal |
| 11 | +in the Software without restriction, including without limitation the rights |
| 12 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +copies of the Software, and to permit persons to whom the Software is |
| 14 | +furnished to do so, subject to the following conditions: |
| 15 | +
|
| 16 | +The above copyright notice and this permission notice shall be included in |
| 17 | +all copies or substantial portions of the Software. |
| 18 | +
|
| 19 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | +THE SOFTWARE. |
| 26 | +
|
| 27 | +---------------------------------------------------------------------------*/ |
| 28 | + |
| 29 | +import { Runtime } from '@sinclair/parsebox' |
| 30 | + |
| 31 | +// ----------------------------------------------------------------------- |
| 32 | +// Json |
| 33 | +// ----------------------------------------------------------------------- |
| 34 | +const Json = Runtime.Union([ |
| 35 | + Runtime.Ref('Number'), |
| 36 | + Runtime.Ref('Boolean'), |
| 37 | + Runtime.Ref('String'), |
| 38 | + Runtime.Ref('Null'), |
| 39 | + Runtime.Ref('Object'), |
| 40 | + Runtime.Ref('Array'), |
| 41 | +]) |
| 42 | +// ----------------------------------------------------------------------- |
| 43 | +// Number |
| 44 | +// ----------------------------------------------------------------------- |
| 45 | +const Number = Runtime.Number() |
| 46 | +// ----------------------------------------------------------------------- |
| 47 | +// String |
| 48 | +// ----------------------------------------------------------------------- |
| 49 | +const String = Runtime.String(['"']) |
| 50 | +// ----------------------------------------------------------------------- |
| 51 | +// Boolean |
| 52 | +// ----------------------------------------------------------------------- |
| 53 | +const Boolean = Runtime.Union([ |
| 54 | + Runtime.Const('true'), |
| 55 | + Runtime.Const('false'), |
| 56 | +]) |
| 57 | + |
| 58 | +// ----------------------------------------------------------------------- |
| 59 | +// Null |
| 60 | +// ----------------------------------------------------------------------- |
| 61 | +const Null = Runtime.Const('null') |
| 62 | +// ----------------------------------------------------------------------- |
| 63 | +// Key |
| 64 | +// ----------------------------------------------------------------------- |
| 65 | +const Key = Runtime.Union([Runtime.String(['"'])]) |
| 66 | +// ----------------------------------------------------------------------- |
| 67 | +// Property |
| 68 | +// ----------------------------------------------------------------------- |
| 69 | +const Property = Runtime.Tuple([Runtime.Ref('Key'), Runtime.Const(':'), Runtime.Ref('Json')]) |
| 70 | +// ----------------------------------------------------------------------- |
| 71 | +// Properties |
| 72 | +// ----------------------------------------------------------------------- |
| 73 | +const Properties = Runtime.Union([ |
| 74 | + Runtime.Tuple([Runtime.Ref('Property'), Runtime.Const(','), Runtime.Ref('Properties')]), |
| 75 | + Runtime.Tuple([Runtime.Ref('Property')]), |
| 76 | + Runtime.Tuple([]), |
| 77 | +]) |
| 78 | +// ----------------------------------------------------------------------- |
| 79 | +// Object |
| 80 | +// ----------------------------------------------------------------------- |
| 81 | +const _Object = Runtime.Tuple([ |
| 82 | + Runtime.Const('{'), |
| 83 | + Runtime.Ref('Properties'), |
| 84 | + Runtime.Const('}'), |
| 85 | +]) |
| 86 | +// ----------------------------------------------------------------------- |
| 87 | +// Elemments |
| 88 | +// ----------------------------------------------------------------------- |
| 89 | +const Elements = Runtime.Union([ |
| 90 | + Runtime.Tuple([Runtime.Ref('Json'), Runtime.Const(','), Runtime.Ref('Elements')]), |
| 91 | + Runtime.Tuple([Runtime.Ref('Json')]), |
| 92 | + Runtime.Tuple([]), |
| 93 | +]) |
| 94 | +// ----------------------------------------------------------------------- |
| 95 | +// Array |
| 96 | +// ----------------------------------------------------------------------- |
| 97 | +const Array = Runtime.Tuple([Runtime.Const('['), Runtime.Ref('Elements'), Runtime.Const(']')]) |
| 98 | + |
| 99 | +// ----------------------------------------------------------------------- |
| 100 | +// Coverage |
| 101 | +// |
| 102 | +// These are only here to test there are no errors running these |
| 103 | +// combinators through the build process. |
| 104 | +// |
| 105 | +// ----------------------------------------------------------------------- |
| 106 | +const Coverage = Runtime.Union([ |
| 107 | + Runtime.BigInt(), |
| 108 | + Runtime.Array(Runtime.String(['"'])), |
| 109 | + Runtime.Ident(), |
| 110 | + Runtime.Integer(), |
| 111 | + Runtime.Until(['x']), |
| 112 | + Runtime.Until_1(['x']), |
| 113 | + Runtime.Union([]), |
| 114 | + Runtime.Optional(Runtime.Ident()), |
| 115 | +]) |
| 116 | +export const JsonModule = new Runtime.Module({ |
| 117 | + // Coverage |
| 118 | + Coverage, |
| 119 | + // Json Core |
| 120 | + Number, |
| 121 | + Boolean, |
| 122 | + String, |
| 123 | + Null, |
| 124 | + Key, |
| 125 | + Property, |
| 126 | + Properties, |
| 127 | + Object: _Object, |
| 128 | + Elements, |
| 129 | + Array, |
| 130 | + Json, |
| 131 | +}) |
0 commit comments