2023-08-23 11:13:21 +00:00
|
|
|
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
2021-08-31 15:00:00 +00:00
|
|
|
# SPDX-License-Identifier: Unlicense
|
2021-06-02 13:51:36 +00:00
|
|
|
|
2023-12-19 20:07:44 +00:00
|
|
|
import std/[options, sets, sequtils, strutils, tables, typetraits]
|
2023-06-11 10:01:15 +00:00
|
|
|
from std/algorithm import sort
|
2021-08-28 09:31:33 +00:00
|
|
|
from std/json import escapeJson, escapeJsonUnquoted
|
2023-12-22 15:55:23 +00:00
|
|
|
import bigints
|
|
|
|
import ./preserves/private/[encoding, decoding, dot, macros, parsing, texts, values]
|
2023-12-19 20:07:44 +00:00
|
|
|
|
|
|
|
export encoding, decoding, parsing, texts, values
|
2021-10-17 09:20:17 +00:00
|
|
|
|
|
|
|
when defined(tracePreserves):
|
2022-12-08 04:03:58 +00:00
|
|
|
when defined(posix):
|
|
|
|
template trace(args: varargs[untyped]) = {.cast(noSideEffect).}: stderr.writeLine(args)
|
|
|
|
else:
|
|
|
|
template trace(args: varargs[untyped]) = {.cast(noSideEffect).}: echo(args)
|
2021-10-17 09:20:17 +00:00
|
|
|
else:
|
|
|
|
template trace(args: varargs[untyped]) = discard
|
2021-08-28 09:17:23 +00:00
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
type Preserve*[E] {.deprecated: "preserves.Preserve[E] is now preserves.Value".} = Value
|
|
|
|
|
|
|
|
proc sortDict(pr: var Value) =
|
|
|
|
sort(pr.dict) do (x, y: DictEntry) -> int:
|
2023-07-23 07:27:01 +00:00
|
|
|
cmp(x.key, y.key)
|
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
proc cannonicalize*(pr: var Value) =
|
2023-06-11 10:01:15 +00:00
|
|
|
## Cannonicalize a compound Preserves value by total ordering.
|
|
|
|
case pr.kind
|
|
|
|
of pkSequence:
|
|
|
|
apply(pr.sequence, cannonicalize)
|
|
|
|
of pkSet:
|
|
|
|
apply(pr.set, cannonicalize)
|
|
|
|
sort(pr.set)
|
|
|
|
of pkDictionary:
|
2023-12-27 15:05:30 +00:00
|
|
|
apply(pr.dict) do (e: var DictEntry):
|
2023-06-11 10:01:15 +00:00
|
|
|
cannonicalize(e.val)
|
2023-07-23 07:27:01 +00:00
|
|
|
sortDict(pr)
|
2023-06-11 10:01:15 +00:00
|
|
|
else:
|
|
|
|
discard
|
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
proc toInt*(pr: Value): Option[int] =
|
2023-12-22 15:55:23 +00:00
|
|
|
case pr.kind
|
|
|
|
of pkRegister: result = some pr.register
|
|
|
|
of pkBigInt: result = toInt[int](pr.bigint)
|
|
|
|
else: discard
|
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
func isAtomic*(pr: Value): bool = pr.kind in atomKinds
|
|
|
|
|
|
|
|
func isBoolean*(pr: Value): bool {.inline.} = pr.kind == pkBoolean
|
|
|
|
## Check if ``pr`` is a Preserve boolean.
|
|
|
|
|
|
|
|
func isFalse*(pr: Value): bool {.inline.} =
|
|
|
|
## Check if ``pr`` is equivalent to the zero-initialized ``Preserve``.
|
|
|
|
pr.kind == pkBoolean and pr.bool == false
|
|
|
|
|
|
|
|
func isFloat*(pr: Value): bool {.inline.} = pr.kind == pkFloat
|
|
|
|
## Check if ``pr`` is a Preserve float.
|
|
|
|
|
|
|
|
func isDouble*(pr: Value): bool {.inline.} = pr.kind == pkDouble
|
|
|
|
## Check if ``pr`` is a Preserve double.
|
|
|
|
|
|
|
|
func isInteger*(pr: Value): bool {.inline.} =
|
|
|
|
## Check if ``pr`` is a Preserve integer.
|
|
|
|
pr.kind == pkRegister or pr.kind == pkBigInt
|
|
|
|
|
|
|
|
func isInteger*(pr: Value; i: SomeInteger): bool {.inline.} =
|
|
|
|
## Check if ``pr`` is a Preserve integer equivalent to `i`.
|
|
|
|
case pr.kind
|
|
|
|
of pkRegister: pr.register == i.int
|
|
|
|
of pkBigInt: pr.int == i.initBigInt
|
|
|
|
else: false
|
|
|
|
|
|
|
|
func isString*(pr: Value): bool {.inline.} = pr.kind == pkString
|
|
|
|
## Check if ``pr`` is a Preserve text string.
|
|
|
|
|
|
|
|
func isString*(pr: Value; s: string): bool {.inline.} =
|
|
|
|
## Check if ``pr`` is a Preserve text string equivalent to `s`.
|
|
|
|
pr.kind == pkString and pr.string == s
|
|
|
|
|
|
|
|
func isByteString*(pr: Value): bool {.inline.} = pr.kind == pkByteString
|
|
|
|
## Check if ``pr`` is a Preserves byte string.
|
|
|
|
|
|
|
|
func isSymbol*(pr: Value): bool {.inline.} = pr.kind == pkSymbol
|
|
|
|
## Check if `pr` is a Preserves symbol.
|
|
|
|
|
|
|
|
func isSymbol*(pr: Value; sym: string|Symbol): bool {.inline.} =
|
|
|
|
## Check if ``pr`` is a Preserves symbol of ``sym``.
|
|
|
|
(pr.kind == pkSymbol) and (pr.symbol == Symbol(sym))
|
|
|
|
|
|
|
|
proc label*(pr: Value): Value {.inline.} =
|
|
|
|
## Return the label of record value.
|
|
|
|
pr.record[pr.record.high]
|
|
|
|
|
|
|
|
proc arity*(pr: Value): int {.inline.} =
|
|
|
|
## Return the number of fields in record `pr`.
|
|
|
|
pred(pr.record.len)
|
|
|
|
|
|
|
|
func isRecord*(pr: Value): bool {.inline.} = (pr.kind == pkRecord) and (pr.record.len > 0)
|
|
|
|
## Check if ``pr`` is a Preserves record.
|
|
|
|
|
|
|
|
func isRecord*(pr: Value; label: string): bool {.inline.} =
|
|
|
|
## Check if ``pr`` is a Preserves record with the given label symbol.
|
|
|
|
pr.kind == pkRecord and
|
|
|
|
pr.record.len > 0 and
|
|
|
|
pr.label.isSymbol(label)
|
|
|
|
|
|
|
|
func isRecord*(pr: Value; label: string; arity: Natural): bool {.inline.} =
|
|
|
|
## Check if ``pr`` is a Preserves record with the given label symbol and field arity.
|
|
|
|
pr.kind == pkRecord and
|
|
|
|
pr.record.len == succ(arity) and
|
|
|
|
pr.label.isSymbol(label)
|
|
|
|
|
|
|
|
proc isSequence*(pr: Value): bool {.inline.} = pr.kind == pkSequence
|
|
|
|
## Check if ``pr`` is a Preserves sequence.
|
|
|
|
|
|
|
|
proc isSet*(pr: Value): bool {.inline.} = pr.kind == pkSet
|
|
|
|
## Check if ``pr`` is a Preserves set.
|
|
|
|
|
|
|
|
proc isDictionary*(pr: Value): bool {.inline.} = pr.kind == pkDictionary
|
|
|
|
## Check if ``pr`` is a Preserves dictionary.
|
|
|
|
|
|
|
|
func isEmbedded*(pr: Value): bool {.inline.} =
|
|
|
|
## Check if ``pr`` is an embedded value.
|
|
|
|
pr.embedded or pr.kind == pkEmbedded
|
|
|
|
|
2023-12-28 16:18:06 +00:00
|
|
|
proc `&`*(x, y: Value): Value =
|
|
|
|
## Concatenate operator.
|
|
|
|
if x.kind != y.kind:
|
|
|
|
raise newException(ValueError, "cannot concatenate heterogenous values")
|
|
|
|
case x.kind
|
|
|
|
of pkString:
|
|
|
|
result = Value(kind: pkString, string: x.string & y.string)
|
|
|
|
of pkByteString:
|
|
|
|
result = Value(kind: pkByteString, bytes: x.bytes & y.bytes)
|
|
|
|
of pkSequence:
|
|
|
|
result = Value(kind: pkSequence, sequence: x.sequence & y.sequence)
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, "cannot concatenate this value type")
|
|
|
|
|
|
|
|
proc `&`*(x: Value; y: seq[Value]): Value =
|
|
|
|
if x.kind != pkSequence:
|
|
|
|
raise newException(ValueError, "cannot concatenate to non-sequence value")
|
|
|
|
result = Value(kind: pkSequence, sequence: x.sequence & y)
|
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
proc pop*(pr: var Value; key: Value; val: var Value): bool =
|
2023-11-02 13:33:34 +00:00
|
|
|
## Deletes the `key` from a Preserves dictionary.
|
|
|
|
## Returns true, if the key existed, and sets `val` to the mapping
|
|
|
|
## of the key. Otherwise, returns false, and the `val` is unchanged.
|
|
|
|
if pr.kind == pkDictionary:
|
|
|
|
var i = 0
|
|
|
|
while i < pr.dict.len:
|
|
|
|
if pr.dict[i].key == key:
|
|
|
|
val = move pr.dict[i].val
|
2023-12-27 15:05:30 +00:00
|
|
|
delete(pr.dict, i .. i)
|
2023-11-02 13:33:34 +00:00
|
|
|
return true
|
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
proc `[]`*(pr, key: Value): Value {.deprecated: "use step instead".} =
|
2022-03-18 14:44:20 +00:00
|
|
|
## Select a value by `key` from `pr`.
|
|
|
|
## Works for sequences, records, and dictionaries.
|
2023-12-22 15:55:23 +00:00
|
|
|
case pr.kind
|
|
|
|
of pkDictionary:
|
2022-03-08 21:24:14 +00:00
|
|
|
for (k, v) in pr.dict.items:
|
|
|
|
if k == key: return v
|
2022-03-14 19:16:38 +00:00
|
|
|
raise newException(KeyError, "value not in Preserves dictionary")
|
2023-12-22 15:55:23 +00:00
|
|
|
of pkRecord, pkSequence:
|
|
|
|
let idx = key.toInt
|
|
|
|
if idx.isSome:
|
|
|
|
result = pr[get idx]
|
|
|
|
else:
|
|
|
|
raise newException(ValueError, "invalid Preserves index")
|
2022-03-08 21:24:14 +00:00
|
|
|
else:
|
|
|
|
raise newException(ValueError, "invalid Preserves indexing")
|
2021-08-28 10:48:50 +00:00
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
proc toSymbol*(s: sink string): Value {.inline.} =
|
2021-09-01 09:37:49 +00:00
|
|
|
## Create a Preserves symbol value.
|
2023-12-27 15:05:30 +00:00
|
|
|
Value(kind: pkSymbol, symbol: Symbol s)
|
2021-09-01 09:37:49 +00:00
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
proc toSymbol*(s: sink string; E: typedesc): Value {.deprecated.} =
|
|
|
|
s.toSymbol
|
|
|
|
|
|
|
|
proc initRecord*(label: Value; arity: Natural = 0): Value =
|
2021-09-23 12:29:31 +00:00
|
|
|
## Create a Preserves record value.
|
2023-12-27 15:05:30 +00:00
|
|
|
result = Value(kind: pkRecord, record: newSeq[Value](arity.succ))
|
2021-09-23 12:29:31 +00:00
|
|
|
result.record[arity] = label
|
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
proc initRecord*(label: Value; E: typedesc): Value {.deprecated.} =
|
|
|
|
initRecord(label)
|
|
|
|
|
|
|
|
proc initRecord*(label: Value; args: varargs[Value]): Value =
|
2021-09-01 09:37:49 +00:00
|
|
|
## Create a Preserves record value.
|
2023-12-27 15:05:30 +00:00
|
|
|
result = Value(kind: pkRecord,
|
|
|
|
record: newSeqOfCap[Value](1+args.len))
|
2021-08-28 10:48:50 +00:00
|
|
|
for arg in args:
|
|
|
|
result.record.add(arg)
|
|
|
|
result.record.add(label)
|
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
proc initRecord*(label: string; args: varargs[Value]): Value {.inline.} =
|
2021-11-11 10:11:40 +00:00
|
|
|
## Create a Preserves record value.
|
2023-12-27 15:05:30 +00:00
|
|
|
initRecord(toSymbol(label), args)
|
2021-11-11 10:11:40 +00:00
|
|
|
|
2023-12-29 16:55:02 +00:00
|
|
|
proc toRecord*(items: varargs[Value, toPreserves]): Value =
|
|
|
|
assert items.len > 0
|
|
|
|
result = initRecord(items[0], items.len.pred)
|
|
|
|
for i in 0..<items.high: result.record[i] = items[succ i]
|
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
proc initSequence*(len: Natural = 0): Value =
|
2021-09-23 12:29:31 +00:00
|
|
|
## Create a Preserves sequence value.
|
2023-12-27 15:05:30 +00:00
|
|
|
Value(kind: pkSequence, sequence: newSeq[Value](len))
|
2021-09-23 12:29:31 +00:00
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
proc initSequence*(len: Natural; E: typedesc): Value {.deprecated.} =
|
|
|
|
initSequence(len)
|
|
|
|
|
|
|
|
proc initSequenceOfCap*(cap: Natural): Value =
|
2023-03-29 22:33:39 +00:00
|
|
|
## Create a Preserves sequence value.
|
2023-12-27 15:05:30 +00:00
|
|
|
Value(kind: pkSequence, sequence: newSeqOfCap[Value](cap))
|
2023-03-29 22:33:39 +00:00
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
proc initSet*(): Value = Value(kind: pkSet)
|
2021-09-01 09:37:49 +00:00
|
|
|
## Create a Preserves set value.
|
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
proc initDictionary*(): Value = Value(kind: pkDictionary)
|
2021-09-01 09:37:49 +00:00
|
|
|
## Create a Preserves dictionary value.
|
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
proc initDictionary*(E: typedesc): Value =
|
|
|
|
initDictionary()
|
|
|
|
|
|
|
|
proc toDictionary*(pairs: openArray[(string, Value)]): Value =
|
2023-04-03 22:03:56 +00:00
|
|
|
## Create a Preserves dictionary value.
|
2023-12-27 15:05:30 +00:00
|
|
|
result = Value(kind: pkDictionary)
|
|
|
|
for (key, val) in pairs: result[toSymbol(key)] = val
|
2023-04-03 22:03:56 +00:00
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
proc embed*(pr: sink Value): Value =
|
2021-09-23 13:20:06 +00:00
|
|
|
## Create a Preserves value that embeds ``e``.
|
2021-10-20 20:15:11 +00:00
|
|
|
result = pr
|
|
|
|
result.embedded = true
|
2021-09-23 13:20:06 +00:00
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
proc embed*(e: sink EmbeddedRef): Value =
|
2021-10-20 20:15:11 +00:00
|
|
|
## Create a Preserves value that embeds ``e``.
|
2023-12-27 15:05:30 +00:00
|
|
|
Value(kind: pkEmbedded, embeddedRef: e, embedded: true)
|
2021-10-17 09:20:17 +00:00
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
proc len*(pr: Value): int =
|
2021-09-08 09:00:38 +00:00
|
|
|
## Return the shallow count of values in ``pr``, that is the number of
|
2021-09-01 09:37:49 +00:00
|
|
|
## fields in a record, items in a sequence, items in a set, or key-value pairs
|
|
|
|
## in a dictionary.
|
2021-09-08 09:00:38 +00:00
|
|
|
case pr.kind
|
|
|
|
of pkRecord: pr.record.len.pred
|
|
|
|
of pkSequence: pr.sequence.len
|
|
|
|
of pkSet: pr.set.len
|
|
|
|
of pkDictionary: pr.dict.len
|
2021-08-28 10:48:50 +00:00
|
|
|
else: 0
|
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
iterator items*(pr: Value): Value =
|
2021-09-08 09:00:38 +00:00
|
|
|
## Shallow iterator over `pr`, yield the fields in a record,
|
2021-09-01 09:37:49 +00:00
|
|
|
## the items of a sequence, the items of a set, or the pairs
|
|
|
|
## of a dictionary.
|
2021-09-08 09:00:38 +00:00
|
|
|
case pr.kind
|
2021-07-09 10:24:18 +00:00
|
|
|
of pkRecord:
|
2021-09-08 09:00:38 +00:00
|
|
|
for i in 0..pr.record.high.pred:
|
|
|
|
yield pr.record[i]
|
2021-07-09 10:24:18 +00:00
|
|
|
of pkSequence:
|
2021-09-08 09:00:38 +00:00
|
|
|
for e in pr.sequence.items: yield e
|
2021-07-09 10:24:18 +00:00
|
|
|
of pkSet:
|
2021-09-08 09:00:38 +00:00
|
|
|
for e in pr.set.items: yield e
|
2021-07-09 10:24:18 +00:00
|
|
|
of pkDictionary:
|
2021-09-08 09:00:38 +00:00
|
|
|
for (k, v) in pr.dict.items:
|
2021-07-09 10:24:18 +00:00
|
|
|
yield k # key can be an arbitrary Preserve
|
|
|
|
yield v
|
|
|
|
else: discard
|
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
iterator pairs*(pr: Value): DictEntry =
|
2021-11-11 10:11:40 +00:00
|
|
|
assert(pr.kind == pkDictionary, "not a dictionary")
|
|
|
|
for i in 0..pr.dict.high: yield pr.dict[i]
|
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
proc fields*(pr: Value): seq[Value] {.inline.} =
|
2021-06-15 10:48:20 +00:00
|
|
|
## Return the fields of a record value.
|
2021-09-08 09:00:38 +00:00
|
|
|
pr.record[0..pr.record.high.pred]
|
2021-06-15 10:48:20 +00:00
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
iterator fields*(pr: Value): Value =
|
2021-06-15 10:48:20 +00:00
|
|
|
## Iterate the fields of a record value.
|
2021-09-08 09:00:38 +00:00
|
|
|
for i in 0..<pr.record.high: yield pr.record[i]
|
2021-06-02 13:51:36 +00:00
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
proc unembed*(pr: Value; E: typedesc): Option[E] =
|
|
|
|
## Unembed an `E` value from a `Value` value.
|
|
|
|
if pr.kind == pkEmbedded and pr.embeddedRef of E:
|
|
|
|
result = some(E pr.embeddedRef)
|
2021-10-24 23:53:12 +00:00
|
|
|
|
2021-10-17 09:20:17 +00:00
|
|
|
template preservesRecord*(label: string) {.pragma.}
|
|
|
|
## Serialize this object or tuple as a record.
|
|
|
|
## See ``toPreserve``.
|
|
|
|
|
2022-04-25 16:33:37 +00:00
|
|
|
proc hasPreservesRecordPragma*(T: typedesc): bool =
|
|
|
|
## Test if a type has a `{.preservesRecord: "…".}` pragma attached.
|
|
|
|
hasCustomPragma(T, preservesRecord)
|
|
|
|
|
|
|
|
proc recordLabel*(T: typedesc): string =
|
|
|
|
## Get the record label set by a pragma on a type.
|
|
|
|
runnableExamples:
|
|
|
|
type Foo {.preservesRecord: "bar".} = object
|
|
|
|
n: int
|
|
|
|
assert recordLabel(Foo) == "bar"
|
|
|
|
T.getCustomPragmaVal(preservesRecord)
|
|
|
|
|
2021-10-17 09:20:17 +00:00
|
|
|
template preservesTuple*() {.pragma.}
|
|
|
|
## Serialize this object or tuple as a tuple.
|
2023-12-27 15:05:30 +00:00
|
|
|
## See ``toPreserves``.
|
2021-10-17 09:20:17 +00:00
|
|
|
|
|
|
|
template preservesTupleTail*() {.pragma.}
|
|
|
|
## Serialize this object field to the end of its containing tuple.
|
2023-12-27 15:05:30 +00:00
|
|
|
## See ``toPreserves``.
|
2021-10-17 09:20:17 +00:00
|
|
|
|
2022-04-25 16:33:37 +00:00
|
|
|
proc hasPreservesTuplePragma*(T: typedesc): bool =
|
|
|
|
## Test if a type has a `preservesTuple` pragma attached.
|
|
|
|
hasCustomPragma(T, preservesTuple)
|
|
|
|
|
2021-10-17 09:20:17 +00:00
|
|
|
template preservesDictionary*() {.pragma.}
|
|
|
|
## Serialize this object or tuple as a dictionary.
|
2023-12-27 15:05:30 +00:00
|
|
|
## See ``toPreserves``.
|
2021-10-17 09:20:17 +00:00
|
|
|
|
2022-04-25 16:33:37 +00:00
|
|
|
proc hasPreservesDictionaryPragma*(T: typedesc): bool =
|
|
|
|
## Test if a type has a `preservesDictionary` pragma attached.
|
|
|
|
hasCustomPragma(T, preservesDictionary)
|
|
|
|
|
2021-10-17 09:20:17 +00:00
|
|
|
template preservesOr*() {.pragma.}
|
|
|
|
## Serialize this object as an ``or`` alternative.
|
2023-12-27 15:05:30 +00:00
|
|
|
## See ``toPreserves``.
|
2021-10-17 09:20:17 +00:00
|
|
|
|
|
|
|
template preservesLiteral*(value: typed) {.pragma.}
|
|
|
|
## Serialize a Preserves literal within this object.
|
2023-12-27 15:05:30 +00:00
|
|
|
## See ``toPreserves``.
|
2021-06-28 10:30:16 +00:00
|
|
|
|
2022-12-06 19:31:21 +00:00
|
|
|
template preservesEmbedded*() {.pragma.}
|
2023-12-27 15:05:30 +00:00
|
|
|
## Pragma to mark a value as embedded by `toPreserves`.
|
2022-12-06 19:31:21 +00:00
|
|
|
|
2021-07-01 10:47:30 +00:00
|
|
|
template unpreservable*() {.pragma.}
|
2023-12-27 15:05:30 +00:00
|
|
|
## Pragma to forbid a type from being converted by ``toPreserves``.
|
2021-10-25 19:33:31 +00:00
|
|
|
## Useful for preventing an embeded type from being encoded
|
|
|
|
## as its native type.
|
|
|
|
## Unpreservability is asserted at runtime.
|
2021-07-01 10:47:30 +00:00
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
proc toPreserves*[T](x: T): Value {.gcsafe.} =
|
2021-09-01 09:37:49 +00:00
|
|
|
## Serializes ``x`` to Preserves. Can be customized by defining
|
2023-12-27 15:05:30 +00:00
|
|
|
## ``toPreservesHook(x: T; E: typedesc)`` in the calling scope.
|
|
|
|
## Any ``toPreservesHook`` that does not compile will be discarded;
|
2021-09-23 13:20:06 +00:00
|
|
|
## *Write tests for your hooks!*
|
2021-10-25 17:07:16 +00:00
|
|
|
##
|
|
|
|
## When `tracePreserves` is defined (`-d:tracePreserves`) a diagnostic
|
|
|
|
## trace is printing during `toPreserve`.
|
2023-12-27 15:05:30 +00:00
|
|
|
when (T is Value): result = x
|
|
|
|
elif T is Value:
|
|
|
|
result = cast[Value](x)
|
|
|
|
elif compiles(toPreservesHook(x)):
|
|
|
|
result = toPreservesHook(x)
|
2023-06-11 20:26:58 +00:00
|
|
|
cannonicalize(result)
|
2021-10-17 09:20:17 +00:00
|
|
|
elif T is enum:
|
2023-12-27 15:05:30 +00:00
|
|
|
result = toSymbol($x)
|
2021-08-25 11:49:35 +00:00
|
|
|
elif T is seq[byte]:
|
2023-12-27 15:05:30 +00:00
|
|
|
result = Value(kind: pkByteString, bytes: x)
|
2021-06-15 10:48:20 +00:00
|
|
|
elif T is array | seq:
|
2023-12-27 15:05:30 +00:00
|
|
|
result = Value(kind: pkSequence, sequence: newSeqOfCap[Value](x.len))
|
|
|
|
for v in x.items: result.sequence.add(toPreserves(v))
|
2022-05-18 18:56:41 +00:00
|
|
|
elif T is set:
|
2023-12-27 15:05:30 +00:00
|
|
|
result = Value(kind: pkSet, set: newSeqOfCap[Value](x.len))
|
|
|
|
for v in x.items: result.incl(toPreserves(v))
|
2023-06-11 20:26:58 +00:00
|
|
|
cannonicalize(result)
|
2021-06-15 10:48:20 +00:00
|
|
|
elif T is bool:
|
2023-12-27 15:05:30 +00:00
|
|
|
result = Value(kind: pkBoolean, bool: x)
|
2022-03-08 21:25:03 +00:00
|
|
|
elif T is float32:
|
2023-12-27 15:05:30 +00:00
|
|
|
result = Value(kind: pkFloat, float: x)
|
2021-06-15 10:48:20 +00:00
|
|
|
elif T is float64:
|
2023-12-27 15:05:30 +00:00
|
|
|
result = Value(kind: pkDouble, double: x)
|
2021-10-17 09:20:17 +00:00
|
|
|
elif T is tuple:
|
2023-12-27 15:05:30 +00:00
|
|
|
result = Value(kind: pkSequence,
|
|
|
|
sequence: newSeqOfCap[Value](tupleLen(T)))
|
2021-10-17 09:20:17 +00:00
|
|
|
for xf in fields(x):
|
2023-12-27 15:05:30 +00:00
|
|
|
result.sequence.add(toPreserves(xf))
|
2021-06-15 10:48:20 +00:00
|
|
|
elif T is Ordinal:
|
2023-12-27 15:05:30 +00:00
|
|
|
result = Value(kind: pkRegister, register: x.ord)
|
2023-12-22 15:55:23 +00:00
|
|
|
assert result.register.T == x
|
2021-06-15 10:48:20 +00:00
|
|
|
elif T is ptr | ref:
|
2023-12-27 15:05:30 +00:00
|
|
|
if system.`==`(x, nil): result = initRecord("null")
|
|
|
|
else: result = toPreserves(x[])
|
2021-06-15 10:48:20 +00:00
|
|
|
elif T is string:
|
2023-12-27 15:05:30 +00:00
|
|
|
result = Value(kind: pkString, string: x)
|
2021-06-15 10:48:20 +00:00
|
|
|
elif T is SomeInteger:
|
2023-12-27 15:05:30 +00:00
|
|
|
result = Value(kind: pkRegister, register: x.int)
|
2023-12-22 15:55:23 +00:00
|
|
|
assert result.register.T == x
|
2022-02-20 04:33:15 +00:00
|
|
|
elif T is Symbol:
|
2023-12-27 15:05:30 +00:00
|
|
|
result = Value(kind: pkSymbol, symbol: x)
|
2022-02-20 04:33:15 +00:00
|
|
|
elif T is distinct:
|
2023-12-27 15:05:30 +00:00
|
|
|
result = toPreserves(x.distinctBase)
|
2021-10-17 09:20:17 +00:00
|
|
|
elif T is object:
|
2023-12-27 15:05:30 +00:00
|
|
|
template applyEmbed(key: string; v: var Value) {.used.} =
|
2022-12-06 19:31:21 +00:00
|
|
|
when x.dot(key).hasCustomPragma(preservesEmbedded):
|
|
|
|
v.embedded = true
|
2023-12-27 15:05:30 +00:00
|
|
|
template fieldToPreserve(key: string; val: typed): Value {.used.} =
|
2022-02-20 04:33:15 +00:00
|
|
|
when x.dot(key).hasCustomPragma(preservesLiteral):
|
2023-12-27 15:21:11 +00:00
|
|
|
const atom = x.dot(key).getCustomPragmaVal(preservesLiteral).parsePreservesAtom
|
2023-12-27 15:05:30 +00:00
|
|
|
atom.toPreservesHook()
|
2021-10-17 09:20:17 +00:00
|
|
|
else:
|
2023-12-27 15:05:30 +00:00
|
|
|
toPreserves(val)
|
2021-10-25 19:33:31 +00:00
|
|
|
when T.hasCustomPragma(unpreservable):
|
2022-10-16 20:29:27 +00:00
|
|
|
raiseAssert($T & " is unpreservable")
|
2021-10-17 09:20:17 +00:00
|
|
|
elif T.hasCustomPragma(preservesOr):
|
|
|
|
var hasKind, hasVariant: bool
|
|
|
|
for k, v in x.fieldPairs:
|
|
|
|
if k == "orKind":
|
|
|
|
assert(not hasKind)
|
|
|
|
hasKind = true
|
|
|
|
else:
|
|
|
|
assert(hasKind and not hasVariant)
|
|
|
|
result = fieldToPreserve(k, v)
|
2022-12-06 19:31:21 +00:00
|
|
|
applyEmbed(k, result)
|
2021-10-17 09:20:17 +00:00
|
|
|
hasVariant = true
|
|
|
|
elif T.hasCustomPragma(preservesRecord):
|
2023-12-27 15:05:30 +00:00
|
|
|
result = Value(kind: pkRecord)
|
2021-10-17 09:20:17 +00:00
|
|
|
for k, v in x.fieldPairs:
|
2022-12-06 19:31:21 +00:00
|
|
|
var pr = fieldToPreserve(k, v)
|
|
|
|
applyEmbed(k, pr)
|
|
|
|
result.record.add(pr)
|
2023-12-27 15:05:30 +00:00
|
|
|
result.record.add(toSymbol(T.getCustomPragmaVal(preservesRecord)))
|
2021-10-17 09:20:17 +00:00
|
|
|
elif T.hasCustomPragma(preservesTuple):
|
2023-12-27 15:05:30 +00:00
|
|
|
result = initSequence(0)
|
2021-10-17 09:20:17 +00:00
|
|
|
for label, field in x.fieldPairs:
|
|
|
|
when x.dot(label).hasCustomPragma(preservesTupleTail):
|
|
|
|
for y in field.items:
|
2023-12-27 15:05:30 +00:00
|
|
|
result.sequence.add(toPreserves(y))
|
2021-10-17 09:20:17 +00:00
|
|
|
# TODO: what if there are fields after the tail?
|
|
|
|
else:
|
2022-12-06 19:31:21 +00:00
|
|
|
var pr = fieldToPreserve(label, field)
|
|
|
|
applyEmbed(label, pr)
|
|
|
|
result.sequence.add(pr)
|
2021-10-17 09:20:17 +00:00
|
|
|
elif T.hasCustomPragma(preservesDictionary):
|
2023-12-27 15:05:30 +00:00
|
|
|
result = initDictionary()
|
2021-10-17 09:20:17 +00:00
|
|
|
for key, val in x.fieldPairs:
|
2024-01-03 12:14:46 +00:00
|
|
|
when val is Option:
|
|
|
|
if val.isSome:
|
|
|
|
var pr = fieldToPreserve(key, val.get)
|
|
|
|
applyEmbed(key, pr)
|
|
|
|
result[key.toSymbol] = pr
|
|
|
|
else:
|
|
|
|
var pr = fieldToPreserve(key, val)
|
|
|
|
applyEmbed(key, pr)
|
|
|
|
result[key.toSymbol] = pr
|
2023-07-23 07:27:01 +00:00
|
|
|
sortDict(result)
|
2022-12-07 02:22:01 +00:00
|
|
|
else:
|
2023-04-28 22:59:47 +00:00
|
|
|
{.warning: "failed to preserve object " & $T .}
|
2023-12-27 15:05:30 +00:00
|
|
|
result = toPreservesHook(x)
|
2022-12-07 02:22:01 +00:00
|
|
|
else:
|
|
|
|
{.warning: "failed to preserve " & $T .}
|
2023-12-27 15:05:30 +00:00
|
|
|
result = toPreservesHook(x)
|
2021-10-17 09:20:17 +00:00
|
|
|
# the hook doesn't compile but produces a useful error
|
|
|
|
trace T, " -> ", result
|
2021-06-02 13:51:36 +00:00
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
proc toPreserve*[T](x: T; E: typedesc = void): Value {.deprecated.} =
|
|
|
|
x.topreserves()
|
|
|
|
|
|
|
|
proc toPreservesHook*(a: Atom): Value =
|
|
|
|
result = Value(kind: a.kind)
|
2023-12-27 15:21:11 +00:00
|
|
|
case a.kind
|
|
|
|
of pkBoolean:
|
|
|
|
result.bool = a.bool
|
|
|
|
of pkFloat:
|
|
|
|
result.float = a.float
|
|
|
|
of pkDouble:
|
|
|
|
result.double = a.double
|
|
|
|
of pkRegister:
|
|
|
|
result.register = a.register
|
|
|
|
of pkBigInt:
|
|
|
|
result.bigint = a.bigint
|
|
|
|
of pkString:
|
|
|
|
result.string = a.string
|
|
|
|
of pkByteString:
|
|
|
|
result.bytes = a.bytes
|
|
|
|
of pkSymbol:
|
|
|
|
result.symbol = a.symbol
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
proc toPreservesHook*[T](set: HashSet[T]): Value =
|
2021-09-01 09:37:49 +00:00
|
|
|
## Hook for preserving ``HashSet``.
|
2023-12-27 15:05:30 +00:00
|
|
|
result = Value(kind: pkSet, set: newSeqOfCap[Value](set.len))
|
|
|
|
for e in set: result.incl toPreserves(e)
|
2023-06-11 20:26:58 +00:00
|
|
|
cannonicalize(result)
|
2021-06-24 15:31:30 +00:00
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
proc toPreservesHook*[A, B](table: Table[A, B]|TableRef[A, B]): Value =
|
2021-09-01 09:37:49 +00:00
|
|
|
## Hook for preserving ``Table``.
|
2023-12-27 15:05:30 +00:00
|
|
|
result = initDictionary()
|
2021-10-17 09:20:17 +00:00
|
|
|
for k, v in table.pairs:
|
2023-12-27 15:05:30 +00:00
|
|
|
result[toPreserves(k)] = toPreserves(v)
|
2023-07-23 07:27:01 +00:00
|
|
|
sortDict(result)
|
2021-08-11 11:00:32 +00:00
|
|
|
|
2024-01-03 12:14:46 +00:00
|
|
|
proc toPreservesHook*(o: Option): Value =
|
|
|
|
o.get.toPreserves
|
|
|
|
|
2023-12-27 15:21:11 +00:00
|
|
|
proc fromAtom*[T](v: var T; a: ATom): bool =
|
|
|
|
if T is Atom:
|
|
|
|
v = a
|
|
|
|
result = true
|
2023-12-27 15:05:30 +00:00
|
|
|
if T is Value:
|
2023-12-27 15:21:11 +00:00
|
|
|
v = a.toPreservesHook
|
|
|
|
result = true
|
|
|
|
elif T is enum:
|
|
|
|
if a.kind == pkSymbol:
|
|
|
|
try:
|
|
|
|
v = parseEnum[T](string a.symbol)
|
|
|
|
result = true
|
|
|
|
except ValueError: discard
|
|
|
|
elif T is bool:
|
|
|
|
if a.kind == pkBoolean:
|
|
|
|
v = a.bool
|
|
|
|
result = true
|
|
|
|
elif T is SomeInteger:
|
|
|
|
if a.kind == pkRegister:
|
|
|
|
result = a.register.T < high(T)
|
|
|
|
if result:
|
|
|
|
v = T a.register
|
|
|
|
elif a.kind == pkBigInt:
|
|
|
|
var o = toInt[T](a.bigint)
|
|
|
|
result = o.isSome
|
|
|
|
if result: v = o.get
|
|
|
|
elif T is seq[byte]:
|
|
|
|
if a.kind == pkByteString:
|
|
|
|
v = a.bytes
|
|
|
|
result = true
|
|
|
|
elif T is float32:
|
|
|
|
if a.kind == pkFloat:
|
|
|
|
v = a.float
|
|
|
|
result = true
|
|
|
|
elif T is float64:
|
|
|
|
case a.kind
|
|
|
|
of pkFloat:
|
|
|
|
v = a.float
|
|
|
|
result = true
|
|
|
|
of pkDouble:
|
|
|
|
v = a.double
|
|
|
|
result = true
|
|
|
|
else: discard
|
|
|
|
elif T is Ordinal | SomeInteger:
|
|
|
|
if a.kind == pkRegister:
|
|
|
|
v = T(a.register)
|
|
|
|
result = int(v) == a.register
|
|
|
|
elif a.kind == pkBigInt:
|
|
|
|
var o = toInt[T](a.bigint)
|
|
|
|
if o.isSome:
|
|
|
|
v = get o
|
|
|
|
result = true
|
|
|
|
elif T is string:
|
|
|
|
if a.kind == pkString:
|
|
|
|
v = a.string
|
|
|
|
result = true
|
|
|
|
elif T is Symbol:
|
|
|
|
if a.kind == pkSymbol:
|
|
|
|
v = a.symbol
|
|
|
|
result = true
|
|
|
|
elif T is distinct:
|
|
|
|
result = fromAtom(v.distinctBase, a)
|
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
proc fromPreserves*[T](v: var T; pr: Value): bool {.gcsafe.} =
|
|
|
|
## Inplace version of `preservesTo`. Returns ``true`` on
|
2021-09-01 09:37:49 +00:00
|
|
|
## a complete match, otherwise returns ``false``.
|
2023-12-27 15:05:30 +00:00
|
|
|
## Can be customized with `fromPreservesHook(x: T; var pr: Value): bool`.
|
|
|
|
## Any ``fromPreservesHook`` that does not compile will be discarded;
|
2021-09-23 13:20:06 +00:00
|
|
|
## *Write tests for your hooks!*
|
2021-10-25 17:07:16 +00:00
|
|
|
##
|
|
|
|
## When `tracePreserves` is defined (`-d:tracePreserves`) a diagnostic
|
|
|
|
## trace is printing during `fromPreserve`.
|
2021-08-25 11:49:35 +00:00
|
|
|
# TODO: {.raises: [].}
|
|
|
|
runnableExamples:
|
2021-10-25 17:07:16 +00:00
|
|
|
type Foo {.preservesRecord: "foo".} = object
|
2021-09-01 09:37:49 +00:00
|
|
|
x, y: int
|
2021-08-25 11:49:35 +00:00
|
|
|
var foo: Foo
|
2021-09-01 09:37:49 +00:00
|
|
|
assert(fromPreserve(foo, parsePreserves("""<foo 1 2>""")))
|
2021-08-25 11:49:35 +00:00
|
|
|
assert(foo.x == 1)
|
|
|
|
assert(foo.y == 2)
|
2023-12-27 15:05:30 +00:00
|
|
|
when T is Value:
|
2021-09-08 09:00:38 +00:00
|
|
|
v = pr
|
2021-08-25 11:49:35 +00:00
|
|
|
result = true
|
2023-12-27 15:05:30 +00:00
|
|
|
elif T is Value:
|
|
|
|
v = pr
|
2022-03-18 17:25:44 +00:00
|
|
|
result = true
|
2023-12-27 15:05:30 +00:00
|
|
|
elif compiles(fromPreservesHook(v, pr)):
|
|
|
|
result = fromPreservesHook(v, pr)
|
2021-10-17 09:20:17 +00:00
|
|
|
elif T is enum:
|
|
|
|
if pr.isSymbol:
|
|
|
|
try:
|
2022-02-20 04:33:15 +00:00
|
|
|
v = parseEnum[T](string pr.symbol)
|
2021-10-17 09:20:17 +00:00
|
|
|
result = true
|
|
|
|
except ValueError: discard
|
2021-06-24 15:31:30 +00:00
|
|
|
elif T is bool:
|
2021-09-08 09:00:38 +00:00
|
|
|
if pr.kind == pkBoolean:
|
|
|
|
v = pr.bool
|
2021-08-25 11:49:35 +00:00
|
|
|
result = true
|
2021-06-24 15:31:30 +00:00
|
|
|
elif T is SomeInteger:
|
2023-12-22 15:55:23 +00:00
|
|
|
if pr.kind == pkRegister:
|
|
|
|
v = T(pr.register)
|
2021-08-25 11:49:35 +00:00
|
|
|
result = true
|
2021-10-17 09:20:17 +00:00
|
|
|
elif T is seq[byte]:
|
|
|
|
if pr.kind == pkByteString:
|
2021-09-08 09:00:38 +00:00
|
|
|
v = pr.bytes
|
2021-08-25 11:49:35 +00:00
|
|
|
result = true
|
2021-10-17 09:20:17 +00:00
|
|
|
elif T is seq:
|
|
|
|
if pr.kind == pkSequence:
|
2021-09-08 09:00:38 +00:00
|
|
|
v.setLen(pr.len)
|
2021-08-25 11:49:35 +00:00
|
|
|
result = true
|
2021-09-08 09:00:38 +00:00
|
|
|
for i, e in pr.sequence:
|
2023-12-27 15:05:30 +00:00
|
|
|
result = result and fromPreserves(v[i], pr.sequence[i])
|
2021-10-17 09:20:17 +00:00
|
|
|
if not result:
|
|
|
|
v.setLen 0
|
|
|
|
break
|
2022-03-08 21:25:03 +00:00
|
|
|
elif T is float32:
|
|
|
|
if pr.kind == pkFloat:
|
|
|
|
v = pr.float
|
|
|
|
result = true
|
2021-06-24 15:31:30 +00:00
|
|
|
elif T is float64:
|
2021-09-08 09:00:38 +00:00
|
|
|
case pr.kind
|
2021-08-25 11:49:35 +00:00
|
|
|
of pkFloat:
|
2021-09-08 09:00:38 +00:00
|
|
|
v = pr.float
|
2021-08-25 11:49:35 +00:00
|
|
|
result = true
|
|
|
|
of pkDouble:
|
2021-09-08 09:00:38 +00:00
|
|
|
v = pr.double
|
2021-08-25 11:49:35 +00:00
|
|
|
result = true
|
2022-03-08 21:25:03 +00:00
|
|
|
else: discard
|
2021-06-28 10:30:16 +00:00
|
|
|
elif T is Ordinal | SomeInteger:
|
2023-12-22 15:55:23 +00:00
|
|
|
case pr.kind
|
|
|
|
of pkRegister:
|
|
|
|
v = (T)pr.register
|
2021-08-25 11:49:35 +00:00
|
|
|
result = true
|
2023-12-22 15:55:23 +00:00
|
|
|
of pkBigInt:
|
|
|
|
var o = toInt[T](pr.bigint)
|
|
|
|
result = o.isSome
|
|
|
|
if result: v = get o
|
|
|
|
else: discard
|
2021-06-24 15:31:30 +00:00
|
|
|
elif T is string:
|
2023-07-20 17:26:11 +00:00
|
|
|
if pr.kind == pkString:
|
2021-09-08 09:00:38 +00:00
|
|
|
v = pr.string
|
2021-08-25 11:49:35 +00:00
|
|
|
result = true
|
2022-02-20 04:33:15 +00:00
|
|
|
elif T is Symbol:
|
|
|
|
if pr.kind == pkSymbol:
|
|
|
|
v = pr.symbol
|
|
|
|
result = true
|
|
|
|
elif T is distinct:
|
|
|
|
result = fromPreserve(v.distinctBase, pr)
|
2021-10-17 09:20:17 +00:00
|
|
|
elif T is tuple:
|
|
|
|
case pr.kind
|
|
|
|
of pkRecord, pkSequence:
|
|
|
|
if pr.len <= tupleLen(T):
|
|
|
|
result = true
|
2022-06-13 04:05:35 +00:00
|
|
|
var i {.used.}: int
|
2021-10-17 09:20:17 +00:00
|
|
|
for f in fields(v):
|
|
|
|
if result and i < pr.len:
|
2023-12-27 15:05:30 +00:00
|
|
|
result = result and fromPreserves(f, pr[i])
|
2021-10-17 09:20:17 +00:00
|
|
|
inc i
|
|
|
|
of pkDictionary:
|
2023-06-11 20:29:10 +00:00
|
|
|
if tupleLen(T) <= pr.len:
|
2021-10-17 09:20:17 +00:00
|
|
|
result = true
|
|
|
|
for key, val in fieldPairs(v):
|
2023-12-27 15:05:30 +00:00
|
|
|
let pv = step(pr, toSymbol(key))
|
2022-10-25 22:42:04 +00:00
|
|
|
result =
|
2023-12-27 15:05:30 +00:00
|
|
|
if pv.isSome: fromPreserves(val, get pv)
|
2022-10-25 22:42:04 +00:00
|
|
|
else: false
|
2022-03-08 21:25:03 +00:00
|
|
|
if not result: break
|
2021-10-17 09:20:17 +00:00
|
|
|
else: discard
|
2023-12-27 15:05:30 +00:00
|
|
|
elif T is EmbeddedRef:
|
|
|
|
if pr.kind == pkEmbedded and pr.embeddedRef of T:
|
|
|
|
v = T(pr.embeddedRef)
|
|
|
|
result = true
|
2021-10-17 09:20:17 +00:00
|
|
|
elif T is ref:
|
|
|
|
if isNil(v): new(v)
|
2023-12-27 15:05:30 +00:00
|
|
|
result = fromPreserves(v[], pr)
|
2021-10-17 09:20:17 +00:00
|
|
|
elif T is object:
|
2023-12-27 15:05:30 +00:00
|
|
|
template fieldFromPreserve(key: string; val: typed; pr: Value): bool {.used.} =
|
2022-02-20 04:33:15 +00:00
|
|
|
when v.dot(key).hasCustomPragma(preservesLiteral):
|
2023-12-27 15:21:11 +00:00
|
|
|
const atom = v.dot(key).getCustomPragmaVal(preservesLiteral).parsePreservesAtom
|
2023-12-27 15:05:30 +00:00
|
|
|
pr == atom.toPreservesHook()
|
2021-10-17 09:20:17 +00:00
|
|
|
else:
|
2023-12-27 15:05:30 +00:00
|
|
|
fromPreserves(val, pr)
|
2021-10-25 19:33:31 +00:00
|
|
|
when T.hasCustomPragma(unpreservable):
|
|
|
|
raiseAssert($T & " is unpreservable")
|
2021-10-24 23:52:09 +00:00
|
|
|
elif T.hasCustomPragma(preservesRecord):
|
2021-10-17 09:20:17 +00:00
|
|
|
if pr.isRecord and pr.label.isSymbol(T.getCustomPragmaVal(preservesRecord)):
|
|
|
|
result = true
|
|
|
|
var i: int
|
2023-09-12 09:35:09 +00:00
|
|
|
for name, field in fieldPairs(v):
|
|
|
|
when v.dot(name).hasCustomPragma(preservesTupleTail):
|
|
|
|
v.dot(name).setLen(pr.record.len.pred - i)
|
|
|
|
var j: int
|
|
|
|
while result and i < pr.record.high:
|
2023-12-27 15:05:30 +00:00
|
|
|
result = result and fromPreserves(v.dot(name)[j], pr.record[i])
|
2023-09-12 09:35:09 +00:00
|
|
|
inc i
|
|
|
|
inc j
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
if result and i <= pr.len:
|
|
|
|
result = result and fieldFromPreserve(name, field, pr.record[i])
|
|
|
|
inc i
|
2023-06-11 20:29:10 +00:00
|
|
|
result = result and (i <= pr.len)
|
2021-10-17 09:20:17 +00:00
|
|
|
elif T.hasCustomPragma(preservesTuple):
|
|
|
|
if pr.isSequence:
|
|
|
|
result = true
|
|
|
|
var i: int
|
|
|
|
for name, field in fieldPairs(v):
|
|
|
|
when v.dot(name).hasCustomPragma(preservesTupleTail):
|
2021-12-23 23:38:02 +00:00
|
|
|
if pr.len >= i:
|
|
|
|
setLen(v.dot(name), pr.len - i)
|
|
|
|
var j: int
|
|
|
|
while result and i < pr.len:
|
|
|
|
result = result and fieldFromPreserve(name, v.dot(name)[j], pr.sequence[i])
|
|
|
|
inc i
|
|
|
|
inc j
|
2021-10-17 09:20:17 +00:00
|
|
|
else:
|
|
|
|
if result and i < pr.len:
|
2021-10-24 23:52:09 +00:00
|
|
|
result = result and fieldFromPreserve(name, field, pr.sequence[i])
|
2021-10-17 09:20:17 +00:00
|
|
|
inc i
|
|
|
|
result = result and (i == pr.len)
|
|
|
|
elif T.hasCustomPragma(preservesDictionary):
|
|
|
|
if pr.isDictionary:
|
|
|
|
result = true
|
|
|
|
var i: int
|
2024-01-03 12:14:46 +00:00
|
|
|
for key, field in fieldPairs(v):
|
2021-10-17 09:20:17 +00:00
|
|
|
if not result: break
|
2024-01-03 12:14:46 +00:00
|
|
|
let val = step(pr, key.toSymbol)
|
|
|
|
when field is Option:
|
|
|
|
if val.isSome:
|
|
|
|
discard fieldFromPreserve(key, v.dot(key), val.get)
|
|
|
|
else:
|
|
|
|
inc i
|
|
|
|
result = result and val.isSome
|
|
|
|
if result:
|
|
|
|
result = result and fieldFromPreserve(key, v.dot(key), val.get)
|
2023-06-11 20:29:10 +00:00
|
|
|
result = result and (i <= pr.len)
|
2021-10-17 09:20:17 +00:00
|
|
|
elif T.hasCustomPragma(preservesOr):
|
|
|
|
for kind in typeof(T.orKind):
|
|
|
|
v = T(orKind: kind)
|
|
|
|
var fieldWasFound = false
|
|
|
|
for key, val in fieldPairs(v):
|
|
|
|
when key != "orKind": # fieldPairs unwraps early
|
|
|
|
result = fieldFromPreserve(key, v.dot(key), pr)
|
|
|
|
fieldWasFound = true
|
|
|
|
break
|
|
|
|
if not fieldWasFound:
|
|
|
|
# hopefully a `discard` of-branch, so discard `pr`
|
|
|
|
result = true
|
|
|
|
if result: break
|
2022-03-08 21:25:03 +00:00
|
|
|
else:
|
|
|
|
if pr.isDictionary:
|
|
|
|
result = true
|
|
|
|
var i: int
|
|
|
|
for key, _ in fieldPairs(v):
|
|
|
|
if not result: break
|
2024-01-03 12:14:46 +00:00
|
|
|
let val = step(pr, key.toSymbol)
|
|
|
|
result = result and val.isSome
|
|
|
|
if result:
|
|
|
|
result = result and fieldFromPreserve(key, v.dot(key), val.get)
|
2022-03-08 21:25:03 +00:00
|
|
|
inc i
|
2023-06-11 20:29:10 +00:00
|
|
|
result = result and (i <= pr.len)
|
2022-03-08 21:25:03 +00:00
|
|
|
else:
|
2023-12-27 15:05:30 +00:00
|
|
|
result = fromPreservesHook(v, pr)
|
2022-03-08 21:25:03 +00:00
|
|
|
# a previous branch determined that the hook does not compile but
|
|
|
|
# calling it here explicitly produces a reasonable compiler error
|
2023-09-13 07:38:11 +00:00
|
|
|
when defined(tracePreserves):
|
|
|
|
if not result:
|
|
|
|
trace T, " !- ", pr
|
|
|
|
else:
|
|
|
|
trace T, " <- ", pr
|
2021-06-24 15:31:30 +00:00
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
proc fromPreserve*[T](v: var T; pr: Value): bool {.deprecated.} = v.frompreserves(pr)
|
|
|
|
|
|
|
|
proc preservesTo*(pr: Value; T: typedesc): Option[T] =
|
2021-06-24 15:31:30 +00:00
|
|
|
## Reverse of `toPreserve`.
|
2021-08-25 11:49:35 +00:00
|
|
|
# TODO: {.raises: [].}
|
|
|
|
runnableExamples:
|
2022-03-18 16:26:52 +00:00
|
|
|
import std/options
|
2021-10-25 17:07:16 +00:00
|
|
|
type Foo {.preservesRecord: "foo".} = object
|
2021-09-01 09:37:49 +00:00
|
|
|
x, y: int
|
2021-08-25 11:49:35 +00:00
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
assert(parsePreserves("""<foo "abc">""").preservesTo(Foo).isNone)
|
|
|
|
assert(parsePreserves("""<bar 1 2>""").preservesTo(Foo).isNone)
|
|
|
|
assert(parsePreserves("""<foo 1 2>""").preservesTo(Foo).isSome)
|
2021-08-25 11:49:35 +00:00
|
|
|
var v: T
|
2023-12-27 15:05:30 +00:00
|
|
|
if fromPreserves(v, pr):
|
2021-08-25 11:49:35 +00:00
|
|
|
result = some(move v)
|
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
proc fromPreservesHook*[T, E](v: var set[T]; pr: Value): bool =
|
2022-05-18 18:56:41 +00:00
|
|
|
## Hook for unpreserving a `set`.
|
|
|
|
if pr.kind == pkSet:
|
|
|
|
reset v
|
|
|
|
result = true
|
|
|
|
var vv: T
|
|
|
|
for e in pr.set:
|
2023-12-27 15:05:30 +00:00
|
|
|
result = fromPreserves(vv, e)
|
2022-05-18 18:56:41 +00:00
|
|
|
if result:
|
|
|
|
v.incl vv
|
|
|
|
else:
|
|
|
|
reset v
|
|
|
|
break
|
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
proc fromPreservesHook*[T, E](set: var HashSet[T]; pr: Value): bool =
|
2021-09-23 13:20:06 +00:00
|
|
|
## Hook for preserving ``HashSet``.
|
|
|
|
if pr.kind == pkSet:
|
|
|
|
result = true
|
|
|
|
set.init(pr.set.len)
|
|
|
|
var e: T
|
|
|
|
for pe in pr.set:
|
2023-12-27 15:05:30 +00:00
|
|
|
result = fromPreserves(e, pe)
|
2021-09-23 13:20:06 +00:00
|
|
|
if not result: break
|
|
|
|
set.incl(move e)
|
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
proc fromPreservesHook*[A,B](t: var (Table[A,B]|TableRef[A,B]); pr: Value): bool =
|
2021-09-08 09:00:38 +00:00
|
|
|
if pr.isDictionary:
|
2022-10-14 04:05:37 +00:00
|
|
|
when t is TableRef[A,B]:
|
|
|
|
if t.isNil: new t
|
2021-09-23 13:20:06 +00:00
|
|
|
result = true
|
|
|
|
var a: A
|
|
|
|
var b: B
|
2021-10-17 09:20:17 +00:00
|
|
|
for (k, v) in pr.dict.items:
|
2023-12-27 15:05:30 +00:00
|
|
|
result = fromPreserves(a, k) and fromPreserves(b, v)
|
2021-10-17 09:20:17 +00:00
|
|
|
if not result:
|
|
|
|
clear t
|
|
|
|
break
|
2021-09-23 13:20:06 +00:00
|
|
|
t[move a] = move b
|
2021-08-11 11:00:32 +00:00
|
|
|
|
2024-01-03 12:14:46 +00:00
|
|
|
proc fromPreservesHook*[T](opt: var Option[T]; pr: Value): bool =
|
|
|
|
opt = some(default T)
|
|
|
|
result = opt.get.fromPreserves(pr)
|
|
|
|
if not result:
|
|
|
|
opt = none(T)
|
|
|
|
|
2021-10-17 09:20:17 +00:00
|
|
|
when isMainModule:
|
|
|
|
var t: Table[int, string]
|
2023-12-27 15:05:30 +00:00
|
|
|
var pr = t.toPreservesHook()
|
|
|
|
assert fromPreservesHook(t, pr)
|
2021-10-17 09:20:17 +00:00
|
|
|
|
2024-01-02 18:53:41 +00:00
|
|
|
func step(pr, idx: Value): Option[Value] =
|
2023-12-27 15:05:30 +00:00
|
|
|
if pr.isDictionary:
|
|
|
|
for (k, v) in pr.dict.items:
|
|
|
|
if k == idx:
|
|
|
|
result = some(v)
|
|
|
|
break
|
|
|
|
elif (pr.isRecord or pr.isSequence):
|
|
|
|
var o = idx.toInt
|
|
|
|
if o.isSome:
|
|
|
|
var i = get o
|
|
|
|
if i < pr.len:
|
|
|
|
result = some(pr[i])
|
|
|
|
|
2024-01-02 18:53:41 +00:00
|
|
|
func step*(pr: Value; path: varargs[Value, toPreserves]): Option[Value] =
|
|
|
|
## Step into `pr` by index `idx`.
|
|
|
|
## Works for sequences, records, and dictionaries.
|
|
|
|
runnableExamples:
|
|
|
|
import std/options
|
|
|
|
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("""[ ]"""), 1.toPreserve) == none(Value)
|
2023-12-27 15:05:30 +00:00
|
|
|
result = some(pr)
|
|
|
|
for index in path:
|
|
|
|
if result.isSome:
|
|
|
|
result = step(result.get, index)
|
|
|
|
|
|
|
|
func step*(pr: Value; key: Symbol): Option[Value] =
|
|
|
|
## Step into dictionary by a `Symbol` key.
|
|
|
|
if pr.isDictionary:
|
|
|
|
for (k, v) in pr.dict.items:
|
|
|
|
if k.isSymbol and k.symbol == key:
|
|
|
|
result = some(v)
|
|
|
|
break
|
|
|
|
|
|
|
|
proc apply*(result: var Value; op: proc(_: var Value) {.gcsafe.}) {.gcsafe.} =
|
|
|
|
proc recurse(result: var Value) = apply(result, op)
|
2022-06-25 20:55:14 +00:00
|
|
|
op(result)
|
|
|
|
case result.kind
|
2023-12-22 15:55:23 +00:00
|
|
|
of pkBoolean, pkFloat, pkDouble, pkRegister, pkBigInt,
|
|
|
|
pkString, pkByteString, pkSymbol, pkEmbedded:
|
|
|
|
discard
|
2022-06-25 20:55:14 +00:00
|
|
|
of pkRecord:
|
|
|
|
apply(result.record, recurse)
|
|
|
|
of pkSequence:
|
|
|
|
apply(result.sequence, recurse)
|
|
|
|
of pkSet:
|
|
|
|
apply(result.set, recurse)
|
|
|
|
of pkDictionary:
|
2023-12-27 15:05:30 +00:00
|
|
|
apply(result.dict) do (e: var DictEntry):
|
2022-06-25 20:55:14 +00:00
|
|
|
recurse(e.key)
|
|
|
|
recurse(e.val)
|
2023-06-11 20:26:58 +00:00
|
|
|
cannonicalize(result)
|
2022-06-25 20:55:14 +00:00
|
|
|
|
2024-01-06 07:45:32 +00:00
|
|
|
proc mapEmbeds*[T](pr: sink Value; op: proc (x: T): Value {.gcsafe.}): Value {.gcsafe.} =
|
|
|
|
## Process all embeds in a `Value` that are of type `T`.
|
|
|
|
case pr.kind
|
|
|
|
of pkBoolean, pkFloat, pkDouble, pkRegister, pkBigInt,
|
|
|
|
pkString, pkByteString, pkSymbol:
|
|
|
|
result = cast[Value](pr)
|
|
|
|
of pkRecord:
|
|
|
|
result = Value(kind: pr.kind)
|
|
|
|
result.record = map(pr.record) do (x: Value) -> Value:
|
|
|
|
mapEmbeds(x, op)
|
|
|
|
of pkSequence:
|
|
|
|
result = Value(kind: pr.kind)
|
|
|
|
result.sequence = map(pr.sequence) do (x: Value) -> Value:
|
|
|
|
mapEmbeds(x, op)
|
|
|
|
of pkSet:
|
|
|
|
result = Value(kind: pr.kind)
|
|
|
|
result.set = map(pr.set) do (x: Value) -> Value:
|
|
|
|
mapEmbeds(x, op)
|
|
|
|
of pkDictionary:
|
|
|
|
result = Value(kind: pr.kind)
|
|
|
|
result.dict = map(pr.dict) do (e: DictEntry) -> DictEntry:
|
|
|
|
(mapEmbeds(e.key, op), mapEmbeds(e.val, op))
|
|
|
|
of pkEmbedded:
|
|
|
|
when T is Value: result = pr
|
|
|
|
else:
|
2023-12-27 15:05:30 +00:00
|
|
|
if pr.embeddedRef of T:
|
2024-01-06 07:45:32 +00:00
|
|
|
result = op(T pr.embeddedRef)
|
|
|
|
result.embedded = true
|
|
|
|
else: result = pr
|
|
|
|
when T is Value:
|
|
|
|
if result.embedded:
|
|
|
|
result = op(pr)
|
2023-06-11 20:26:58 +00:00
|
|
|
cannonicalize(result)
|
2024-01-06 07:45:32 +00:00
|
|
|
# TODO: is this necessary?
|
2022-12-07 09:23:10 +00:00
|
|
|
|
2023-12-27 15:05:30 +00:00
|
|
|
proc getOrDefault*[T, V](pr: Value; key: string; default: V): V =
|
2022-10-31 02:09:17 +00:00
|
|
|
## Retrieves the value of `pr[key]` if `pr` is a dictionary containing `key`
|
|
|
|
## or returns the `default` value.
|
|
|
|
var sym = toSymbol(key, T)
|
|
|
|
if pr.kind == pkDictionary:
|
|
|
|
for (k, v) in pr.dict:
|
|
|
|
if sym == k:
|
2023-12-27 15:05:30 +00:00
|
|
|
if fromPreserves(result, v): return
|
2022-10-31 02:09:17 +00:00
|
|
|
else: break
|
|
|
|
default
|