@thomasfosterau/effect-parse 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (144) hide show
  1. package/README.md +44 -1
  2. package/dist/Ascii.d.ts +0 -2
  3. package/dist/Ascii.d.ts.map +1 -1
  4. package/dist/Ascii.js +6 -6
  5. package/dist/Ascii.js.map +1 -1
  6. package/dist/Binary.d.ts +0 -2
  7. package/dist/Binary.d.ts.map +1 -1
  8. package/dist/Binary.js +1 -2
  9. package/dist/Bits.d.ts +0 -2
  10. package/dist/Bits.d.ts.map +1 -1
  11. package/dist/Bits.js +2 -3
  12. package/dist/Bits.js.map +1 -1
  13. package/dist/Compiler.d.ts +0 -2
  14. package/dist/Compiler.d.ts.map +1 -1
  15. package/dist/Compiler.js +1 -2
  16. package/dist/Encoding.d.ts +0 -2
  17. package/dist/Encoding.d.ts.map +1 -1
  18. package/dist/Encoding.js +1 -2
  19. package/dist/Expression.d.ts +0 -2
  20. package/dist/Expression.d.ts.map +1 -1
  21. package/dist/Expression.js +61 -18
  22. package/dist/Expression.js.map +1 -1
  23. package/dist/Input.d.ts +80 -7
  24. package/dist/Input.d.ts.map +1 -1
  25. package/dist/Input.js +149 -18
  26. package/dist/Input.js.map +1 -1
  27. package/dist/Nom.d.ts +1 -3
  28. package/dist/Nom.d.ts.map +1 -1
  29. package/dist/Nom.js +1 -2
  30. package/dist/ParseError.d.ts +0 -2
  31. package/dist/ParseError.d.ts.map +1 -1
  32. package/dist/ParseError.js +6 -10
  33. package/dist/ParseError.js.map +1 -1
  34. package/dist/Parser.d.ts +2 -2
  35. package/dist/Parser.d.ts.map +1 -1
  36. package/dist/Parser.js +58 -32
  37. package/dist/Parser.js.map +1 -1
  38. package/dist/ParserSchema.d.ts +198 -2
  39. package/dist/ParserSchema.d.ts.map +1 -1
  40. package/dist/ParserSchema.js +646 -18
  41. package/dist/ParserSchema.js.map +1 -1
  42. package/dist/ParserStream.d.ts +0 -2
  43. package/dist/ParserStream.d.ts.map +1 -1
  44. package/dist/ParserStream.js +15 -9
  45. package/dist/ParserStream.js.map +1 -1
  46. package/dist/Syntax.d.ts +0 -2
  47. package/dist/Syntax.d.ts.map +1 -1
  48. package/dist/Syntax.js +13 -11
  49. package/dist/Syntax.js.map +1 -1
  50. package/dist/Text.d.ts +0 -2
  51. package/dist/Text.d.ts.map +1 -1
  52. package/dist/Text.js +1 -2
  53. package/dist/Token.d.ts +0 -2
  54. package/dist/Token.d.ts.map +1 -1
  55. package/dist/Token.js +1 -2
  56. package/dist/internal/compiler/closure.d.ts +5 -2
  57. package/dist/internal/compiler/closure.d.ts.map +1 -1
  58. package/dist/internal/compiler/closure.js +160 -92
  59. package/dist/internal/compiler/closure.js.map +1 -1
  60. package/dist/internal/compiler/entries.d.ts +0 -2
  61. package/dist/internal/compiler/entries.d.ts.map +1 -1
  62. package/dist/internal/compiler/entries.js +6 -7
  63. package/dist/internal/compiler/entries.js.map +1 -1
  64. package/dist/internal/compiler/first.d.ts +0 -2
  65. package/dist/internal/compiler/first.d.ts.map +1 -1
  66. package/dist/internal/compiler/first.js +10 -10
  67. package/dist/internal/compiler/first.js.map +1 -1
  68. package/dist/internal/compiler/jit.d.ts +0 -2
  69. package/dist/internal/compiler/jit.d.ts.map +1 -1
  70. package/dist/internal/compiler/jit.js +52 -51
  71. package/dist/internal/compiler/jit.js.map +1 -1
  72. package/dist/internal/compiler/registry.d.ts +0 -2
  73. package/dist/internal/compiler/registry.d.ts.map +1 -1
  74. package/dist/internal/compiler/registry.js +1 -2
  75. package/dist/internal/compiler/sets.d.ts +2 -2
  76. package/dist/internal/compiler/sets.d.ts.map +1 -1
  77. package/dist/internal/compiler/sets.js +8 -3
  78. package/dist/internal/compiler/sets.js.map +1 -1
  79. package/dist/internal/entries.d.ts +0 -2
  80. package/dist/internal/entries.d.ts.map +1 -1
  81. package/dist/internal/entries.js +1 -2
  82. package/dist/internal/key.d.ts +10 -0
  83. package/dist/internal/key.d.ts.map +1 -0
  84. package/dist/internal/key.js +11 -0
  85. package/dist/internal/key.js.map +1 -0
  86. package/dist/internal/node.d.ts +11 -4
  87. package/dist/internal/node.d.ts.map +1 -1
  88. package/dist/internal/node.js +15 -3
  89. package/dist/internal/node.js.map +1 -1
  90. package/dist/internal/numbers.d.ts +0 -2
  91. package/dist/internal/numbers.d.ts.map +1 -1
  92. package/dist/internal/numbers.js +1 -2
  93. package/dist/internal/resume.d.ts +5 -6
  94. package/dist/internal/resume.d.ts.map +1 -1
  95. package/dist/internal/resume.js +1 -2
  96. package/dist/internal/resume.js.map +1 -1
  97. package/dist/internal/run.d.ts +0 -2
  98. package/dist/internal/run.d.ts.map +1 -1
  99. package/dist/internal/run.js +2 -3
  100. package/dist/internal/run.js.map +1 -1
  101. package/dist/internal/stack.d.ts +0 -2
  102. package/dist/internal/stack.d.ts.map +1 -1
  103. package/dist/internal/stack.js +12 -3
  104. package/dist/internal/stack.js.map +1 -1
  105. package/dist/internal/syntax.d.ts +30 -19
  106. package/dist/internal/syntax.d.ts.map +1 -1
  107. package/dist/internal/syntax.js +74 -63
  108. package/dist/internal/syntax.js.map +1 -1
  109. package/dist/internal/text.d.ts +5 -2
  110. package/dist/internal/text.d.ts.map +1 -1
  111. package/dist/internal/text.js +14 -5
  112. package/dist/internal/text.js.map +1 -1
  113. package/dist/internal/ties.d.ts +28 -0
  114. package/dist/internal/ties.d.ts.map +1 -0
  115. package/dist/internal/ties.js +66 -0
  116. package/dist/internal/ties.js.map +1 -0
  117. package/dist/internal/tokens.d.ts +10 -9
  118. package/dist/internal/tokens.d.ts.map +1 -1
  119. package/dist/internal/tokens.js +4 -8
  120. package/dist/internal/tokens.js.map +1 -1
  121. package/package.json +6 -6
  122. package/src/Ascii.ts +6 -5
  123. package/src/Bits.ts +1 -1
  124. package/src/Expression.ts +27 -23
  125. package/src/Input.ts +157 -21
  126. package/src/ParseError.ts +10 -8
  127. package/src/Parser.ts +67 -32
  128. package/src/ParserSchema.ts +982 -23
  129. package/src/ParserStream.ts +20 -8
  130. package/src/Syntax.ts +12 -9
  131. package/src/internal/compiler/closure.ts +202 -90
  132. package/src/internal/compiler/entries.ts +14 -11
  133. package/src/internal/compiler/first.ts +9 -8
  134. package/src/internal/compiler/jit.ts +95 -74
  135. package/src/internal/compiler/sets.ts +7 -0
  136. package/src/internal/key.ts +11 -0
  137. package/src/internal/node.ts +19 -2
  138. package/src/internal/resume.ts +5 -4
  139. package/src/internal/run.ts +1 -1
  140. package/src/internal/stack.ts +6 -1
  141. package/src/internal/syntax.ts +80 -84
  142. package/src/internal/text.ts +24 -4
  143. package/src/internal/ties.ts +83 -0
  144. package/src/internal/tokens.ts +20 -13
