diff --git a/src/preserves.nim b/src/preserves.nim index 1648410..b6f6196 100644 --- a/src/preserves.nim +++ b/src/preserves.nim @@ -256,8 +256,7 @@ func step*(pr, idx: Preserve): Option[Preserve] = ## Step into `pr` by index `idx`. ## Works for sequences, records, and dictionaries. runnableExamples: - import options - import preserves, preserves/parse + import std/options assert step(parsePreserves(""""""), 1.toPreserve) == some(2.toPreserve) assert step(parsePreserves("""{ foo: 1 bar: 2}"""), "foo".toSymbol) == some(1.toPreserve) assert step(parsePreserves("""[ ]"""), 1.toPreserve) == none(Preserve[void]) @@ -786,7 +785,6 @@ proc fromPreserve*[T, E](v: var T; pr: Preserve[E]): bool = ## trace is printing during `fromPreserve`. # TODO: {.raises: [].} runnableExamples: - import preserves, preserves/parse type Foo {.preservesRecord: "foo".} = object x, y: int var foo: Foo @@ -967,7 +965,7 @@ proc preserveTo*(pr: Preserve; T: typedesc): Option[T] = ## Reverse of `toPreserve`. # TODO: {.raises: [].} runnableExamples: - import std/options, preserves, preserves/parse + import std/options type Foo {.preservesRecord: "foo".} = object x, y: int @@ -1136,3 +1134,5 @@ proc concat[E](result: var string; pr: Preserve[E]) = proc `$`*(pr: Preserve): string = concat(result, pr) ## Generate the textual representation of ``pr``. + +include ./preserves/private/parse diff --git a/src/preserves/pegs.nim b/src/preserves/pegs.nim index e9969e5..2afdbe1 100644 --- a/src/preserves/pegs.nim +++ b/src/preserves/pegs.nim @@ -1,6 +1,8 @@ # SPDX-FileCopyrightText: 2021 ☭ Emery Hemingway # SPDX-License-Identifier: Unlicense +## NPEG rules for Preserves. + import npeg, npeg/lib/utf8 when defined(nimHasUsed): {.used.} diff --git a/src/preserves/parse.nim b/src/preserves/private/parse.nim similarity index 91% rename from src/preserves/parse.nim rename to src/preserves/private/parse.nim index b368c04..bf4cea8 100644 --- a/src/preserves/parse.nim +++ b/src/preserves/private/parse.nim @@ -1,9 +1,11 @@ # SPDX-FileCopyrightText: 2021 ☭ Emery Hemingway # SPDX-License-Identifier: Unlicense -import std/[base64, parseutils, sets, strutils, tables] +# this module is included in ../../preserves.nim + +import std/[parseutils, strutils] import npeg -import ../preserves, ./pegs +import ../pegs type Value = Preserve[void] @@ -15,6 +17,9 @@ proc shrink(stack: var Stack; n: int) = stack.setLen(stack.len - n) template pushStack(v: Value) = stack.add((v, capture[0].si)) proc parsePreserves*(text: string): Preserve[void] {.gcsafe.} = + ## Parse a text-encoded Preserves `string` to a `Preserve` value. + runnableExamples: + assert parsePreserves"[ 1 2 3 ]" == [ 1, 2, 3 ].toPreserve const pegParser = peg("Document", stack: Stack): # Override rules from pegs.nim @@ -105,8 +110,6 @@ proc parsePreserves*(text: string): Preserve[void] {.gcsafe.} = stack.pop.value proc parsePreserves*(text: string; E: typedesc): Preserve[E] {.gcsafe.} = + ## Parse a text-encoded Preserves `string` to a `Preserve[E]` value for embedded type `E`. when E is void: parsePreserves(text) else: mapEmbeds(parsePreserves(text), E) - -when isMainModule: - assert(parsePreserves("#f") == Preserve())