Compare commits

...

3 Commits

4 changed files with 11 additions and 8 deletions

View File

@ -1039,7 +1039,11 @@ proc concat[E](result: var string; pr: Preserve[E]) =
of pkString:
result.add escapeJson(pr.string)
of pkByteString:
if sequtils.any(pr.bytes, proc (b: byte): bool = b.char in {'\20'..'\21', '#'..'[', ']'..'~'}):
if pr.bytes.allIt(char(it) in {' '..'!', '#'..'~'}):
result.add("#\"")
result.add(cast[string](pr.bytes))
result.add('"')
else:
if pr.bytes.len > 64:
result.add("#[") #]#
result.add(base64.encode(pr.bytes))
@ -1051,10 +1055,6 @@ proc concat[E](result: var string; pr: Preserve[E]) =
result.add(alphabet[int(b shr 4)])
result.add(alphabet[int(b and 0xf)])
result.add('"')
else:
result.add("#\"")
result.add(cast[string](pr.bytes))
result.add('"')
of pkSymbol:
result.add(escapeJsonUnquoted(pr.symbol))
of pkRecord:

View File

@ -38,8 +38,7 @@ grammar "Preserves":
exp <- 'e' * ?('-'|'+') * +Digit
flt <- int * ((frac * exp) | frac | exp)
stringBody <- ?escape * *( +( {'\x20'..'\xff'} - {'"'} - {'\\'}) * *escape)
String <- '"' * stringBody * '"'
String <- '"' * *(escaped | (utf8.any - '"')) * '"'
ByteString <- charByteString | hexByteString | b64ByteString
charByteString <- '#' * >('"' * >(*binchar) * '"')

View File

@ -418,10 +418,13 @@ proc typeDef(scm: Schema; name: string; pat: Pattern; ty: PNode): PNode =
proc typeDef(scm: Schema; name: string; def: Definition; ty: PNode): PNode =
case def.orKind
of DefinitionKind.or:
let pragma = nn(nkPragma, ident"preservesOr")
if isSymbolEnum(scm, def):
pragma.add ident"pure"
nn(nkTypeDef,
nn(nkPragmaExpr,
name.ident.accQuote.toExport,
nn(nkPragma, ident"preservesOr")),
pragma),
embeddingParams(isEmbeddable(scm, def)),
ty)
of DefinitionKind.and:

View File

@ -9,6 +9,7 @@ const examples = [
("""[1 2 3 4]""", "\xB5\x91\x92\x93\x94\x84"),
("""[-2 -1 0 1]""", "\xB5\x9E\x9F\x90\x91\x84"),
(""""hello"""", "\xB1\x05hello"),
("""" \"hello\" """", "\xB1\x09 \"hello\" "),
("""["a" b #"c" [] #{} #t #f]""", "\xB5\xB1\x01a\xB3\x01b\xB2\x01c\xB5\x84\xB6\x84\x81\x80\x84"),
("""-257""", "\xA1\xFE\xFF"),
("""-1""", "\x9F"),