@@ -1,6 +1,27 @@
1
+ import * as Result from "effect/Result";
1
2
  import { Parser } from "./Parser.js";
2
3
  import * as Schema from "effect/Schema";
3
- //#region src/ParserSchema.d.ts
4
+ /**
5
+ * The key of the annotation that says how a schema is written as text
6
+ * ({@link Annotation}), namespaced to this package so it cannot collide with
7
+ * another library's. {@link annotate} sets it.
8
+ *
9
+ * @since 0.3.0
10
+ * @category annotations
11
+ */
12
+ export declare const annotationKey: "@thomasfosterau/effect-parse/ParserSchema";
13
+ declare module "effect/Schema" {
14
+ namespace Annotations {
15
+ interface Annotations {
16
+ /**
17
+ * How the schema is written as text, read by `ParserSchema.toParser`
18
+ * and `ParserSchema.toPrinter`: a parser for the schema's type, or a
19
+ * `ParserSchema.Grammar`.
20
+ */
21
+ readonly [annotationKey]?: Annotation<any> | undefined;
22
+ }
23
+ }
24
+ }
4
25
  /**
5
26
  * Options for {@link make}.
6
27
  *
@@ -28,5 +49,180 @@ export interface ParserSchema<A> extends Schema.decodeTo<Schema.declare<A>, Sche
28
49
  * @category constructors
29
50
  */
