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

View File

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

View File

@ -1,3 +1,4 @@
# SPDX-FileCopyrightText: ☭ 2021 Emery Hemingway
# SPDX-License-Identifier: ISC # SPDX-License-Identifier: ISC
import std/[base64, parseutils, sets, strutils, tables] import std/[base64, parseutils, sets, strutils, tables]
@ -12,7 +13,8 @@ proc shrink(stack: var Stack; n: int) = stack.setLen(stack.len - n)
template pushStack(v: Preserve) = stack.add((v, capture[0].si)) template pushStack(v: Preserve) = stack.add((v, capture[0].si))
const pegParser = peg("Document", stack: Stack): proc parsePreserves*(text: string): Preserve {.gcsafe.} =
const pegParser = peg("Document", stack: Stack):
# Override rules from pegs.nim # Override rules from pegs.nim
Document <- Preserves.Document Document <- Preserves.Document
@ -38,20 +40,20 @@ const pegParser = peg("Document", stack: Stack):
pushStack Preserve(kind: pkSequence, sequence: move sequence) pushStack Preserve(kind: pkSequence, sequence: move sequence)
Preserves.Dictionary <- Preserves.Dictionary: Preserves.Dictionary <- Preserves.Dictionary:
var dict: Table[Preserve, Preserve] var prs = Preserve(kind: pkDictionary)
for i in countDown(stack.high.pred, 0, 2): for i in countDown(stack.high.pred, 0, 2):
if stack[i].pos < capture[0].si: break if stack[i].pos < capture[0].si: break
dict[move stack[i].value] = move stack[i.succ].value prs[move stack[i].value] = stack[i.succ].value
stack.shrink 2*dict.len stack.shrink prs.dict.len*2
pushStack Preserve(kind: pkDictionary, dict: move dict) pushStack prs
Preserves.Set <- Preserves.Set: Preserves.Set <- Preserves.Set:
var set: HashSet[Preserve] var prs = Preserve(kind: pkSet)
for frame in stack.mitems: for frame in stack.mitems:
if frame.pos > capture[0].si: if frame.pos > capture[0].si:
set.incl(move frame.value) prs.incl(move frame.value)
stack.shrink set.len stack.shrink prs.set.len
pushStack Preserve(kind: pkSet, set: move set) pushStack prs
Preserves.Boolean <- Preserves.Boolean: Preserves.Boolean <- Preserves.Boolean:
case $0 case $0
@ -86,13 +88,20 @@ const pegParser = peg("Document", stack: Stack):
Preserves.Symbol <- Preserves.Symbol: Preserves.Symbol <- Preserves.Symbol:
pushStack Preserve(kind: pkSymbol, symbol: $0) pushStack Preserve(kind: pkSymbol, symbol: $0)
Preserves.Embedded <- Preserves.Embedded:
pushStack Preserve(
kind: pkEmbedded,
embedded: stack.pop.value)
Preserves.Compact <- Preserves.Compact: Preserves.Compact <- Preserves.Compact:
pushStack decodePreserves(stack.pop.value.bytes) pushStack decodePreserves(stack.pop.value.bytes)
proc parsePreserves*(text: string): Preserve {.gcsafe.} =
var stack: Stack var stack: Stack
let match = pegParser.match(text, stack) let match = pegParser.match(text, stack)
if not match.ok: if not match.ok:
raise newException(ValueError, "failed to parse Preserves:\n" & text[match.matchMax..text.high]) raise newException(ValueError, "failed to parse Preserves:\n" & text[match.matchMax..text.high])
assert(stack.len == 1) assert(stack.len == 1)
stack.pop.value stack.pop.value
when isMainModule:
assert(parsePreserves("#f") == Preserve())

View File

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

View File

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

View File

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

View File

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