Embedded types

This commit is contained in:
Emery Hemingway 2021-08-28 12:48:50 +02:00
parent 41b1328e4e
commit 5ca277b8c0
7 changed files with 416 additions and 333 deletions

View File

@ -2,7 +2,7 @@
# SPDX-License-Identifier: ISC
import bigints
import std/[base64, endians, hashes, options, sets, streams, strutils, tables, typetraits]
import std/[algorithm, base64, endians, hashes, options, sets, sequtils, streams, strutils, tables, typetraits]
from std/json import escapeJson, escapeJsonUnquoted
from std/macros import hasCustomPragma, getCustomPragmaVal
@ -12,8 +12,10 @@ type
pkBoolean, pkFloat, pkDouble, pkSignedInteger, pkBigInteger, pkString,
pkByteString, pkSymbol, pkRecord, pkSequence, pkSet, pkDictionary, pkEmbedded
Preserve* {.acyclic.} = object
## Type that stores a Preserves value.
DictEntry[EmbededType] = tuple[key: PreserveGen[EmbededType], val: PreserveGen[EmbededType]]
PreserveGen*[EmbeddedType] {.acyclic.} = ref object
## Generic ``Preserve`` type before embedding.
case kind*: PreserveKind
of pkBoolean:
bool*: bool
@ -32,47 +34,39 @@ type
of pkSymbol:
symbol*: string
of pkRecord:
record*: seq[Preserve] # label is last
record*: seq[PreserveGen[EmbeddedType]] # label is last
of pkSequence:
sequence*: seq[Preserve]
sequence*: seq[PreserveGen[EmbeddedType]]
of pkSet:
set*: HashSet[Preserve]
set*: seq[PreserveGen[EmbeddedType]]
# HashSet templates not hygenic enough for this type
of pkDictionary:
dict*: Table[Preserve, Preserve]
dict*: seq[DictEntry[EmbeddedType]]
# Tables templates not hygenic enough for this type
of pkEmbedded:
embedded*: pointer
when EmbeddedType is void:
embedded*: PreserveGen[EmbeddedType]
else:
embedded*: EmbeddedType
proc assertValid*(prs: Preserve) =
case prs.kind
of pkBigInteger:
assert(BiggestInt.low.initBigInt < prs.bigint and prs.bigint < BiggestInt.high.initBigInt)
of pkRecord:
assert(prs.record.len > 0, "invalid Preserves record " & prs.repr)
assert(prs.record[prs.record.high].kind < pkRecord)
for v in prs.record: assertValid(v)
of pkSequence:
for v in prs.sequence: assertValid(v)
of pkSet:
for v in prs.set: assertValid(v)
of pkDictionary:
for key, val in prs.dict.pairs:
# assert(key.kind < pkRecord)
assertValid(key)
assertValid(val)
else:
discard
template PreserveOf*(T: typedesc): untyped = PreserveGen[T]
## Customize ``PreserveGen`` with an embedded type.
## ```
## type MyPreserve = PreserveOf(MyEmbbededType)
## ```
proc isNil*(prs: Preserve): bool =
## Check if ``prs`` is equivalent to the zero-initialized ``Preserve``.
prs.kind == pkBoolean and prs.bool == false
type
Preserve* = PreserveOf(void)
## Type of Preserves with all embedded values
## converted to an unembedded representation.
proc `<`(x, y: string | seq[byte]): bool =
for i in 0 .. min(x.high, y.high):
if x[i] < y[i]:
return true
if x[i] < y[i]: return true
if x[i] != y[i]: return false
x.len < y.len
proc `<`*(x, y: Preserve): bool =
proc `<`*[E](x, y: PreserveGen[E]): bool =
if x.kind != y.kind:
if x.kind == pkSignedInteger and y.kind == pkBigInteger:
result = x.int.initBigInt < y.bigint
@ -84,6 +78,10 @@ proc `<`*(x, y: Preserve): bool =
case x.kind
of pkBoolean:
result = (not x.bool) and y.bool
of pkFloat:
result = x.float < y.float
of pkDouble:
result = x.double < y.double
of pkSignedInteger:
result = x.int < y.int
of pkBigInteger:
@ -94,10 +92,70 @@ proc `<`*(x, y: Preserve): bool =
result = x.bytes < y.bytes
of pkSymbol:
result = x.symbol < y.symbol
else:
discard
of pkRecord:
if x.record[x.record.high] < y.record[y.record.high]: return true
for i in 0..<min(x.record.high, y.record.high):
if x.record[i] < y.record[i]: return true
if x.record[i] != y.record[i]: return false
result = x.record.len < y.record.len
of pkSequence:
for i in 0..min(x.sequence.high, y.sequence.high):
if x.sequence[i] < y.sequence[i]: return true
if x.sequence[i] != y.sequence[i]: return false
result = x.sequence.len < y.sequence.len
of pkSet:
for i in 0..min(x.set.high, y.set.high):
if x.set[i] < y.set[i]: return true
if x.set[i] != y.set[i]: return false
result = x.set.len < y.set.len
of pkDictionary:
for i in 0..min(x.dict.high, y.dict.high):
if x.dict[i].key < y.dict[i].key: return true
if x.dict[i].key == y.dict[i].key:
if x.dict[i].val < y.dict[i].val: return true
if x.dict[i].val != y.dict[i].val: return false
result = x.dict.len < y.dict.len
of pkEmbedded:
when not E is void:
result = x.embedded < y.embedded
proc hash*(prs: Preserve): Hash =
proc `==`*[E](x, y: PreserveGen[E]): bool =
# TODO: is this necessary to define?
if x.isNil or y.isNil:
result = x.isNil and y.isNil
elif x.kind == y.kind:
case x.kind
of pkBoolean:
result = x.bool == y.bool
of pkFloat:
result = x.float == y.float
of pkDouble:
result = x.double == y.double
of pkSignedInteger:
result = x.int == y.int
of pkBigInteger:
result = x.bigint == y.bigint
of pkString:
result = x.string == y.string
of pkByteString:
result = x.bytes == y.bytes
of pkSymbol:
result = x.symbol == y.symbol
of pkRecord:
result = x.record == y.record
of pkSequence:
for i, val in x.sequence:
if y.sequence[i] != val: return false
result = true
of pkSet:
result = x.set == y.set
of pkDictionary:
result = x.dict == y.dict
of pkEmbedded:
result = x.embedded == y.embedded
proc hash*[E](prs: PreserveGen[E]): Hash =
type Value = PreserveGen[E]
var h = hash(prs.kind.int)
case prs.kind
of pkBoolean:
@ -127,118 +185,79 @@ proc hash*(prs: Preserve): Hash =
for val in prs.set.items:
h = h !& hash(val)
of pkDictionary:
for (key, val) in prs.dict.pairs:
h = h !& hash(val)
for (key, val) in prs.dict.items:
h = h !& hash(key) !& hash(val)
of pkEmbedded:
h = h !& hash(prs.embedded)
when not E is void:
h = h !& hash(prs.embedded)
!$h
proc `==`*(x, y: Preserve): bool =
if x.kind == y.kind:
case x.kind
of pkBoolean:
result = x.bool == y.bool
of pkFloat:
result = x.float == y.float
of pkDouble:
result = x.double == y.double
of pkSignedInteger:
result = x.int == y.int
of pkBigInteger:
result = x.bigint == y.bigint
of pkString:
result = x.string == y.string
of pkByteString:
result = x.bytes == y.bytes
of pkSymbol:
result = x.symbol == y.symbol
of pkRecord:
result = x.record == y.record
of pkSequence:
for i, val in x.sequence:
if y.sequence[i] != val: return false
result = true
of pkSet:
for val in x.set.items:
if not y.set.contains(val): return false
for val in y.set.items:
if not x.set.contains(val): return false
result = true
of pkDictionary:
for (key, val) in x.dict.pairs:
if y.dict[key] != val: return false
result = true
of pkEmbedded:
result = x.embedded == y.embedded
proc `[]`*(prs: Preserve; i: int): Preserve =
case prs.kind
of pkRecord: prs.record[i]
of pkSequence: prs.sequence[i]
else:
raise newException(ValueError, "`[]` is not valid for " & $prs.kind)
proc concat(result: var string; prs: Preserve) =
case prs.kind:
of pkBoolean:
case prs.bool
of false: result.add "#f"
of true: result.add "#t"
of pkFloat:
result.add($prs.float & "f")
of pkDouble:
result.add $prs.double
of pkSignedInteger:
result.add $prs.int
of pkBigInteger:
result.add $prs.bigint
of pkString:
result.add escapeJson(prs.string)
of pkByteString:
for b in prs.bytes:
if b.char notin {'\20'..'\21', '#'..'[', ']'..'~'}:
result.add("#[") #]#
result.add(base64.encode(prs.bytes))
result.add(']')
return
result.add("#\"")
result.add(cast[string](prs.bytes))
result.add('"')
of pkSymbol:
result.add(escapeJsonUnquoted(prs.symbol))
of pkRecord:
assert(prs.record.len > 0)
result.add('<')
result.concat(prs.record[prs.record.high])
for i in 0..<prs.record.high:
result.add(' ')
result.concat(prs.record[i])
result.add('>')
of pkSequence:
result.add('[')
for i, val in prs.sequence:
if i > 0:
result.add(' ')
result.concat(val)
result.add(']')
of pkSet:
result.add("#{")
for val in prs.set.items:
result.concat(val)
result.add(' ')
if prs.set.len > 1:
result.setLen(result.high)
result.add('}')
of pkDictionary:
result.add('{')
var i = 0
for (key, value) in prs.dict.pairs:
if i > 0:
result.add(' ')
result.concat(key)
result.add(": ")
result.concat(value)
inc i
result.add('}')
of pkEmbedded:
result.add(prs.embedded.repr)
proc incl*[E](prs: var PreserveGen[E]; key: PreserveGen[E]) =
for i in 0..prs.set.high:
if key < prs.set[i]:
insert(prs.set, [key], i)
return
prs.set.add(key)
proc `$`*(prs: Preserve): string = concat(result, prs)
proc excl*[E](prs: var PreserveGen[E]; key: PreserveGen[E]) =
for i in 0..prs.set.high:
if prs.set[i] == key:
delete(prs.set, i, i)
break
iterator items*(prs: Preserve): Preserve =
proc `[]`*[E](prs: var PreserveGen[E]; key: PreserveGen[E]): PreserveGen[E] =
for (k, v) in prs.dict.items:
if k == key: return v
raise newException(KeyError, "value not in Preserves dictionary")
proc `[]=`*[E](prs: var PreserveGen[E]; key, val: PreserveGen[E]) =
for i in 0..prs.dict.high:
if key < prs.dict[i].key:
insert(prs.dict, [(key, val,)], i)
return
elif key == prs.dict[i].key:
prs.dict[i].val = val
return
prs.dict.add((key, val,))
proc initRecord*(label: Preserve; args: varargs[Preserve, toPreserve]): Preserve =
## Record constructor.
result = Preserve(kind: pkRecord,
record: newSeqOfCap[Preserve](1+args.len))
for arg in args:
#assertValid(arg)
result.record.add(arg)
result.record.add(label)
proc len*[E](prs: PreserveGen[E]): int =
## Return the number of values one level below ``prs``.
case prs.kind
of pkRecord: prs.record.len.pred
of pkSequence: prs.sequence.len
of pkSet: prs.set.len
of pkDictionary: prs.dict.len
else: 0
proc symbol*(s: string; E = void): PreserveGen[E] {.inline.} =
## Symbol constructor.
PreserveGen[E](kind: pkSymbol, symbol: s)
proc initRecord*(label: string; args: varargs[Preserve, toPreserve]): Preserve {.inline.} =
## Record constructor that converts ``label`` to a symbol.
initRecord(symbol(label), args)
proc initSet*(E = void): PreserveGen[E] = PreserveGen[E](kind: pkSet)
proc initDictionary*(E = void): PreserveGen[E] = PreserveGen[E](kind: pkDictionary)
iterator items*[E](prs: PreserveGen[E]): PreserveGen[E] =
case prs.kind
of pkRecord:
for i in 0..prs.record.high.pred:
@ -248,11 +267,18 @@ iterator items*(prs: Preserve): Preserve =
of pkSet:
for e in prs.set.items: yield e
of pkDictionary:
for k, v in prs.dict.pairs:
for (k, v) in prs.dict.items:
yield k # key can be an arbitrary Preserve
yield v
else: discard
proc isFalse*[E](prs: PreserveGen[E]): bool =
## Check if ``prs`` is equivalent to the zero-initialized ``Preserve``.
prs.kind == pkBoolean and prs.bool == false
proc isSymbol*(prs: Preserve; sym: string): bool =
(prs.kind == pkSymbol) and (prs.symbol == sym)
func isRecord*(prs: Preserve): bool =
if prs.kind == pkRecord:
result = true
@ -277,10 +303,6 @@ iterator fields*(prs: Preserve): Preserve =
## Iterate the fields of a record value.
for i in 0..<prs.record.high: yield prs.record[i]
proc symbol*(s: string): Preserve {.inline.} =
## Symbol constructor.
Preserve(kind: pkSymbol, symbol: s)
proc writeVarint(s: Stream; n: int) =
var n = n
while true:
@ -301,7 +323,7 @@ proc readVarint(s: Stream): int =
break
shift.inc 7
proc write*(str: Stream; prs: Preserve) =
proc write*[E](str: Stream; prs: PreserveGen[E]) =
case prs.kind:
of pkBoolean:
case prs.bool
@ -389,112 +411,114 @@ proc write*(str: Stream; prs: Preserve) =
str.write(0x84'u8)
of pkDictionary:
str.write(0xb7'u8)
for (key, value) in prs.dict.pairs:
for (key, value) in prs.dict.items:
str.write(key)
str.write(value)
str.write(0x84'u8)
of pkEmbedded:
str.write(0x86'u8)
raiseAssert("binary representation of embedded values is undefined")
when E is void:
str.write(0x80'u8)
else:
str.write(prs.embedded.toPreserve)
proc encode*(prs: Preserve): string =
proc encode*[E](prs: PreserveGen[E]): string =
let s = newStringStream()
s.write prs
s.setPosition 0
result = s.readAll
proc decodePreserves*(s: Stream): Preserve =
proc decodePreserves*(s: Stream; E = void): PreserveGen[E] =
type Value = PreserveGen[E]
proc assertStream(check: bool) =
if not check:
raise newException(ValueError, "invalid Preserves stream")
const endMarker = 0x84
let tag = s.readUint8()
case tag
of 0x80: result = Preserve(kind: pkBoolean, bool: false)
of 0x81: result = Preserve(kind: pkBoolean, bool: true)
of 0x80: result = Value(kind: pkBoolean, bool: false)
of 0x81: result = Value(kind: pkBoolean, bool: true)
of 0x82:
when system.cpuEndian == bigEndian:
result = Preserve(kind: pkFloat, float: s.readFloat32())
result = Value(kind: pkFloat, float: s.readFloat32())
else:
result = Preserve(kind: pkFloat)
result = Value(kind: pkFloat)
var be = s.readFloat32()
swapEndian32(result.float.addr, be.addr)
of 0x83:
when system.cpuEndian == bigEndian:
result = Preserve(kind: pkDouble, double: s.readFloat64())
result = Value(kind: pkDouble, double: s.readFloat64())
else:
result = Preserve(kind: pkDouble)
result = Value(kind: pkDouble)
var be = s.readFloat64()
swapEndian64(result.double.addr, be.addr)
of 0x84:
assertStream(false)
of 0x86:
result = Value(kind: pkEmbedded, embedded: decodePreserves(s, E))
of 0xb1:
result = Preserve(kind: pkString)
result = Value(kind: pkString)
let len = s.readVarint()
result.string = s.readStr(len)
of 0xb2:
result = Preserve(kind: pkByteString)
result = Value(kind: pkByteString)
let len = s.readVarint()
result.bytes = cast[seq[byte]](s.readStr(len))
of 0xb3:
let len = s.readVarint()
result = symbol(s.readStr(len))
result = Value(kind: pkSymbol, symbol: s.readStr(len))
of 0xb4:
result = Preserve(kind: pkRecord)
var label = s.decodePreserves()
result = Value(kind: pkRecord)
var label = decodePreserves(s, E)
while s.peekUint8() != endMarker:
result.record.add(s.decodePreserves())
result.record.add(label)
result.record.add decodePreserves(s, E)
result.record.add(move label)
discard s.readUint8()
of 0xb5:
result = Preserve(kind: pkSequence)
result = Value(kind: pkSequence)
while s.peekUint8() != endMarker:
result.sequence.add(s.decodePreserves())
result.sequence.add decodePreserves(s, E)
discard s.readUint8()
of 0xb6:
result = Preserve(kind: pkSet)
result = Value(kind: pkSet)
while s.peekUint8() != endMarker:
result.set.incl(s.decodePreserves())
incl(result, decodePreserves(s, E))
discard s.readUint8()
of 0xb7:
result = Preserve(kind: pkDictionary)
result = Value(kind: pkDictionary)
while s.peekUint8() != endMarker:
let key = s.decodePreserves()
let val = s.decodePreserves()
result.dict[key] = val
result[decodePreserves(s, E)] = decodePreserves(s, E)
discard s.readUint8()
of 0xb0:
let len = s.readVarint()
result = Preserve(kind: pkBigInteger, bigint: initBigint 0)
result = Value(kind: pkBigInteger, bigint: initBigint 0)
for _ in 1..len:
result.bigint = (result.bigint shl 8) + s.readUint8().int32
of endMarker:
assertStream(false)
else:
case 0xf0 and tag
of 0x90:
var n = tag.BiggestInt
result = Preserve(kind: pkSignedInteger,
result = Value(kind: pkSignedInteger,
int: n - (if n > 0x9c: 0xa0 else: 0x90))
of 0xa0:
let len = (tag.int and 0x0f) + 1
if len <= 8:
result = Preserve(kind: pkSignedInteger, int: s.readUint8().BiggestInt)
result = Value(kind: pkSignedInteger, int: s.readUint8().BiggestInt)
if (result.int and 0x80) != 0: result.int.dec(0x100)
for i in 1..<len:
result.int = (result.int shl 8) or s.readUint8().BiggestInt
else:
result = Preserve(kind: pkBigInteger)
result = Value(kind: pkBigInteger)
for i in 0..<len:
result.bigint = (result.bigint shl 8) + s.readUint8().int32
else:
assertStream(false)
proc decodePreserves*(s: string): Preserve =
s.newStringStream.decodePreserves
proc decodePreserves*(s: string, E = void): PreserveGen[E] =
s.newStringStream.decodePreserves E
proc decodePreserves*(s: seq[byte]): Preserve =
cast[string](s).newStringStream.decodePreserves
proc initDictionary*(): Preserve = Preserve(kind: pkDictionary)
proc decodePreserves*(s: seq[byte], E = void): PreserveGen[E] =
cast[string](s).decodePreserves E
template record*(label: string) {.pragma.}
## Serialize this object or tuple as a record.
@ -509,47 +533,48 @@ template record*(label: string) {.pragma.}
template unpreservable*() {.pragma.}
## Pragma to forbid a type from being converted by `toPreserve`.
proc toPreserve*[T](x: T): Preserve =
proc toPreserve*[T](x: T; E = void): PreserveGen[E] =
## Serializes `x` to Preserves; uses `toPreserveHook(x: T)` if it's in scope to
## customize serialization.
when T is Preserve: result = x
type Value = PreserveGen[E]
when (T is Value): result = x
elif T is E: result = Value(kind: pkEmbedded, embedded: x)
elif compiles(toPreserveHook(x)):
result = toPreserveHook(x)
elif T is Bigint:
result = Preserve(kind: pkBigInteger, bigint: x)
result = Value(kind: pkBigInteger, bigint: x)
elif T is seq[byte]:
result = Preserve(kind: pkByteString, bytes: x)
result = Value(kind: pkByteString, bytes: x)
elif T is array | seq:
result = Preserve(kind: pkSequence)
for v in x.items: result.sequence.add(toPreserve(v))
result = Value(kind: pkSequence)
for v in x.items: result.sequence.add(toPreserve(v, E))
elif T is bool:
result = Preserve(kind: pkBoolean, bool: x)
result = Value(kind: pkBoolean, bool: x)
elif T is distinct:
result = toPreserve(x.distinctBase)
elif T is float:
result = Preserve(kind: pkFloat, float: x)
result = Value(kind: pkFloat, float: x)
elif T is float64:
result = Preserve(kind: pkDouble, double: x)
result = Value(kind: pkDouble, double: x)
elif T is object | tuple:
when T.hasCustomPragma(unpreservable): {.fatal: "unpreservable type".}
elif T.hasCustomPragma(record):
result = Preserve(kind: pkRecord)
result = Value(kind: pkRecord)
for _, f in x.fieldPairs: result.record.add(toPreserve(f))
result.record.add(symbol(T.getCustomPragmaVal(record)))
else:
result = Preserve(kind: pkDictionary)
for k, v in x.fieldPairs: result.dict[symbol(k)] = toPreserve(v)
result = Value(kind: pkDictionary)
for k, v in x.fieldPairs:
result[symbol(k, E)] = toPreserve(v, E)
elif T is Ordinal:
result = Preserve(kind: pkSignedInteger, int: x.ord.BiggestInt)
result = Value(kind: pkSignedInteger, int: x.ord.BiggestInt)
elif T is ptr | ref:
if system.`==`(x, nil): result = symbol"null"
if system.`==`(x, nil): result = symbol("null", E)
else: result = toPreserve(x[])
elif T is string:
result = Preserve(kind: pkString, string: x)
result = Value(kind: pkString, string: x)
elif T is SomeInteger:
result = Preserve(kind: pkSignedInteger, int: x.BiggestInt)
elif compiles(%x):
result = %x
result = Value(kind: pkSignedInteger, int: x.BiggestInt)
else:
raiseAssert("unpreservable type" & $T)
@ -558,9 +583,9 @@ proc toPreserveHook*[T](set: HashSet[T]): Preserve =
proc toPreserveHook*[A,B](table: Table[A,B]|TableRef[A,B]): Preserve =
result = Preserve(kind: pkDictionary, dict: initTable[Preserve, Preserve](table.len))
for k, v in table.pairs: result.dict[toPreserve k] = toPreserve v
for k, v in table.pairs: result.dict.add((toPreserve(k), toPreserve(v),))
proc fromPreserve*[T](v: var T; prs: Preserve): bool =
proc fromPreserve*[E,T](v: var T; prs: PreserveGen[E]): bool =
## Inplace version of `preserveTo`.
## Partial matches on compond values may leave artifacts in ``v``.
# TODO: {.raises: [].}
@ -574,12 +599,12 @@ proc fromPreserve*[T](v: var T; prs: Preserve): bool =
assert(foo.x == 1)
assert(foo.y == 2)
assert(foo.z == 3)
when compiles(fromPreserveHook(v, prs)):
result = fromPreserveHook(v, prs)
elif T is Preserve:
type Value = PreserveGen[E]
when T is Value:
v = prs
result = true
elif compiles(fromPreserveHook(v, prs)):
result = fromPreserveHook(v, prs)
elif T is Bigint:
case prs.kind
of pkSignedInteger:
@ -622,8 +647,7 @@ proc fromPreserve*[T](v: var T; prs: Preserve): bool =
case prs.kind
of pkRecord:
when T.hasCustomPragma(record):
const label = symbol(T.getCustomPragmaVal(record))
if prs.record[prs.record.high] == label:
if prs.record[prs.record.high].isSymbol T.getCustomPragmaVal(record):
result = true
var i = 0
for fname, field in v.fieldPairs:
@ -633,15 +657,22 @@ proc fromPreserve*[T](v: var T; prs: Preserve): bool =
result = result and (i == prs.record.high) # arity equivalence check=
of pkDictionary:
result = true
var fieldCount = 0
for key, val in v.fieldPairs:
result = result and fromPreserve(val, prs.dict.getOrDefault(symbol(key)))
inc fieldCount
for (pk, pv) in prs.dict.items:
var sym = symbol(key, E)
if sym == pk:
result = result and fromPreserve(val, pv)
break
result = result and prs.dict.len == fieldCount
else: discard
elif T is Ordinal | SomeInteger:
if prs.kind == pkSignedInteger:
v = (T)prs.int
result = true
elif T is ref:
if prs != symbol"null":
if prs != symbol("null", E):
new v
result = fromPreserve(v[], prs)
elif T is string:
@ -653,7 +684,7 @@ proc fromPreserve*[T](v: var T; prs: Preserve): bool =
else:
raiseAssert("no conversion of type Preserve to " & $T)
proc preserveTo*(prs: Preserve; T: typedesc): Option[T] =
proc preserveTo*[E](prs: PreserveGen[E]; T: typedesc): Option[T] =
## Reverse of `toPreserve`.
# TODO: {.raises: [].}
runnableExamples:
@ -674,31 +705,73 @@ proc fromPreserveHook*[A,B](t: var Table[A,B]|TableRef[A,B]; prs: Preserve): boo
t[preserveTo(k,A)] = preserveTo(k,B)
result = true
proc len*(prs: Preserve): int =
## Return the number of values one level below ``prs``.
case prs.kind
of pkRecord: prs.record.len.pred
of pkSequence: prs.sequence.len
of pkSet: prs.set.len
of pkDictionary: prs.dict.len
else: 0
proc concat[E](result: var string; prs: PreserveGen[E]) =
case prs.kind:
of pkBoolean:
case prs.bool
of false: result.add "#f"
of true: result.add "#t"
of pkFloat:
result.add($prs.float & "f")
of pkDouble:
result.add $prs.double
of pkSignedInteger:
result.add $prs.int
of pkBigInteger:
result.add $prs.bigint
of pkString:
result.add escapeJson(prs.string)
of pkByteString:
for b in prs.bytes:
if b.char notin {'\20'..'\21', '#'..'[', ']'..'~'}:
result.add("#[") #]#
result.add(base64.encode(prs.bytes))
result.add(']')
return
result.add("#\"")
result.add(cast[string](prs.bytes))
result.add('"')
of pkSymbol:
result.add(escapeJsonUnquoted(prs.symbol))
of pkRecord:
assert(prs.record.len > 0)
result.add('<')
result.concat(prs.record[prs.record.high])
for i in 0..<prs.record.high:
result.add(' ')
result.concat(prs.record[i])
result.add('>')
of pkSequence:
result.add('[')
for i, val in prs.sequence:
if i > 0:
result.add(' ')
result.concat(val)
result.add(']')
of pkSet:
result.add("#{")
for val in prs.set.items:
result.concat(val)
result.add(' ')
if prs.set.len > 1:
result.setLen(result.high)
result.add('}')
of pkDictionary:
result.add('{')
var i = 0
for (key, value) in prs.dict.items:
if i > 0:
result.add(' ')
result.concat(key)
result.add(": ")
result.concat(value)
inc i
result.add('}')
of pkEmbedded:
result.add("#!")
when E is void:
result.add("#f")
else:
result.add($prs.embedded)
proc `[]`*(prs: Preserve; i: int): Preserve =
case prs.kind
of pkRecord: prs.record[i]
of pkSequence: prs.sequence[i]
else:
raise newException(ValueError, "`[]` is not valid for " & $prs.kind)
proc initRecord*(label: Preserve; args: varargs[Preserve, toPreserve]): Preserve =
## Record constructor.
result = Preserve(kind: pkRecord,
record: newSeqOfCap[Preserve](1+args.len))
for arg in args:
assertValid(arg)
result.record.add(arg)
result.record.add(label)
proc initRecord*(label: string; args: varargs[Preserve, toPreserve]): Preserve {.inline.} =
## Record constructor that converts ``label`` to a symbol.
initRecord(symbol(label), args)
proc `$`*[E](prs: PreserveGen[E]): string = concat(result, prs)

View File

@ -21,7 +21,7 @@ proc toPreserveHook*(js: JsonNode): Preserve =
of JObject:
result = Preserve(kind: pkDictionary)
for key, val in js.fields.pairs:
result.dict[Preserve(kind: pkString, string: key)] = toPreserveHook(val)
result[Preserve(kind: pkString, string: key)] = toPreserveHook(val)
of JArray:
result = Preserve(kind: pkSequence,
sequence: newSeq[Preserve](js.elems.len))
@ -64,7 +64,7 @@ proc toJsonHook*(prs: Preserve): JsonNode =
raise newException(ValueError, "cannot convert set to JSON")
of pkDictionary:
result = newJObject()
for (key, val) in prs.dict.pairs:
for (key, val) in prs.dict.items:
if key.kind != pkString:
raise newException(ValueError, "cannot convert non-string dictionary key to JSON")
result[key.string] = toJsonHook(val)

View File

@ -1,3 +1,4 @@
# SPDX-FileCopyrightText: ☭ 2021 Emery Hemingway
# SPDX-License-Identifier: ISC
import std/[base64, parseutils, sets, strutils, tables]
@ -12,87 +13,95 @@ proc shrink(stack: var Stack; n: int) = stack.setLen(stack.len - n)
template pushStack(v: Preserve) = stack.add((v, capture[0].si))
const pegParser = peg("Document", stack: Stack):
# Override rules from pegs.nim
Document <- Preserves.Document
Preserves.Record <- Preserves.Record:
var
record: seq[Preserve]
labelOff: int
while stack[labelOff].pos < capture[0].si:
inc labelOff
for i in labelOff.succ..stack.high:
record.add(move stack[i].value)
record.add(move stack[labelOff].value)
stack.shrink record.len
pushStack Preserve(kind: pkRecord, record: move record)
Preserves.Sequence <- Preserves.Sequence:
var sequence: seq[Preserve]
for frame in stack.mitems:
if frame.pos > capture[0].si:
sequence.add(move frame.value)
stack.shrink sequence.len
pushStack Preserve(kind: pkSequence, sequence: move sequence)
Preserves.Dictionary <- Preserves.Dictionary:
var dict: Table[Preserve, Preserve]
for i in countDown(stack.high.pred, 0, 2):
if stack[i].pos < capture[0].si: break
dict[move stack[i].value] = move stack[i.succ].value
stack.shrink 2*dict.len
pushStack Preserve(kind: pkDictionary, dict: move dict)
Preserves.Set <- Preserves.Set:
var set: HashSet[Preserve]
for frame in stack.mitems:
if frame.pos > capture[0].si:
set.incl(move frame.value)
stack.shrink set.len
pushStack Preserve(kind: pkSet, set: move set)
Preserves.Boolean <- Preserves.Boolean:
case $0
of "#f": pushStack Preserve(kind: pkBoolean)
of "#t": pushStack Preserve(kind: pkBoolean, bool: true)
else: discard
Preserves.Float <- Preserves.Float:
pushStack Preserve(kind: pkFloat, float: parseFloat($1))
Preserves.Double <- Preserves.Double:
pushStack Preserve(kind: pkDouble)
let i = stack.high
discard parseBiggestFloat($0, stack[i].value.double)
Preserves.SignedInteger <- Preserves.SignedInteger:
pushStack Preserve(kind: pkSignedInteger, int: parseInt($0))
Preserves.String <- Preserves.String:
pushStack Preserve(kind: pkString, string: unescape($0))
Preserves.charByteString <- Preserves.charByteString:
let s = unescape($1)
pushStack Preserve(kind: pkByteString, bytes: cast[seq[byte]](s))
Preserves.hexByteString <- Preserves.hexByteString:
pushStack Preserve(kind: pkByteString, bytes: cast[seq[byte]](parseHexStr($1)))
Preserves.b64ByteString <- Preserves.b64ByteString:
pushStack Preserve(kind: pkByteString, bytes: cast[seq[byte]](base64.decode($1)))
Preserves.Symbol <- Preserves.Symbol:
pushStack Preserve(kind: pkSymbol, symbol: $0)
Preserves.Compact <- Preserves.Compact:
pushStack decodePreserves(stack.pop.value.bytes)
proc parsePreserves*(text: string): Preserve {.gcsafe.} =
const pegParser = peg("Document", stack: Stack):
# Override rules from pegs.nim
Document <- Preserves.Document
Preserves.Record <- Preserves.Record:
var
record: seq[Preserve]
labelOff: int
while stack[labelOff].pos < capture[0].si:
inc labelOff
for i in labelOff.succ..stack.high:
record.add(move stack[i].value)
record.add(move stack[labelOff].value)
stack.shrink record.len
pushStack Preserve(kind: pkRecord, record: move record)
Preserves.Sequence <- Preserves.Sequence:
var sequence: seq[Preserve]
for frame in stack.mitems:
if frame.pos > capture[0].si:
sequence.add(move frame.value)
stack.shrink sequence.len
pushStack Preserve(kind: pkSequence, sequence: move sequence)
Preserves.Dictionary <- Preserves.Dictionary:
var prs = Preserve(kind: pkDictionary)
for i in countDown(stack.high.pred, 0, 2):
if stack[i].pos < capture[0].si: break
prs[move stack[i].value] = stack[i.succ].value
stack.shrink prs.dict.len*2
pushStack prs
Preserves.Set <- Preserves.Set:
var prs = Preserve(kind: pkSet)
for frame in stack.mitems:
if frame.pos > capture[0].si:
prs.incl(move frame.value)
stack.shrink prs.set.len
pushStack prs
Preserves.Boolean <- Preserves.Boolean:
case $0
of "#f": pushStack Preserve(kind: pkBoolean)
of "#t": pushStack Preserve(kind: pkBoolean, bool: true)
else: discard
Preserves.Float <- Preserves.Float:
pushStack Preserve(kind: pkFloat, float: parseFloat($1))
Preserves.Double <- Preserves.Double:
pushStack Preserve(kind: pkDouble)
let i = stack.high
discard parseBiggestFloat($0, stack[i].value.double)
Preserves.SignedInteger <- Preserves.SignedInteger:
pushStack Preserve(kind: pkSignedInteger, int: parseInt($0))
Preserves.String <- Preserves.String:
pushStack Preserve(kind: pkString, string: unescape($0))
Preserves.charByteString <- Preserves.charByteString:
let s = unescape($1)
pushStack Preserve(kind: pkByteString, bytes: cast[seq[byte]](s))
Preserves.hexByteString <- Preserves.hexByteString:
pushStack Preserve(kind: pkByteString, bytes: cast[seq[byte]](parseHexStr($1)))
Preserves.b64ByteString <- Preserves.b64ByteString:
pushStack Preserve(kind: pkByteString, bytes: cast[seq[byte]](base64.decode($1)))
Preserves.Symbol <- Preserves.Symbol:
pushStack Preserve(kind: pkSymbol, symbol: $0)
Preserves.Embedded <- Preserves.Embedded:
pushStack Preserve(
kind: pkEmbedded,
embedded: stack.pop.value)
Preserves.Compact <- Preserves.Compact:
pushStack decodePreserves(stack.pop.value.bytes)
var stack: Stack
let match = pegParser.match(text, stack)
if not match.ok:
raise newException(ValueError, "failed to parse Preserves:\n" & text[match.matchMax..text.high])
assert(stack.len == 1)
stack.pop.value
when isMainModule:
assert(parsePreserves("#f") == Preserve())

View File

@ -3,9 +3,9 @@
import std/[macros, typetraits]
import ../preserves
type RecordClass* = object
type RecordClass*[EmbeddedType] = object
## Type of a preserves record.
label*: Preserve
label*: PreserveGen[EmbeddedType]
arity*: Natural
proc `$`*(rec: RecordClass): string =
@ -17,17 +17,17 @@ proc isClassOf*(rec: RecordClass; val: Preserve): bool =
assert(val.record.len > 0)
result = val.label == rec.label and rec.arity == val.arity
proc classOf*(val: Preserve): RecordClass =
proc classOf*[E](val: PreserveGen[E]): RecordClass[E] =
## Derive the ``RecordClass`` of ``val``.
if val.kind != pkRecord:
raise newException(Defect, "cannot derive class of non-record value " & $val)
assert(val.record.len > 0)
RecordClass(label: val.label, arity: val.arity)
RecordClass[E](label: val.label, arity: val.arity)
proc classOf*[T](x: T): RecordClass =
proc classOf*[T](x: T; E = void): RecordClass[E] =
## Derive the ``RecordClass`` of ``x``.
when not T.hasCustomPragma(record): {.error: "no {.record.} pragma on " & $T.}
result.label = preserves.symbol(T.getCustomPragmaVal(record))
result.label = preserves.symbol(T.getCustomPragmaVal(record), E)
for k, v in x.fieldPairs: inc(result.arity)
proc classOf*(T: typedesc[tuple]): RecordClass =

View File

@ -61,7 +61,7 @@ type
Schema* = ref object
version*: int
embeddedType*: Preserve
embeddedType*: string
definitions*: OrderedTable[string, SchemaNode]
ParseState = object
@ -146,8 +146,8 @@ proc `$`*(n: SchemaNode): string =
proc `$`*(scm: Schema): string =
result.add("version = $1 .\n" % $scm.version)
if not scm.embeddedType.isNil:
result.add("EmbeddedTypeName = $1 .\n" % $scm.embeddedType)
if scm.embeddedType != "":
result.add("EmbeddedTypeName = $1 .\n" % scm.embeddedType)
for n, d in scm.definitions.pairs:
result.add("$1 = $2 .\n" % [n, $d])
@ -214,8 +214,8 @@ const parser = peg("Schema", p: ParseState):
discard parseInt($1, p.schema.version)
EmbeddedTypeName <- "embeddedType" * S * >("#f" | Ref):
if not p.schema.embeddedType.isNil: fail()
if $1 != "#f": p.schema.embeddedType = symbol($1)
if p.schema.embeddedType != "": fail()
if $1 != "#f": p.schema.embeddedType = $1
Include <- "include" * S * >(+Alnum):
# TODO: may be relative or absolute

View File

@ -1,6 +1,7 @@
# SPDX-FileCopyrightText: 2021 ☭ Emery Hemingway
# SPDX-License-Identifier: ISC
import std/[options, streams, strutils, unittest]
import std/[options, unittest]
import bigints, preserves, preserves/records
suite "conversions":

View File

@ -2,7 +2,7 @@
# SPDX-License-Identifier: ISC
import preserves, preserves/jsonhooks
import std/[json,jsonutils,streams, unittest]
import std/[json, jsonutils, streams, unittest]
let testVectors = [
"""