30
51
  export declare const make: <A, E>(parser: Parser<A, E, never, string>, options?: Options<A>) => ParserSchema<A>;
31
- //#endregion
52
+ /**
53
+ * How a schema is written as text, given in its annotation (see
54
+ * {@link annotate}) and read by {@link toParser} and {@link toPrinter}.
55
+ * Every field is optional; a schema without one is written as described on
56
+ * {@link toParser}.
57
+ *
58
+ * @since 0.3.0
59
+ * @category models
60
+ */
61
+ export interface Grammar<A = unknown> {
62
+ /**
63
+ * Parses the schema's type outright, replacing what {@link toParser} would
64
+ * derive. Its output is still validated against the schema's type and
65
+ * checks, and its failures' causes become `SchemaError`s with their message.
66
+ * Give a {@link Grammar.print} with it, or the schema cannot be printed.
67
+ */
68
+ readonly parser?: Parser<A, unknown, never, string> | undefined;
69
+ /** Writes a value as the text {@link Grammar.parser} reads, replacing what {@link toPrinter} would derive. */
70
+ readonly print?: ((value: A) => string) | undefined;
71
+ /** Text written before the value. */
72
+ readonly prefix?: string | undefined;
73
+ /** Text written after the value. */
74
+ readonly suffix?: string | undefined;
75
+ /** Text written between the fields of a struct, the elements of a tuple or array, or the entries of a record. */
76
+ readonly separator?: string | undefined;
77
+ /**
78
+ * Text written between a key and its value. On a struct, it writes each
79
+ * field as `key`, separator, value, in any order; records always need one.
80
+ */
81
+ readonly keySeparator?: string | undefined;
82
+ /**
83
+ * Whether whitespace may surround the text this schema writes (its prefix,
84
+ * suffix and separators), and whitespace around unannotated strings is
85
+ * trimmed. Inherited by the schemas inside it. Printers write no
86
+ * whitespace.
87
+ */
88
+ readonly whitespace?: boolean | undefined;
89
+ }
90
+ /**
91
+ * The value of the annotation under {@link annotationKey}: a
92
+ * {@link Grammar}, or a parser for the schema's type (short for
93
+ * `{ parser }`, which cannot be printed).
94
+ *
95
+ * @since 0.3.0
96
+ * @category models
97
+ */
98
+ export type Annotation<A = unknown> = Grammar<A> | Parser<A, unknown, never, string>;
99
+ /**
100
+ * Annotates a schema with how it is written as text, for {@link toParser}
101
+ * and {@link toPrinter}. The same as
102
+ * `schema.annotate({ [ParserSchema.annotationKey]: annotation })`, except
103
+ * that the parser's output must have the schema's type.
104
+ *
105
+ * @example
106
+ * ```ts
107
+ * import { Schema } from "effect"
108
+ * import { ParserSchema } from "@thomasfosterau/effect-parse"
109
+ *
110
+ * const Point = Schema.Struct({ x: Schema.Int, y: Schema.Int }).pipe(
111
+ * ParserSchema.annotate({ prefix: "(", separator: ",", suffix: ")" })
112
+ * )
113
+ * ```
114
+ *
115
+ * @since 0.3.0
116
+ * @category annotations
117
+ */
118
+ export declare const annotate: {
119
+ <A = never>(annotation: Annotation<A>): <S extends Schema.Top>(self: S & ([A] extends [S["Type"]] ? unknown : never)) => S["Rebuild"];
120
+ <S extends Schema.Top>(self: S, annotation: Annotation<S["Type"]>): S["Rebuild"];
121
+ };
122
+ /**
123
+ * Derives a parser from a schema. The parser reads the text the schema's
124
+ * annotations ({@link Grammar}) describe and returns a value of the schema's
125
+ * type, which the schema's checks and transformations have already
126
+ * validated; their failures become parse errors whose `cause` is the
127
+ * `SchemaError`.
128
+ *
129
+ * Without an annotation, a schema is parsed as follows:
130
+ *
131
+ * - `String`: one or more characters up to the next separator, key
132
+ * separator, suffix or literal around it (to the end of the input at the
133
+ * top level).
134
+ * - `Number`: a decimal number (`Ascii.float`), or a decimal integer when
135
+ * the schema checks `isInt` (as `Schema.Int` does). `BigInt`: a decimal
136
+ * integer. `Boolean`: `true` or `false`. `Null`: `null`. `Undefined` and
137
+ * `Void`: nothing.
138
+ * - `Literal`, `Enum` and `TemplateLiteral`: their text.
139
+ * - `Struct` and `Tuple`: their fields or elements in order, between
140
+ * separators; an optional one may be left out along with its separator.
141
+ * `Array`: elements between separators.
142
+ * - `Record`, and `Struct` with a `keySeparator`: `key`, key separator,
143
+ * value entries in any order, between separators.
144
+ * - `Union`: the first member that parses, in order (literals longest
145
+ * first). `suspend`: recursion.
146
+ * - A transformation (a `Class`, `NumberFromString`, ...): its encoded side,
147
+ * decoded by the schema. A grammar on it with a separator applies to the
148
+ * encoded side.
149
+ *
150
+ * A parser that fails where the schema starts reports the schema's
151
+ * `identifier` annotation (or its `expected` annotation, unless that belongs
152
+ * to a check) as what it expected. Other declarations, `Unknown`, `Any`,
153
+ * symbols and `object` need a parser in their annotation; without one,
154
+ * `toParser` throws. Transformations must decode synchronously.
155
+ *
156
+ * @example
157
+ * ```ts
158
+ * import { Schema } from "effect"
159
+ * import { Parser, ParserSchema } from "@thomasfosterau/effect-parse"
160
+ *
161
+ * const Version = Schema.Struct({ major: Schema.Int, minor: Schema.Int }).pipe(
162
+ * ParserSchema.annotate({ separator: "." })
163
+ * )
164
+ *
165
+ * Parser.parse(ParserSchema.toParser(Version), "1.2") // Result.succeed({ major: 1, minor: 2 })
166
+ * ```
167
+ *
168
+ * @since 0.3.0
169
+ * @category derivation
170
+ */
171
+ export declare const toParser: <S extends Schema.ConstraintDecoder<unknown>>(schema: S) => Parser<S["Type"], Schema.SchemaError, never, string>;
172
+ /**
173
+ * Derives a printer from a schema: it writes a value of the schema's type as
174
+ * the text {@link toParser} reads, following the same annotations, with no
175
+ * optional whitespace. Transformations encode the value first, and a
176
+ * {@link Grammar.print} replaces the derived printer.
177
+ *
178
+ * The printer fails with a `SchemaError` when the value is not of the
179
+ * schema's type, or cannot be written so that it reads back: a string that
180
+ * is empty or contains a character that would end it (a separator, a
181
+ * suffix), or a part annotated with a parser but no `print`. Where the
182
+ * grammar is ambiguous (an optional field followed by a field of the same
183
+ * type), the text can still read back as a different value;
184
+ * {@link fromSchema} checks that it doesn't.
185
+ *
186
+ * @example
187
+ * ```ts
188
+ * import { Schema } from "effect"
189
+ * import { ParserSchema } from "@thomasfosterau/effect-parse"
190
+ *
191
+ * const Version = Schema.Struct({ major: Schema.Int, minor: Schema.Int }).pipe(
192
+ * ParserSchema.annotate({ separator: "." })
193
+ * )
194
+ *
195
+ * ParserSchema.toPrinter(Version)({ major: 1, minor: 2 }) // Result.succeed("1.2")
196
+ * ```
197
+ *
198
+ * @since 0.3.0
199
+ * @category derivation
200
+ */
201
+ export declare const toPrinter: <S extends Schema.ConstraintCodec<unknown, unknown, unknown, never>>(schema: S) => ((value: S["Type"]) => Result.Result<string, Schema.SchemaError>);
202
+ /**
203
+ * Creates a schema that decodes a string with the parser {@link toParser}
204
+ * derives from `schema`, into `schema`'s type, and encodes with the printer
205
+ * {@link toPrinter} derives, or with `encode` when given. A derived printer's
206
+ * text is parsed back, and encoding fails unless it gives a value equivalent
207
+ * to the one encoded, so decoding what was encoded always gives it back.
208
+ *
209
+ * @example
210
+ * ```ts
211
+ * import { Schema } from "effect"
212
+ * import { ParserSchema } from "@thomasfosterau/effect-parse"
213
+ *
214
+ * const Pair = ParserSchema.fromSchema(
215
+ * Schema.Tuple([Schema.String, Schema.Number]).pipe(ParserSchema.annotate({ separator: "=" }))
216
+ * )
217
+ *
218
+ * Schema.decodeUnknownSync(Pair)("width=10") // ["width", 10]
219
+ * Schema.encodeSync(Pair)(["height", 2]) // "height=2"
220
+ * ```
221
+ *
222
+ * @since 0.3.0
223
+ * @category constructors
224
+ */
225
+ export declare const fromSchema: <S extends Schema.ConstraintCodec<unknown, unknown, never, never> & Schema.Top>(schema: S, options?: {
226
+ readonly encode?: ((value: S["Type"]) => string) | undefined;
227
+ }) => Schema.decodeTo<Schema.toType<S>, Schema.String>;
32
228
  //# sourceMappingURL=ParserSchema.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ParserSchema.d.ts","names":[],"sources":["../src/ParserSchema.ts"],"mappings":";;;;;;;;;iBAyCiB,QAAQ;;WAEd,WAAW,OAAO;;WAElB;;;;;;;;iBASM,aAAa,WAAW,OAAO,SAAS,OAAO,QAAQ,IAAI,OAAO;;;;;;;;qBAStE,OAAQ,GAAG,GAAG,QAAQ,OAAO,GAAG,mBAAmB,UAAU,QAAQ,OAAK,aAAa"}
