diff --git a/src/preserves.nim b/src/preserves.nim index 659538e..049579c 100644 --- a/src/preserves.nim +++ b/src/preserves.nim @@ -2,7 +2,7 @@ # SPDX-License-Identifier: Unlicense import bigints -import std/[algorithm, base64, endians, hashes, options, sets, sequtils, streams, strutils, tables, typetraits] +import std/[base64, endians, hashes, options, sets, sequtils, streams, tables, typetraits] from std/json import escapeJson, escapeJsonUnquoted from std/macros import hasCustomPragma, getCustomPragmaVal @@ -10,18 +10,16 @@ from std/macros import hasCustomPragma, getCustomPragmaVal type PreserveKind* = enum pkBoolean, pkFloat, pkDouble, pkSignedInteger, pkBigInteger, pkString, pkByteString, pkSymbol, - pkRecord, pkSequence, pkSet, pkDictionary, - pkEmbedded + pkRecord, pkSequence, pkSet, pkDictionary const atomKinds* = {pkBoolean, pkFloat, pkDouble, pkSignedInteger, pkBigInteger, pkString, pkByteString, pkSymbol} compoundKinds* = {pkRecord, pkSequence, pkSet, pkDictionary} type - DictEntry[EmbededType] = tuple[key: PreserveGen[EmbededType], val: PreserveGen[EmbededType]] + DictEntry = tuple[key: Preserve, val: Preserve] - PreserveGen*[EmbeddedType] {.acyclic.} = ref object - ## Generic ``Preserve`` type before embedding. + Preserve* {.acyclic.} = object case kind*: PreserveKind of pkBoolean: bool*: bool @@ -40,31 +38,47 @@ type of pkSymbol: symbol*: string of pkRecord: - record*: seq[PreserveGen[EmbeddedType]] # label is last + record*: seq[Preserve] # label is last of pkSequence: - sequence*: seq[PreserveGen[EmbeddedType]] + sequence*: seq[Preserve] of pkSet: - set*: seq[PreserveGen[EmbeddedType]] - # HashSet templates not hygenic enough for this type + set*: seq[Preserve] + # TODO: HashSet of pkDictionary: - dict*: seq[DictEntry[EmbeddedType]] - # Tables templates not hygenic enough for this type - of pkEmbedded: - when EmbeddedType is void: - embedded*: PreserveGen[EmbeddedType] - else: - embedded*: EmbeddedType + dict*: seq[DictEntry] + # TODO: Tables + embedded*: bool -template PreserveOf*(T: typedesc): untyped = PreserveGen[T] - ## Customize ``PreserveGen`` with an embedded type. - ## ``` - ## type MyPreserve = PreserveOf(MyEmbbededType) - ## ``` - -type - Preserve* = PreserveOf(void) - ## Type of Preserves with all embedded values - ## converted to an unembedded representation. +proc `==`*(x, y: Preserve): bool = + ## Check `x` and `y` for equivalence. + if x.kind == y.kind and x.embedded == y.embedded: + 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 proc `<`(x, y: string | seq[byte]): bool = for i in 0 .. min(x.high, y.high): @@ -72,9 +86,11 @@ proc `<`(x, y: string | seq[byte]): bool = if x[i] != y[i]: return false x.len < y.len -proc `<`*[E](x, y: PreserveGen[E]): bool = +proc `<`*(x, y: Preserve): bool = ## Preserves have a total order over Values. Check if `x` is ordered before `y`. - if x.kind != y.kind: + if x.embedded != y.embedded: + result = y.embedded + elif x.kind != y.kind: if x.kind == pkSignedInteger and y.kind == pkBigInteger: result = x.int.initBigInt < y.bigint elif x.kind == pkBigInteger and y.kind == pkSignedInteger: @@ -103,7 +119,7 @@ proc `<`*[E](x, y: PreserveGen[E]): bool = if x.record[x.record.high] < y.record[y.record.high]: return true for i in 0..") initRecord(symbol(label), args) -proc initSet*(E = void): PreserveGen[E] = PreserveGen[E](kind: pkSet) +proc initSet*(): Preserve = Preserve(kind: pkSet) ## Create a Preserves set value. -proc initDictionary*(E = void): PreserveGen[E] = PreserveGen[E](kind: pkDictionary) +proc initDictionary*(): Preserve = Preserve(kind: pkDictionary) ## Create a Preserves dictionary value. -proc len*[E](prs: PreserveGen[E]): int = - ## Return the shallow count of values in ``prs``, that is the number of +proc len*(pr: Preserve): int = + ## Return the shallow count of values in ``pr``, that is the number of ## fields in a record, items in a sequence, items in a set, or key-value pairs ## in a dictionary. - case prs.kind - of pkRecord: prs.record.len.pred - of pkSequence: prs.sequence.len - of pkSet: prs.set.len - of pkDictionary: prs.dict.len + case pr.kind + of pkRecord: pr.record.len.pred + of pkSequence: pr.sequence.len + of pkSet: pr.set.len + of pkDictionary: pr.dict.len else: 0 -iterator items*[E](prs: PreserveGen[E]): PreserveGen[E] = - ## Shallow iterator over `prs`, yield the fields in a record, +iterator items*(pr: Preserve): Preserve = + ## Shallow iterator over `pr`, yield the fields in a record, ## the items of a sequence, the items of a set, or the pairs ## of a dictionary. - case prs.kind + case pr.kind of pkRecord: - for i in 0..prs.record.high.pred: - yield prs.record[i] + for i in 0..pr.record.high.pred: + yield pr.record[i] of pkSequence: - for e in prs.sequence.items: yield e + for e in pr.sequence.items: yield e of pkSet: - for e in prs.set.items: yield e + for e in pr.set.items: yield e of pkDictionary: - for (k, v) in prs.dict.items: + for (k, v) in pr.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 isFalse*(pr: Preserve): bool = + ## Check if ``pr`` is equivalent to the zero-initialized ``Preserve``. + pr.kind == pkBoolean and pr.bool == false -proc isSymbol*[E](prs: PreserveGen[E]; sym: string): bool = - ## Check if `prs` is a Preserves symbol. - (prs.kind == pkSymbol) and (prs.symbol == sym) +proc isSymbol*(pr: Preserve; sym: string): bool = + ## Check if `pr` is a Preserves symbol. + (pr.kind == pkSymbol) and (pr.symbol == sym) -proc isRecord*[E](prs: PreserveGen[E]): bool = - ## Check if `prs` is a Preserves record. - if prs.kind == pkRecord: +proc isRecord*(pr: Preserve): bool = + ## Check if `pr` is a Preserves record. + if pr.kind == pkRecord: result = true - assert(prs.record.len > 0) + assert(pr.record.len > 0) -proc isDictionary*[E](prs: PreserveGen[E]): bool = - ## Check if `prs` is a Preserves dictionary. - prs.kind == pkDictionary +proc isDictionary*(pr: Preserve): bool = + ## Check if `pr` is a Preserves dictionary. + pr.kind == pkDictionary -proc label*[E](prs: PreserveGen[E]): PreserveGen[E] {.inline.} = +proc label*(pr: Preserve): Preserve {.inline.} = ## Return the label of record value. - prs.record[prs.record.high] + pr.record[pr.record.high] -proc arity*[E](prs: PreserveGen[E]): int {.inline.} = - ## Return the number of fields in record `prs`. - pred(prs.record.len) +proc arity*(pr: Preserve): int {.inline.} = + ## Return the number of fields in record `pr`. + pred(pr.record.len) -proc fields*[E](prs: PreserveGen[E]): seq[PreserveGen[E]] {.inline.} = +proc fields*(pr: Preserve): seq[Preserve] {.inline.} = ## Return the fields of a record value. - prs.record[0..prs.record.high.pred] + pr.record[0..pr.record.high.pred] -iterator fields*[E](prs: PreserveGen[E]): PreserveGen[E] = +iterator fields*(pr: Preserve): Preserve = ## Iterate the fields of a record value. - for i in 0.. 0: write(n.pred, i shr 8) str.write(i.uint8) - write(byteCount, prs.int) + write(byteCount, pr.int) of pkBigInteger: - doAssert(Negative notin prs.bigint.flags, "negative big integers not implemented") - var bytes = newSeqOfCap[uint8](prs.bigint.limbs.len * 4) + doAssert(Negative notin pr.bigint.flags, "negative big integers not implemented") + var bytes = newSeqOfCap[uint8](pr.bigint.limbs.len * 4) var begun = false - for i in countdown(prs.bigint.limbs.high, 0): - let limb = prs.bigint.limbs[i] + for i in countdown(pr.bigint.limbs.high, 0): + let limb = pr.bigint.limbs[i] for j in countdown(24, 0, 8): let b = uint8(limb shr j) begun = begun or (b != 0) @@ -409,118 +383,110 @@ proc write*[E](str: Stream; prs: PreserveGen[E]) = str.write(cast[string](bytes)) of pkString: str.write(0xb1'u8) - str.writeVarint(prs.string.len) - str.write(prs.string) + str.writeVarint(pr.string.len) + str.write(pr.string) of pkByteString: str.write(0xb2'u8) - str.writeVarint(prs.bytes.len) - str.write(cast[string](prs.bytes)) + str.writeVarint(pr.bytes.len) + str.write(cast[string](pr.bytes)) of pkSymbol: str.write(0xb3'u8) - str.writeVarint(prs.symbol.len) - str.write(prs.symbol) + str.writeVarint(pr.symbol.len) + str.write(pr.symbol) of pkRecord: - assert(prs.record.len > 0) + assert(pr.record.len > 0) str.write(0xb4'u8) - str.write(prs.record[prs.record.high]) - for i in 0.. 0x9c: 0xa0 else: 0x90)) of 0xa0: let len = (tag.int and 0x0f) + 1 if len <= 8: - result = Value(kind: pkSignedInteger, int: s.readUint8().BiggestInt) + result = Preserve(kind: pkSignedInteger, int: s.readUint8().BiggestInt) if (result.int and 0x80) != 0: result.int.dec(0x100) for i in 1.."""))) assert(foo.x == 1) assert(foo.y == 2) - type Value = PreserveGen[E] + type Value = Preserve when T is Value: - v = prs + v = pr result = true - elif compiles(fromPreserveHook(v, prs)): - result = fromPreserveHook(v, prs) + elif compiles(fromPreserveHook(v, pr)): + result = fromPreserveHook(v, pr) elif T is Bigint: - case prs.kind + case pr.kind of pkSignedInteger: - v = initBigint(prs.int) + v = initBigint(pr.int) result = true of pkBigInteger: - v = prs.bigint + v = pr.bigint result = true else: disard elif T is bool: - if prs.kind == pkBoolean: - v = prs.bool + if pr.kind == pkBoolean: + v = pr.bool result = true elif T is SomeInteger: - if prs.kind == pkSignedInteger: - v = T(prs.int) + if pr.kind == pkSignedInteger: + v = T(pr.int) result = true elif T is float: - if prs.kind == pkFloat: - v = prs.float + if pr.kind == pkFloat: + v = pr.float result = true elif T is seq: - if T is seq[byte] and prs.kind == pkByteString: - v = prs.bytes + if T is seq[byte] and pr.kind == pkByteString: + v = pr.bytes result = true - elif prs.kind == pkSequence: - v.setLen(prs.len) + elif pr.kind == pkSequence: + v.setLen(pr.len) result = true - for i, e in prs.sequence: + for i, e in pr.sequence: result = result and fromPreserve(v[i], e) elif T is float64: - case prs.kind + case pr.kind of pkFloat: - v = prs.float + v = pr.float result = true of pkDouble: - v = prs.double + v = pr.double result = true elif T is object | tuple: - case prs.kind + case pr.kind of pkRecord: when T.hasCustomPragma(record): - if prs.record[prs.record.high].isSymbol T.getCustomPragmaVal(record): + if pr.record[pr.record.high].isSymbol T.getCustomPragmaVal(record): result = true var i = 0 for fname, field in v.fieldPairs: - if not result or (i == prs.record.high): break - result = result and fromPreserve(field, prs.record[i]) + if not result or (i == pr.record.high): break + result = result and fromPreserve(field, pr.record[i]) inc(i) - result = result and (i == prs.record.high) # arity equivalence check= + result = result and (i == pr.record.high) # arity equivalence check= of pkDictionary: result = true var fieldCount = 0 for key, val in v.fieldPairs: inc fieldCount - for (pk, pv) in prs.dict.items: - var sym = symbol(key, E) + for (pk, pv) in pr.dict.items: + var sym = symbol(key) if sym == pk: result = result and fromPreserve(val, pv) break - result = result and prs.dict.len == fieldCount + result = result and pr.dict.len == fieldCount else: discard elif T is Ordinal | SomeInteger: - if prs.kind == pkSignedInteger: - v = (T)prs.int + if pr.kind == pkSignedInteger: + v = (T)pr.int result = true elif T is ref: - if prs != symbol("null", E): + if pr != symbol("null"): new v - result = fromPreserve(v[], prs) + result = fromPreserve(v[], pr) elif T is string: - if prs.kind == pkString: - v = prs.string + if pr.kind == pkString: + v = pr.string result = true elif T is distinct: - result = fromPreserve(result.distinctBase, prs) + result = fromPreserve(result.distinctBase, pr) else: raiseAssert("no conversion of type Preserve to " & $T) if not result: reset v -proc preserveTo*[E](prs: PreserveGen[E]; T: typedesc): Option[T] = +proc preserveTo*(pr: Preserve; T: typedesc): Option[T] = ## Reverse of `toPreserve`. ## # TODO: {.raises: [].} @@ -726,70 +688,71 @@ proc preserveTo*[E](prs: PreserveGen[E]; T: typedesc): Option[T] = assert(parsePreserves("""""").preserveTo(Foo).isNone) assert(parsePreserves("""""").preserveTo(Foo).isSome) var v: T - if fromPreserve(v, prs): + if fromPreserve(v, pr): result = some(move v) -proc fromPreserveHook*[A,B](t: var Table[A,B]|TableRef[A,B]; prs: Preserve): bool = - if prs.isDictionary: - for k, v in prs.pairs: - t[preserveTo(k, A)] = preserveTo(k, B) +proc fromPreserveHook*[A,B,E](t: var Table[A,B]|TableRef[A,B]; pr: Preserve): bool = + if pr.isDictionary: + for k, v in pr.pairs: + t[preserveTo(k, A)] = preserveTo(v, B) result = true -proc concat[E](result: var string; prs: PreserveGen[E]) = - case prs.kind: +proc concat(result: var string; pr: Preserve) = + if pr.embedded: result.add("#!") + case pr.kind: of pkBoolean: - case prs.bool + case pr.bool of false: result.add "#f" of true: result.add "#t" of pkFloat: - result.add($prs.float & "f") + result.add($pr.float & "f") of pkDouble: - result.add $prs.double + result.add $pr.double of pkSignedInteger: - result.add $prs.int + result.add $pr.int of pkBigInteger: - result.add $prs.bigint + result.add $pr.bigint of pkString: - result.add escapeJson(prs.string) + result.add escapeJson(pr.string) of pkByteString: - for b in prs.bytes: + for b in pr.bytes: if b.char notin {'\20'..'\21', '#'..'[', ']'..'~'}: result.add("#[") #]# - result.add(base64.encode(prs.bytes)) + result.add(base64.encode(pr.bytes)) result.add(']') return result.add("#\"") - result.add(cast[string](prs.bytes)) + result.add(cast[string](pr.bytes)) result.add('"') of pkSymbol: - result.add(escapeJsonUnquoted(prs.symbol)) + result.add(escapeJsonUnquoted(pr.symbol)) of pkRecord: - assert(prs.record.len > 0) + assert(pr.record.len > 0) result.add('<') - result.concat(prs.record[prs.record.high]) - for i in 0..') of pkSequence: result.add('[') - for i, val in prs.sequence: + for i, val in pr.sequence: if i > 0: result.add(' ') result.concat(val) result.add(']') of pkSet: result.add("#{") - for val in prs.set.items: + for val in pr.set.items: result.concat(val) result.add(' ') - if prs.set.len > 1: + if pr.set.len > 1: result.setLen(result.high) result.add('}') of pkDictionary: result.add('{') var i = 0 - for (key, value) in prs.dict.items: + for (key, value) in pr.dict.items: if i > 0: result.add(' ') result.concat(key) @@ -797,12 +760,6 @@ proc concat[E](result: var string; prs: PreserveGen[E]) = result.concat(value) inc i result.add('}') - of pkEmbedded: - result.add("#!") - when E is void: - result.add("#f") - else: - result.add($prs.embedded) -proc `$`*[E](prs: PreserveGen[E]): string = concat(result, prs) - ## Generate the textual representation of ``prs``. +proc `$`*(pr: Preserve): string = concat(result, pr) + ## Generate the textual representation of ``pr``. diff --git a/src/preserves/jsonhooks.nim b/src/preserves/jsonhooks.nim index 35a33a1..d6527a1 100644 --- a/src/preserves/jsonhooks.nim +++ b/src/preserves/jsonhooks.nim @@ -28,45 +28,45 @@ proc toPreserveHook*(js: JsonNode): Preserve = for i, e in js.elems: result.sequence[i] = toPreserveHook(e) -proc toJsonHook*(prs: Preserve): JsonNode = +proc fromPreserveHook*(js: var JsonNode; prs: Preserve): bool = case prs.kind: of pkBoolean: - result = newJBool(prs.bool) + js = newJBool(prs.bool) of pkFloat: - result = newJFloat(prs.float) + js = newJFloat(prs.float) of pkDouble: - result = newJFloat(prs.double) + js = newJFloat(prs.double) of pkSignedInteger: - result = newJInt(prs.int) - of pkBigInteger: - raise newException(ValueError, "cannot convert big integer to JSON") + js = newJInt(prs.int) of pkString: - result = newJString(prs.string) - of pkByteString: - raise newException(ValueError, "cannot convert bytes to JSON") + js = newJString(prs.string) of pkSymbol: case prs.symbol of "false": - result = newJBool(false) + js = newJBool(false) of "true": - result = newJBool(true) + js = newJBool(true) of "null": - result = newJNull() + js = newJNull() else: - raise newException(ValueError, "cannot convert symbol to JSON") - of pkRecord: - raise newException(ValueError, "cannot convert record to JSON") + return false of pkSequence: - result = newJArray() - for val in prs.sequence: - result.add(toJsonHook(val)) - of pkSet: - raise newException(ValueError, "cannot convert set to JSON") + js = newJArray() + js.elems.setLen(prs.sequence.len) + for i, val in prs.sequence: + if not fromPreserve(js.elems[i], val): + return false of pkDictionary: - result = newJObject() + js = newJObject() 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) - of pkEmbedded: - raise newException(ValueError, "cannot convert embedded value to JSON") + return false + var jsVal: JsonNode + if not fromPreserve(jsVal, val): return false + js[key.string] = jsVal + else: return false + true + +proc toJsonHook*(pr: Preserve): JsonNode = + if not fromPreserve(result, pr): + raise newException(ValueError, "cannot convert Preserves value to JSON") diff --git a/src/preserves/parse.nim b/src/preserves/parse.nim index 1ed35d0..15b5f63 100644 --- a/src/preserves/parse.nim +++ b/src/preserves/parse.nim @@ -89,9 +89,9 @@ proc parsePreserves*(text: string): Preserve {.gcsafe.} = pushStack Preserve(kind: pkSymbol, symbol: $0) Preserves.Embedded <- Preserves.Embedded: - pushStack Preserve( - kind: pkEmbedded, - embedded: stack.pop.value) + var v = stack.pop.value + v.embedded = true + pushStack v Preserves.Compact <- Preserves.Compact: pushStack decodePreserves(stack.pop.value.bytes) diff --git a/src/preserves/records.nim b/src/preserves/records.nim index d364487..6a95365 100644 --- a/src/preserves/records.nim +++ b/src/preserves/records.nim @@ -4,9 +4,9 @@ import std/[macros, typetraits] import ../preserves -type RecordClass*[EmbeddedType] = object +type RecordClass* = object ## Type of a preserves record. - label*: PreserveGen[EmbeddedType] + label*: Preserve arity*: Natural proc `$`*(rec: RecordClass): string = @@ -18,17 +18,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*[E](val: PreserveGen[E]): RecordClass[E] = +proc classOf*(val: Preserve): RecordClass = ## 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[E](label: val.label, arity: val.arity) + RecordClass(label: val.label, arity: val.arity) -proc classOf*[T](x: T; E = void): RecordClass[E] = +proc classOf*[T](x: T): RecordClass = ## Derive the ``RecordClass`` of ``x``. when not T.hasCustomPragma(record): {.error: "no {.record.} pragma on " & $T.} - result.label = preserves.symbol(T.getCustomPragmaVal(record), E) + result.label = preserves.symbol(T.getCustomPragmaVal(record)) for k, v in x.fieldPairs: inc(result.arity) proc classOf*(T: typedesc[tuple]): RecordClass = diff --git a/tests/test_parser.nim b/tests/test_parser.nim index c34f872..34dab8b 100644 --- a/tests/test_parser.nim +++ b/tests/test_parser.nim @@ -37,4 +37,4 @@ suite "parse": let a = encode test b = bin - check(a.toHex == b.toHex) + check(cast[string](a).toHex == b.toHex)