Move preserves/parse module into preserves
Some macros in preserves call `parsePreserves` so include that proc implicitly.
This commit is contained in:
parent
5408cb859a
commit
1dcad539f4
|
@ -256,8 +256,7 @@ func step*(pr, idx: Preserve): Option[Preserve] =
|
||||||
## Step into `pr` by index `idx`.
|
## Step into `pr` by index `idx`.
|
||||||
## Works for sequences, records, and dictionaries.
|
## Works for sequences, records, and dictionaries.
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
import options
|
import std/options
|
||||||
import preserves, preserves/parse
|
|
||||||
assert step(parsePreserves("""<foo 1 2>"""), 1.toPreserve) == some(2.toPreserve)
|
assert step(parsePreserves("""<foo 1 2>"""), 1.toPreserve) == some(2.toPreserve)
|
||||||
assert step(parsePreserves("""{ foo: 1 bar: 2}"""), "foo".toSymbol) == some(1.toPreserve)
|
assert step(parsePreserves("""{ foo: 1 bar: 2}"""), "foo".toSymbol) == some(1.toPreserve)
|
||||||
assert step(parsePreserves("""[ ]"""), 1.toPreserve) == none(Preserve[void])
|
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`.
|
## trace is printing during `fromPreserve`.
|
||||||
# TODO: {.raises: [].}
|
# TODO: {.raises: [].}
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
import preserves, preserves/parse
|
|
||||||
type Foo {.preservesRecord: "foo".} = object
|
type Foo {.preservesRecord: "foo".} = object
|
||||||
x, y: int
|
x, y: int
|
||||||
var foo: Foo
|
var foo: Foo
|
||||||
|
@ -967,7 +965,7 @@ proc preserveTo*(pr: Preserve; T: typedesc): Option[T] =
|
||||||
## Reverse of `toPreserve`.
|
## Reverse of `toPreserve`.
|
||||||
# TODO: {.raises: [].}
|
# TODO: {.raises: [].}
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
import std/options, preserves, preserves/parse
|
import std/options
|
||||||
type Foo {.preservesRecord: "foo".} = object
|
type Foo {.preservesRecord: "foo".} = object
|
||||||
x, y: int
|
x, y: int
|
||||||
|
|
||||||
|
@ -1136,3 +1134,5 @@ proc concat[E](result: var string; pr: Preserve[E]) =
|
||||||
|
|
||||||
proc `$`*(pr: Preserve): string = concat(result, pr)
|
proc `$`*(pr: Preserve): string = concat(result, pr)
|
||||||
## Generate the textual representation of ``pr``.
|
## Generate the textual representation of ``pr``.
|
||||||
|
|
||||||
|
include ./preserves/private/parse
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
# SPDX-FileCopyrightText: 2021 ☭ Emery Hemingway
|
# SPDX-FileCopyrightText: 2021 ☭ Emery Hemingway
|
||||||
# SPDX-License-Identifier: Unlicense
|
# SPDX-License-Identifier: Unlicense
|
||||||
|
|
||||||
|
## NPEG rules for Preserves.
|
||||||
|
|
||||||
import npeg, npeg/lib/utf8
|
import npeg, npeg/lib/utf8
|
||||||
|
|
||||||
when defined(nimHasUsed): {.used.}
|
when defined(nimHasUsed): {.used.}
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
# SPDX-FileCopyrightText: 2021 ☭ Emery Hemingway
|
# SPDX-FileCopyrightText: 2021 ☭ Emery Hemingway
|
||||||
# SPDX-License-Identifier: Unlicense
|
# 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 npeg
|
||||||
import ../preserves, ./pegs
|
import ../pegs
|
||||||
|
|
||||||
type
|
type
|
||||||
Value = Preserve[void]
|
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))
|
template pushStack(v: Value) = stack.add((v, capture[0].si))
|
||||||
|
|
||||||
proc parsePreserves*(text: string): Preserve[void] {.gcsafe.} =
|
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):
|
const pegParser = peg("Document", stack: Stack):
|
||||||
# Override rules from pegs.nim
|
# Override rules from pegs.nim
|
||||||
|
|
||||||
|
@ -105,8 +110,6 @@ proc parsePreserves*(text: string): Preserve[void] {.gcsafe.} =
|
||||||
stack.pop.value
|
stack.pop.value
|
||||||
|
|
||||||
proc parsePreserves*(text: string; E: typedesc): Preserve[E] {.gcsafe.} =
|
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)
|
when E is void: parsePreserves(text)
|
||||||
else: mapEmbeds(parsePreserves(text), E)
|
else: mapEmbeds(parsePreserves(text), E)
|
||||||
|
|
||||||
when isMainModule:
|
|
||||||
assert(parsePreserves("#f") == Preserve())
|
|
Loading…
Reference in New Issue