1
+ {"version":3,"file":"ParserSchema.d.ts","names":[],"sources":["../src/ParserSchema.ts"],"mappings":";;;;;;;;;;;qBA4Da;;YAGD;cACE;;;;;;gBAME,iBAAiB;;;;;;;;;;iBAWhB,QAAQ;;WAEd,WAAW,OAAO;;WAElB;;;;;;;;iBASM,aAAa,WAAW,OAAO,SAAS,OAAO,QAAQ,IAAI,OAAO;;;;;;;;qBA0CtE,OAAQ,GAAG,GAAG,QAAQ,OAAO,GAAG,mBAAmB,UAAU,QAAQ,OAAK,aAAa;;;;;;;;;;iBAuBnF,QAAQ;;;;;;;WAOd,SAAS,OAAO;;WAEhB,UAAU,OAAO;;WAEjB;;WAEA;;WAEA;;;;;WAKA;;;;;;;WAOA;;;;;;;;;;YAWC,WAAW,eAAe,QAAQ,KAAK,OAAO;;;;;;;;;;;;;;;;;;;;qBAqB7C;GAEV,WACC,YAAY,WAAW,MACrB,UAAU,OAAO,KAAK,MAAM,MAAM,YAAY,kCAAkC;GACnF,UAAU,OAAO,KAAK,MAAM,GAAG,YAAY,WAAW,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBAsDzD,WAAY,UAAU,OAAO,4BACxC,QAAQ,MACP,OAAO,WAAW,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBAmCf,YAAa,UAAU,OAAO,mDACzC,QAAQ,QACL,OAAO,cAAc,OAAO,eAAe,OAAO;;;;;;;;;;;;;;;;;;;;;;;;qBAmC1C,aAAc,UAAU,OAAO,kDAAkD,OAAO,KACnG,QAAQ,GACR;WAAqB,WAAW,OAAO;MACtC,OAAO,SAAS,OAAO,OAAO,IAAI,OAAO"}