Compare commits
No commits in common. "main" and "@preserves/core@0.992.4" have entirely different histories.
main
...
@preserves
1
Makefile
1
Makefile
|
@ -22,3 +22,4 @@ test-all:
|
|||
(cd implementations/javascript; npm test)
|
||||
(cd implementations/python; make test)
|
||||
(cd implementations/racket/preserves; make testonly)
|
||||
(cd implementations/rust; cargo test)
|
||||
|
|
2
NOTICE
2
NOTICE
|
@ -1,2 +1,2 @@
|
|||
Preserves: an Expressive Data Language
|
||||
Copyright 2018-2024 Tony Garnock-Jones
|
||||
Copyright 2018-2022 Tony Garnock-Jones
|
||||
|
|
|
@ -39,11 +39,11 @@ automatic, perfect-fidelity conversion between syntaxes.
|
|||
#### Implementations of the data model, plus Preserves textual and binary transfer syntax
|
||||
|
||||
| Language[^pre-alpha-implementations] | Code | Package | Docs |
|
||||
|--------------------------------------|------------------------------------------------------------------------------|--------------------------------------------------------------------------------|-------------------------------------------|
|
||||
|-----------------------|------------------------------------------------------------------------------|--------------------------------------------------------------------------------|-------------------------------------------|
|
||||
| Nim | [git.syndicate-lang.org](https://git.syndicate-lang.org/ehmry/preserves-nim) | | |
|
||||
| Python | [preserves.dev]({{page.projecttree}}/implementations/python/) | [`pip install preserves`](https://pypi.org/project/preserves/) | [docs](python/latest/) |
|
||||
| Racket | [preserves.dev]({{page.projecttree}}/implementations/racket/preserves/) | [`raco pkg install preserves`](https://pkgs.racket-lang.org/package/preserves) | |
|
||||
| Rust | [preserves.dev](https://gitlab.com/preserves/preserves-rs/) | [`cargo add preserves`](https://crates.io/crates/preserves) | [docs](https://docs.rs/preserves/latest/) |
|
||||
| Rust | [preserves.dev]({{page.projecttree}}/implementations/rust/) | [`cargo add preserves`](https://crates.io/crates/preserves) | [docs](https://docs.rs/preserves/latest/) |
|
||||
| Squeak Smalltalk | [SqueakSource](https://squeaksource.com/Preserves.html) | `Installer ss project: 'Preserves';`<br>` install: 'Preserves'` | |
|
||||
| TypeScript/JavaScript | [preserves.dev]({{page.projecttree}}/implementations/javascript/) | [`yarn add @preserves/core`](https://www.npmjs.com/package/@preserves/core) | |
|
||||
|
||||
|
@ -88,6 +88,6 @@ Tony Garnock-Jones <tonyg@leastfixedpoint.com>
|
|||
The contents of this repository are made available to you under the
|
||||
[Apache License, version 2.0](LICENSE)
|
||||
(<http://www.apache.org/licenses/LICENSE-2.0>), and are Copyright
|
||||
2018-2024 Tony Garnock-Jones.
|
||||
2018-2022 Tony Garnock-Jones.
|
||||
|
||||
## Notes
|
||||
|
|
|
@ -105,7 +105,7 @@ A few more interesting differences:
|
|||
{"dictionaries": "as keys???"}: "well, why not?"}
|
||||
```
|
||||
|
||||
Preserves technically provides various types of numbers:
|
||||
Preserves technically provides a few types of numbers:
|
||||
|
||||
```
|
||||
# Signed Integers
|
||||
|
@ -114,6 +114,9 @@ Preserves technically provides various types of numbers:
|
|||
5907212309572059846509324862304968273468909473609826340
|
||||
-5907212309572059846509324862304968273468909473609826340
|
||||
|
||||
# Floats (Single-precision IEEE floats) (notice the trailing f)
|
||||
3.1415927f
|
||||
|
||||
# Doubles (Double-precision IEEE floats)
|
||||
3.141592653589793
|
||||
```
|
||||
|
|
|
@ -13,5 +13,5 @@ defaults:
|
|||
layout: page
|
||||
|
||||
title: "Preserves"
|
||||
version_date: "March 2024"
|
||||
version: "0.995.0"
|
||||
version_date: "October 2023"
|
||||
version: "0.992.0"
|
||||
|
|
|
@ -1,9 +1,5 @@
|
|||
[
|
||||
{"version":"0.995.1","title":"0.995.1","aliases":["latest"]},
|
||||
{"version":"0.995.0","title":"0.995.0","aliases":[]},
|
||||
{"version":"0.994.0","title":"0.994.0","aliases":[]},
|
||||
{"version":"0.993.0","title":"0.993.0","aliases":[]},
|
||||
{"version":"0.992.2","title":"0.992.2","aliases":[]},
|
||||
{"version":"0.992.2","title":"0.992.2","aliases":["latest"]},
|
||||
{"version":"0.992.1","title":"0.992.1","aliases":[]},
|
||||
{"version":"0.992.0","title":"0.992.0","aliases":[]},
|
||||
{"version":"0.991.0","title":"0.991.0","aliases":[]},
|
||||
|
|
|
@ -5,8 +5,9 @@ For a value `V`, we write `«V»` for the binary encoding of `V`.
|
|||
«#t» = [0x81]
|
||||
|
||||
«@W V» = [0x85] ++ «W» ++ «V»
|
||||
«#:V» = [0x86] ++ «V»
|
||||
«#!V» = [0x86] ++ «V»
|
||||
|
||||
«V» if V ∈ Float = [0x87, 0x04] ++ binary32(V)
|
||||
«V» if V ∈ Double = [0x87, 0x08] ++ binary64(V)
|
||||
|
||||
«V» if V ∈ SignedInteger = [0xB0] ++ varint(|intbytes(V)|) ++ intbytes(V)
|
||||
|
@ -28,4 +29,5 @@ For a value `V`, we write `«V»` for the binary encoding of `V`.
|
|||
signedBigEndian(n >> 8) ++ [n & 255] otherwise
|
||||
```
|
||||
|
||||
The function `binary64(D)` yields the big-endian 8-byte IEEE 754 binary representation of `D`.
|
||||
The functions `binary32(F)` and `binary64(D)` yield big-endian 4- and
|
||||
8-byte IEEE 754 binary representations of `F` and `D`, respectively.
|
||||
|
|
|
@ -8,9 +8,10 @@ class="postcard-grammar binarysyntax">*V*</span>.
|
|||
|
||||
{:.postcard-grammar.binarysyntax}
|
||||
«`@`*W* *V*» | = | `85` «*W*» «*V*»
|
||||
«`#:`*V*» | = | `86` «*V*»
|
||||
«`#!`*V*» | = | `86` «*V*»
|
||||
|
||||
{:.postcard-grammar.binarysyntax}
|
||||
«*V*» | = | `87``04` **binary32**(*V*) | if *V* ∈ Float
|
||||
«*V*» | = | `87``08` **binary64**(*V*) | if *V* ∈ Double
|
||||
|
||||
{:.postcard-grammar.binarysyntax}
|
||||
|
@ -36,9 +37,10 @@ class="postcard-grammar binarysyntax">*V*</span>.
|
|||
**signedBigEndian**(*n*) | = | <span class="outputish">*n* & 255</span> | if −128 ≤ *n* ≤ 127
|
||||
| | **signedBigEndian**(*n* >> 8) <span class="outputish">*n* & 255</span> | otherwise
|
||||
|
||||
The function <span class="postcard-grammar binarysyntax">**binary64**(*D*)</span> yields the
|
||||
big-endian 8-byte IEEE 754 binary representation of <span class="postcard-grammar
|
||||
binarysyntax">*D*</span>.
|
||||
The functions <span class="postcard-grammar binarysyntax">**binary32**(*F*)</span> and <span
|
||||
class="postcard-grammar binarysyntax">**binary64**(*D*)</span> yield big-endian 4- and 8-byte
|
||||
IEEE 754 binary representations of <span class="postcard-grammar binarysyntax">*F*</span> and
|
||||
<span class="postcard-grammar binarysyntax">*D*</span>, respectively.
|
||||
|
||||
<!--
|
||||
Together, <span class="postcard-grammar binarysyntax">**div**</span> and <span
|
||||
|
|
|
@ -15,7 +15,7 @@ Set := `#{` Expr* Trailer ws `}`
|
|||
|
||||
Trailer := (ws Annotation)*
|
||||
|
||||
Embedded := `#:` SimpleExpr
|
||||
Embedded := `#!` SimpleExpr
|
||||
Annotated := Annotation SimpleExpr
|
||||
Annotation := `@` SimpleExpr | `#` ((space | tab | `!`) linecomment) (cr | lf)
|
||||
Annotation := `@` SimpleExpr | `#` ((space | tab) linecomment) (cr | lf)
|
||||
```
|
||||
|
|
|
@ -18,6 +18,6 @@ The definitions of `Atom`, `ws`, and `linecomment` are as given in the Preserves
|
|||
| *Trailer* | := | (**ws** *Annotation*)<sup>⋆</sup>
|
||||
|
||||
{:.postcard-grammar.textsyntax}
|
||||
| *Embedded* | := | `#:` *SimpleExpr*
|
||||
| *Embedded* | := | `#!` *SimpleExpr*
|
||||
| *Annotated* | := | *Annotation* *SimpleExpr*
|
||||
| *Annotation* | := | `@` *SimpleExpr* | `#` ((**space** | **tab** | `!`) *linecomment*) (**cr** | **lf**)
|
||||
| *Annotation* | := | `@` *SimpleExpr* | `#` ((**space** | **tab**) *linecomment*) (**cr** | **lf**)
|
||||
|
|
|
@ -9,9 +9,9 @@ Set := `#{` (commas Value)* commas `}`
|
|||
Dictionary := `{` (commas Value ws `:` Value)* commas `}`
|
||||
commas := (ws `,`)* ws
|
||||
|
||||
Embedded := `#:` Value
|
||||
Embedded := `#!` Value
|
||||
Annotated := Annotation Value
|
||||
Annotation := `@` Value | `#` ((space | tab | `!`) linecomment) (cr | lf)
|
||||
Annotation := `@` Value | `#` ((space | tab) linecomment) (cr | lf)
|
||||
|
||||
Atom := Boolean | ByteString | String | QuotedSymbol | Symbol | Number
|
||||
Boolean := `#t` | `#f`
|
||||
|
@ -21,7 +21,8 @@ ByteString := `#"` binchar* `"`
|
|||
String := `"` («any unicode scalar except `\` or `"`» | escaped | `\"`)* `"`
|
||||
QuotedSymbol := `|` («any unicode scalar except `\` or `|`» | escaped | `\|`)* `|`
|
||||
Symbol := (`A`..`Z` | `a`..`z` | `0`..`9` | sympunct | symuchar)+
|
||||
Number := Double | SignedInteger
|
||||
Number := Float | Double | SignedInteger
|
||||
Float := flt (`f`|`F`) | `#xf"` (ws hex hex)4 ws `"`
|
||||
Double := flt | `#xd"` (ws hex hex)8 ws `"`
|
||||
SignedInteger := int
|
||||
|
||||
|
|
|
@ -11,9 +11,9 @@
|
|||
| **commas** | := | (**ws** `,`)<sup>⋆</sup> **ws** |
|
||||
|
||||
{:.postcard-grammar.textsyntax}
|
||||
| *Embedded* | := | `#:`*Value* |
|
||||
| *Embedded* | := | `#!`*Value* |
|
||||
| *Annotated* | := | *Annotation* *Value* |
|
||||
| *Annotation* | := | `@`*Value* |`#` ((**space** | **tab** | `!`) *linecomment*) (**cr** | **lf**) |
|
||||
| *Annotation* | := | `@`*Value* |`#` ((**space** | **tab**) *linecomment*) (**cr** | **lf**) |
|
||||
|
||||
{:.postcard-grammar.textsyntax}
|
||||
| *Atom* | := | *Boolean* | *ByteString* | *String* | *QuotedSymbol* | *Symbol* | *Number* |
|
||||
|
@ -22,7 +22,8 @@
|
|||
| *String* | := | `"` (« any unicode scalar value except `\` or `"` » | *escaped* |`\"`)<sup>⋆</sup> `"` |
|
||||
| *QuotedSymbol* | := | `|` (« any unicode scalar value except `\` or `|` » | *escaped* |`\|`)<sup>⋆</sup> `|` |
|
||||
| *Symbol* | := | (`A`..`Z`|`a`..`z`|`0`..`9`| *sympunct* | *symuchar*)<sup>+</sup> |
|
||||
| *Number* | := | *Double* | *SignedInteger* |
|
||||
| *Number* | := | *Float* | *Double* | *SignedInteger* |
|
||||
| *Float* | := | *flt* (`f`|`F`) |`#xf"` (**ws** *hex* *hex*)<sup>4</sup> **ws**`"` |
|
||||
| *Double* | := | *flt* |`#xd"` (**ws** *hex* *hex*)<sup>8</sup> **ws**`"` |
|
||||
| *SignedInteger* | := | *int* |
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
Python's strings, byte strings, integers, booleans, and double-precision floats stand directly
|
||||
for their Preserves counterparts. Wrapper objects for
|
||||
for their Preserves counterparts. Wrapper objects for [Float][preserves.values.Float] and
|
||||
[Symbol][preserves.values.Symbol] complete the suite of atomic types.
|
||||
|
||||
Python's lists and tuples correspond to Preserves `Sequence`s, and dicts and sets to
|
||||
|
|
|
@ -2,6 +2,7 @@ Here are a few example values, written using the [text
|
|||
syntax](https://preserves.dev/preserves-text.html):
|
||||
|
||||
Boolean : #t #f
|
||||
Float : 1.0f 10.4e3f -100.6f
|
||||
Double : 1.0 10.4e3 -100.6
|
||||
Integer : 1 0 -100
|
||||
String : "Hello, world!\n"
|
||||
|
@ -11,6 +12,6 @@ syntax](https://preserves.dev/preserves-text.html):
|
|||
Sequence : [value1 value2 ...]
|
||||
Set : #{value1 value2 ...}
|
||||
Dictionary : {key1: value1 key2: value2 ...: ...}
|
||||
Embedded : #:value
|
||||
Embedded : #!value
|
||||
|
||||
Commas are optional in sequences, sets, and dictionaries.
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
| Embedded
|
||||
|
||||
Atom = Boolean
|
||||
| Float
|
||||
| Double
|
||||
| SignedInteger
|
||||
| String
|
||||
|
|
|
@ -38,7 +38,7 @@ representations of their keys.[^no-need-for-by-value]
|
|||
**Other kinds of `Value`.**
|
||||
There are no special canonicalization restrictions on
|
||||
`SignedInteger`s, `String`s, `ByteString`s, `Symbol`s, `Boolean`s,
|
||||
`Double`s, `Record`s, `Sequence`s, or `Embedded`s. The
|
||||
`Float`s, `Double`s, `Record`s, `Sequence`s, or `Embedded`s. The
|
||||
constraints given for these `Value`s in the [specification][spec]
|
||||
suffice to ensure canonicity.
|
||||
|
||||
|
|
|
@ -23,10 +23,10 @@ Appropriately-labelled `Record`s denote these domain-specific data
|
|||
types.[^why-dictionaries]
|
||||
|
||||
[^why-dictionaries]: Given `Record`'s existence, it may seem odd
|
||||
that `Dictionary`, `Set`, `Double`, etc. are given special
|
||||
that `Dictionary`, `Set`, `Float`, etc. are given special
|
||||
treatment. Preserves aims to offer a useful basic equivalence
|
||||
predicate to programmers, and so if a data type demands a special
|
||||
equivalence predicate, as `Dictionary`, `Set` and `Double` all do,
|
||||
equivalence predicate, as `Dictionary`, `Set` and `Float` all do,
|
||||
then the type should be included in the base language. Otherwise,
|
||||
it can be represented as a `Record` and treated separately.
|
||||
`Boolean`, `String` and `Symbol` are seeming exceptions. The first
|
||||
|
@ -99,22 +99,6 @@ the usual `@`-prefixed annotation notation can also be used.
|
|||
#x"0C0D0E0F"
|
||||
]
|
||||
|
||||
## Interpreter specification lines ("shebang" lines).
|
||||
|
||||
Unix systems interpret `#!` at the beginning of an executable file specially. The text
|
||||
following `#!` on the first line is interpreted as a specification for an interpreter for the
|
||||
executable file. Preserves offers special support for `#!`, reading it similarly to a comment,
|
||||
but producing an `<interpreter ...>` annotation instead of a string.
|
||||
|
||||
For example,
|
||||
|
||||
#!/usr/bin/preserves-tool convert
|
||||
[1, 2, 3]
|
||||
|
||||
is read as
|
||||
|
||||
@<interpreter "/usr/bin/preserves-tool convert"> [1, 2, 3]
|
||||
|
||||
## MIME-type tagged binary data.
|
||||
|
||||
Many internet protocols use
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
/build.python/
|
||||
/*.deb
|
|
@ -1,10 +0,0 @@
|
|||
PYTHON_PACKAGEVERSION := $(shell ../implementations/python/print-package-version)
|
||||
|
||||
all: python3-preserves_$(PYTHON_PACKAGEVERSION)_all.deb
|
||||
|
||||
python3-preserves_%_all.deb:
|
||||
./build-python-deb
|
||||
|
||||
clean:
|
||||
rm -f *.deb
|
||||
rm -rf build.*
|
|
@ -1,37 +0,0 @@
|
|||
#!/bin/sh
|
||||
|
||||
set -e
|
||||
|
||||
PYTHON_PACKAGEVERSION=$(../implementations/python/print-package-version)
|
||||
DIRTY=
|
||||
if ! git diff-index --quiet HEAD --
|
||||
then
|
||||
DIRTY=+
|
||||
fi
|
||||
GITSUFFIX=$(git log --date=format:%Y%m%d%H%M%S --pretty=~git%cd.%h -1)
|
||||
VERSION=${PYTHON_PACKAGEVERSION}${GITSUFFIX}${DIRTY}
|
||||
|
||||
echo "Building deb for ${VERSION}"
|
||||
(cd ../implementations/python && . ./.envrc && make build-only)
|
||||
|
||||
rm -rf build.python
|
||||
mkdir build.python
|
||||
|
||||
(
|
||||
cd build.python
|
||||
tar -zxvf ../../implementations/python/dist/preserves-${PYTHON_PACKAGEVERSION}.tar.gz
|
||||
(
|
||||
cd preserves-${PYTHON_PACKAGEVERSION}
|
||||
cp -a ../../python ./debian
|
||||
cat > ./debian/changelog <<EOF
|
||||
preserves (${VERSION}) UNRELEASED; urgency=low
|
||||
|
||||
* Unofficial debian packaging of Python Preserves
|
||||
|
||||
-- Tony Garnock-Jones <tonyg@leastfixedpoint.com> $(date --rfc-email)
|
||||
EOF
|
||||
|
||||
dpkg-buildpackage
|
||||
)
|
||||
cp *.deb ..
|
||||
)
|
|
@ -1,14 +0,0 @@
|
|||
Source: preserves
|
||||
Section: python
|
||||
Priority: optional
|
||||
Maintainer: Tony Garnock-Jones <tonyg@leastfixedpoint.com>
|
||||
Build-Depends: debhelper-compat (= 12), python3, dh-python, pybuild-plugin-pyproject
|
||||
Standards-Version: 3.9.3
|
||||
Homepage: https://preserves.dev/
|
||||
Vcs-Git: https://gitlab.com/preserves/preserves.git
|
||||
Vcs-Browser: https://gitlab.com/preserves/preserves
|
||||
|
||||
Package: python3-preserves
|
||||
Architecture: all
|
||||
Depends: ${python3:Depends}
|
||||
Description: Python implementation of the Preserves data language
|
|
@ -1,6 +0,0 @@
|
|||
#!/usr/bin/make -f
|
||||
#export DH_VERBOSE=1
|
||||
export PYBUILD_NAME=preserves
|
||||
|
||||
%:
|
||||
dh $@ --with python3 --buildsystem=pybuild
|
|
@ -31,6 +31,7 @@ fi
|
|||
# Ensure that various copies of schema.prs, schema.bin, path.bin,
|
||||
# samples.pr and samples.bin are in fact identical.
|
||||
${COMMAND} path/path.bin implementations/python/preserves/path.prb
|
||||
${COMMAND} path/path.bin implementations/rust/preserves-path/path.bin
|
||||
|
||||
${COMMAND} schema/schema.bin implementations/python/preserves/schema.prb
|
||||
${COMMAND} schema/schema.prs implementations/racket/preserves/preserves-schema/schema.prs
|
||||
|
@ -39,4 +40,11 @@ ${COMMAND} tests/samples.bin implementations/python/tests/samples.bin
|
|||
${COMMAND} tests/samples.pr implementations/python/tests/samples.pr
|
||||
${COMMAND} tests/samples.pr implementations/racket/preserves/preserves/tests/samples.pr
|
||||
|
||||
${COMMAND} _includes/what-is-preserves.md implementations/rust/preserves/doc/what-is-preserves.md
|
||||
${COMMAND} _includes/cheatsheet-binary-plaintext.md implementations/rust/preserves/doc/cheatsheet-binary-plaintext.md
|
||||
${COMMAND} _includes/cheatsheet-text-plaintext.md implementations/rust/preserves/doc/cheatsheet-text-plaintext.md
|
||||
${COMMAND} _includes/value-grammar.md implementations/rust/preserves/doc/value-grammar.md
|
||||
|
||||
${COMMAND} _includes/what-is-preserves-schema.md implementations/rust/preserves-schema/doc/what-is-preserves-schema.md
|
||||
|
||||
[ -z "$failed" ]
|
||||
|
|
|
@ -13,9 +13,9 @@ Here you may find:
|
|||
- [racket](racket/), an implementation for Racket 7.x and newer
|
||||
(though older Rackets may also work with it).
|
||||
|
||||
- [rust](rust/), an implementation for Rust that interoperates with
|
||||
serde.
|
||||
|
||||
Other implementations are also available:
|
||||
|
||||
- [Preserves for Rust](https://gitlab.com/preserves/preserves-rs/), an implementation for Rust
|
||||
that interoperates with serde.
|
||||
|
||||
- [Preserves for Squeak Smalltalk](https://squeaksource.com/Preserves.html)
|
||||
|
|
|
@ -261,6 +261,7 @@ PRESERVES_OUTOFLINE
|
|||
|
||||
typedef enum preserves_type_tag {
|
||||
PRESERVES_BOOLEAN = 0,
|
||||
PRESERVES_FLOAT,
|
||||
PRESERVES_DOUBLE,
|
||||
|
||||
PRESERVES_SIGNED_INTEGER,
|
||||
|
@ -282,6 +283,7 @@ typedef enum preserves_type_tag {
|
|||
PRESERVES_OUTOFLINE(char const *preserves_type_tag_name(preserves_type_tag_t type), {
|
||||
switch (type) {
|
||||
case PRESERVES_BOOLEAN: return "BOOLEAN";
|
||||
case PRESERVES_FLOAT: return "FLOAT";
|
||||
case PRESERVES_DOUBLE: return "DOUBLE";
|
||||
case PRESERVES_SIGNED_INTEGER: return "SIGNED_INTEGER";
|
||||
case PRESERVES_STRING: return "STRING";
|
||||
|
@ -364,6 +366,7 @@ PRESERVES_OUTOFLINE
|
|||
|
||||
/*
|
||||
PRESERVES_BOOLEAN: repr==PRESERVES_REPR_NONE, len=0, data._boolean
|
||||
PRESERVES_FLOAT: repr=PRESERVES_REPR_NONE, len=0, data._float
|
||||
PRESERVES_DOUBLE: repr=PRESERVES_REPR_NONE, len=0, data._double
|
||||
|
||||
PRESERVES_SIGNED_INTEGER:
|
||||
|
@ -415,6 +418,7 @@ typedef struct preserves_index_entry {
|
|||
|
||||
union {
|
||||
bool _boolean;
|
||||
float _float;
|
||||
double _double;
|
||||
int64_t _signed;
|
||||
uint64_t _unsigned;
|
||||
|
@ -814,6 +818,18 @@ PRESERVES_OUTOFLINE
|
|||
uint8_t *bs = _preserves_reader_next_bytes(r, len);
|
||||
if (bs == NULL) return _preserves_reader_finish(r, PRESERVES_END_INCOMPLETE_INPUT);
|
||||
switch (len) {
|
||||
case 4: {
|
||||
uint32_t i;
|
||||
memcpy(&i, bs, 4);
|
||||
i = ntohl(i);
|
||||
float f;
|
||||
memcpy(&f, &i, 4);
|
||||
RETURN_ON_FAIL(_preserves_reader_emit_entry(r, &count, (preserves_index_entry_t) {
|
||||
.type = PRESERVES_FLOAT, .repr = PRESERVES_REPR_NONE, .len = 0, .data = {
|
||||
._float = f
|
||||
}}));
|
||||
break;
|
||||
}
|
||||
case 8: {
|
||||
uint32_t lo, hi;
|
||||
memcpy(&hi, bs, 4);
|
||||
|
@ -979,6 +995,10 @@ PRESERVES_IMPLEMENTATION_CHUNK
|
|||
fprintf(f, i->data._boolean ? " #t" : " #f");
|
||||
break;
|
||||
|
||||
case PRESERVES_FLOAT:
|
||||
fprintf(f, " %f", i->data._float);
|
||||
break;
|
||||
|
||||
case PRESERVES_DOUBLE:
|
||||
fprintf(f, " %f", i->data._double);
|
||||
break;
|
||||
|
|
|
@ -41,6 +41,15 @@ namespace Preserves {
|
|||
decodeEmbedded(decodeEmbedded)
|
||||
{}
|
||||
|
||||
boost::optional<float> next_float() {
|
||||
uint8_t buf[4];
|
||||
if (!next_chunk(buf, sizeof(buf))) return boost::none;
|
||||
uint32_t n = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
|
||||
float f;
|
||||
memcpy(&f, &n, sizeof(f));
|
||||
return f;
|
||||
}
|
||||
|
||||
boost::optional<double> next_double() {
|
||||
uint8_t buf[8];
|
||||
if (!next_chunk(buf, sizeof(buf))) return boost::none;
|
||||
|
@ -104,6 +113,7 @@ namespace Preserves {
|
|||
return BinaryReader<>(i).next().map(decodeEmbedded).map(Value<T>::from_embedded);
|
||||
case BinaryTag::Ieee754: return varint(i).flat_map([&](size_t len)-> boost::optional<Value<T>> {
|
||||
switch (len) {
|
||||
case 4: return next_float().map(Value<T>::from_float);
|
||||
case 8: return next_double().map(Value<T>::from_double);
|
||||
default: return boost::none;
|
||||
}
|
||||
|
|
|
@ -151,6 +151,19 @@ namespace Preserves {
|
|||
return (*this) << (b ? BinaryTag::True : BinaryTag::False);
|
||||
}
|
||||
|
||||
BinaryWriter& operator<<(float f) {
|
||||
uint32_t n;
|
||||
memcpy(&n, &f, sizeof(f));
|
||||
uint8_t buf[4];
|
||||
buf[0] = (n >> 24) & 0xff;
|
||||
buf[1] = (n >> 16) & 0xff;
|
||||
buf[2] = (n >> 8) & 0xff;
|
||||
buf[3] = (n) & 0xff;
|
||||
(*this) << BinaryTag::Ieee754;
|
||||
put(uint8_t(sizeof(buf)));
|
||||
return write(buf, sizeof(buf));
|
||||
}
|
||||
|
||||
BinaryWriter& operator<<(double d) {
|
||||
uint64_t n;
|
||||
memcpy(&n, &d, sizeof(d));
|
||||
|
|
|
@ -35,6 +35,13 @@ namespace Preserves {
|
|||
BinaryWriter& write(BinaryWriter& w) const override {
|
||||
return w << this->_value();
|
||||
});
|
||||
PRESERVES_ATOMIC_VALUE_CLASS(Float, float, float, ValueKind::Float, as_float,
|
||||
BinaryWriter& write(BinaryWriter& w) const override {
|
||||
return w << this->_value();
|
||||
}
|
||||
boost::optional<double> as_double() const override {
|
||||
return this->value;
|
||||
});
|
||||
PRESERVES_ATOMIC_VALUE_CLASS(Double, double, double, ValueKind::Double, as_double,
|
||||
BinaryWriter& write(BinaryWriter& w) const override {
|
||||
return w << this->_value();
|
||||
|
@ -50,6 +57,13 @@ namespace Preserves {
|
|||
return boost::none;
|
||||
}
|
||||
}
|
||||
boost::optional<float> as_float() const override {
|
||||
if (uint64_t(float(this->value)) == this->value) {
|
||||
return float(this->value);
|
||||
} else {
|
||||
return boost::none;
|
||||
}
|
||||
}
|
||||
boost::optional<double> as_double() const override {
|
||||
if (uint64_t(double(this->value)) == this->value) {
|
||||
return double(this->value);
|
||||
|
@ -68,6 +82,13 @@ namespace Preserves {
|
|||
return boost::none;
|
||||
}
|
||||
}
|
||||
boost::optional<float> as_float() const override {
|
||||
if (int64_t(float(this->value)) == this->value) {
|
||||
return float(this->value);
|
||||
} else {
|
||||
return boost::none;
|
||||
}
|
||||
}
|
||||
boost::optional<double> as_double() const override {
|
||||
if (int64_t(double(this->value)) == this->value) {
|
||||
return double(this->value);
|
||||
|
@ -274,6 +295,7 @@ namespace Preserves {
|
|||
bool is_mutable() const override { return underlying.is_mutable(); }
|
||||
|
||||
boost::optional<bool> as_bool() const override { return underlying.as_bool(); }
|
||||
boost::optional<float> as_float() const override { return underlying.as_float(); }
|
||||
boost::optional<double> as_double() const override { return underlying.as_double(); }
|
||||
boost::optional<uint64_t> as_unsigned() const override { return underlying.as_unsigned(); }
|
||||
boost::optional<int64_t> as_signed() const override { return underlying.as_signed(); }
|
||||
|
@ -333,6 +355,12 @@ namespace Preserves {
|
|||
return Value<T>(new Boolean<T>(b));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Value<T> Value<T>::from_float(float f)
|
||||
{
|
||||
return Value<T>(new Float<T>(f));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Value<T> Value<T>::from_double(double d)
|
||||
{
|
||||
|
|
|
@ -14,6 +14,7 @@ namespace Preserves {
|
|||
|
||||
enum class ValueKind {
|
||||
Boolean,
|
||||
Float,
|
||||
Double,
|
||||
SignedInteger,
|
||||
String,
|
||||
|
@ -42,6 +43,7 @@ namespace Preserves {
|
|||
std::shared_ptr<ValueImpl<T>> _impl() const { return p; }
|
||||
|
||||
static Value from_bool(bool b);
|
||||
static Value from_float(float f);
|
||||
static Value from_double(double d);
|
||||
static Value from_int(uint64_t i);
|
||||
static Value from_int(int64_t i);
|
||||
|
@ -65,9 +67,11 @@ namespace Preserves {
|
|||
|
||||
static Value from_number(uint64_t i) { return from_int(i); }
|
||||
static Value from_number(int64_t i) { return from_int(i); }
|
||||
static Value from_number(float f) { return from_float(f); }
|
||||
static Value from_number(double d) { return from_double(d); }
|
||||
|
||||
static Value from(bool b) { return from_bool(b); }
|
||||
static Value from(float f) { return from_float(f); }
|
||||
static Value from(double d) { return from_double(d); }
|
||||
static Value from(uint64_t i) { return from_int(i); }
|
||||
static Value from(unsigned i) { return from_int(uint64_t(i)); }
|
||||
|
@ -91,6 +95,7 @@ namespace Preserves {
|
|||
bool is_mutable() const;
|
||||
|
||||
bool is_bool() const { return value_kind() == ValueKind::Boolean; }
|
||||
bool is_float() const { return value_kind() == ValueKind::Float; }
|
||||
bool is_double() const { return value_kind() == ValueKind::Double; }
|
||||
bool is_int() const { return value_kind() == ValueKind::SignedInteger; }
|
||||
bool is_string() const { return value_kind() == ValueKind::String; }
|
||||
|
@ -104,6 +109,9 @@ namespace Preserves {
|
|||
boost::optional<bool> as_bool() const;
|
||||
bool to_bool() const { return as_bool().value(); }
|
||||
|
||||
boost::optional<float> as_float() const;
|
||||
float to_float() const { return as_float().value(); }
|
||||
|
||||
boost::optional<double> as_double() const;
|
||||
double to_double() const { return as_double().value(); }
|
||||
|
||||
|
@ -167,6 +175,7 @@ namespace Preserves {
|
|||
virtual bool is_mutable() const { return false; }
|
||||
|
||||
virtual boost::optional<bool> as_bool() const { return boost::none; }
|
||||
virtual boost::optional<float> as_float() const { return boost::none; }
|
||||
virtual boost::optional<double> as_double() const { return boost::none; }
|
||||
virtual boost::optional<uint64_t> as_unsigned() const { return boost::none; }
|
||||
virtual boost::optional<int64_t> as_signed() const { return boost::none; }
|
||||
|
@ -210,6 +219,7 @@ namespace Preserves {
|
|||
#define PRESERVES_DELEGATE_CAST(t, name) \
|
||||
template <typename T> boost::optional<t> Value<T>::name() const { return p->name(); }
|
||||
PRESERVES_DELEGATE_CAST(bool, as_bool);
|
||||
PRESERVES_DELEGATE_CAST(float, as_float);
|
||||
PRESERVES_DELEGATE_CAST(double, as_double);
|
||||
PRESERVES_DELEGATE_CAST(uint64_t, as_unsigned);
|
||||
PRESERVES_DELEGATE_CAST(int64_t, as_signed);
|
||||
|
@ -255,6 +265,7 @@ namespace Preserves {
|
|||
if (bKind < aKind) return false;
|
||||
switch (aKind) {
|
||||
case ValueKind::Boolean: return a.to_bool() < b.to_bool();
|
||||
case ValueKind::Float: return a.to_float() < b.to_float();
|
||||
case ValueKind::Double: return a.to_double() < b.to_double();
|
||||
case ValueKind::SignedInteger: {
|
||||
if (auto av = a.as_signed()) {
|
||||
|
|
|
@ -41,7 +41,7 @@ let render
|
|||
)
|
||||
m
|
||||
++ " }"
|
||||
, embedded = λ(value : Text) → "#:${value}"
|
||||
, embedded = λ(value : Text) → "#!${value}"
|
||||
}
|
||||
|
||||
let Preserves/boolean = ./boolean.dhall
|
||||
|
@ -94,7 +94,7 @@ let example0 =
|
|||
)}
|
||||
''
|
||||
≡ ''
|
||||
{ a: 1 b: [ 2 3 ] c: { d: 1.0 e: -1.0 } d: #:#t e: <capture <_>> }
|
||||
{ a: 1 b: [ 2 3 ] c: { d: 1.0 e: -1.0 } d: #!#t e: <capture <_>> }
|
||||
''
|
||||
|
||||
in render
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@preserves/core",
|
||||
"version": "0.995.206",
|
||||
"version": "0.992.4",
|
||||
"description": "Preserves data serialization format",
|
||||
"homepage": "https://gitlab.com/preserves/preserves",
|
||||
"license": "Apache-2.0",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { Tag } from "./constants";
|
||||
import { is, isAnnotated, IsPreservesAnnotated } from "./is";
|
||||
import type { Embeddable, GenericEmbedded } from "./embedded";
|
||||
import type { GenericEmbedded } from "./embedded";
|
||||
import type { Value } from "./values";
|
||||
import type { Encoder, Preservable } from "./encoder";
|
||||
import type { Writer, PreserveWritable } from "./writer";
|
||||
|
@ -52,7 +52,7 @@ export function formatPosition(p: Position | null | string): string {
|
|||
}
|
||||
}
|
||||
|
||||
export class Annotated<T extends Embeddable = GenericEmbedded> implements Preservable<T>, PreserveWritable<T> {
|
||||
export class Annotated<T = GenericEmbedded> implements Preservable<T>, PreserveWritable<T> {
|
||||
readonly annotations: Array<Value<T>>;
|
||||
readonly pos: Position | null;
|
||||
readonly item: Value<T>;
|
||||
|
@ -67,7 +67,7 @@ export class Annotated<T extends Embeddable = GenericEmbedded> implements Preser
|
|||
return this;
|
||||
}
|
||||
|
||||
static __from_preserve__<T extends Embeddable>(v: Value<T>): undefined | Annotated<T> {
|
||||
static __from_preserve__<T>(v: Value<T>): undefined | Annotated<T> {
|
||||
return isAnnotated<T>(v) ? v : void 0;
|
||||
}
|
||||
|
||||
|
@ -109,24 +109,21 @@ export class Annotated<T extends Embeddable = GenericEmbedded> implements Preser
|
|||
return true;
|
||||
}
|
||||
|
||||
static isAnnotated<T extends Embeddable = GenericEmbedded>(x: any): x is Annotated<T> {
|
||||
static isAnnotated<T = GenericEmbedded>(x: any): x is Annotated<T> {
|
||||
return isAnnotated(x);
|
||||
}
|
||||
}
|
||||
|
||||
export function annotate<T extends Embeddable = GenericEmbedded>(
|
||||
v0: Value<T>,
|
||||
...anns: Value<T>[]
|
||||
): Annotated<T> {
|
||||
export function annotate<T = GenericEmbedded>(v0: Value<T>, ...anns: Value<T>[]): Annotated<T> {
|
||||
const v = Annotated.isAnnotated<T>(v0) ? v0 : new Annotated(v0);
|
||||
anns.forEach((a) => v.annotations.push(a));
|
||||
return v;
|
||||
}
|
||||
|
||||
export function annotations<T extends Embeddable = GenericEmbedded>(v: Value<T>): Array<Value<T>> {
|
||||
export function annotations<T = GenericEmbedded>(v: Value<T>): Array<Value<T>> {
|
||||
return Annotated.isAnnotated<T>(v) ? v.annotations : [];
|
||||
}
|
||||
|
||||
export function position<T extends Embeddable = GenericEmbedded>(v: Value<T>): Position | null {
|
||||
export function position<T = GenericEmbedded>(v: Value<T>): Position | null {
|
||||
return Annotated.isAnnotated<T>(v) ? v.pos : null;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { Tag } from './constants';
|
||||
import type { Embeddable, GenericEmbedded } from './embedded';
|
||||
import { GenericEmbedded } from './embedded';
|
||||
import { Encoder, Preservable } from './encoder';
|
||||
import { Value } from './values';
|
||||
import type { Writer, PreserveWritable } from './writer';
|
||||
|
@ -52,18 +52,6 @@ export class Bytes implements Preservable<any>, PreserveWritable<any> {
|
|||
return new Bytes(Uint8Array.of(...bytes));
|
||||
}
|
||||
|
||||
static fromLatin1(s: string): Bytes {
|
||||
// Takes codepoints in [0..255] from s, treats them as bytes.
|
||||
// Codepoints outside that range trigger an exception.
|
||||
const result = new Bytes(s.length); // assume all the codepoints are OK
|
||||
for (let i = 0; i < s.length; i++) {
|
||||
const n = s.charCodeAt(i);
|
||||
if (n >= 256) throw new Error("Codepoint out of range for 'latin1' byte encoding");
|
||||
result._view[i] = n;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static fromBase64(s: string): Bytes {
|
||||
return new Bytes(decodeBase64(s));
|
||||
}
|
||||
|
@ -148,18 +136,14 @@ export class Bytes implements Preservable<any>, PreserveWritable<any> {
|
|||
return textDecoder.decode(this._view);
|
||||
}
|
||||
|
||||
__as_preserve__<T extends Embeddable = GenericEmbedded>(): Value<T> {
|
||||
__as_preserve__<T = GenericEmbedded>(): Value<T> {
|
||||
return this;
|
||||
}
|
||||
|
||||
static __from_preserve__<T extends Embeddable>(v: Value<T>): undefined | Bytes {
|
||||
static __from_preserve__<T>(v: Value<T>): undefined | Bytes {
|
||||
return Bytes.isBytes(v) ? v : void 0;
|
||||
}
|
||||
|
||||
toLatin1(): string {
|
||||
return String.fromCharCode.apply(null, this._view as any as number[]);
|
||||
}
|
||||
|
||||
toBase64(): string {
|
||||
return encodeBase64(this._view);
|
||||
}
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
import type { Compound, Value } from "./values";
|
||||
import type { Embeddable, GenericEmbedded } from "./embedded";
|
||||
import type { GenericEmbedded } from "./embedded";
|
||||
import { Dictionary, Set } from "./dictionary";
|
||||
|
||||
export function isCompound<T extends Embeddable = GenericEmbedded>(x: Value<T>): x is Compound<T>
|
||||
export function isCompound<T = GenericEmbedded>(x: Value<T>): x is Compound<T>
|
||||
{
|
||||
return (Array.isArray(x) || Set.isSet(x) || Dictionary.isDictionary(x));
|
||||
}
|
||||
|
||||
export function isSequence<T extends Embeddable = GenericEmbedded>(x: Value<T>): x is Array<Value<T>> {
|
||||
export function isSequence<T = GenericEmbedded>(x: Value<T>): x is Array<Value<T>> {
|
||||
return (Array.isArray(x) && !('label' in x));
|
||||
}
|
||||
|
|
|
@ -1,25 +1,24 @@
|
|||
import { Annotated } from "./annotated";
|
||||
import { DecodeError, ShortPacket } from "./codec";
|
||||
import { Tag } from "./constants";
|
||||
import { Set, Dictionary, DictionaryMap } from "./dictionary";
|
||||
import { DoubleFloat } from "./float";
|
||||
import { Set, Dictionary } from "./dictionary";
|
||||
import { DoubleFloat, SingleFloat } from "./float";
|
||||
import { Record } from "./record";
|
||||
import { Bytes, BytesLike, underlying, hexDigit } from "./bytes";
|
||||
import { Value } from "./values";
|
||||
import { is } from "./is";
|
||||
import { GenericEmbedded, Embeddable, EmbeddedTypeDecode } from "./embedded";
|
||||
import { ReaderStateOptions } from "./reader";
|
||||
import { stringify } from "./text";
|
||||
import { embed, GenericEmbedded, Embedded, EmbeddedTypeDecode } from "./embedded";
|
||||
import { ReaderStateOptions } from "reader";
|
||||
|
||||
export interface DecoderOptions {
|
||||
includeAnnotations?: boolean;
|
||||
}
|
||||
|
||||
export interface DecoderEmbeddedOptions<T extends Embeddable> extends DecoderOptions {
|
||||
export interface DecoderEmbeddedOptions<T> extends DecoderOptions {
|
||||
embeddedDecode?: EmbeddedTypeDecode<T>;
|
||||
}
|
||||
|
||||
export interface TypedDecoder<T extends Embeddable> {
|
||||
export interface TypedDecoder<T> {
|
||||
atEnd(): boolean;
|
||||
|
||||
mark(): any;
|
||||
|
@ -27,13 +26,14 @@ export interface TypedDecoder<T extends Embeddable> {
|
|||
|
||||
skip(): void;
|
||||
next(): Value<T>;
|
||||
withEmbeddedDecode<S extends Embeddable, R>(
|
||||
withEmbeddedDecode<S, R>(
|
||||
embeddedDecode: EmbeddedTypeDecode<S>,
|
||||
body: (d: TypedDecoder<S>) => R): R;
|
||||
|
||||
nextBoolean(): boolean | undefined;
|
||||
nextFloat(): SingleFloat | undefined;
|
||||
nextDouble(): DoubleFloat | undefined;
|
||||
nextEmbedded(): T | undefined;
|
||||
nextEmbedded(): Embedded<T> | undefined;
|
||||
nextSignedInteger(): number | bigint | undefined;
|
||||
nextString(): string | undefined;
|
||||
nextByteString(): Bytes | undefined;
|
||||
|
@ -47,7 +47,7 @@ export interface TypedDecoder<T extends Embeddable> {
|
|||
closeCompound(): boolean;
|
||||
}
|
||||
|
||||
export function asLiteral<T extends Embeddable, E extends Exclude<Value<T>, Annotated<T>>>(
|
||||
export function asLiteral<T, E extends Exclude<Value<T>, Annotated<T>>>(
|
||||
actual: Value<T>,
|
||||
expected: E): E | undefined
|
||||
{
|
||||
|
@ -166,11 +166,11 @@ export class DecoderState {
|
|||
}
|
||||
}
|
||||
|
||||
wrap<T extends Embeddable>(v: Value<T>): Value<T> {
|
||||
wrap<T>(v: Value<T>): Value<T> {
|
||||
return this.includeAnnotations ? new Annotated(v) : v;
|
||||
}
|
||||
|
||||
unshiftAnnotation<T extends Embeddable>(a: Value<T>, v: Annotated<T>): Annotated<T> {
|
||||
unshiftAnnotation<T>(a: Value<T>, v: Annotated<T>): Annotated<T> {
|
||||
if (this.includeAnnotations) {
|
||||
v.annotations.unshift(a);
|
||||
}
|
||||
|
@ -188,7 +188,7 @@ export const neverEmbeddedTypeDecode: EmbeddedTypeDecode<never> = {
|
|||
},
|
||||
};
|
||||
|
||||
export class Decoder<T extends Embeddable = never> implements TypedDecoder<T> {
|
||||
export class Decoder<T = never> implements TypedDecoder<T> {
|
||||
state: DecoderState;
|
||||
embeddedDecode: EmbeddedTypeDecode<T>;
|
||||
|
||||
|
@ -218,14 +218,13 @@ export class Decoder<T extends Embeddable = never> implements TypedDecoder<T> {
|
|||
return result;
|
||||
}
|
||||
|
||||
static dictionaryFromArray<T extends Embeddable>(vs: Value<T>[]): Dictionary<T> {
|
||||
const d = new DictionaryMap<T>();
|
||||
static dictionaryFromArray<T>(vs: Value<T>[]): Dictionary<T> {
|
||||
const d = new Dictionary<T>();
|
||||
if (vs.length % 2) throw new DecodeError("Missing dictionary value");
|
||||
for (let i = 0; i < vs.length; i += 2) {
|
||||
if (d.has(vs[i])) throw new DecodeError(`Duplicate key: ${stringify(vs[i])}`);
|
||||
d.set(vs[i], vs[i+1]);
|
||||
}
|
||||
return d.simplifiedValue();
|
||||
return d;
|
||||
}
|
||||
|
||||
next(): Value<T> {
|
||||
|
@ -239,9 +238,10 @@ export class Decoder<T extends Embeddable = never> implements TypedDecoder<T> {
|
|||
const v = this.next() as Annotated<T>;
|
||||
return this.state.unshiftAnnotation(a, v);
|
||||
}
|
||||
case Tag.Embedded: return this.state.wrap<T>(this.embeddedDecode.decode(this.state));
|
||||
case Tag.Embedded: return this.state.wrap<T>(embed(this.embeddedDecode.decode(this.state)));
|
||||
case Tag.Ieee754:
|
||||
switch (this.state.varint()) {
|
||||
case 4: return this.state.wrap<T>(SingleFloat.fromBytes(this.state.nextbytes(4)));
|
||||
case 8: return this.state.wrap<T>(DoubleFloat.fromBytes(this.state.nextbytes(8)));
|
||||
default: throw new DecodeError("Invalid IEEE754 size");
|
||||
}
|
||||
|
@ -255,14 +255,7 @@ export class Decoder<T extends Embeddable = never> implements TypedDecoder<T> {
|
|||
return this.state.wrap<T>(Record(vs[0], vs.slice(1)));
|
||||
}
|
||||
case Tag.Sequence: return this.state.wrap<T>(this.nextvalues());
|
||||
case Tag.Set: {
|
||||
const s = new Set<T>();
|
||||
for (const v of this.nextvalues()) {
|
||||
if (s.has(v)) throw new DecodeError(`Duplicate value: ${stringify(v)}`);
|
||||
s.add(v);
|
||||
}
|
||||
return this.state.wrap<T>(s);
|
||||
}
|
||||
case Tag.Set: return this.state.wrap<T>(new Set(this.nextvalues()));
|
||||
case Tag.Dictionary: return this.state.wrap<T>(Decoder.dictionaryFromArray(this.nextvalues()));
|
||||
default: throw new DecodeError("Unsupported Preserves tag: " + tag);
|
||||
}
|
||||
|
@ -289,7 +282,7 @@ export class Decoder<T extends Embeddable = never> implements TypedDecoder<T> {
|
|||
this.next();
|
||||
}
|
||||
|
||||
withEmbeddedDecode<S extends Embeddable, R>(
|
||||
withEmbeddedDecode<S, R>(
|
||||
embeddedDecode: EmbeddedTypeDecode<S>,
|
||||
body: (d: TypedDecoder<S>) => R): R
|
||||
{
|
||||
|
@ -315,6 +308,14 @@ export class Decoder<T extends Embeddable = never> implements TypedDecoder<T> {
|
|||
});
|
||||
}
|
||||
|
||||
nextFloat(): SingleFloat | undefined {
|
||||
return this.skipAnnotations((reset) => {
|
||||
if (this.state.nextbyte() !== Tag.Ieee754) return reset();
|
||||
if (this.state.nextbyte() !== 4) return reset();
|
||||
return SingleFloat.fromBytes(this.state.nextbytes(4));
|
||||
});
|
||||
}
|
||||
|
||||
nextDouble(): DoubleFloat | undefined {
|
||||
return this.skipAnnotations((reset) => {
|
||||
if (this.state.nextbyte() !== Tag.Ieee754) return reset();
|
||||
|
@ -323,10 +324,10 @@ export class Decoder<T extends Embeddable = never> implements TypedDecoder<T> {
|
|||
});
|
||||
}
|
||||
|
||||
nextEmbedded(): T | undefined {
|
||||
nextEmbedded(): Embedded<T> | undefined {
|
||||
return this.skipAnnotations((reset) => {
|
||||
switch (this.state.nextbyte()) {
|
||||
case Tag.Embedded: return this.embeddedDecode.decode(this.state);
|
||||
case Tag.Embedded: return embed(this.embeddedDecode.decode(this.state));
|
||||
default: return reset();
|
||||
}
|
||||
});
|
||||
|
@ -395,16 +396,11 @@ export class Decoder<T extends Embeddable = never> implements TypedDecoder<T> {
|
|||
}
|
||||
}
|
||||
|
||||
export function decode<T extends Embeddable>(
|
||||
bs: BytesLike,
|
||||
options: DecoderEmbeddedOptions<T> = {},
|
||||
): Value<T> {
|
||||
export function decode<T>(bs: BytesLike, options: DecoderEmbeddedOptions<T> = {}): Value<T> {
|
||||
return new Decoder(bs, options).next();
|
||||
}
|
||||
|
||||
export function decodeWithAnnotations<T extends Embeddable>(
|
||||
bs: BytesLike,
|
||||
options: DecoderEmbeddedOptions<T> = {},
|
||||
): Annotated<T> {
|
||||
export function decodeWithAnnotations<T>(bs: BytesLike,
|
||||
options: DecoderEmbeddedOptions<T> = {}): Annotated<T> {
|
||||
return decode(bs, { ... options, includeAnnotations: true }) as Annotated<T>;
|
||||
}
|
||||
|
|
|
@ -1,22 +1,19 @@
|
|||
import { Encoder, canonicalString } from "./encoder";
|
||||
import { Tag } from "./constants";
|
||||
import { FlexMap, FlexSet, _iterMap, IdentitySet, Equivalence, IsMap } from "./flex";
|
||||
import { FlexMap, FlexSet, _iterMap, IdentitySet } from "./flex";
|
||||
import { Value } from "./values";
|
||||
import { Bytes } from './bytes';
|
||||
import { Embeddable, GenericEmbedded, isEmbedded } from "./embedded";
|
||||
import { GenericEmbedded } from "./embedded";
|
||||
import type { Preservable } from "./encoder";
|
||||
import type { Writer, PreserveWritable } from "./writer";
|
||||
import { annotations, Annotated } from "./annotated";
|
||||
import { Float } from "./float";
|
||||
import { JsDictionary } from "./jsdictionary";
|
||||
import { unannotate } from "./strip";
|
||||
|
||||
export type DictionaryType = 'Dictionary' | 'Set';
|
||||
export const DictionaryType = Symbol.for('DictionaryType');
|
||||
|
||||
export type CompoundKey<T extends Embeddable> = Value<T> | (Preservable<T> & PreserveWritable<T>);
|
||||
export type CompoundKey<T> = Value<T> | (Preservable<T> & PreserveWritable<T>);
|
||||
|
||||
export class EncodableDictionary<T extends Embeddable, K, V> extends FlexMap<K, V>
|
||||
export class EncodableDictionary<K, V, T = GenericEmbedded> extends FlexMap<K, V>
|
||||
implements Preservable<T>, PreserveWritable<T>
|
||||
{
|
||||
constructor(
|
||||
|
@ -42,16 +39,16 @@ export class EncodableDictionary<T extends Embeddable, K, V> extends FlexMap<K,
|
|||
}
|
||||
}
|
||||
|
||||
export class KeyedDictionary<T extends Embeddable = GenericEmbedded, K extends CompoundKey<T> = Value<T>, V = Value<T>>
|
||||
extends EncodableDictionary<T, K, V>
|
||||
export class KeyedDictionary<K extends CompoundKey<T>, V, T = GenericEmbedded>
|
||||
extends EncodableDictionary<K, V, T>
|
||||
{
|
||||
get [DictionaryType](): DictionaryType {
|
||||
return 'Dictionary';
|
||||
}
|
||||
|
||||
static isKeyedDictionary<T extends Embeddable = GenericEmbedded, K extends CompoundKey<T> = Value<T>, V = Value<T>>(
|
||||
static isKeyedDictionary<K extends CompoundKey<T>, V, T = GenericEmbedded>(
|
||||
x: any,
|
||||
): x is KeyedDictionary<T, K, V> {
|
||||
): x is KeyedDictionary<K, V, T> {
|
||||
return x?.[DictionaryType] === 'Dictionary';
|
||||
}
|
||||
|
||||
|
@ -61,204 +58,33 @@ export class KeyedDictionary<T extends Embeddable = GenericEmbedded, K extends C
|
|||
super(k => k, v => v as CompoundKey<T>, items);
|
||||
}
|
||||
|
||||
clone(): KeyedDictionary<T, K, V> {
|
||||
mapEntries<W, S extends Value<R>, R = GenericEmbedded>(f: (entry: [K, V]) => [S, W]): KeyedDictionary<S, W, R> {
|
||||
const result = new KeyedDictionary<S, W, R>();
|
||||
for (let oldEntry of this.entries()) {
|
||||
const newEntry = f(oldEntry);
|
||||
result.set(newEntry[0], newEntry[1])
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
clone(): KeyedDictionary<K, V, T> {
|
||||
return new KeyedDictionary(this);
|
||||
}
|
||||
|
||||
get [Symbol.toStringTag]() { return 'Dictionary'; }
|
||||
|
||||
equals(otherAny: any, eqv: Equivalence<V> = (v1, v2) => v1 === v2): boolean {
|
||||
const otherMap = Dictionary.asMap(otherAny);
|
||||
if (!otherMap) return false;
|
||||
return super.equals(otherMap, eqv);
|
||||
}
|
||||
}
|
||||
|
||||
export type Dictionary<T extends Embeddable = GenericEmbedded, V = Value<T>> =
|
||||
JsDictionary<V> | KeyedDictionary<T, Value<T>, V>;
|
||||
|
||||
export class DictionaryMap<T extends Embeddable = GenericEmbedded, V = Value<T>> implements Map<Value<T>, V> {
|
||||
get [IsMap](): boolean { return true; }
|
||||
|
||||
j: JsDictionary<V> | undefined;
|
||||
k: KeyedDictionary<T, Value<T>, V> | undefined;
|
||||
|
||||
constructor(input?: Dictionary<T, V>) {
|
||||
if (input === void 0) {
|
||||
this.j = {};
|
||||
this.k = void 0;
|
||||
} else if (DictionaryType in input) {
|
||||
this.j = void 0;
|
||||
this.k = input;
|
||||
} else {
|
||||
this.j = input;
|
||||
this.k = void 0;
|
||||
}
|
||||
export class Dictionary<T = GenericEmbedded, V = Value<T>> extends KeyedDictionary<Value<T>, V, T> {
|
||||
static isDictionary<T = GenericEmbedded, V = Value<T>>(x: any): x is Dictionary<T, V> {
|
||||
return x?.[DictionaryType] === 'Dictionary';
|
||||
}
|
||||
|
||||
static from<T extends Embeddable = GenericEmbedded, V = Value<T>>(
|
||||
entries: [Value<T>, V][] | Iterable<[Value<T>, V]>,
|
||||
): DictionaryMap<T, V> {
|
||||
const r = new DictionaryMap<T, V>();
|
||||
for (const [key, value] of entries) r.set(key, value);
|
||||
return r;
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
if (this.j) {
|
||||
JsDictionary.clear(this.j);
|
||||
} else {
|
||||
this.k!.clear();
|
||||
}
|
||||
}
|
||||
|
||||
delete(key: Value<T>): boolean {
|
||||
if (this.j) {
|
||||
key = unannotate(key);
|
||||
if (typeof key !== 'symbol') return false;
|
||||
return JsDictionary.remove(this.j, key);
|
||||
} else {
|
||||
return this.k!.delete(key);
|
||||
}
|
||||
}
|
||||
|
||||
forEach(callbackfn: (value: V, key: Value<T>, map: Map<Value<T>, V>) => void, thisArg?: any): void {
|
||||
if (this.j) {
|
||||
JsDictionary.forEach(this.j, (v, k) => callbackfn.call(thisArg, v, k, this));
|
||||
} else {
|
||||
this.k!.forEach(callbackfn, thisArg);
|
||||
}
|
||||
}
|
||||
|
||||
get(key: Value<T>): V | undefined {
|
||||
if (this.j) {
|
||||
key = unannotate(key);
|
||||
if (typeof key !== 'symbol') return void 0;
|
||||
return JsDictionary.get(this.j, key);
|
||||
} else {
|
||||
return this.k!.get(key);
|
||||
}
|
||||
}
|
||||
|
||||
has(key: Value<T>): boolean {
|
||||
if (this.j) {
|
||||
key = unannotate(key);
|
||||
if (typeof key !== 'symbol') return false;
|
||||
return JsDictionary.has(this.j, key);
|
||||
} else {
|
||||
return this.k!.has(key);
|
||||
}
|
||||
}
|
||||
|
||||
set(key: Value<T>, value: V): this {
|
||||
if (this.j) {
|
||||
if (typeof key === 'symbol') {
|
||||
JsDictionary.set(this.j, key, value);
|
||||
return this;
|
||||
}
|
||||
this.k = new KeyedDictionary<T, Value<T>, V>(JsDictionary.entries(this.j));
|
||||
this.j = void 0;
|
||||
}
|
||||
this.k!.set(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
get size(): number {
|
||||
return this.j ? JsDictionary.size(this.j) : this.k!.size;
|
||||
}
|
||||
|
||||
entries(): IterableIterator<[Value<T>, V]> {
|
||||
return this.j ? JsDictionary.entries(this.j) : this.k!.entries();
|
||||
}
|
||||
|
||||
keys(): IterableIterator<Value<T>> {
|
||||
return this.j ? JsDictionary.keys(this.j) : this.k!.keys();
|
||||
}
|
||||
|
||||
values(): IterableIterator<V> {
|
||||
return this.j ? JsDictionary.values(this.j) : this.k!.values();
|
||||
}
|
||||
|
||||
[Symbol.iterator](): IterableIterator<[Value<T>, V]> {
|
||||
return this.entries();
|
||||
}
|
||||
|
||||
get [Symbol.toStringTag](): string {
|
||||
return 'DictionaryMap';
|
||||
}
|
||||
|
||||
clone(): DictionaryMap<T, V> {
|
||||
return new DictionaryMap<T, V>(this.j ? JsDictionary.clone(this.j) : this.k!.clone());
|
||||
}
|
||||
|
||||
get value(): Dictionary<T, V> {
|
||||
return this.j ?? this.k!;
|
||||
}
|
||||
|
||||
simplify(): void {
|
||||
if (!this.j) {
|
||||
const r: JsDictionary<V> = {};
|
||||
for (const [key, value] of this.k!.entries()) {
|
||||
if (typeof key !== 'symbol') return;
|
||||
r[key.description!] = value;
|
||||
}
|
||||
this.j = r;
|
||||
this.k = void 0;
|
||||
}
|
||||
}
|
||||
|
||||
simplifiedValue(): Dictionary<T, V> {
|
||||
this.simplify();
|
||||
return this.value;
|
||||
}
|
||||
|
||||
asJsDictionary(): JsDictionary<V> {
|
||||
this.simplify();
|
||||
if (!this.j) throw new Error("Cannot represent general dictionary as JsDictionary");
|
||||
return this.j;
|
||||
}
|
||||
|
||||
asKeyedDictionary(): KeyedDictionary<T, Value<T>, V> {
|
||||
return this.k ?? new KeyedDictionary<T, Value<T>, V>(JsDictionary.entries(this.j!));
|
||||
}
|
||||
}
|
||||
|
||||
export namespace Dictionary {
|
||||
export function isDictionary<T extends Embeddable = GenericEmbedded, V = Value<T>>(
|
||||
x: any
|
||||
): x is Dictionary<T, V> {
|
||||
if (typeof x !== 'object' || x === null) return false;
|
||||
switch (x[DictionaryType]) {
|
||||
case 'Dictionary': return true;
|
||||
case void 0: return JsDictionary.isJsDictionary(x);
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function asMap<T extends Embeddable = GenericEmbedded, V = Value<T>>(
|
||||
x: Dictionary<T, V>
|
||||
): DictionaryMap<T, V>;
|
||||
export function asMap<T extends Embeddable = GenericEmbedded, V = Value<T>>(
|
||||
x: any
|
||||
): DictionaryMap<T, V> | undefined;
|
||||
export function asMap<T extends Embeddable = GenericEmbedded, V = Value<T>>(
|
||||
x: any
|
||||
): DictionaryMap<T, V> | undefined {
|
||||
return isDictionary<T, V>(x) ? new DictionaryMap(x) : void 0;
|
||||
}
|
||||
|
||||
export function from<T extends Embeddable = GenericEmbedded, V = Value<T>>(
|
||||
entries: [Value<T>, V][] | Iterable<[Value<T>, V]>,
|
||||
): Dictionary<T, V> {
|
||||
return DictionaryMap.from(entries).simplifiedValue();
|
||||
}
|
||||
|
||||
export function __from_preserve__<T extends Embeddable>(v: Value<T>): undefined | Dictionary<T> {
|
||||
static __from_preserve__<T>(v: Value<T>): undefined | Dictionary<T> {
|
||||
return Dictionary.isDictionary<T>(v) ? v : void 0;
|
||||
}
|
||||
}
|
||||
|
||||
export function encodeDictionaryOn<T extends Embeddable, K, V>(
|
||||
export function encodeDictionaryOn<K, V, T>(
|
||||
dict: Map<K, V>,
|
||||
encoder: Encoder<T>,
|
||||
encodeK: (k: K, encoder: Encoder<T>) => void,
|
||||
|
@ -288,7 +114,7 @@ export function encodeDictionaryOn<T extends Embeddable, K, V>(
|
|||
}
|
||||
}
|
||||
|
||||
export function writeDictionaryOn<T extends Embeddable, K, V>(
|
||||
export function writeDictionaryOn<K, V, T>(
|
||||
dict: Map<K, V>,
|
||||
w: Writer<T>,
|
||||
writeK: (k: K, w: Writer<T>) => void,
|
||||
|
@ -309,7 +135,7 @@ export function writeDictionaryOn<T extends Embeddable, K, V>(
|
|||
});
|
||||
}
|
||||
|
||||
export class EncodableSet<T extends Embeddable, V> extends FlexSet<V>
|
||||
export class EncodableSet<V, T = GenericEmbedded> extends FlexSet<V>
|
||||
implements Preservable<T>, PreserveWritable<T>
|
||||
{
|
||||
constructor(
|
||||
|
@ -328,16 +154,16 @@ export class EncodableSet<T extends Embeddable, V> extends FlexSet<V>
|
|||
}
|
||||
}
|
||||
|
||||
export class KeyedSet<T extends Embeddable = GenericEmbedded, K extends CompoundKey<T> = Value<T>>
|
||||
extends EncodableSet<T, K>
|
||||
export class KeyedSet<K extends CompoundKey<T>, T = GenericEmbedded>
|
||||
extends EncodableSet<K, T>
|
||||
{
|
||||
get [DictionaryType](): DictionaryType {
|
||||
return 'Set';
|
||||
}
|
||||
|
||||
static isKeyedSet<T extends Embeddable = GenericEmbedded, K extends CompoundKey<T> = Value<T>>(
|
||||
static isKeyedSet<K extends CompoundKey<T>, T = GenericEmbedded>(
|
||||
x: any,
|
||||
): x is KeyedSet<T, K> {
|
||||
): x is KeyedSet<K, T> {
|
||||
return x?.[DictionaryType] === 'Set';
|
||||
}
|
||||
|
||||
|
@ -345,36 +171,34 @@ export class KeyedSet<T extends Embeddable = GenericEmbedded, K extends Compound
|
|||
super(k => k, items);
|
||||
}
|
||||
|
||||
map<R extends Embeddable = GenericEmbedded, S extends Value<R> = Value<R>>(
|
||||
f: (value: K) => S,
|
||||
): KeyedSet<R, S> {
|
||||
map<S extends Value<R>, R = GenericEmbedded>(f: (value: K) => S): KeyedSet<S, R> {
|
||||
return new KeyedSet(_iterMap(this[Symbol.iterator](), f));
|
||||
}
|
||||
|
||||
filter(f: (value: K) => boolean): KeyedSet<T, K> {
|
||||
const result = new KeyedSet<T, K>();
|
||||
filter(f: (value: K) => boolean): KeyedSet<K, T> {
|
||||
const result = new KeyedSet<K, T>();
|
||||
for (let k of this) if (f(k)) result.add(k);
|
||||
return result;
|
||||
}
|
||||
|
||||
clone(): KeyedSet<T, K> {
|
||||
clone(): KeyedSet<K, T> {
|
||||
return new KeyedSet(this);
|
||||
}
|
||||
|
||||
get [Symbol.toStringTag]() { return 'Set'; }
|
||||
}
|
||||
|
||||
export class Set<T extends Embeddable = GenericEmbedded> extends KeyedSet<T> {
|
||||
static isSet<T extends Embeddable = GenericEmbedded>(x: any): x is Set<T> {
|
||||
export class Set<T = GenericEmbedded> extends KeyedSet<Value<T>, T> {
|
||||
static isSet<T = GenericEmbedded>(x: any): x is Set<T> {
|
||||
return x?.[DictionaryType] === 'Set';
|
||||
}
|
||||
|
||||
static __from_preserve__<T extends Embeddable>(v: Value<T>): undefined | Set<T> {
|
||||
static __from_preserve__<T>(v: Value<T>): undefined | Set<T> {
|
||||
return Set.isSet<T>(v) ? v : void 0;
|
||||
}
|
||||
}
|
||||
|
||||
export function encodeSetOn<T extends Embeddable, V>(
|
||||
export function encodeSetOn<V, T>(
|
||||
s: IdentitySet<V>,
|
||||
encoder: Encoder<T>,
|
||||
encodeV: (v: V, encoder: Encoder<T>) => void,
|
||||
|
@ -395,7 +219,7 @@ export function encodeSetOn<T extends Embeddable, V>(
|
|||
}
|
||||
}
|
||||
|
||||
export function writeSetOn<T extends Embeddable, V>(
|
||||
export function writeSetOn<V, T>(
|
||||
s: IdentitySet<V>,
|
||||
w: Writer<T>,
|
||||
writeV: (v: V, w: Writer<T>) => void,
|
||||
|
|
|
@ -3,17 +3,7 @@ import type { EncoderState } from "./encoder";
|
|||
import type { Value } from "./values";
|
||||
import { ReaderStateOptions } from "./reader";
|
||||
|
||||
export const IsEmbedded = Symbol.for('IsEmbedded');
|
||||
|
||||
export interface Embeddable {
|
||||
readonly [IsEmbedded]: true;
|
||||
}
|
||||
|
||||
export function isEmbedded<T extends Embeddable>(v: any): v is T {
|
||||
return !!v?.[IsEmbedded];
|
||||
}
|
||||
|
||||
export type EmbeddedTypeEncode<T extends Embeddable> = {
|
||||
export type EmbeddedTypeEncode<T> = {
|
||||
encode(s: EncoderState, v: T): void;
|
||||
}
|
||||
|
||||
|
@ -22,22 +12,46 @@ export type EmbeddedTypeDecode<T> = {
|
|||
fromValue(v: Value<GenericEmbedded>, options: ReaderStateOptions): T;
|
||||
}
|
||||
|
||||
export type EmbeddedType<T extends Embeddable> = EmbeddedTypeEncode<T> & EmbeddedTypeDecode<T>;
|
||||
export type EmbeddedType<T> = EmbeddedTypeEncode<T> & EmbeddedTypeDecode<T>;
|
||||
|
||||
export class Embedded<T> {
|
||||
get [IsEmbedded](): true { return true; }
|
||||
embeddedValue: T;
|
||||
|
||||
constructor(public readonly value: T) {}
|
||||
|
||||
equals(other: any): boolean {
|
||||
return typeof other === 'object' && 'value' in other && Object.is(this.value, other.value);
|
||||
constructor(embeddedValue: T) {
|
||||
this.embeddedValue = embeddedValue;
|
||||
}
|
||||
|
||||
equals(other: any, is: (a: any, b: any) => boolean) {
|
||||
return isEmbedded<T>(other) && is(this.embeddedValue, other.embeddedValue);
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return '#!' + (this.embeddedValue as any).toString();
|
||||
}
|
||||
|
||||
__as_preserve__<R>(): T extends R ? Value<R> : never {
|
||||
return this as any;
|
||||
}
|
||||
|
||||
static __from_preserve__<T>(v: Value<T>): undefined | Embedded<T> {
|
||||
return isEmbedded<T>(v) ? v : void 0;
|
||||
}
|
||||
}
|
||||
|
||||
export function embed<T>(embeddedValue: T): Embedded<T> {
|
||||
return new Embedded(embeddedValue);
|
||||
}
|
||||
|
||||
export function isEmbedded<T>(v: Value<T>): v is Embedded<T> {
|
||||
return typeof v === 'object' && 'embeddedValue' in v;
|
||||
}
|
||||
|
||||
export class GenericEmbedded {
|
||||
get [IsEmbedded](): true { return true; }
|
||||
generic: Value;
|
||||
|
||||
constructor(public readonly generic: Value) {}
|
||||
constructor(generic: Value) {
|
||||
this.generic = generic;
|
||||
}
|
||||
|
||||
equals(other: any, is: (a: any, b: any) => boolean) {
|
||||
return typeof other === 'object' && 'generic' in other && is(this.generic, other.generic);
|
||||
|
|
|
@ -3,18 +3,17 @@ import { Bytes, unhexDigit } from "./bytes";
|
|||
import { Value } from "./values";
|
||||
import { EncodeError } from "./codec";
|
||||
import { Record, Tuple } from "./record";
|
||||
import { EmbeddedTypeEncode, isEmbedded } from "./embedded";
|
||||
import type { Embeddable } from "./embedded";
|
||||
import { DictionaryMap, encodeDictionaryOn } from "./dictionary";
|
||||
import { EmbeddedTypeEncode } from "./embedded";
|
||||
import type { Embedded } from "./embedded";
|
||||
|
||||
export type Encodable<T extends Embeddable> =
|
||||
export type Encodable<T> =
|
||||
Value<T> | Preservable<T> | Iterable<Value<T>> | ArrayBufferView;
|
||||
|
||||
export interface Preservable<T extends Embeddable> {
|
||||
export interface Preservable<T> {
|
||||
__preserve_on__(encoder: Encoder<T>): void;
|
||||
}
|
||||
|
||||
export function isPreservable<T extends Embeddable>(v: any): v is Preservable<T> {
|
||||
export function isPreservable<T>(v: any): v is Preservable<T> {
|
||||
return typeof v === 'object' && v !== null && '__preserve_on__' in v && typeof v.__preserve_on__ === 'function';
|
||||
}
|
||||
|
||||
|
@ -23,11 +22,11 @@ export interface EncoderOptions {
|
|||
includeAnnotations?: boolean;
|
||||
}
|
||||
|
||||
export interface EncoderEmbeddedOptions<T extends Embeddable> extends EncoderOptions {
|
||||
export interface EncoderEmbeddedOptions<T> extends EncoderOptions {
|
||||
embeddedEncode?: EmbeddedTypeEncode<T>;
|
||||
}
|
||||
|
||||
function asLatin1(bs: Uint8Array): string {
|
||||
export function asLatin1(bs: Uint8Array): string {
|
||||
return String.fromCharCode.apply(null, bs as any as number[]);
|
||||
}
|
||||
|
||||
|
@ -200,7 +199,7 @@ export class EncoderState {
|
|||
}
|
||||
}
|
||||
|
||||
export class Encoder<T extends Embeddable> {
|
||||
export class Encoder<T = object> {
|
||||
state: EncoderState;
|
||||
embeddedEncode: EmbeddedTypeEncode<T>;
|
||||
|
||||
|
@ -219,7 +218,7 @@ export class Encoder<T extends Embeddable> {
|
|||
}
|
||||
}
|
||||
|
||||
withEmbeddedEncode<S extends Embeddable>(
|
||||
withEmbeddedEncode<S>(
|
||||
embeddedEncode: EmbeddedTypeEncode<S>,
|
||||
body: (e: Encoder<S>) => void): this
|
||||
{
|
||||
|
@ -250,7 +249,7 @@ export class Encoder<T extends Embeddable> {
|
|||
}
|
||||
|
||||
push(v: Encodable<T>) {
|
||||
if (isPreservable<any>(v)) {
|
||||
if (isPreservable<unknown>(v)) {
|
||||
v.__preserve_on__(this);
|
||||
}
|
||||
else if (isPreservable<T>(v)) {
|
||||
|
@ -289,21 +288,17 @@ export class Encoder<T extends Embeddable> {
|
|||
for (let i of v) this.push(i);
|
||||
});
|
||||
}
|
||||
else if (isEmbedded<T>(v)) {
|
||||
this.state.emitbyte(Tag.Embedded);
|
||||
this.embeddedEncode.encode(this.state, v);
|
||||
}
|
||||
else {
|
||||
encodeDictionaryOn(new DictionaryMap<T>(v),
|
||||
this,
|
||||
(k, e) => e.push(k),
|
||||
(v, e) => e.push(v));
|
||||
((v: Embedded<T>) => {
|
||||
this.state.emitbyte(Tag.Embedded);
|
||||
this.embeddedEncode.encode(this.state, v.embeddedValue);
|
||||
})(v);
|
||||
}
|
||||
return this; // for chaining
|
||||
}
|
||||
}
|
||||
|
||||
export function encode<T extends Embeddable>(
|
||||
export function encode<T>(
|
||||
v: Encodable<T>,
|
||||
options: EncoderEmbeddedOptions<T> = {}): Bytes
|
||||
{
|
||||
|
@ -337,9 +332,7 @@ export function canonicalString(v: Encodable<any>): string {
|
|||
}
|
||||
}
|
||||
|
||||
export function encodeWithAnnotations<T extends Embeddable>(
|
||||
v: Encodable<T>,
|
||||
options: EncoderEmbeddedOptions<T> = {},
|
||||
): Bytes {
|
||||
export function encodeWithAnnotations<T>(v: Encodable<T>,
|
||||
options: EncoderEmbeddedOptions<T> = {}): Bytes {
|
||||
return encode(v, { ... options, includeAnnotations: true });
|
||||
}
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
import { Tag } from "./constants";
|
||||
import { Value } from "./values";
|
||||
import type { Embeddable, GenericEmbedded } from "./embedded";
|
||||
import type { GenericEmbedded } from "./embedded";
|
||||
import type { Encoder, Preservable } from "./encoder";
|
||||
import type { Writer, PreserveWritable } from "./writer";
|
||||
import { Bytes, dataview } from "./bytes";
|
||||
|
||||
// v Previously included 'Single'; may again in future. Also, 'Half', 'Quad'?
|
||||
export type FloatType = 'Double';
|
||||
export type FloatType = 'Single' | 'Double';
|
||||
export const FloatType = Symbol.for('FloatType');
|
||||
|
||||
export abstract class Float {
|
||||
|
@ -38,6 +37,7 @@ export abstract class Float {
|
|||
abstract get [FloatType](): FloatType;
|
||||
|
||||
static isFloat = (x: any): x is Float => x?.[FloatType] !== void 0;
|
||||
static isSingle = (x: any): x is SingleFloat => x?.[FloatType] === 'Single';
|
||||
static isDouble = (x: any): x is DoubleFloat => x?.[FloatType] === 'Double';
|
||||
}
|
||||
|
||||
|
@ -58,42 +58,79 @@ export function floatlikeString(f: number): string {
|
|||
return s + '.0';
|
||||
}
|
||||
|
||||
// -- These snippets are useful to keep in mind for promoting 4-byte, single-precision floats
|
||||
// -- to 8-byte, double-precision floats *while preserving NaN bit-patterns*:
|
||||
//
|
||||
// static fromBytes(bs: Bytes | DataView): SingleFloat {
|
||||
// const view = dataview(bs);
|
||||
// const vf = view.getInt32(0, false);
|
||||
// if ((vf & 0x7f800000) === 0x7f800000) {
|
||||
// // NaN or inf. Preserve quiet/signalling bit by manually expanding to double-precision.
|
||||
// const sign = vf >> 31;
|
||||
// const payload = vf & 0x007fffff;
|
||||
// const dbs = new Bytes(8);
|
||||
// const dview = dataview(dbs);
|
||||
// dview.setInt16(0, (sign << 15) | 0x7ff0 | (payload >> 19), false);
|
||||
// dview.setInt32(2, (payload & 0x7ffff) << 13, false);
|
||||
// return new SingleFloat(dview.getFloat64(0, false));
|
||||
// } else {
|
||||
// return new SingleFloat(dataview(bs).getFloat32(0, false));
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// __w(v: DataView, offset: number) {
|
||||
// if (Number.isNaN(this.value)) {
|
||||
// const dbs = new Bytes(8);
|
||||
// const dview = dataview(dbs);
|
||||
// dview.setFloat64(0, this.value, false);
|
||||
// const sign = dview.getInt8(0) >> 7;
|
||||
// const payload = (dview.getInt32(1, false) >> 5) & 0x007fffff;
|
||||
// const vf = (sign << 31) | 0x7f800000 | payload;
|
||||
// v.setInt32(offset, vf, false);
|
||||
// } else {
|
||||
// v.setFloat32(offset, this.value, false);
|
||||
// }
|
||||
// }
|
||||
export class SingleFloat extends Float implements Preservable<any>, PreserveWritable<any> {
|
||||
__as_preserve__<T = GenericEmbedded>(): Value<T> {
|
||||
return this;
|
||||
}
|
||||
|
||||
static fromBytes(bs: Bytes | DataView): SingleFloat {
|
||||
const view = dataview(bs);
|
||||
const vf = view.getInt32(0, false);
|
||||
if ((vf & 0x7f800000) === 0x7f800000) {
|
||||
// NaN or inf. Preserve quiet/signalling bit by manually expanding to double-precision.
|
||||
const sign = vf >> 31;
|
||||
const payload = vf & 0x007fffff;
|
||||
const dbs = new Bytes(8);
|
||||
const dview = dataview(dbs);
|
||||
dview.setInt16(0, (sign << 15) | 0x7ff0 | (payload >> 19), false);
|
||||
dview.setInt32(2, (payload & 0x7ffff) << 13, false);
|
||||
return new SingleFloat(dview.getFloat64(0, false));
|
||||
} else {
|
||||
return new SingleFloat(dataview(bs).getFloat32(0, false));
|
||||
}
|
||||
}
|
||||
|
||||
static __from_preserve__<T>(v: Value<T>): undefined | SingleFloat {
|
||||
return Float.isSingle(v) ? v : void 0;
|
||||
}
|
||||
|
||||
__w(v: DataView, offset: number) {
|
||||
if (Number.isNaN(this.value)) {
|
||||
const dbs = new Bytes(8);
|
||||
const dview = dataview(dbs);
|
||||
dview.setFloat64(0, this.value, false);
|
||||
const sign = dview.getInt8(0) >> 7;
|
||||
const payload = (dview.getInt32(1, false) >> 5) & 0x007fffff;
|
||||
const vf = (sign << 31) | 0x7f800000 | payload;
|
||||
v.setInt32(offset, vf, false);
|
||||
} else {
|
||||
v.setFloat32(offset, this.value, false);
|
||||
}
|
||||
}
|
||||
|
||||
__preserve_on__(encoder: Encoder<any>) {
|
||||
encoder.state.emitbyte(Tag.Ieee754);
|
||||
encoder.state.emitbyte(4);
|
||||
encoder.state.makeroom(4);
|
||||
this.__w(encoder.state.view, encoder.state.index);
|
||||
encoder.state.index += 4;
|
||||
}
|
||||
|
||||
toBytes(): Bytes {
|
||||
const bs = new Bytes(4);
|
||||
this.__w(bs.dataview(), 0);
|
||||
return bs;
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
if (Number.isFinite(this.value)) {
|
||||
return floatlikeString(this.value) + 'f';
|
||||
} else {
|
||||
return '#xf"' + this.toBytes().toHex() + '"';
|
||||
}
|
||||
}
|
||||
|
||||
get [FloatType](): 'Single' {
|
||||
return 'Single';
|
||||
}
|
||||
}
|
||||
|
||||
export function Single(value: number | Float): SingleFloat {
|
||||
return new SingleFloat(value);
|
||||
}
|
||||
|
||||
export class DoubleFloat extends Float implements Preservable<any>, PreserveWritable<any> {
|
||||
__as_preserve__<T extends Embeddable = GenericEmbedded>(): Value<T> {
|
||||
__as_preserve__<T = GenericEmbedded>(): Value<T> {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -101,7 +138,7 @@ export class DoubleFloat extends Float implements Preservable<any>, PreserveWrit
|
|||
return new DoubleFloat(dataview(bs).getFloat64(0, false));
|
||||
}
|
||||
|
||||
static __from_preserve__<T extends Embeddable>(v: Value<T>): undefined | DoubleFloat {
|
||||
static __from_preserve__<T>(v: Value<T>): undefined | DoubleFloat {
|
||||
return Float.isDouble(v) ? v : void 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
import { Record, Tuple } from "./record";
|
||||
import { Bytes } from "./bytes";
|
||||
import { Value } from "./values";
|
||||
import { Set, KeyedDictionary, Dictionary, DictionaryMap } from "./dictionary";
|
||||
import { Set, Dictionary } from "./dictionary";
|
||||
import { annotate, Annotated } from "./annotated";
|
||||
import { Double, Float } from "./float";
|
||||
import { Embeddable, isEmbedded } from "./embedded";
|
||||
import { Double, Float, Single } from "./float";
|
||||
import { Embedded } from "./embedded";
|
||||
|
||||
export enum ValueClass {
|
||||
Boolean,
|
||||
Float,
|
||||
Double,
|
||||
SignedInteger,
|
||||
String,
|
||||
|
@ -21,10 +22,11 @@ export enum ValueClass {
|
|||
Annotated, // quasi-class
|
||||
}
|
||||
|
||||
export type Fold<T extends Embeddable, R = Value<T>> = (v: Value<T>) => R;
|
||||
export type Fold<T, R = Value<T>> = (v: Value<T>) => R;
|
||||
|
||||
export interface FoldMethods<T extends Embeddable, R> {
|
||||
export interface FoldMethods<T, R> {
|
||||
boolean(b: boolean): R;
|
||||
single(f: number): R;
|
||||
double(f: number): R;
|
||||
integer(i: number | bigint): R;
|
||||
string(s: string): R;
|
||||
|
@ -34,42 +36,46 @@ export interface FoldMethods<T extends Embeddable, R> {
|
|||
record(r: Record<Value<T>, Tuple<Value<T>>, T>, k: Fold<T, R>): R;
|
||||
array(a: Array<Value<T>>, k: Fold<T, R>): R;
|
||||
set(s: Set<T>, k: Fold<T, R>): R;
|
||||
dictionary(d: DictionaryMap<T>, k: Fold<T, R>): R;
|
||||
dictionary(d: Dictionary<T>, k: Fold<T, R>): R;
|
||||
|
||||
annotated(a: Annotated<T>, k: Fold<T, R>): R;
|
||||
|
||||
embedded(t: T, k: Fold<T, R>): R;
|
||||
embedded(t: Embedded<T>, k: Fold<T, R>): R;
|
||||
}
|
||||
|
||||
export class VoidFold<T extends Embeddable> implements FoldMethods<T, void> {
|
||||
boolean(_b: boolean): void {}
|
||||
double(_f: number): void {}
|
||||
integer(_i: number | bigint): void {}
|
||||
string(_s: string): void {}
|
||||
bytes(_b: Bytes): void {}
|
||||
symbol(_s: symbol): void {}
|
||||
export class VoidFold<T> implements FoldMethods<T, void> {
|
||||
boolean(b: boolean): void {}
|
||||
single(f: number): void {}
|
||||
double(f: number): void {}
|
||||
integer(i: number | bigint): void {}
|
||||
string(s: string): void {}
|
||||
bytes(b: Bytes): void {}
|
||||
symbol(s: symbol): void {}
|
||||
record(r: Record<Value<T>, Tuple<Value<T>>, T>, k: Fold<T, void>): void {
|
||||
k(r.label);
|
||||
r.forEach(k);
|
||||
}
|
||||
array(a: Value<T>[], k: Fold<T, void>): void { a.forEach(k); }
|
||||
set(s: Set<T>, k: Fold<T, void>): void { s.forEach(k); }
|
||||
dictionary(d: DictionaryMap<T>, k: Fold<T, void>): void {
|
||||
dictionary(d: Dictionary<T>, k: Fold<T, void>): void {
|
||||
d.forEach((value, key) => { k(key); k(value); });
|
||||
}
|
||||
annotated(a: Annotated<T>, k: Fold<T, void>): void { k(a.item); a.annotations.forEach(k); }
|
||||
embedded(_t: T, _k: Fold<T, void>): void {}
|
||||
embedded(_t: Embedded<T>, _k: Fold<T, void>): void {}
|
||||
}
|
||||
|
||||
export class ForEachEmbedded<T extends Embeddable> extends VoidFold<T> {
|
||||
export class ForEachEmbedded<T> extends VoidFold<T> {
|
||||
constructor(public readonly f: (t: T, k: Fold<T, void>) => void) { super(); }
|
||||
embedded(t: T, k: Fold<T, void>): void { this.f(t, k); }
|
||||
embedded(t: Embedded<T>, k: Fold<T, void>): void { this.f(t.embeddedValue, k); }
|
||||
}
|
||||
|
||||
export abstract class ValueFold<T extends Embeddable, R extends Embeddable = T> implements FoldMethods<T, Value<R>> {
|
||||
export abstract class ValueFold<T, R = T> implements FoldMethods<T, Value<R>> {
|
||||
boolean(b: boolean): Value<R> {
|
||||
return b;
|
||||
}
|
||||
single(f: number): Value<R> {
|
||||
return Single(f);
|
||||
}
|
||||
double(f: number): Value<R> {
|
||||
return Double(f);
|
||||
}
|
||||
|
@ -94,24 +100,22 @@ export abstract class ValueFold<T extends Embeddable, R extends Embeddable = T>
|
|||
set(s: Set<T>, k: Fold<T, Value<R>>): Value<R> {
|
||||
return s.map(k);
|
||||
}
|
||||
dictionary(d: DictionaryMap<T>, k: Fold<T, Value<R>>): Value<R> {
|
||||
const result = new DictionaryMap<R>();
|
||||
d.forEach((value, key) => result.set(k(key), k(value)));
|
||||
return result.simplifiedValue();
|
||||
dictionary(d: Dictionary<T>, k: Fold<T, Value<R>>): Value<R> {
|
||||
return d.mapEntries(([key, value]) => [k(key), k(value)]);
|
||||
}
|
||||
annotated(a: Annotated<T>, k: Fold<T, Value<R>>): Value<R> {
|
||||
return annotate(k(a.item), ...a.annotations.map(k));
|
||||
}
|
||||
abstract embedded(t: T, k: Fold<T, Value<R>>): Value<R>;
|
||||
abstract embedded(t: Embedded<T>, k: Fold<T, Value<R>>): Value<R>;
|
||||
}
|
||||
|
||||
export class IdentityFold<T extends Embeddable> extends ValueFold<T, T> {
|
||||
embedded(t: T, _k: Fold<T, Value<T>>): Value<T> {
|
||||
export class IdentityFold<T> extends ValueFold<T, T> {
|
||||
embedded(t: Embedded<T>, _k: Fold<T, Value<T>>): Value<T> {
|
||||
return t;
|
||||
}
|
||||
}
|
||||
|
||||
export class MapFold<T extends Embeddable, R extends Embeddable> extends ValueFold<T, R> {
|
||||
export class MapFold<T, R> extends ValueFold<T, R> {
|
||||
readonly f: (t: T) => Value<R>;
|
||||
|
||||
constructor(f: (t: T) => Value<R>) {
|
||||
|
@ -119,18 +123,18 @@ export class MapFold<T extends Embeddable, R extends Embeddable> extends ValueFo
|
|||
this.f = f;
|
||||
}
|
||||
|
||||
embedded(t: T, _k: Fold<T, Value<R>>): Value<R> {
|
||||
return this.f(t);
|
||||
embedded(t: Embedded<T>, _k: Fold<T, Value<R>>): Value<R> {
|
||||
return this.f(t.embeddedValue);
|
||||
}
|
||||
}
|
||||
|
||||
export function valueClass<T extends Embeddable>(v: Value<T>): ValueClass {
|
||||
export function valueClass<T>(v: Value<T>): ValueClass {
|
||||
switch (typeof v) {
|
||||
case 'boolean':
|
||||
return ValueClass.Boolean;
|
||||
case 'number':
|
||||
if (!Number.isInteger(v)) {
|
||||
throw new Error("Non-integer number in Preserves valueClass; missing Float wrapper?");
|
||||
throw new Error("Non-integer number in Preserves valueClass; missing SingleFloat/DoubleFloat wrapper?");
|
||||
} else {
|
||||
return ValueClass.SignedInteger;
|
||||
}
|
||||
|
@ -153,10 +157,12 @@ export function valueClass<T extends Embeddable>(v: Value<T>): ValueClass {
|
|||
return ValueClass.Annotated;
|
||||
} else if (Bytes.isBytes(v)) {
|
||||
return ValueClass.ByteString;
|
||||
} else if (Float.isSingle(v)) {
|
||||
return ValueClass.Float;
|
||||
} else if (Float.isDouble(v)) {
|
||||
return ValueClass.Double;
|
||||
} else {
|
||||
return ((_v: T) => ValueClass.Embedded)(v);
|
||||
return ValueClass.Embedded;
|
||||
}
|
||||
default:
|
||||
((_v: never): never => { throw new Error("Internal error"); })(v);
|
||||
|
@ -165,7 +171,7 @@ export function valueClass<T extends Embeddable>(v: Value<T>): ValueClass {
|
|||
|
||||
export const IDENTITY_FOLD = new IdentityFold<any>();
|
||||
|
||||
export function fold<T extends Embeddable, R>(v: Value<T>, o: FoldMethods<T, R>): R {
|
||||
export function fold<T, R>(v: Value<T>, o: FoldMethods<T, R>): R {
|
||||
const walk = (v: Value<T>): R => {
|
||||
switch (typeof v) {
|
||||
case 'boolean':
|
||||
|
@ -190,16 +196,18 @@ export function fold<T extends Embeddable, R>(v: Value<T>, o: FoldMethods<T, R>)
|
|||
return o.array(v, walk);
|
||||
} else if (Set.isSet<T>(v)) {
|
||||
return o.set(v, walk);
|
||||
} else if (isEmbedded(v)) {
|
||||
return o.embedded(v, walk);
|
||||
} else if (Dictionary.isDictionary<T>(v)) {
|
||||
return o.dictionary(v, walk);
|
||||
} else if (Annotated.isAnnotated<T>(v)) {
|
||||
return o.annotated(v, walk);
|
||||
} else if (Bytes.isBytes(v)) {
|
||||
return o.bytes(v);
|
||||
} else if (Float.isSingle(v)) {
|
||||
return o.single(v.value);
|
||||
} else if (Float.isDouble(v)) {
|
||||
return o.double(v.value);
|
||||
} else if (Dictionary.isDictionary<T>(v)) {
|
||||
return o.dictionary(new DictionaryMap(v), walk);
|
||||
} else {
|
||||
return o.embedded(v, walk);
|
||||
}
|
||||
default:
|
||||
((_v: never): never => { throw new Error("Internal error"); })(v);
|
||||
|
@ -208,7 +216,7 @@ export function fold<T extends Embeddable, R>(v: Value<T>, o: FoldMethods<T, R>)
|
|||
return walk(v);
|
||||
}
|
||||
|
||||
export function mapEmbeddeds<T extends Embeddable, R extends Embeddable>(
|
||||
export function mapEmbeddeds<T, R>(
|
||||
v: Value<T>,
|
||||
f: (t: T) => Value<R>,
|
||||
): Value<R>
|
||||
|
@ -216,9 +224,6 @@ export function mapEmbeddeds<T extends Embeddable, R extends Embeddable>(
|
|||
return fold(v, new MapFold(f));
|
||||
}
|
||||
|
||||
export function forEachEmbedded<T extends Embeddable>(
|
||||
v: Value<T>,
|
||||
f: (t: T, k: Fold<T, void>) => void,
|
||||
): void {
|
||||
export function forEachEmbedded<T>(v: Value<T>, f: (t: T, k: Fold<T, void>) => void): void {
|
||||
return fold(v, new ForEachEmbedded(f));
|
||||
}
|
||||
|
|
|
@ -1,27 +1,15 @@
|
|||
import { Embeddable, GenericEmbedded, isEmbedded } from "./embedded";
|
||||
import { embed, GenericEmbedded } from "./embedded";
|
||||
import { Bytes } from "./bytes";
|
||||
import { Record, Tuple } from "./record";
|
||||
import { Value } from "./values";
|
||||
import { Dictionary, KeyedDictionary, Set } from "./dictionary";
|
||||
import { JsDictionary } from "./jsdictionary";
|
||||
import { Dictionary, Set } from "./dictionary";
|
||||
|
||||
export interface FromJSOptions<T extends Embeddable = GenericEmbedded> {
|
||||
onNonInteger?(n: number): Value<T> | undefined;
|
||||
}
|
||||
|
||||
export function fromJS<T extends Embeddable = GenericEmbedded>(x: any): Value<T> {
|
||||
return fromJS_options(x);
|
||||
}
|
||||
|
||||
export function fromJS_options<T extends Embeddable = GenericEmbedded>(x: any, options?: FromJSOptions<T>): Value<T> {
|
||||
function walk(x: any): Value<T> {
|
||||
export function fromJS<T = GenericEmbedded>(x: any): Value<T> {
|
||||
switch (typeof x) {
|
||||
case 'number':
|
||||
if (!Number.isInteger(x)) {
|
||||
// We require that clients be explicit about integer vs. non-integer types.
|
||||
const converted = options?.onNonInteger?.(x) ?? void 0;
|
||||
if (converted !== void 0) return converted;
|
||||
throw new TypeError("Refusing to autoconvert non-integer number to Double");
|
||||
throw new TypeError("Refusing to autoconvert non-integer number to Single or Double");
|
||||
}
|
||||
// FALL THROUGH
|
||||
case 'bigint':
|
||||
|
@ -45,30 +33,23 @@ export function fromJS_options<T extends Embeddable = GenericEmbedded>(x: any, o
|
|||
return x;
|
||||
}
|
||||
if (Array.isArray(x)) {
|
||||
return x.map<Value<T>>(walk);
|
||||
return x.map<Value<T>>(fromJS);
|
||||
}
|
||||
if (ArrayBuffer.isView(x) || x instanceof ArrayBuffer) {
|
||||
return Bytes.from(x);
|
||||
}
|
||||
if (Map.isMap(x)) {
|
||||
const d = new KeyedDictionary<T>();
|
||||
x.forEach((v, k) => d.set(walk(k), walk(v)));
|
||||
const d = new Dictionary<T>();
|
||||
x.forEach((v, k) => d.set(fromJS(k), fromJS(v)));
|
||||
return d;
|
||||
}
|
||||
if (Set.isSet(x)) {
|
||||
const s = new Set<T>();
|
||||
x.forEach(v => s.add(walk(v)));
|
||||
x.forEach(v => s.add(fromJS(v)));
|
||||
return s;
|
||||
}
|
||||
if (isEmbedded<T>(x)) {
|
||||
return x;
|
||||
}
|
||||
// Handle plain JS objects to build a JsDictionary
|
||||
{
|
||||
const r: JsDictionary<Value<T>> = {};
|
||||
Object.entries(x).forEach(([k, v]) => r[k] = walk(v));
|
||||
return r;
|
||||
}
|
||||
// Just... assume it's a T.
|
||||
return embed(x as T);
|
||||
|
||||
default:
|
||||
break;
|
||||
|
@ -77,21 +58,15 @@ export function fromJS_options<T extends Embeddable = GenericEmbedded>(x: any, o
|
|||
throw new TypeError("Cannot represent JavaScript value as Preserves: " + x);
|
||||
}
|
||||
|
||||
return walk(x);
|
||||
}
|
||||
|
||||
declare module "./dictionary" {
|
||||
namespace Dictionary {
|
||||
export function stringMap<T extends Embeddable = GenericEmbedded>(
|
||||
x: object
|
||||
): KeyedDictionary<T, string, Value<T>>;
|
||||
export function fromJS<T = GenericEmbedded, V = GenericEmbedded>(x: object): Dictionary<T, Value<V>>;
|
||||
}
|
||||
}
|
||||
|
||||
Dictionary.stringMap = function <T extends Embeddable = GenericEmbedded>(
|
||||
x: object
|
||||
): KeyedDictionary<T, string, Value<T>> {
|
||||
const r = new KeyedDictionary<T, string, Value<T>>();
|
||||
Object.entries(x).forEach(([key, value]) => r.set(key, fromJS(value)));
|
||||
return r;
|
||||
Dictionary.fromJS = function <T = GenericEmbedded, V = GenericEmbedded>(x: object): Dictionary<T, Value<V>> {
|
||||
if (Dictionary.isDictionary<T, Value<V>>(x)) return x;
|
||||
const d = new Dictionary<T, Value<V>>();
|
||||
Object.entries(x).forEach(([key, value]) => d.set(key, fromJS(value)));
|
||||
return d;
|
||||
};
|
||||
|
|
|
@ -1,17 +1,15 @@
|
|||
export * from './runtime';
|
||||
export * as Constants from './constants';
|
||||
export * as Pexpr from './pexpr';
|
||||
|
||||
import type { Embeddable } from './embedded';
|
||||
import type { Value } from './values';
|
||||
|
||||
declare global {
|
||||
interface ArrayConstructor {
|
||||
__from_preserve__<T extends Embeddable>(v: Value<T>): undefined | Array<Value<T>>;
|
||||
__from_preserve__<T>(v: Value<T>): undefined | Array<Value<T>>;
|
||||
}
|
||||
}
|
||||
|
||||
Array.__from_preserve__ = <T extends Embeddable>(v: Value<T>) => {
|
||||
Array.__from_preserve__ = <T>(v: Value<T>) => {
|
||||
return Array.isArray(v) ? v : void 0;
|
||||
};
|
||||
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
import type { Embeddable, GenericEmbedded } from "./embedded";
|
||||
import type { GenericEmbedded } from "./embedded";
|
||||
import type { Annotated } from "./annotated";
|
||||
import { Dictionary } from "./dictionary";
|
||||
|
||||
export const IsPreservesAnnotated = Symbol.for('IsPreservesAnnotated');
|
||||
|
||||
export function isAnnotated<T extends Embeddable = GenericEmbedded>(x: any): x is Annotated<T>
|
||||
export function isAnnotated<T = GenericEmbedded>(x: any): x is Annotated<T>
|
||||
{
|
||||
return !!x?.[IsPreservesAnnotated];
|
||||
}
|
||||
|
@ -31,17 +30,6 @@ export function is(a: any, b: any): boolean {
|
|||
for (let i = 0; i < a.length; i++) if (!is(a[i], b[i])) return false;
|
||||
return true;
|
||||
}
|
||||
{
|
||||
const aMap = Dictionary.asMap(a);
|
||||
const bMap = Dictionary.asMap(b);
|
||||
if (!aMap || !bMap) return false;
|
||||
if (aMap.size !== bMap.size) return false;
|
||||
for (const k of aMap.keys()) {
|
||||
if (!bMap.has(k)) return false;
|
||||
if (!is(aMap.get(k), bMap.get(k))) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -1,89 +0,0 @@
|
|||
import { isEmbedded } from './embedded';
|
||||
import { Equivalence, _iterMap } from './flex';
|
||||
|
||||
export interface JsDictionary<V> {
|
||||
[key: string]: V;
|
||||
}
|
||||
|
||||
export namespace JsDictionary {
|
||||
export function isJsDictionary<V>(x: any): x is JsDictionary<V> {
|
||||
// We accept only literal objects and objects created via `new Object` as dictionaries.
|
||||
// Furthermore, we require no function-valued `__as_preserve__` property to exist.
|
||||
return typeof x === 'object'
|
||||
&& x !== null
|
||||
&& Object.getPrototypeOf(Object.getPrototypeOf(x)) === null
|
||||
&& typeof x.__as_preserve__ !== 'function'
|
||||
&& !isEmbedded(x);
|
||||
}
|
||||
|
||||
export function from<V>(entries: Iterable<[symbol, V]>): JsDictionary<V> {
|
||||
const r: JsDictionary<V> = {};
|
||||
for (const [key, value] of entries) r[key.description!] = value;
|
||||
return r;
|
||||
}
|
||||
|
||||
export function clear<V>(j: JsDictionary<V>): void {
|
||||
for (const key in j) delete j[key];
|
||||
}
|
||||
|
||||
export function remove<V>(j: JsDictionary<V>, key: symbol): boolean {
|
||||
const result = has(j, key);
|
||||
delete j[key.description!];
|
||||
return result;
|
||||
}
|
||||
|
||||
export function forEach<V>(
|
||||
j: JsDictionary<V>,
|
||||
callbackfn: (value: V, key: symbol) => void,
|
||||
): void {
|
||||
Object.entries(j).forEach(([key, val]) => callbackfn(val, Symbol.for(key)));
|
||||
}
|
||||
|
||||
export function get<V>(j: JsDictionary<V>, key: symbol): V | undefined {
|
||||
return j[key.description!];
|
||||
}
|
||||
|
||||
export function has<V>(j: JsDictionary<V>, key: symbol): boolean {
|
||||
return Object.hasOwnProperty.call(j, key.description!);
|
||||
}
|
||||
|
||||
export function set<V>(j: JsDictionary<V>, key: symbol, value: V): JsDictionary<V> {
|
||||
j[key.description!] = value;
|
||||
return j;
|
||||
}
|
||||
|
||||
export function size<V>(j: JsDictionary<V>): number {
|
||||
return Object.keys(j).length;
|
||||
}
|
||||
|
||||
export function entries<V>(j: JsDictionary<V>): IterableIterator<[symbol, V]> {
|
||||
return _iterMap(Object.entries(j).values(), ([k, v]) => [Symbol.for(k), v]);
|
||||
}
|
||||
|
||||
export function keys<V>(j: JsDictionary<V>): IterableIterator<symbol> {
|
||||
return _iterMap(Object.keys(j).values(), k => Symbol.for(k));
|
||||
}
|
||||
|
||||
export function values<V>(j: JsDictionary<V>): IterableIterator<V> {
|
||||
return Object.values(j).values();
|
||||
}
|
||||
|
||||
export function clone<V>(j: JsDictionary<V>): JsDictionary<V> {
|
||||
const r: JsDictionary<V> = {};
|
||||
Object.keys(j).forEach(k => r[k] = j[k]);
|
||||
return r;
|
||||
}
|
||||
|
||||
export function equals<V>(
|
||||
j1: JsDictionary<V>,
|
||||
j2: JsDictionary<V>,
|
||||
eqv: Equivalence<V> = (v1, v2) => v1 === v2,
|
||||
): boolean {
|
||||
if (size(j1) !== size(j2)) return false;
|
||||
for (let [k, v] of entries(j1)) {
|
||||
if (!has(j2, k)) return false;
|
||||
if (!eqv(v, get(j2, k)!)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -3,15 +3,13 @@ import { Bytes } from "./bytes";
|
|||
import { fold } from "./fold";
|
||||
import { is } from "./is";
|
||||
import { Value } from "./values";
|
||||
import { Set, Dictionary, KeyedDictionary, DictionaryMap } from "./dictionary";
|
||||
import { Set, Dictionary } from "./dictionary";
|
||||
import { Annotated } from "./annotated";
|
||||
import { unannotate } from "./strip";
|
||||
import { isEmbedded } from "./embedded";
|
||||
import { embed, isEmbedded, Embedded } from "./embedded";
|
||||
import { isCompound } from "./compound";
|
||||
import type { Embeddable } from "./embedded";
|
||||
import { JsDictionary } from "./jsdictionary";
|
||||
|
||||
export function merge<T extends Embeddable>(
|
||||
export function merge<T>(
|
||||
mergeEmbeddeds: (a: T, b: T) => T | undefined,
|
||||
item0: Value<T>,
|
||||
... items: Array<Value<T>>): Value<T>
|
||||
|
@ -34,6 +32,7 @@ export function merge<T extends Embeddable>(
|
|||
}
|
||||
return fold<T, Value<T>>(a, {
|
||||
boolean: die,
|
||||
single(_f: number) { return is(a, b) ? a : die(); },
|
||||
double(_f: number) { return is(a, b) ? a : die(); },
|
||||
integer: die,
|
||||
string: die,
|
||||
|
@ -44,38 +43,33 @@ export function merge<T extends Embeddable>(
|
|||
if (!Record.isRecord<Value<T>, Tuple<Value<T>>, T>(b)) die();
|
||||
return Record(walk(r.label, b.label), walkMany(r, b));
|
||||
},
|
||||
|
||||
array(a: Array<Value<T>>) {
|
||||
if (!Array.isArray(b) || Record.isRecord(b)) die();
|
||||
return walkMany(a, b);
|
||||
},
|
||||
|
||||
set(_s: Set<T>) { die(); },
|
||||
|
||||
dictionary(aMap: DictionaryMap<T>) {
|
||||
const bMap = Dictionary.asMap<T>(b);
|
||||
if (bMap === void 0) die();
|
||||
|
||||
const r = new DictionaryMap<T>();
|
||||
aMap.forEach((av,ak) => {
|
||||
const bv = bMap.get(ak);
|
||||
dictionary(d: Dictionary<T>) {
|
||||
if (!Dictionary.isDictionary<T>(b)) die();
|
||||
const r = new Dictionary<T>();
|
||||
d.forEach((av,ak) => {
|
||||
const bv = b.get(ak);
|
||||
r.set(ak, bv === void 0 ? av : walk(av, bv));
|
||||
});
|
||||
bMap.forEach((bv, bk) => {
|
||||
if (!aMap.has(bk)) r.set(bk, bv);
|
||||
b.forEach((bv, bk) => {
|
||||
if (!d.has(bk)) r.set(bk, bv);
|
||||
});
|
||||
return r.simplifiedValue();
|
||||
return r;
|
||||
},
|
||||
|
||||
annotated(a: Annotated<T>) {
|
||||
return walk(a, unannotate(b));
|
||||
},
|
||||
|
||||
embedded(t: T) {
|
||||
embedded(t: Embedded<T>) {
|
||||
if (!isEmbedded<T>(b)) die();
|
||||
const r = mergeEmbeddeds(t, b);
|
||||
const r = mergeEmbeddeds(t.embeddedValue, b.embeddedValue);
|
||||
if (r === void 0) die();
|
||||
return r;
|
||||
return embed(r);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
|
@ -2,12 +2,12 @@
|
|||
|
||||
import { Annotated } from './annotated';
|
||||
import { Bytes } from './bytes';
|
||||
import { Set, EncodableDictionary } from './dictionary';
|
||||
import { Set, Dictionary } from './dictionary';
|
||||
import { stringify } from './text';
|
||||
|
||||
import * as util from 'util';
|
||||
|
||||
[Bytes, Annotated, Set, EncodableDictionary].forEach((C) => {
|
||||
[Bytes, Annotated, Set, Dictionary].forEach((C) => {
|
||||
(C as any).prototype[util.inspect.custom] =
|
||||
function (_depth: any, _options: any) {
|
||||
return stringify(this, { indent: 2 });
|
||||
|
|
|
@ -1,14 +1,13 @@
|
|||
import { is, isAnnotated } from './is';
|
||||
import { Bytes } from './bytes';
|
||||
import { Set, Dictionary } from './dictionary';
|
||||
import { isEmbedded } from './embedded';
|
||||
import { Embedded, isEmbedded } from './embedded';
|
||||
import { Float } from './float';
|
||||
import { Value } from './values';
|
||||
import { Record } from './record';
|
||||
import type { Embeddable } from './embedded';
|
||||
|
||||
export function typeCode<T extends Embeddable>(v: Value<T>): number {
|
||||
if (isAnnotated<T>(v)) v = v.item;
|
||||
export function typeCode<V>(v: Value<V>): number {
|
||||
if (isAnnotated<V>(v)) v = v.item;
|
||||
switch (typeof v) {
|
||||
case 'boolean':
|
||||
return 0;
|
||||
|
@ -20,28 +19,30 @@ export function typeCode<T extends Embeddable>(v: Value<T>): number {
|
|||
case 'symbol':
|
||||
return 6;
|
||||
case 'object':
|
||||
if (Float.isFloat(v)) return 2; // 1 was for single-precision floats
|
||||
if (Float.isFloat(v)) {
|
||||
return Float.isSingle(v) ? 1 : 2;
|
||||
}
|
||||
if (Bytes.isBytes(v)) return 5;
|
||||
if (Array.isArray(v)) {
|
||||
return ('label' in v) ? 7 : 8;
|
||||
}
|
||||
if (Set.isSet<T>(v)) return 9;
|
||||
if (Dictionary.isDictionary<T>(v)) return 10;
|
||||
if (Set.isSet<V>(v)) return 9;
|
||||
if (Dictionary.isDictionary<V>(v)) return 10;
|
||||
if (isEmbedded(v)) return 11;
|
||||
/* fall through */
|
||||
default:
|
||||
throw new Error("Invalid Value<T> in typeCode");
|
||||
throw new Error("Invalid Value<V> in typeCode");
|
||||
}
|
||||
}
|
||||
|
||||
export function compare<T extends Embeddable>(
|
||||
a: Value<T>,
|
||||
b: Value<T>,
|
||||
compare_embedded: (a: T, b: T) => number = (a, b) => is(a, b) ? 0 : a < b ? -1 : 1,
|
||||
export function compare<V>(
|
||||
a: Value<V>,
|
||||
b: Value<V>,
|
||||
compare_embedded: (a: V, b: V) => number = (a, b) => is(a, b) ? 0 : a < b ? -1 : 1,
|
||||
): number {
|
||||
function cmp(a: Value<T>, b: Value<T>): number {
|
||||
if (isAnnotated<T>(a)) a = a.item;
|
||||
if (isAnnotated<T>(b)) b = b.item;
|
||||
function cmp(a: Value<V>, b: Value<V>): number {
|
||||
if (isAnnotated<V>(a)) a = a.item;
|
||||
if (isAnnotated<V>(b)) b = b.item;
|
||||
const ta = typeCode(a);
|
||||
const tb = typeCode(b);
|
||||
if (ta < tb) return -1;
|
||||
|
@ -54,7 +55,7 @@ export function compare<T extends Embeddable>(
|
|||
const vb = b as any;
|
||||
return va < vb ? -1 : va > vb ? 1 : 0;
|
||||
}
|
||||
// case 1: // was single-precision
|
||||
case 1:
|
||||
case 2: {
|
||||
const va = (a as Float).value;
|
||||
const vb = (b as Float).value;
|
||||
|
@ -68,14 +69,14 @@ export function compare<T extends Embeddable>(
|
|||
return va < vb ? -1 : va > vb ? 1 : 0;
|
||||
}
|
||||
case 7: {
|
||||
const lr = cmp((a as Record<Value<T>, Value<T>[], T>).label,
|
||||
(b as Record<Value<T>, Value<T>[], T>).label);
|
||||
const lr = cmp((a as Record<Value<V>, Value<V>[], V>).label,
|
||||
(b as Record<Value<V>, Value<V>[], V>).label);
|
||||
if (lr !== 0) return lr;
|
||||
/* fall through */
|
||||
}
|
||||
case 8: {
|
||||
const va = a as Value<T>[];
|
||||
const vb = b as Value<T>[];
|
||||
const va = a as Value<V>[];
|
||||
const vb = b as Value<V>[];
|
||||
const l = Math.min(va.length, vb.length)
|
||||
for (let i = 0; i < l; i++) {
|
||||
const c = cmp(va[i], vb[i]);
|
||||
|
@ -84,17 +85,18 @@ export function compare<T extends Embeddable>(
|
|||
return va.length < vb.length ? -1 : va.length > vb.length ? 1 : 0;
|
||||
}
|
||||
case 9: {
|
||||
const va = Array.from(a as Set<T>).sort(cmp);
|
||||
const vb = Array.from(b as Set<T>).sort(cmp);
|
||||
const va = Array.from(a as Set<V>).sort(cmp);
|
||||
const vb = Array.from(b as Set<V>).sort(cmp);
|
||||
return cmp(va, vb);
|
||||
}
|
||||
case 10: {
|
||||
const va = Array.from(Dictionary.asMap<T>(a)!.entries()).sort(cmp);
|
||||
const vb = Array.from(Dictionary.asMap<T>(b)!.entries()).sort(cmp);
|
||||
const va = Array.from(a as Dictionary<V>).sort(cmp);
|
||||
const vb = Array.from(b as Dictionary<V>).sort(cmp);
|
||||
return cmp(va, vb);
|
||||
}
|
||||
case 11:
|
||||
return compare_embedded(a as T, b as T);
|
||||
return compare_embedded((a as Embedded<V>).embeddedValue,
|
||||
(b as Embedded<V>).embeddedValue);
|
||||
default:
|
||||
throw new Error("Invalid typeCode: " + ta);
|
||||
}
|
||||
|
|
|
@ -1,353 +0,0 @@
|
|||
// Preserves-Expressions. https://preserves.dev/preserves-expressions.html
|
||||
|
||||
import { ReaderBase } from './reader';
|
||||
import { Atom, Value } from './values';
|
||||
import { Position, annotate, formatPosition } from './annotated';
|
||||
import { Record as VRecord } from './record';
|
||||
import { Embeddable, GenericEmbedded } from './embedded';
|
||||
import { fromJS } from './fromjs';
|
||||
import { DictionaryMap, Set as VSet } from './dictionary';
|
||||
|
||||
export type Expr = SimpleExpr | Punct;
|
||||
export type SimpleExpr = Atom | Compound | Embedded;
|
||||
|
||||
export type Positioned<I> = { position: Position, item: I, annotations?: Annotations };
|
||||
|
||||
export class Punct {
|
||||
constructor(public text: string) {}
|
||||
__as_preserve__(): Value { return VRecord(Symbol.for('p'), [Symbol.for(this.text)]); }
|
||||
|
||||
isComma(): boolean { return this.text === ','; }
|
||||
static isComma(v: Expr): boolean { return v instanceof Punct && v.isComma(); }
|
||||
|
||||
isColon(n = 1): boolean { return this.text === ':'.repeat(n); }
|
||||
static isColon(v: Expr, n = 1): boolean { return v instanceof Punct && v.isColon(n); }
|
||||
}
|
||||
|
||||
export class Embedded {
|
||||
constructor(public expr: SimpleExpr, public annotations?: Annotations) {}
|
||||
__as_preserve__(): Value {
|
||||
const v = fromJS(this.expr);
|
||||
return new GenericEmbedded(this.annotations?.wrap(v) ?? v);
|
||||
}
|
||||
}
|
||||
|
||||
export class BaseCompound<I> {
|
||||
positions: Position[] = [];
|
||||
exprs: I[] = [];
|
||||
annotations?: Annotations[] = void 0; // sparse array when non-void
|
||||
|
||||
get(i: number): Positioned<I> | undefined {
|
||||
if (i >= this.exprs.length) return void 0;
|
||||
return {
|
||||
position: this.positions[i],
|
||||
item: this.exprs[i],
|
||||
annotations: this.annotations && this.annotations[i],
|
||||
};
|
||||
}
|
||||
|
||||
push(p: Positioned<I>): true;
|
||||
push(expr: I, position: Position, annotations?: Annotations): true;
|
||||
push(v: Positioned<I> | I, position?: Position, annotations?: Annotations) {
|
||||
if (position === void 0) {
|
||||
const p = v as Positioned<I>;
|
||||
if (p.annotations) this._ensureAnnotations()[this.exprs.length] = p.annotations;
|
||||
this.positions.push(p.position);
|
||||
this.exprs.push(p.item);
|
||||
} else {
|
||||
if (annotations) this._ensureAnnotations()[this.exprs.length] = annotations;
|
||||
this.positions.push(position);
|
||||
this.exprs.push(v as I);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
_ensureAnnotations(): Annotations[] {
|
||||
if (this.annotations === void 0) this.annotations = [];
|
||||
return this.annotations;
|
||||
}
|
||||
|
||||
_annotationsAt(index: number): Annotations {
|
||||
return this._ensureAnnotations()[index] ??= new Annotations();
|
||||
}
|
||||
|
||||
preservesValues(): Value[] {
|
||||
return this.exprs.map((p, i) => {
|
||||
const v = fromJS(p);
|
||||
if (this.annotations?.[i] !== void 0) {
|
||||
return this.annotations[i].wrap(v);
|
||||
} else {
|
||||
return v;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
__as_preserve__(): Value {
|
||||
return this.preservesValues();
|
||||
}
|
||||
|
||||
map<R>(f: (item: Positioned<I>, index: number) => R, offset = 0): R[] {
|
||||
const result: R[] = [];
|
||||
for (let i = offset; i < this.exprs.length; i++) {
|
||||
result.push(f(this.get(i)!, i));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
[Symbol.iterator](): IterableIterator<Positioned<I>> {
|
||||
let c = this;
|
||||
let i = 0;
|
||||
return {
|
||||
next(): IteratorResult<Positioned<I>> {
|
||||
if (i < c.exprs.length) {
|
||||
return { done: false, value: c.get(i++)! };
|
||||
} else {
|
||||
return { done: true, value: void 0 };
|
||||
}
|
||||
},
|
||||
[Symbol.iterator]() { return c[Symbol.iterator](); }
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export class Document extends BaseCompound<Expr> {}
|
||||
|
||||
export class Annotations extends BaseCompound<SimpleExpr> {
|
||||
wrap(v: Value): Value {
|
||||
return annotate(v, ... this.preservesValues());
|
||||
}
|
||||
}
|
||||
|
||||
export type CompoundVariant = 'sequence' | 'record' | 'block' | 'group' | 'set';
|
||||
|
||||
export abstract class Compound extends BaseCompound<Expr> {
|
||||
abstract get variant(): CompoundVariant;
|
||||
__as_preserve__(): Value {
|
||||
const vs = this.preservesValues();
|
||||
switch (this.variant) {
|
||||
case 'sequence': return vs;
|
||||
case 'record': return VRecord(Symbol.for('r'), vs);
|
||||
case 'block': return VRecord(Symbol.for('b'), vs);
|
||||
case 'group': return VRecord(Symbol.for('g'), vs);
|
||||
case 'set': return VRecord(Symbol.for('s'), vs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class Sequence extends Compound {
|
||||
get variant(): CompoundVariant { return 'sequence'; }
|
||||
}
|
||||
|
||||
export class Record extends Compound {
|
||||
get variant(): CompoundVariant { return 'record'; }
|
||||
}
|
||||
|
||||
export class Block extends Compound {
|
||||
get variant(): CompoundVariant { return 'block'; }
|
||||
}
|
||||
|
||||
export class Group extends Compound {
|
||||
get variant(): CompoundVariant { return 'group'; }
|
||||
}
|
||||
|
||||
export class Set extends Compound {
|
||||
get variant(): CompoundVariant { return 'set'; }
|
||||
}
|
||||
|
||||
export class Reader extends ReaderBase<never> {
|
||||
nextDocument(howMany: 'all' | 'one' = 'all'): Document {
|
||||
const doc = new Document();
|
||||
this.readExpr(doc);
|
||||
if (howMany === 'all') {
|
||||
while (this.readExpr(doc)) {}
|
||||
}
|
||||
return doc;
|
||||
}
|
||||
|
||||
readCompound(c: Compound, terminator: string): Compound {
|
||||
while (this.readExpr(c, terminator)) {}
|
||||
return c;
|
||||
}
|
||||
|
||||
readSimpleExpr(c: BaseCompound<SimpleExpr>): boolean {
|
||||
return this._readInto(c, false);
|
||||
}
|
||||
|
||||
readExpr(c: BaseCompound<Expr>, terminator: string | null = null): boolean {
|
||||
return this._readInto(c as BaseCompound<SimpleExpr> /* yuck */, true, terminator);
|
||||
}
|
||||
|
||||
_checkTerminator(actual: string, expected: string | null, startPos: Position): false {
|
||||
if (actual === expected) return false;
|
||||
this.state.error('Unexpected ' + actual, startPos);
|
||||
}
|
||||
|
||||
_readInto(c: BaseCompound<SimpleExpr>, acceptPunct: boolean, terminator: string | null = null): boolean {
|
||||
while (true) {
|
||||
this.state.skipws();
|
||||
if (this.state.atEnd() && terminator === null) return false;
|
||||
const startPos = this.state.copyPos();
|
||||
const ch = this.state.nextchar();
|
||||
switch (ch) {
|
||||
case '"':
|
||||
return c.push(this.state.readString('"'), startPos);
|
||||
case '|':
|
||||
return c.push(Symbol.for(this.state.readString('|')), startPos);
|
||||
case ';':
|
||||
if (acceptPunct) {
|
||||
return (c as BaseCompound<Expr>).push(new Punct(';'), startPos);
|
||||
} else {
|
||||
this.state.error('Semicolon is not permitted at this location', startPos);
|
||||
}
|
||||
case '@':
|
||||
if (!this.readSimpleExpr(c._annotationsAt(c.exprs.length))) {
|
||||
this.state.error('Missing annotation', startPos);
|
||||
}
|
||||
continue;
|
||||
case ':': {
|
||||
let colons: string = ch;
|
||||
while (!this.state.atEnd() && this.state.peek() === ':') {
|
||||
colons = colons + ':';
|
||||
this.state.advance();
|
||||
}
|
||||
if (acceptPunct) {
|
||||
return (c as BaseCompound<Expr>).push(new Punct(colons), startPos);
|
||||
} else {
|
||||
this.state.error('Colons are not permitted at this location', startPos);
|
||||
}
|
||||
}
|
||||
case '#': {
|
||||
const ch = this.state.nextchar();
|
||||
switch (ch) {
|
||||
case ' ': case '\t': {
|
||||
const here = this.state.copyPos();
|
||||
c._annotationsAt(c.exprs.length).push(this.state.readCommentLine(), here);
|
||||
continue;
|
||||
}
|
||||
case '\n': case '\r': {
|
||||
const here = this.state.copyPos();
|
||||
c._annotationsAt(c.exprs.length).push('', here);
|
||||
continue;
|
||||
}
|
||||
case '!': {
|
||||
const here = this.state.copyPos();
|
||||
const r = new Record();
|
||||
r.push(Symbol.for('interpreter'), here);
|
||||
r.push(this.state.readCommentLine(), here);
|
||||
c._annotationsAt(c.exprs.length).push(r, here);
|
||||
continue;
|
||||
}
|
||||
case 'f': this.state.requireDelimiter('#f'); return c.push(false, startPos);
|
||||
case 't': this.state.requireDelimiter('#t'); return c.push(true, startPos);
|
||||
case '{': return c.push(this.readCompound(new Set(), '}'), startPos);
|
||||
case '"': return c.push(this.state.readLiteralBinary(), startPos);
|
||||
case 'x': switch (this.state.nextchar()) {
|
||||
case '"': return c.push(this.state.readHexBinary(), startPos);
|
||||
case 'd': return c.push(this.state.readHexFloat(), startPos);
|
||||
default: this.state.error('Invalid #x syntax', startPos);
|
||||
}
|
||||
case '[': return c.push(this.state.readBase64Binary(), startPos);
|
||||
case ':': {
|
||||
const r = new BaseCompound<SimpleExpr>();
|
||||
if (!this.readSimpleExpr(r)) return false;
|
||||
const e = new Embedded(r.exprs[0], r.annotations && r.annotations[0]);
|
||||
return c.push(e, startPos);
|
||||
}
|
||||
default:
|
||||
this.state.error(`Invalid # syntax: ${ch}`, startPos);
|
||||
}
|
||||
}
|
||||
case '(': return c.push(this.readCompound(new Group(), ')'), startPos);
|
||||
case '<': return c.push(this.readCompound(new Record(), '>'), startPos);
|
||||
case '[': return c.push(this.readCompound(new Sequence(), ']'), startPos);
|
||||
case '{': return c.push(this.readCompound(new Block(), '}'), startPos);
|
||||
case ')': return this._checkTerminator(ch, terminator, startPos);
|
||||
case '>': return this._checkTerminator(ch, terminator, startPos);
|
||||
case ']': return this._checkTerminator(ch, terminator, startPos);
|
||||
case '}': return this._checkTerminator(ch, terminator, startPos);
|
||||
case ',':
|
||||
if (acceptPunct) {
|
||||
return (c as BaseCompound<Expr>).push(new Punct(','), startPos);
|
||||
} else {
|
||||
this.state.error('Comma is not permitted at this location', startPos);
|
||||
}
|
||||
default:
|
||||
return c.push(this.state.readRawSymbolOrNumber(ch), startPos);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export interface AsPreservesOptions<T extends Embeddable> {
|
||||
onGroup?: (g: Positioned<Group>) => Value<T>;
|
||||
onEmbedded?: (e: Positioned<Expr>, walk: (p: Positioned<Expr>) => Value<T>) => Value<T>;
|
||||
error?: (tag: string, position: Position) => Value<T>;
|
||||
}
|
||||
|
||||
export function asPreserves<T extends Embeddable>(
|
||||
p: Positioned<Expr>,
|
||||
options: AsPreservesOptions<T> = {},
|
||||
): Value<T> {
|
||||
const error = options.error ?? ((tag, position) => {
|
||||
throw new Error(formatPosition(position) + ": " + tag);
|
||||
});
|
||||
|
||||
function nonCommas(p: Compound): Positioned<Expr>[] {
|
||||
return Array.from(p).filter(p => !Punct.isComma(p.item));
|
||||
}
|
||||
|
||||
function walk(p: Positioned<Expr>): Value<T> {
|
||||
if (p.item instanceof Punct) {
|
||||
return error('invalid-punctuation', p.position);
|
||||
} else if (p.item instanceof Embedded) {
|
||||
if (options.onEmbedded) {
|
||||
return options.onEmbedded({ position: p.position, item: p.item.expr }, walk);
|
||||
} else {
|
||||
return error('unexpected-embedded', p.position);
|
||||
}
|
||||
} else if (p.item instanceof Compound) {
|
||||
switch (p.item.variant) {
|
||||
case 'sequence':
|
||||
return nonCommas(p.item).map(walk);
|
||||
case 'record': {
|
||||
const vs = nonCommas(p.item).map(walk);
|
||||
if (vs.length < 1) {
|
||||
return error('invalid-record', p.position);
|
||||
}
|
||||
const r = vs.slice(1) as unknown as VRecord<Value<T>, Value<T>[], T>;
|
||||
r.label = vs[0];
|
||||
return r;
|
||||
}
|
||||
case 'block': {
|
||||
const d = new DictionaryMap<T>();
|
||||
const vs = nonCommas(p.item);
|
||||
if ((vs.length % 3) !== 0) {
|
||||
return error('invalid-dictionary', p.position);
|
||||
}
|
||||
for (let i = 0; i < vs.length; i += 3) {
|
||||
if (!Punct.isColon(vs[i + 1].item)) {
|
||||
return error('missing-colon', vs[i + 1].position);
|
||||
}
|
||||
const k = walk(vs[i]);
|
||||
const v = walk(vs[i + 2]);
|
||||
d.set(k, v);
|
||||
}
|
||||
return d.simplifiedValue();
|
||||
}
|
||||
case 'group': {
|
||||
if (options.onGroup) {
|
||||
return options.onGroup(p as Positioned<Group>);
|
||||
} else {
|
||||
return error('unexpected-group', p.position);
|
||||
}
|
||||
}
|
||||
case 'set':
|
||||
return new VSet(nonCommas(p.item).map(walk));
|
||||
}
|
||||
} else {
|
||||
return p.item;
|
||||
}
|
||||
}
|
||||
|
||||
return walk(p);
|
||||
}
|
|
@ -2,15 +2,15 @@
|
|||
|
||||
import type { Value } from './values';
|
||||
import { DecodeError, ShortPacket } from './codec';
|
||||
import { Dictionary, DictionaryMap, Set } from './dictionary';
|
||||
import { Dictionary, Set } from './dictionary';
|
||||
import { strip } from './strip';
|
||||
import { Bytes, unhexDigit } from './bytes';
|
||||
import { Decoder, DecoderState, neverEmbeddedTypeDecode } from './decoder';
|
||||
import { Record } from './record';
|
||||
import { Annotated, newPosition, Position, updatePosition } from './annotated';
|
||||
import { Double, DoubleFloat } from './float';
|
||||
import { Double, DoubleFloat, FloatType, Single, SingleFloat } from './float';
|
||||
import { stringify } from './text';
|
||||
import { Embeddable, GenericEmbedded, EmbeddedTypeDecode } from './embedded';
|
||||
import { embed, GenericEmbedded, EmbeddedTypeDecode } from './embedded';
|
||||
|
||||
export interface ReaderStateOptions {
|
||||
includeAnnotations?: boolean;
|
||||
|
@ -24,10 +24,12 @@ export interface ReaderOptions<T> extends ReaderStateOptions {
|
|||
const MAX_SAFE_INTEGERn = BigInt(Number.MAX_SAFE_INTEGER);
|
||||
const MIN_SAFE_INTEGERn = BigInt(Number.MIN_SAFE_INTEGER);
|
||||
|
||||
export const NUMBER_RE: RegExp = /^([-+]?\d+)((\.\d+([eE][-+]?\d+)?)|([eE][-+]?\d+))?$/;
|
||||
export const NUMBER_RE: RegExp = /^([-+]?\d+)(((\.\d+([eE][-+]?\d+)?)|([eE][-+]?\d+))([fF]?))?$/;
|
||||
// Groups:
|
||||
// 1 - integer part and sign
|
||||
// 2 - decimal part and exponent
|
||||
// 2 - decimal part, exponent and Float marker
|
||||
// 3 - decimal part and exponent
|
||||
// 7 - Float marker
|
||||
|
||||
export class ReaderState {
|
||||
buffer: string;
|
||||
|
@ -129,15 +131,21 @@ export class ReaderState {
|
|||
}
|
||||
}
|
||||
|
||||
readHexFloat(): DoubleFloat {
|
||||
readHexFloat(precision: FloatType): SingleFloat | DoubleFloat {
|
||||
const pos = this.copyPos();
|
||||
if (this.nextchar() !== '"') {
|
||||
this.error("Missing open-double-quote in hex-encoded floating-point number", pos);
|
||||
}
|
||||
const bs = this.readHexBinary();
|
||||
switch (precision) {
|
||||
case 'Single':
|
||||
if (bs.length !== 4) this.error("Incorrect number of bytes in hex-encoded Float", pos);
|
||||
return SingleFloat.fromBytes(bs);
|
||||
case 'Double':
|
||||
if (bs.length !== 8) this.error("Incorrect number of bytes in hex-encoded Double", pos);
|
||||
return DoubleFloat.fromBytes(bs);
|
||||
}
|
||||
}
|
||||
|
||||
readBase64Binary(): Bytes {
|
||||
let acc = '';
|
||||
|
@ -155,15 +163,13 @@ export class ReaderState {
|
|||
this.error(`Delimiter must follow ${prefix}`, this.pos);
|
||||
}
|
||||
|
||||
static readonly DELIMITERS = '(){}[]<>";,@#:|';
|
||||
|
||||
delimiterFollows(): boolean {
|
||||
if (this.atEnd()) return true;
|
||||
const ch = this.peek();
|
||||
return (ReaderState.DELIMITERS.indexOf(ch) !== -1) || isSpace(ch);
|
||||
return ('(){}[]<>";,@#:|'.indexOf(ch) !== -1) || isSpace(ch);
|
||||
}
|
||||
|
||||
readRawSymbolOrNumber(acc: string): number | bigint | symbol | DoubleFloat {
|
||||
readRawSymbolOrNumber<T>(acc: string): Value<T> {
|
||||
while (!this.delimiterFollows()) acc = acc + this.nextchar();
|
||||
const m = NUMBER_RE.exec(acc);
|
||||
if (m) {
|
||||
|
@ -174,8 +180,10 @@ export class ReaderState {
|
|||
} else {
|
||||
return Number(v);
|
||||
}
|
||||
} else if (m[7] === '') {
|
||||
return Double(parseFloat(m[1] + m[3]));
|
||||
} else {
|
||||
return Double(parseFloat(acc));
|
||||
return Single(parseFloat(m[1] + m[3]));
|
||||
}
|
||||
} else {
|
||||
return Symbol.for(acc);
|
||||
|
@ -250,16 +258,6 @@ export class ReaderState {
|
|||
'x',
|
||||
() => this.readHex2());
|
||||
}
|
||||
|
||||
readCommentLine(): string {
|
||||
let acc = '';
|
||||
while (true) {
|
||||
if (this.atEnd()) return acc;
|
||||
const c = this.nextchar();
|
||||
if (c === '\n' || c === '\r') return acc;
|
||||
acc = acc + c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const genericEmbeddedTypeDecode: EmbeddedTypeDecode<GenericEmbedded> = {
|
||||
|
@ -272,7 +270,7 @@ export const genericEmbeddedTypeDecode: EmbeddedTypeDecode<GenericEmbedded> = {
|
|||
},
|
||||
};
|
||||
|
||||
export class ReaderBase<T extends Embeddable> {
|
||||
export class Reader<T> {
|
||||
state: ReaderState;
|
||||
embeddedType: EmbeddedTypeDecode<T>;
|
||||
|
||||
|
@ -295,13 +293,17 @@ export class ReaderBase<T extends Embeddable> {
|
|||
write(data: string) {
|
||||
this.state.write(data);
|
||||
}
|
||||
}
|
||||
|
||||
export class Reader<T extends Embeddable> extends ReaderBase<T> {
|
||||
|
||||
readCommentLine(): Value<T> {
|
||||
const startPos = this.state.copyPos();
|
||||
return this.wrap(this.state.readCommentLine(), startPos);
|
||||
let acc = '';
|
||||
while (true) {
|
||||
const c = this.state.nextchar();
|
||||
if (c === '\n' || c === '\r') {
|
||||
return this.wrap(acc, startPos);
|
||||
}
|
||||
acc = acc + c;
|
||||
}
|
||||
}
|
||||
|
||||
wrap(v: Value<T>, pos: Position): Value<T> {
|
||||
|
@ -352,22 +354,20 @@ export class Reader<T extends Embeddable> extends ReaderBase<T> {
|
|||
switch (c) {
|
||||
case ' ': case '\t': return this.annotateNextWith(this.readCommentLine());
|
||||
case '\n': case '\r': return this.annotateNextWith('');
|
||||
case '!':
|
||||
return this.annotateNextWith(
|
||||
Record(Symbol.for('interpreter'), [this.readCommentLine()]));
|
||||
case 'f': this.state.requireDelimiter('#f'); return false;
|
||||
case 't': this.state.requireDelimiter('#t'); return true;
|
||||
case '{': return this.readSet();
|
||||
case '"': return this.state.readLiteralBinary();
|
||||
case 'x': switch (this.state.nextchar()) {
|
||||
case '"': return this.state.readHexBinary();
|
||||
case 'd': return this.state.readHexFloat();
|
||||
case 'f': return this.state.readHexFloat('Single');
|
||||
case 'd': return this.state.readHexFloat('Double');
|
||||
default: this.state.error('Invalid #x syntax', startPos);
|
||||
}
|
||||
case '[': return this.state.readBase64Binary();
|
||||
case ':': return this.embeddedType.fromValue(
|
||||
case '!': return embed(this.embeddedType.fromValue(
|
||||
new Reader<GenericEmbedded>(this.state, genericEmbeddedTypeDecode).next(),
|
||||
this.state.options);
|
||||
this.state.options));
|
||||
default:
|
||||
this.state.error(`Invalid # syntax: ${c}`, startPos);
|
||||
}
|
||||
|
@ -406,18 +406,22 @@ export class Reader<T extends Embeddable> extends ReaderBase<T> {
|
|||
}
|
||||
|
||||
readDictionary(): Dictionary<T> {
|
||||
return this.seq(true, new DictionaryMap<T>(), (k, acc) => {
|
||||
return this.seq(true,
|
||||
new Dictionary<T>(),
|
||||
(k, acc) => {
|
||||
this.state.skipws();
|
||||
switch (this.state.peek()) {
|
||||
case ':':
|
||||
if (acc.has(k)) this.state.error(
|
||||
`Duplicate key: ${stringify(k)}`, this.state.pos);
|
||||
this.state.advance();
|
||||
if (acc.has(k)) this.state.error(`Duplicate key: ${stringify(k)}`, this.state.pos);
|
||||
acc.set(k, this.next());
|
||||
break;
|
||||
default:
|
||||
this.state.error('Missing key/value separator', this.state.pos);
|
||||
}
|
||||
}, '}').simplifiedValue();
|
||||
},
|
||||
'}');
|
||||
}
|
||||
|
||||
readSet(): Set<T> {
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
import { Embeddable, GenericEmbedded } from "./embedded";
|
||||
import { GenericEmbedded } from "./embedded";
|
||||
import { is } from "./is";
|
||||
import { Value } from "./values";
|
||||
import { Writer } from "./writer";
|
||||
|
||||
export type Tuple<T> = Array<T> | [T];
|
||||
|
||||
export type Record<LabelType extends Value<T>, FieldsType extends Tuple<Value<T>>, T extends Embeddable = GenericEmbedded>
|
||||
export type Record<LabelType extends Value<T>, FieldsType extends Tuple<Value<T>>, T = GenericEmbedded>
|
||||
= FieldsType & { label: LabelType };
|
||||
|
||||
export type RecordGetters<Fs, R> = {
|
||||
|
@ -14,7 +15,7 @@ export type RecordGetters<Fs, R> = {
|
|||
export type CtorTypes<Fs, Names extends Tuple<keyof Fs>> =
|
||||
{ [K in keyof Names]: Fs[keyof Fs & Names[K]] } & any[];
|
||||
|
||||
export interface RecordConstructor<L extends Value<T>, Fs, Names extends Tuple<keyof Fs>, T extends Embeddable = GenericEmbedded> {
|
||||
export interface RecordConstructor<L extends Value<T>, Fs, Names extends Tuple<keyof Fs>, T = GenericEmbedded> {
|
||||
(...fields: CtorTypes<Fs, Names>): Record<L, CtorTypes<Fs, Names>, T>;
|
||||
constructorInfo: RecordConstructorInfo<L, T>;
|
||||
isClassOf(v: any): v is Record<L, CtorTypes<Fs, Names>, T>;
|
||||
|
@ -22,7 +23,7 @@ export interface RecordConstructor<L extends Value<T>, Fs, Names extends Tuple<k
|
|||
fieldNumbers: { [K in string & keyof Fs]: number };
|
||||
};
|
||||
|
||||
export interface RecordConstructorInfo<L extends Value<T>, T extends Embeddable = GenericEmbedded> {
|
||||
export interface RecordConstructorInfo<L extends Value<T>, T = GenericEmbedded> {
|
||||
label: L;
|
||||
arity: number;
|
||||
}
|
||||
|
@ -47,23 +48,23 @@ export function Record<L, FieldsType extends Tuple<any>>(
|
|||
}
|
||||
|
||||
export namespace Record {
|
||||
export function isRecord<L extends Value<T>, FieldsType extends Tuple<Value<T>>, T extends Embeddable = GenericEmbedded>(x: any): x is Record<L, FieldsType, T> {
|
||||
export function isRecord<L extends Value<T>, FieldsType extends Tuple<Value<T>>, T = GenericEmbedded>(x: any): x is Record<L, FieldsType, T> {
|
||||
return Array.isArray(x) && 'label' in x;
|
||||
}
|
||||
|
||||
export function constructorInfo<L extends Value<T>, FieldsType extends Tuple<Value<T>>, T extends Embeddable = GenericEmbedded>(
|
||||
export function constructorInfo<L extends Value<T>, FieldsType extends Tuple<Value<T>>, T = GenericEmbedded>(
|
||||
r: Record<L, FieldsType, T>): RecordConstructorInfo<L, T>
|
||||
{
|
||||
return { label: r.label, arity: r.length };
|
||||
}
|
||||
|
||||
export function isClassOf<L extends Value<T>, FieldsType extends Tuple<Value<T>>, T extends Embeddable = GenericEmbedded>(
|
||||
export function isClassOf<L extends Value<T>, FieldsType extends Tuple<Value<T>>, T = GenericEmbedded>(
|
||||
ci: RecordConstructorInfo<L, T>, v: any): v is Record<L, FieldsType, T>
|
||||
{
|
||||
return (Record.isRecord(v)) && is(ci.label, v.label) && (ci.arity === v.length);
|
||||
}
|
||||
|
||||
export function makeConstructor<Fs, T extends Embeddable = GenericEmbedded>()
|
||||
export function makeConstructor<Fs, T = GenericEmbedded>()
|
||||
: (<L extends Value<T>, Names extends Tuple<keyof Fs>>(label: L, fieldNames: Names) =>
|
||||
RecordConstructor<L, Fs, Names, T>)
|
||||
{
|
||||
|
|
|
@ -13,7 +13,6 @@ export * from './float';
|
|||
export * from './fold';
|
||||
export * from './fromjs';
|
||||
export * from './is';
|
||||
export * from './jsdictionary';
|
||||
export * from './merge';
|
||||
export * from './order';
|
||||
export * from './reader';
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
import { Value } from "./values";
|
||||
import { Annotated } from "./annotated";
|
||||
import { Record, Tuple } from "./record";
|
||||
import { Set, Dictionary, DictionaryMap } from "./dictionary";
|
||||
import type { Embeddable, GenericEmbedded } from "./embedded";
|
||||
import { Set, Dictionary } from "./dictionary";
|
||||
import type { GenericEmbedded } from "./embedded";
|
||||
|
||||
export function unannotate<T extends Embeddable = GenericEmbedded>(v: Value<T>): Value<T> {
|
||||
export function unannotate<T = GenericEmbedded>(v: Value<T>): Value<T> {
|
||||
return Annotated.isAnnotated<T>(v) ? v.item : v;
|
||||
}
|
||||
|
||||
export function peel<T extends Embeddable = GenericEmbedded>(v: Value<T>): Value<T> {
|
||||
export function peel<T = GenericEmbedded>(v: Value<T>): Value<T> {
|
||||
return strip(v, 1);
|
||||
}
|
||||
|
||||
export function strip<T extends Embeddable = GenericEmbedded>(
|
||||
export function strip<T = GenericEmbedded>(
|
||||
v: Value<T>,
|
||||
depth: number = Infinity): Value<T>
|
||||
{
|
||||
|
@ -34,9 +34,7 @@ export function strip<T extends Embeddable = GenericEmbedded>(
|
|||
} else if (Set.isSet<T>(v.item)) {
|
||||
return v.item.map(walk);
|
||||
} else if (Dictionary.isDictionary<T>(v.item)) {
|
||||
const result = new DictionaryMap<T>();
|
||||
new DictionaryMap<T>(v.item).forEach((val, key) => result.set(walk(key), walk(val)));
|
||||
return result.simplifiedValue();
|
||||
return v.item.mapEntries((e) => [walk(e[0]), walk(e[1])]);
|
||||
} else {
|
||||
return v.item;
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { Embeddable, GenericEmbedded, isEmbedded } from './embedded';
|
||||
import { Embedded, GenericEmbedded } from './embedded';
|
||||
import type { Value } from './values';
|
||||
|
||||
import { Annotated } from './annotated';
|
||||
|
@ -8,28 +8,22 @@ import { Writer, WriterOptions, EmbeddedWriter, WriterState } from './writer';
|
|||
import { fromJS } from './fromjs';
|
||||
import { Reader, ReaderOptions } from './reader';
|
||||
|
||||
export function parse<T extends Embeddable = GenericEmbedded>(
|
||||
buffer: string,
|
||||
options?: ReaderOptions<T>,
|
||||
): Value<T> {
|
||||
export function parse<T = GenericEmbedded>(buffer: string, options?: ReaderOptions<T>): Value<T> {
|
||||
return new Reader<T>(buffer, options).next();
|
||||
}
|
||||
|
||||
export function parseAll<T extends Embeddable = GenericEmbedded>(
|
||||
buffer: string,
|
||||
options?: ReaderOptions<T>,
|
||||
): Value<T>[] {
|
||||
export function parseAll<T = GenericEmbedded>(buffer: string, options?: ReaderOptions<T>): Value<T>[] {
|
||||
return new Reader<T>(buffer, options).readToEnd();
|
||||
}
|
||||
|
||||
export const stringifyEmbeddedWrite = {
|
||||
export const stringifyEmbeddedWrite: EmbeddedWriter<any> = {
|
||||
write(s: WriterState, v: any): void {
|
||||
if (v instanceof GenericEmbedded) {
|
||||
new Writer(s, this).push(v.generic);
|
||||
} else {
|
||||
try {
|
||||
const j = fromJS(v);
|
||||
if (!isEmbedded(j)) {
|
||||
if (!(j instanceof Embedded)) {
|
||||
new Writer(s, this).push(j);
|
||||
return;
|
||||
}
|
||||
|
@ -43,7 +37,7 @@ export const stringifyEmbeddedWrite = {
|
|||
}
|
||||
};
|
||||
|
||||
export function stringify<T extends Embeddable = GenericEmbedded>(x: any, options?: WriterOptions<T>): string {
|
||||
export function stringify<T = GenericEmbedded>(x: any, options?: WriterOptions<T>): string {
|
||||
options = { ... (options ?? {}) };
|
||||
options.embeddedWrite = options.embeddedWrite ?? stringifyEmbeddedWrite;
|
||||
return Writer.stringify(fromJS<T>(x), options);
|
||||
|
|
|
@ -1,25 +1,25 @@
|
|||
// Preserves Values.
|
||||
|
||||
import type { Bytes } from './bytes';
|
||||
import type { DoubleFloat } from './float';
|
||||
import type { DoubleFloat, SingleFloat } from './float';
|
||||
import type { Annotated } from './annotated';
|
||||
import type { JsDictionary } from './jsdictionary';
|
||||
import { Set, KeyedDictionary } from './dictionary';
|
||||
import type { Embeddable, GenericEmbedded } from './embedded';
|
||||
import type { Set, Dictionary } from './dictionary';
|
||||
import type { Embedded, GenericEmbedded } from './embedded';
|
||||
|
||||
export type Value<T extends Embeddable = GenericEmbedded> =
|
||||
export type Value<T = GenericEmbedded> =
|
||||
| Atom
|
||||
| Compound<T>
|
||||
| T
|
||||
| Embedded<T>
|
||||
| Annotated<T>;
|
||||
export type Atom =
|
||||
| boolean
|
||||
| SingleFloat
|
||||
| DoubleFloat
|
||||
| number | bigint
|
||||
| string
|
||||
| Bytes
|
||||
| symbol;
|
||||
export type Compound<T extends Embeddable = GenericEmbedded> =
|
||||
export type Compound<T = GenericEmbedded> =
|
||||
| (Array<Value<T>> | [Value<T>]) & { label: Value<T> }
|
||||
// ^ expanded from definition of Record<> in record.ts,
|
||||
// because if we use Record<Value<T>, Tuple<Value<T>>, T>,
|
||||
|
@ -28,7 +28,4 @@ export type Compound<T extends Embeddable = GenericEmbedded> =
|
|||
// Value<T> to any.
|
||||
| Array<Value<T>>
|
||||
| Set<T>
|
||||
// v Expanded from definition of Dictionary<> in dictionary.ts,
|
||||
// because of circular-use-of-Value<T> issues.
|
||||
| JsDictionary<Value<T>>
|
||||
| KeyedDictionary<T>;
|
||||
| Dictionary<T>;
|
||||
|
|
|
@ -1,20 +1,19 @@
|
|||
import { isAnnotated } from './is';
|
||||
import { Record, Tuple } from "./record";
|
||||
import { Embeddable, GenericEmbedded, EmbeddedTypeEncode, isEmbedded } from "./embedded";
|
||||
import type { GenericEmbedded, Embedded, EmbeddedTypeEncode } from "./embedded";
|
||||
import { Encoder, EncoderState } from "./encoder";
|
||||
import type { Value } from "./values";
|
||||
import { NUMBER_RE } from './reader';
|
||||
import { encodeBase64 } from './base64';
|
||||
import { DictionaryMap, writeDictionaryOn } from './dictionary';
|
||||
|
||||
export type Writable<T extends Embeddable> =
|
||||
export type Writable<T> =
|
||||
Value<T> | PreserveWritable<T> | Iterable<Value<T>> | ArrayBufferView;
|
||||
|
||||
export interface PreserveWritable<T extends Embeddable> {
|
||||
export interface PreserveWritable<T> {
|
||||
__preserve_text_on__(writer: Writer<T>): void;
|
||||
}
|
||||
|
||||
export function isPreserveWritable<T extends Embeddable>(v: any): v is PreserveWritable<T> {
|
||||
export function isPreserveWritable<T>(v: any): v is PreserveWritable<T> {
|
||||
return typeof v === 'object' && v !== null && '__preserve_text_on__' in v && typeof v.__preserve_text_on__ === 'function';
|
||||
}
|
||||
|
||||
|
@ -192,7 +191,7 @@ export class WriterState {
|
|||
this.pieces.push(buf + '"');
|
||||
}
|
||||
|
||||
couldBeFlat<T extends Embeddable>(vs: Writable<T>[]): boolean {
|
||||
couldBeFlat<T>(vs: Writable<T>[]): boolean {
|
||||
let seenCompound = false;
|
||||
for (let v of vs) {
|
||||
if (Array.isArray(v) || Set.isSet(v) || Map.isMap(v)) {
|
||||
|
@ -207,7 +206,7 @@ export class WriterState {
|
|||
}
|
||||
}
|
||||
|
||||
export class Writer<T extends Embeddable> {
|
||||
export class Writer<T> {
|
||||
state: WriterState;
|
||||
embeddedWrite: EmbeddedWriter<T>;
|
||||
|
||||
|
@ -226,7 +225,7 @@ export class Writer<T extends Embeddable> {
|
|||
}
|
||||
}
|
||||
|
||||
static stringify<T extends Embeddable>(v: Writable<T>, options?: WriterOptions<T>): string {
|
||||
static stringify<T>(v: Writable<T>, options?: WriterOptions<T>): string {
|
||||
const w = new Writer(options);
|
||||
w.push(v);
|
||||
return w.contents();
|
||||
|
@ -263,10 +262,7 @@ export class Writer<T extends Embeddable> {
|
|||
this.state.pieces.push('' + v);
|
||||
break;
|
||||
case 'object':
|
||||
if (v === null) {
|
||||
throw new Error("Cannot encode null in Preserves Writer.push");
|
||||
}
|
||||
else if (isPreserveWritable<any>(v)) {
|
||||
if (isPreserveWritable<unknown>(v)) {
|
||||
v.__preserve_text_on__(this);
|
||||
}
|
||||
else if (isPreserveWritable<T>(v)) {
|
||||
|
@ -299,19 +295,16 @@ export class Writer<T extends Embeddable> {
|
|||
else if (isIterable(v)) {
|
||||
this.state.writeSeq('[', ']', v, vv => this.push(vv));
|
||||
}
|
||||
else if (isEmbedded(v)) {
|
||||
this.state.pieces.push('#:');
|
||||
else {
|
||||
((v: Embedded<T>) => {
|
||||
this.state.pieces.push('#!');
|
||||
if ('write' in this.embeddedWrite) {
|
||||
this.embeddedWrite.write(this.state, v);
|
||||
this.embeddedWrite.write(this.state, v.embeddedValue);
|
||||
} else {
|
||||
new Writer(this.state, genericEmbeddedTypeEncode)
|
||||
.push(this.embeddedWrite.toValue(v));
|
||||
.push(this.embeddedWrite.toValue(v.embeddedValue));
|
||||
}
|
||||
} else {
|
||||
writeDictionaryOn(new DictionaryMap<T>(v),
|
||||
this,
|
||||
(k, w) => w.push(k),
|
||||
(v, w) => w.push(v));
|
||||
})(v);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
|
|
|
@ -118,11 +118,3 @@ describe('base64 decoder', () => {
|
|||
it('gQ==', () => expect(d64('gQ==')).is(Bytes.of(0x81)));
|
||||
});
|
||||
});
|
||||
|
||||
describe('latin1 quasi-decoder', () => {
|
||||
it('decodes ascii', () => expect(Bytes.fromLatin1('abc')).is(Bytes.of(97, 98, 99)));
|
||||
it('encodes ascii', () => expect(Bytes.of(97, 98, 99).toLatin1()).is('abc'));
|
||||
it('decodes unprintables', () => expect(Bytes.fromLatin1('\x00\x01a\xfe\xff')).is(Bytes.of(0, 1, 97, 254, 255)));
|
||||
it('encodes unprintables', () => expect(Bytes.of(0, 1, 97, 254, 255).toLatin1()).is('\x00\x01a\xfe\xff'));
|
||||
it('rejects out-of-bounds', () => expect(() => Bytes.fromLatin1('ac╔b')).toThrowError('Codepoint out of range'));
|
||||
});
|
||||
|
|
|
@ -4,6 +4,7 @@ import {
|
|||
decode, decodeWithAnnotations, encode, canonicalEncode,
|
||||
DecodeError, ShortPacket,
|
||||
Bytes, Record,
|
||||
annotate,
|
||||
strip, peel,
|
||||
preserves,
|
||||
stringify,
|
||||
|
@ -15,11 +16,11 @@ import {
|
|||
EmbeddedType,
|
||||
DecoderState,
|
||||
Decoder,
|
||||
Embedded,
|
||||
embed,
|
||||
genericEmbeddedTypeDecode,
|
||||
genericEmbeddedTypeEncode,
|
||||
parse,
|
||||
Embedded,
|
||||
KeyedDictionary,
|
||||
} from '../src/index';
|
||||
const { Tag } = Constants;
|
||||
import './test-utils';
|
||||
|
@ -77,7 +78,7 @@ describe('parsing from subarray', () => {
|
|||
|
||||
describe('reusing buffer space', () => {
|
||||
it('should be done safely, even with nested dictionaries', () => {
|
||||
expect(canonicalEncode(fromJS(['aaa', Dictionary.stringMap({a: 1}), 'zzz'])).toHex()).is(
|
||||
expect(canonicalEncode(fromJS(['aaa', Dictionary.fromJS({a: 1}), 'zzz'])).toHex()).is(
|
||||
`b5
|
||||
b103616161
|
||||
b7
|
||||
|
@ -89,29 +90,33 @@ describe('reusing buffer space', () => {
|
|||
});
|
||||
|
||||
describe('encoding and decoding embeddeds', () => {
|
||||
class LookasideEmbeddedType implements EmbeddedType<Embedded<object>> {
|
||||
readonly objects: Embedded<object>[];
|
||||
class LookasideEmbeddedType implements EmbeddedType<object> {
|
||||
readonly objects: object[];
|
||||
|
||||
constructor(objects: Embedded<object>[]) {
|
||||
constructor(objects: object[]) {
|
||||
this.objects = objects;
|
||||
}
|
||||
|
||||
decode(d: DecoderState): Embedded<object> {
|
||||
decode(d: DecoderState): object {
|
||||
return this.fromValue(new Decoder<GenericEmbedded>(d).next());
|
||||
}
|
||||
|
||||
encode(e: EncoderState, v: Embedded<object>): void {
|
||||
encode(e: EncoderState, v: object): void {
|
||||
new Encoder(e).push(this.toValue(v));
|
||||
}
|
||||
|
||||
fromValue(v: Value<GenericEmbedded>): Embedded<object> {
|
||||
equals(a: object, b: object): boolean {
|
||||
return Object.is(a, b);
|
||||
}
|
||||
|
||||
fromValue(v: Value<GenericEmbedded>): object {
|
||||
if (typeof v !== 'number' || v < 0 || v >= this.objects.length) {
|
||||
throw new Error(`Unknown embedded target: ${stringify(v)}`);
|
||||
throw new Error("Unknown embedded target");
|
||||
}
|
||||
return this.objects[v];
|
||||
}
|
||||
|
||||
toValue(v: Embedded<object>): number {
|
||||
toValue(v: object): number {
|
||||
let i = this.objects.indexOf(v);
|
||||
if (i !== -1) return i;
|
||||
this.objects.push(v);
|
||||
|
@ -120,8 +125,8 @@ describe('encoding and decoding embeddeds', () => {
|
|||
}
|
||||
|
||||
it('should encode using embeddedId when no function has been supplied', () => {
|
||||
const A1 = new Embedded({a: 1});
|
||||
const A2 = new Embedded({a: 1});
|
||||
const A1 = embed({a: 1});
|
||||
const A2 = embed({a: 1});
|
||||
const bs1 = canonicalEncode(A1);
|
||||
const bs2 = canonicalEncode(A2);
|
||||
const bs3 = canonicalEncode(A1);
|
||||
|
@ -138,24 +143,24 @@ describe('encoding and decoding embeddeds', () => {
|
|||
.toThrow("Embeddeds not permitted at this point in Preserves document");
|
||||
});
|
||||
it('should encode properly', () => {
|
||||
const objects: Embedded<object>[] = [];
|
||||
const objects: object[] = [];
|
||||
const pt = new LookasideEmbeddedType(objects);
|
||||
const A = new Embedded({a: 1});
|
||||
const B = new Embedded({b: 2});
|
||||
const A = embed({a: 1});
|
||||
const B = embed({b: 2});
|
||||
expect(encode([A, B], { embeddedEncode: pt })).is(
|
||||
Bytes.from([Tag.Sequence,
|
||||
Tag.Embedded, Tag.SignedInteger, 0,
|
||||
Tag.Embedded, Tag.SignedInteger, 1, 1,
|
||||
Tag.End]));
|
||||
expect(objects).toEqual([A, B]);
|
||||
expect(objects).toEqual([A.embeddedValue, B.embeddedValue]);
|
||||
});
|
||||
it('should decode properly', () => {
|
||||
const objects: Embedded<object>[] = [];
|
||||
const objects: object[] = [];
|
||||
const pt = new LookasideEmbeddedType(objects);
|
||||
const X = new Embedded({x: 123});
|
||||
const Y = new Embedded({y: 456});
|
||||
objects.push(X);
|
||||
objects.push(Y);
|
||||
const X: Embedded<object> = embed({x: 123});
|
||||
const Y: Embedded<object> = embed({y: 456});
|
||||
objects.push(X.embeddedValue);
|
||||
objects.push(Y.embeddedValue);
|
||||
expect(decode(Bytes.from([
|
||||
Tag.Sequence,
|
||||
Tag.Embedded, Tag.SignedInteger, 0,
|
||||
|
@ -164,17 +169,17 @@ describe('encoding and decoding embeddeds', () => {
|
|||
]), { embeddedDecode: pt })).is([X, Y]);
|
||||
});
|
||||
it('should store embeddeds embedded in map keys correctly', () => {
|
||||
const A1a = new Embedded({a: 1});
|
||||
const A1 = A1a;
|
||||
const A2 = new Embedded({a: 1});
|
||||
const m = new KeyedDictionary<Embedded<object>, Value<Embedded<object>>, number>();
|
||||
const A1a = {a: 1};
|
||||
const A1: Embedded<object> = embed(A1a);
|
||||
const A2: Embedded<object> = embed({a: 1});
|
||||
const m = new Dictionary<object, number>();
|
||||
m.set([A1], 1);
|
||||
m.set([A2], 2);
|
||||
expect(m.get(A1)).toBeUndefined();
|
||||
expect(m.get([A1])).toBe(1);
|
||||
expect(m.get([A2])).toBe(2);
|
||||
expect(m.get([{a: 1}])).toBeUndefined();
|
||||
A1a.value.a = 3;
|
||||
expect(m.get([embed({a: 1})])).toBeUndefined();
|
||||
A1a.a = 3;
|
||||
expect(m.get([A1])).toBe(1);
|
||||
});
|
||||
});
|
||||
|
@ -304,7 +309,7 @@ describe('common test suite', () => {
|
|||
|
||||
const tests = (peel(TestCases._.cases(peel(samples) as TestCases)) as
|
||||
Dictionary<GenericEmbedded>);
|
||||
Dictionary.asMap(tests).forEach((t0, tName0) => {
|
||||
tests.forEach((t0: Value<GenericEmbedded>, tName0: Value<GenericEmbedded>) => {
|
||||
const tName = Symbol.keyFor(strip(tName0) as symbol)!;
|
||||
const t = peel(t0) as Record<symbol, any, GenericEmbedded>;
|
||||
switch (t.label) {
|
||||
|
|
|
@ -1,45 +0,0 @@
|
|||
import { Pexpr, Value, fromJS, parse, stringify } from '../src/index';
|
||||
import './test-utils';
|
||||
|
||||
function P(s: string): Value<any>[] {
|
||||
return parse(s, { includeAnnotations: true });
|
||||
}
|
||||
|
||||
describe('basics', () => {
|
||||
it('simple example', () => {
|
||||
const r = new Pexpr.Reader('#!foo\n<bar {zot ::quux}, [a; b; c;]>');
|
||||
const d = r.nextDocument();
|
||||
expect(fromJS(d)).is(P(`[\n#!foo\n
|
||||
<r bar <b zot <p |::|> quux> <p |,|> [a <p |;|> b <p |;|> c <p |;|>]>]`));
|
||||
});
|
||||
|
||||
it('simple group', () => {
|
||||
const r = new Pexpr.Reader('(+ 1 2)');
|
||||
expect(fromJS(r.nextDocument())).is(P('[<g + 1 2>]'));
|
||||
});
|
||||
|
||||
it('asPreserves', () => {
|
||||
const s = '{a: b, c: d, e: [1, 2 <r 3 4>]}';
|
||||
const d = new Pexpr.Reader(s).nextDocument();
|
||||
const v = Pexpr.asPreserves(d.get(0)!);
|
||||
expect(v).is(P(s));
|
||||
});
|
||||
});
|
||||
|
||||
describe('trailing comments', () => {
|
||||
it('basics 1', () => {
|
||||
const d = new Pexpr.Reader('# a comment with nothing after').nextDocument();
|
||||
expect(d.annotations?.[d.exprs.length].get(0)?.item).toBe('a comment with nothing after');
|
||||
});
|
||||
|
||||
it('basics 2', () => {
|
||||
const d = new Pexpr.Reader('# a comment with nothing after\n').nextDocument();
|
||||
expect(d.annotations?.[d.exprs.length].get(0)?.item).toBe('a comment with nothing after');
|
||||
});
|
||||
|
||||
it('inside a sequence', () => {
|
||||
const d = new Pexpr.Reader('[\n1\n# a comment with nothing after\n]\n').nextDocument();
|
||||
const seq = d.get(0)?.item as Pexpr.Compound;
|
||||
expect(seq.annotations?.[seq.exprs.length].get(0)?.item).toBe('a comment with nothing after');
|
||||
});
|
||||
});
|
|
@ -1,4 +1,4 @@
|
|||
import { Bytes, Decoder, genericEmbeddedType, encode, Reader, Double, KeyedDictionary, Value } from '../src/index';
|
||||
import { Bytes, Decoder, genericEmbeddedType, encode, Reader, Double } from '../src/index';
|
||||
import './test-utils';
|
||||
|
||||
import * as fs from 'fs';
|
||||
|
@ -36,23 +36,4 @@ describe('reading common test suite', () => {
|
|||
expect(new Reader('123.0').next()).toEqual(Double(123.0));
|
||||
expect(new Reader('123.00').next()).toEqual(Double(123.0));
|
||||
});
|
||||
|
||||
it('should produce a sensible JS object for symbol-keyed dictionaries', () => {
|
||||
expect(new Reader('{a: 1, b: 2}').next()).toEqual({a: 1, b: 2});
|
||||
});
|
||||
|
||||
it('should produce a sensible dictionary for mixed-keyed dictionaries', () => {
|
||||
expect(new Reader('{a: 1, "b": 2}').next()).is(
|
||||
new KeyedDictionary([[Symbol.for('a'), 1], ["b", 2]] as [Value, Value][]));
|
||||
});
|
||||
|
||||
it('should produce a sensible dictionary for string-keyed dictionaries', () => {
|
||||
expect(new Reader('{"a": 1, "b": 2}').next()).is(
|
||||
new KeyedDictionary([["a", 1], ["b", 2]] as [Value, Value][]));
|
||||
});
|
||||
|
||||
it('should produce a sensible dictionary for integer-keyed dictionaries', () => {
|
||||
expect(new Reader('{9: 1, 8: 2}').next()).is(
|
||||
new KeyedDictionary([[9, 1], [8, 2]] as [Value, Value][]));
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
import { Value, is, preserves, Embeddable } from '../src/index';
|
||||
import { Value, is, preserves } from '../src/index';
|
||||
import '../src/node_support';
|
||||
|
||||
declare global {
|
||||
namespace jest {
|
||||
interface Matchers<R> {
|
||||
is<T extends Embeddable>(expected: Value<T>): R;
|
||||
is<T>(expected: Value<T>): R;
|
||||
toThrowFilter(f: (e: Error) => boolean): R;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,12 @@
|
|||
import { Double, fromJS, IDENTITY_FOLD, fold, mapEmbeddeds, Value, preserves, KeyedDictionary, Embeddable, Embedded } from '../src/index';
|
||||
import { Single, Double, fromJS, Dictionary, IDENTITY_FOLD, fold, mapEmbeddeds, Value, embed, preserves } from '../src/index';
|
||||
import './test-utils';
|
||||
|
||||
describe('Single', () => {
|
||||
it('should print reasonably', () => {
|
||||
expect(Single(123.45).toString()).toEqual("123.45f");
|
||||
});
|
||||
});
|
||||
|
||||
describe('Double', () => {
|
||||
it('should print reasonably', () => {
|
||||
expect(Double(123.45).toString()).toEqual("123.45");
|
||||
|
@ -8,26 +14,26 @@ describe('Double', () => {
|
|||
});
|
||||
|
||||
describe('fold', () => {
|
||||
function mkv<T extends Embeddable>(t: T): Value<T> {
|
||||
function mkv<T extends object>(t: T): Value<T> {
|
||||
return fromJS<T>([
|
||||
1,
|
||||
2,
|
||||
new KeyedDictionary<T>([[[3, 4], fromJS([5, 6])],
|
||||
new Dictionary([[[3, 4], fromJS([5, 6])],
|
||||
['a', 1],
|
||||
['b', true]]),
|
||||
Double(3.4),
|
||||
Single(3.4),
|
||||
t,
|
||||
]);
|
||||
}
|
||||
|
||||
it('should support identity', () => {
|
||||
const w = new Date();
|
||||
const v = mkv(new Embedded(w));
|
||||
const v = mkv(w);
|
||||
expect(fold(v, IDENTITY_FOLD)).is(v);
|
||||
const w1 = new Date();
|
||||
const v1 = mkv(new Embedded(w1));
|
||||
const v1 = mkv(w1);
|
||||
expect(fold(v, IDENTITY_FOLD)).not.is(v1);
|
||||
expect(mapEmbeddeds(v, _t => new Embedded(w1))).is(v1);
|
||||
expect(mapEmbeddeds(v, _t => embed(w1))).is(v1);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -69,12 +75,6 @@ describe('is()', () => {
|
|||
expect(c).not.toBe(3);
|
||||
expect(c).not.is(3);
|
||||
});
|
||||
it('should compare equivalent JsDictionary and KeyedDictionary values sensibly', () => {
|
||||
const a = {a: 1, b: 2};
|
||||
const b = new KeyedDictionary(
|
||||
[[Symbol.for('a'), 1], [Symbol.for('b'), 2]] as [Value, Value][]);
|
||||
expect(a).is(b);
|
||||
});
|
||||
});
|
||||
|
||||
describe('`preserves` formatter', () => {
|
||||
|
@ -88,18 +88,4 @@ describe('`preserves` formatter', () => {
|
|||
expect(preserves`>${BigInt("12345678123456781234567812345678")}<`)
|
||||
.toBe('>12345678123456781234567812345678<');
|
||||
});
|
||||
it('should format regular JS objects', () => {
|
||||
expect(preserves`>${({a: 1, b: 2})}<`)
|
||||
.toBe('>{a: 1 b: 2}<');
|
||||
});
|
||||
it('should format dictionaries with string keys', () => {
|
||||
const v = new KeyedDictionary([["a", 1], ["b", 2]]);
|
||||
expect(preserves`>${v}<`)
|
||||
.toBe('>{"a": 1 "b": 2}<');
|
||||
});
|
||||
it('should format dictionaries with symbol keys', () => {
|
||||
const v = new KeyedDictionary([[Symbol.for("a"), 1], [Symbol.for("b"), 2]]);
|
||||
expect(preserves`>${v}<`)
|
||||
.toBe('>{a: 1 b: 2}<');
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@preserves/schema-cli",
|
||||
"version": "0.995.206",
|
||||
"version": "0.992.4",
|
||||
"description": "Command-line tools for Preserves Schema",
|
||||
"homepage": "https://gitlab.com/preserves/preserves",
|
||||
"license": "Apache-2.0",
|
||||
|
@ -26,8 +26,8 @@
|
|||
"@types/minimatch": "^3.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@preserves/core": "^0.995.206",
|
||||
"@preserves/schema": "^0.995.206",
|
||||
"@preserves/core": "^0.992.4",
|
||||
"@preserves/schema": "^0.992.4",
|
||||
"chalk": "^4.1",
|
||||
"chokidar": "^3.5",
|
||||
"commander": "^7.2",
|
||||
|
|
|
@ -24,9 +24,10 @@ export function run(options: CommandLineArguments): void {
|
|||
|
||||
if (failures.length === 0) {
|
||||
if (options.bundle) {
|
||||
fs.writeSync(1, underlying(canonicalEncode(M.fromBundle(M.Bundle(
|
||||
new KeyedDictionary<M.InputEmbedded, M.ModulePath, M.Schema>(
|
||||
inputFiles.map(i => [i.modulePath, i.schema])))))));
|
||||
fs.writeSync(1, underlying(canonicalEncode(M.fromBundle({
|
||||
modules: new KeyedDictionary<M.ModulePath, M.Schema, M.InputEmbedded>(
|
||||
inputFiles.map(i => [i.modulePath, i.schema])),
|
||||
}))));
|
||||
} else {
|
||||
fs.writeSync(1, underlying(canonicalEncode(M.fromSchema(inputFiles[0].schema))));
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@preserves/schema",
|
||||
"version": "0.995.206",
|
||||
"version": "0.992.4",
|
||||
"description": "Schema support for Preserves data serialization format",
|
||||
"homepage": "https://gitlab.com/preserves/preserves",
|
||||
"license": "Apache-2.0",
|
||||
|
@ -20,16 +20,12 @@
|
|||
"compile:watch": "yarn compile -w",
|
||||
"rollup": "rollup -c",
|
||||
"rollup:watch": "yarn rollup -w",
|
||||
"copy-schema": "mkdir -p ./dist && cp -a ../../../../schema/schema.prs ../../../../schema/host.prs ./dist",
|
||||
"copy-schema": "mkdir -p ./dist && cp -a ../../../../schema/*.prs ./dist",
|
||||
"test": "jest",
|
||||
"test:watch": "jest --watch",
|
||||
"veryclean": "yarn run clean && rm -rf node_modules"
|
||||
},
|
||||
"dependencies": {
|
||||
"@preserves/core": "^0.995.206"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/js-beautify": "1.14",
|
||||
"js-beautify": "1.15"
|
||||
"@preserves/core": "^0.992.4"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -68,9 +68,33 @@
|
|||
}
|
||||
}
|
||||
|
||||
I.moduleTree(Schema);
|
||||
// I.env.forEach((_schema, path) => console.log(PreservesSchema.Meta.formatModulePath(path)));
|
||||
|
||||
for (const [modulePath, schema] of I.env) {
|
||||
let container = Schema;
|
||||
modulePath.slice(0, -1).forEach(n => {
|
||||
if (!(n.description in container)) container[n.description] = {};
|
||||
container = container[n.description];
|
||||
});
|
||||
container[modulePath[modulePath.length - 1].description] = moduleFor(modulePath, schema);
|
||||
}
|
||||
|
||||
schemaReady();
|
||||
}
|
||||
|
||||
function moduleFor(modulePath, schema) {
|
||||
const { Meta, GenType, Type } = PreservesSchema;
|
||||
const mod = {};
|
||||
schema.definitions.forEach((d, n) => {
|
||||
const definitionName = n.description;
|
||||
const definitionId = Meta.jsId(definitionName);
|
||||
mod[`${definitionId}`] = I.definitionConstructor(modulePath, n);
|
||||
mod[`from${definitionId}`] = I.unparser(modulePath, n);
|
||||
mod[`to${definitionId}`] = v => I.tryParse(modulePath, n, v);
|
||||
mod[`as${definitionId}`] = v => I.parse(modulePath, n, v);
|
||||
});
|
||||
return mod;
|
||||
}
|
||||
|
||||
window.addEventListener('DOMContentLoaded', translateScripts);
|
||||
})();
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
import { JsDictionary, stringify } from '@preserves/core';
|
||||
import { stringify } from '@preserves/core';
|
||||
import * as M from './meta';
|
||||
|
||||
export function checkSchema(schema: M.Schema): (
|
||||
{ ok: true, schema: M.Schema } | { ok: false, problems: Array<string> })
|
||||
{
|
||||
const checker = new Checker();
|
||||
JsDictionary.forEach(schema.definitions, checker.checkDefinition.bind(checker));
|
||||
schema.definitions.forEach(checker.checkDefinition.bind(checker));
|
||||
if (checker.problems.length > 0) {
|
||||
return { ok: false, problems: checker.problems };
|
||||
} else {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { encode, JsDictionary, stringify } from "@preserves/core";
|
||||
import { encode, stringify } from "@preserves/core";
|
||||
import * as M from "./meta";
|
||||
import { CompilerOptions, ModuleContext } from "./compiler/context";
|
||||
import { Formatter, block, seq, braces } from "./compiler/block";
|
||||
|
@ -29,7 +29,7 @@ export function compile(
|
|||
mod.defineType(seq(`export type _embedded = `, mod.embeddedType, `;`));
|
||||
}
|
||||
|
||||
for (const [name, def] of JsDictionary.entries(schema.definitions)) {
|
||||
for (const [name, def] of schema.definitions) {
|
||||
const t = typeForDefinition(mod.resolver(), def);
|
||||
const nameStr = stringify(name);
|
||||
const resultTypeItem = seq(nameStr, mod.genericArgsFor(t));
|
||||
|
@ -49,7 +49,7 @@ export function compile(
|
|||
}
|
||||
}
|
||||
|
||||
for (const [name0, def] of JsDictionary.entries(schema.definitions)) {
|
||||
for (const [name0, def] of schema.definitions) {
|
||||
const t = typeForDefinition(mod.resolver(), def);
|
||||
const name = name0 as symbol;
|
||||
const nameStr = name0.description!;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { KeyedSet, FlexSet, Position, stringify, DictionaryMap, JsDictionary } from "@preserves/core";
|
||||
import { Dictionary, KeyedSet, FlexSet, Position, stringify } from "@preserves/core";
|
||||
import { refPosition } from "../reader";
|
||||
import * as M from "../meta";
|
||||
import { anglebrackets, block, braces, commas, formatItems, Item, keyvalue, seq } from "./block";
|
||||
|
@ -27,11 +27,11 @@ export class ModuleContext {
|
|||
readonly options: CompilerOptions;
|
||||
readonly embeddedType: Item;
|
||||
|
||||
readonly literals = new DictionaryMap<M.InputEmbedded, string>();
|
||||
readonly literals = new Dictionary<M.InputEmbedded, string>();
|
||||
readonly preamble: Item[] = [];
|
||||
readonly typedefs: Item[] = [];
|
||||
readonly functiondefs: Item[] = [];
|
||||
readonly imports = new KeyedSet<M.InputEmbedded, [M.ModulePath, string, string, string]>();
|
||||
readonly imports = new KeyedSet<[M.ModulePath, string, string, string]>();
|
||||
|
||||
constructor(
|
||||
env: M.Environment,
|
||||
|
@ -137,7 +137,7 @@ export class ModuleContext {
|
|||
null,
|
||||
null);
|
||||
} else {
|
||||
const p = JsDictionary.get(e.schema.definitions, name.name);
|
||||
const p = e.schema.definitions.get(name.name);
|
||||
if (p !== void 0) {
|
||||
let t = () => typeForDefinition(this.resolver(soughtModule), p);
|
||||
if (name.module.length) {
|
||||
|
@ -158,7 +158,7 @@ export class ModuleContext {
|
|||
}
|
||||
|
||||
genericParameters(): Item {
|
||||
return anglebrackets(seq('_embedded extends _.Embeddable = ', this.embeddedType));
|
||||
return anglebrackets(seq('_embedded = ', this.embeddedType));
|
||||
}
|
||||
|
||||
genericParametersFor(t: Type): Item {
|
||||
|
|
|
@ -2,7 +2,7 @@ import { FunctionContext } from "./context";
|
|||
import * as M from '../meta';
|
||||
import { Item, seq, parens, anglebrackets } from "./block";
|
||||
import { simpleType, typeFor } from "./gentype";
|
||||
import { ANY_TYPE, isSymbolType, Type } from "./type";
|
||||
import { ANY_TYPE, Type } from "./type";
|
||||
import { renderType } from "./rendertype";
|
||||
|
||||
export function converterForDefinition(
|
||||
|
@ -87,7 +87,7 @@ function converterForTuple(ctx: FunctionContext,
|
|||
}
|
||||
|
||||
const lengthCheck = variablePattern === void 0
|
||||
? seq(` && ${src}.length >= ${ps.length}`)
|
||||
? seq(` && ${src}.length === ${ps.length}`)
|
||||
: ((ps.length === 0) ? '' : seq(` && ${src}.length >= ${ps.length}`));
|
||||
|
||||
return knownArray
|
||||
|
@ -154,6 +154,7 @@ export function converterForSimple(
|
|||
let valexp: Item = `${src}`;
|
||||
switch (p.atomKind._variant) {
|
||||
case 'Boolean': test = `typeof ${src} === 'boolean'`; break;
|
||||
case 'Float': test = `_.Float.isSingle(${src})`; valexp = `${src}.value`; break;
|
||||
case 'Double': test =`_.Float.isDouble(${src})`; valexp = `${src}.value`; break;
|
||||
case 'SignedInteger': test = `typeof ${src} === 'number'`; break;
|
||||
case 'String': test = `typeof ${src} === 'string'`; break;
|
||||
|
@ -163,7 +164,7 @@ export function converterForSimple(
|
|||
return [seq(`${dest} = `, test, ` ? `, valexp, ` : void 0`)];
|
||||
}
|
||||
case 'embedded':
|
||||
return [`${dest} = _.isEmbedded<_embedded>(${src}) ? ${src} : void 0`];
|
||||
return [`${dest} = _.isEmbedded<_embedded>(${src}) ? ${src}.embeddedValue : void 0`];
|
||||
case 'lit':
|
||||
return [`${dest} = _.is(${src}, ${ctx.mod.literal(p.value)}) ? {} : void 0`];
|
||||
|
||||
|
@ -192,7 +193,7 @@ export function converterForSimple(
|
|||
const v = ctx.gentempname();
|
||||
return [
|
||||
seq(`${dest} = new _.EncodableSet`,
|
||||
anglebrackets('_embedded', renderType(ctx.mod, vt)),
|
||||
anglebrackets(renderType(ctx.mod, vt), '_embedded'),
|
||||
parens(encoderForSimplePattern(ctx, p.pattern) ?? `v => v`)),
|
||||
seq(`for (const ${v} of ${src}) `, ctx.block(() => [
|
||||
... converterFor(ctx, M.anonymousSimplePattern(p.pattern), v, vv =>
|
||||
|
@ -203,30 +204,20 @@ export function converterForSimple(
|
|||
case 'dictof':
|
||||
return [`${dest} = void 0`,
|
||||
seq(`if (_.Dictionary.isDictionary<_embedded>(${src})) `, ctx.block(() => {
|
||||
const srcMap = ctx.gentempname();
|
||||
const resolver = ctx.mod.resolver();
|
||||
const kt = simpleType(resolver, p.key);
|
||||
const vt = simpleType(resolver, p.value);
|
||||
const k = ctx.gentempname();
|
||||
const v = ctx.gentempname();
|
||||
const symbolKeyed = isSymbolType(kt);
|
||||
return [
|
||||
seq(`const ${srcMap} = new _.DictionaryMap(${src})`),
|
||||
(symbolKeyed
|
||||
? seq(`${dest} = {}`)
|
||||
: seq(`${dest} = new _.EncodableDictionary`,
|
||||
anglebrackets('_embedded', renderType(ctx.mod, kt), renderType(ctx.mod, vt)),
|
||||
seq(`${dest} = new _.EncodableDictionary`,
|
||||
anglebrackets(renderType(ctx.mod, kt), renderType(ctx.mod, vt), '_embedded'),
|
||||
parens(encoderForSimplePattern(ctx, p.key) ?? `k => k`,
|
||||
encoderForSimplePattern(ctx, p.value) ?? `v => v`))),
|
||||
seq(`for (const [${k}, ${v}] of ${srcMap}) `, ctx.block(() => [
|
||||
encoderForSimplePattern(ctx, p.value) ?? `v => v`)),
|
||||
seq(`for (const [${k}, ${v}] of ${src}) `, ctx.block(() => [
|
||||
... converterFor(ctx, M.anonymousSimplePattern(p.key), k, kk =>
|
||||
converterFor(ctx, M.anonymousSimplePattern(p.value), v, vv =>
|
||||
[
|
||||
(symbolKeyed
|
||||
? `${dest}[${kk}.description!] = ${vv}`
|
||||
: `${dest}.set(${kk}, ${vv})`),
|
||||
`continue`
|
||||
])),
|
||||
[`${dest}.set(${kk}, ${vv})`, `continue`])),
|
||||
seq(`${dest} = void 0`),
|
||||
seq(`break`)]))];
|
||||
}))];
|
||||
|
@ -261,13 +252,12 @@ function converterForCompound(
|
|||
case 'tuplePrefix':
|
||||
return converterForTuple(ctx, p.fixed, src, knownArray, p.variable, ks);
|
||||
case 'dict': {
|
||||
const srcMap = ctx.gentempname();
|
||||
const entries = Array.from(p.entries);
|
||||
function loop(i: number): Item[] {
|
||||
if (i < entries.length) {
|
||||
const [k, n] = entries[i];
|
||||
const tmpSrc = ctx.gentemp();
|
||||
return [seq(`if ((${tmpSrc} = ${srcMap}.get(${ctx.mod.literal(k)})) !== void 0) `,
|
||||
return [seq(`if ((${tmpSrc} = ${src}.get(${ctx.mod.literal(k)})) !== void 0) `,
|
||||
ctx.block(() =>
|
||||
converterFor(
|
||||
ctx,
|
||||
|
@ -278,9 +268,7 @@ function converterForCompound(
|
|||
return ks();
|
||||
}
|
||||
}
|
||||
return [seq(`if (_.Dictionary.isDictionary<_embedded>(${src})) `, ctx.block(() => [
|
||||
seq(`const ${srcMap} = new _.DictionaryMap(${src})`),
|
||||
... loop(0)]))];
|
||||
return [seq(`if (_.Dictionary.isDictionary<_embedded>(${src})) `, ctx.block(() => loop(0)))];
|
||||
}
|
||||
default:
|
||||
((_p: never) => {})(p);
|
||||
|
|
|
@ -37,6 +37,7 @@ export function simpleType(resolver: RefResolver, p: M.SimplePattern): FieldType
|
|||
case 'atom':
|
||||
switch (p.atomKind._variant) {
|
||||
case 'Boolean': return Type.ref(`boolean`, null);
|
||||
case 'Float': return Type.ref(`number`, null);
|
||||
case 'Double': return Type.ref(`number`, null);
|
||||
case 'SignedInteger': return Type.ref(`number`, null);
|
||||
case 'String': return Type.ref(`string`, null);
|
||||
|
|
|
@ -40,13 +40,14 @@ function unconverterFor(ctx: FunctionContext, p: M.Pattern, src: string): Item {
|
|||
return `${src}`;
|
||||
case 'atom':
|
||||
switch (p.atomKind._variant) {
|
||||
case 'Float': return `_.Single(${src})`;
|
||||
case 'Double': return `_.Double(${src})`;
|
||||
default: return `${src}`;
|
||||
}
|
||||
case 'lit':
|
||||
return ctx.mod.literal(p.value);
|
||||
case 'embedded':
|
||||
return `${src}`;
|
||||
return `_.embed(${src})`;
|
||||
case 'seqof':
|
||||
return seq(`${src}.map(v => `,
|
||||
unconverterFor(ctx, M.Pattern.SimplePattern(p.pattern), 'v'),
|
||||
|
@ -57,10 +58,8 @@ function unconverterFor(ctx: FunctionContext, p: M.Pattern, src: string): Item {
|
|||
unconverterFor(ctx, M.Pattern.SimplePattern(p.pattern), 'v'),
|
||||
`)`)));
|
||||
case 'dictof':
|
||||
return seq(`_.Dictionary.from<_embedded>`, parens(seq(
|
||||
`_.Array.from(`,
|
||||
M.isSymbolPattern(p.key) ? `_.JsDictionary.entries(${src})` : `${src}.entries()`,
|
||||
`).map(([k, v]) => `,
|
||||
return seq(`new _.Dictionary<_embedded>`, parens(seq(
|
||||
`_.Array.from(${src}.entries()).map(([k, v]) => `,
|
||||
brackets(
|
||||
unconverterFor(ctx, M.Pattern.SimplePattern(p.key), 'k'),
|
||||
unconverterFor(ctx, M.Pattern.SimplePattern(p.value), 'v')),
|
||||
|
@ -97,7 +96,7 @@ function unconverterFor(ctx: FunctionContext, p: M.Pattern, src: string): Item {
|
|||
}
|
||||
}
|
||||
case 'dict':
|
||||
return seq(`_.Dictionary.from<_embedded>`, parens(
|
||||
return seq(`new _.Dictionary<_embedded>`, parens(
|
||||
brackets(... Array.from(p.entries.entries()).map(([k, n]) =>
|
||||
brackets(
|
||||
ctx.mod.literal(k),
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { isSymbolType, SimpleType, Type } from "./type";
|
||||
import { SimpleType, Type } from "./type";
|
||||
import { anglebrackets, braces, Item, keyvalue, opseq, seq } from "./block";
|
||||
import { ModuleContext } from "./context";
|
||||
|
||||
|
@ -47,17 +47,12 @@ export function renderType(ctxt: ModuleContext, t: Type): Item {
|
|||
return seq(t.typeName, ctxt.genericArgsFor(t));
|
||||
}
|
||||
case 'set': return seq('_.EncodableSet', anglebrackets(
|
||||
'_embedded',
|
||||
renderType(ctxt, t.type)));
|
||||
case 'dictionary':
|
||||
if (isSymbolType(t.key)) {
|
||||
return seq('_.JsDictionary', anglebrackets(renderType(ctxt, t.value)));
|
||||
} else {
|
||||
return seq('_.EncodableDictionary', anglebrackets(
|
||||
'_embedded',
|
||||
renderType(ctxt, t.type),
|
||||
'_embedded'));
|
||||
case 'dictionary': return seq('_.EncodableDictionary', anglebrackets(
|
||||
renderType(ctxt, t.key),
|
||||
renderType(ctxt, t.value)));
|
||||
}
|
||||
renderType(ctxt, t.value),
|
||||
'_embedded'));
|
||||
case 'array': return seq('Array', anglebrackets(renderType(ctxt, t.type)));
|
||||
case 'record': return braces(... simpleTypeFields(ctxt, t));
|
||||
default:
|
||||
|
|
|
@ -35,7 +35,3 @@ export namespace Type {
|
|||
}
|
||||
|
||||
export const ANY_TYPE: FieldType = Type.ref('_.Value', null);
|
||||
|
||||
export function isSymbolType(ty: FieldType): ty is { kind: 'ref', typeName: 'symbol', ref: null } {
|
||||
return ty.kind === 'ref' && ty.typeName === 'symbol' && ty.ref === null;
|
||||
}
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
import { Annotated, Bytes, Set, Fold, fold, Record, Tuple, Value, stringify, DictionaryMap } from "@preserves/core";
|
||||
import { Annotated, Bytes, Set, Dictionary, Fold, fold, Record, Tuple, Value, stringify, Embedded } from "@preserves/core";
|
||||
import { brackets, Item, parens, seq } from "./block";
|
||||
import * as M from '../meta';
|
||||
|
||||
export function sourceCodeFor(v: Value<M.InputEmbedded>): Item {
|
||||
return fold(v, {
|
||||
boolean(b: boolean): Item { return b.toString(); },
|
||||
single(f: number): Item { return f.toString(); },
|
||||
double(f: number): Item { return f.toString(); },
|
||||
integer(i: number): Item { return i.toString(); },
|
||||
string(s: string): Item { return JSON.stringify(s); },
|
||||
|
@ -22,8 +23,8 @@ export function sourceCodeFor(v: Value<M.InputEmbedded>): Item {
|
|||
set(s: Set<M.InputEmbedded>, k: Fold<M.InputEmbedded, Item>): Item {
|
||||
return seq('new _.Set<_.Value<_embedded>>', parens(brackets(... Array.from(s).map(k))));
|
||||
},
|
||||
dictionary(d: DictionaryMap<M.InputEmbedded>, k: Fold<M.InputEmbedded, Item>): Item {
|
||||
return seq('_.Dictionary.from<_embedded>', parens(brackets(... Array.from(d).map(([kk,vv]) =>
|
||||
dictionary(d: Dictionary<M.InputEmbedded>, k: Fold<M.InputEmbedded, Item>): Item {
|
||||
return seq('new _.Dictionary<_embedded>', parens(brackets(... Array.from(d).map(([kk,vv]) =>
|
||||
brackets(k(kk), k(vv))))));
|
||||
},
|
||||
|
||||
|
@ -31,7 +32,7 @@ export function sourceCodeFor(v: Value<M.InputEmbedded>): Item {
|
|||
return seq('_.annotate<_embedded>', parens(k(a.item), ... a.annotations.map(k)));
|
||||
},
|
||||
|
||||
embedded(t: M.InputEmbedded, _k: Fold<M.InputEmbedded, Item>): Item {
|
||||
embedded(t: Embedded<M.InputEmbedded>, _k: Fold<M.InputEmbedded, Item>): Item {
|
||||
throw new Error(`Cannot emit source code for construction of embedded ${stringify(t)}`);
|
||||
},
|
||||
});
|
||||
|
|
|
@ -30,18 +30,14 @@ export type Definition = (
|
|||
) &
|
||||
_.Preservable<any> &
|
||||
_.PreserveWritable<any> &
|
||||
{
|
||||
__as_preserve__<_embedded extends _.Embeddable = _.GenericEmbedded>(): _.Value<_embedded>
|
||||
}
|
||||
{__as_preserve__<_embedded = _.GenericEmbedded>(): _.Value<_embedded>}
|
||||
);
|
||||
|
||||
export type Variant = (
|
||||
{"label": symbol, "type": Simple} &
|
||||
_.Preservable<any> &
|
||||
_.PreserveWritable<any> &
|
||||
{
|
||||
__as_preserve__<_embedded extends _.Embeddable = _.GenericEmbedded>(): _.Value<_embedded>
|
||||
}
|
||||
{__as_preserve__<_embedded = _.GenericEmbedded>(): _.Value<_embedded>}
|
||||
);
|
||||
|
||||
export type Simple = (
|
||||
|
@ -51,27 +47,21 @@ export type Simple = (
|
|||
) &
|
||||
_.Preservable<any> &
|
||||
_.PreserveWritable<any> &
|
||||
{
|
||||
__as_preserve__<_embedded extends _.Embeddable = _.GenericEmbedded>(): _.Value<_embedded>
|
||||
}
|
||||
{__as_preserve__<_embedded = _.GenericEmbedded>(): _.Value<_embedded>}
|
||||
);
|
||||
|
||||
export type Record = (
|
||||
{"fields": Array<NamedField>} &
|
||||
_.Preservable<any> &
|
||||
_.PreserveWritable<any> &
|
||||
{
|
||||
__as_preserve__<_embedded extends _.Embeddable = _.GenericEmbedded>(): _.Value<_embedded>
|
||||
}
|
||||
{__as_preserve__<_embedded = _.GenericEmbedded>(): _.Value<_embedded>}
|
||||
);
|
||||
|
||||
export type NamedField = (
|
||||
{"name": symbol, "type": Field} &
|
||||
_.Preservable<any> &
|
||||
_.PreserveWritable<any> &
|
||||
{
|
||||
__as_preserve__<_embedded extends _.Embeddable = _.GenericEmbedded>(): _.Value<_embedded>
|
||||
}
|
||||
{__as_preserve__<_embedded = _.GenericEmbedded>(): _.Value<_embedded>}
|
||||
);
|
||||
|
||||
export type Field = (
|
||||
|
@ -87,9 +77,7 @@ export type Field = (
|
|||
) &
|
||||
_.Preservable<any> &
|
||||
_.PreserveWritable<any> &
|
||||
{
|
||||
__as_preserve__<_embedded extends _.Embeddable = _.GenericEmbedded>(): _.Value<_embedded>
|
||||
}
|
||||
{__as_preserve__<_embedded = _.GenericEmbedded>(): _.Value<_embedded>}
|
||||
);
|
||||
|
||||
|
||||
|
@ -349,13 +337,13 @@ export namespace Field {
|
|||
};
|
||||
}
|
||||
|
||||
export function asDefinition<_embedded extends _.Embeddable = _.GenericEmbedded>(v: _.Value<_embedded>): Definition {
|
||||
export function asDefinition<_embedded = _.GenericEmbedded>(v: _.Value<_embedded>): Definition {
|
||||
let result = toDefinition(v);
|
||||
if (result === void 0) throw new TypeError(`Invalid Definition: ${_.stringify(v)}`);
|
||||
return result;
|
||||
}
|
||||
|
||||
export function toDefinition<_embedded extends _.Embeddable = _.GenericEmbedded>(v: _.Value<_embedded>): undefined | Definition {
|
||||
export function toDefinition<_embedded = _.GenericEmbedded>(v: _.Value<_embedded>): undefined | Definition {
|
||||
let result: undefined | Definition;
|
||||
if (_.Record.isRecord<_.Value<_embedded>, _.Tuple<_.Value<_embedded>>, _embedded>(v)) {
|
||||
let _tmp0: ({}) | undefined;
|
||||
|
@ -402,22 +390,22 @@ export function toDefinition<_embedded extends _.Embeddable = _.GenericEmbedded>
|
|||
|
||||
export namespace Definition {export const __from_preserve__ = toDefinition;}
|
||||
|
||||
export function fromDefinition<_embedded extends _.Embeddable = _.GenericEmbedded>(_v: Definition): _.Value<_embedded> {
|
||||
export function fromDefinition<_embedded = _.GenericEmbedded>(_v: Definition): _.Value<_embedded> {
|
||||
switch (_v._variant) {
|
||||
case "union": {return _.Record($union, [_v["variants"].map(v => fromVariant<_embedded>(v))]);};
|
||||
case "Simple": {return fromSimple<_embedded>(_v.value);};
|
||||
};
|
||||
}
|
||||
|
||||
export function asVariant<_embedded extends _.Embeddable = _.GenericEmbedded>(v: _.Value<_embedded>): Variant {
|
||||
export function asVariant<_embedded = _.GenericEmbedded>(v: _.Value<_embedded>): Variant {
|
||||
let result = toVariant(v);
|
||||
if (result === void 0) throw new TypeError(`Invalid Variant: ${_.stringify(v)}`);
|
||||
return result;
|
||||
}
|
||||
|
||||
export function toVariant<_embedded extends _.Embeddable = _.GenericEmbedded>(v: _.Value<_embedded>): undefined | Variant {
|
||||
export function toVariant<_embedded = _.GenericEmbedded>(v: _.Value<_embedded>): undefined | Variant {
|
||||
let result: undefined | Variant;
|
||||
if (_.isSequence(v) && v.length >= 2) {
|
||||
if (_.isSequence(v) && v.length === 2) {
|
||||
let _tmp0: (symbol) | undefined;
|
||||
_tmp0 = typeof v[0] === 'symbol' ? v[0] : void 0;
|
||||
if (_tmp0 !== void 0) {
|
||||
|
@ -439,15 +427,15 @@ export function toVariant<_embedded extends _.Embeddable = _.GenericEmbedded>(v:
|
|||
|
||||
Variant.__from_preserve__ = toVariant;
|
||||
|
||||
export function fromVariant<_embedded extends _.Embeddable = _.GenericEmbedded>(_v: Variant): _.Value<_embedded> {return [_v["label"], fromSimple<_embedded>(_v["type"])];}
|
||||
export function fromVariant<_embedded = _.GenericEmbedded>(_v: Variant): _.Value<_embedded> {return [_v["label"], fromSimple<_embedded>(_v["type"])];}
|
||||
|
||||
export function asSimple<_embedded extends _.Embeddable = _.GenericEmbedded>(v: _.Value<_embedded>): Simple {
|
||||
export function asSimple<_embedded = _.GenericEmbedded>(v: _.Value<_embedded>): Simple {
|
||||
let result = toSimple(v);
|
||||
if (result === void 0) throw new TypeError(`Invalid Simple: ${_.stringify(v)}`);
|
||||
return result;
|
||||
}
|
||||
|
||||
export function toSimple<_embedded extends _.Embeddable = _.GenericEmbedded>(v: _.Value<_embedded>): undefined | Simple {
|
||||
export function toSimple<_embedded = _.GenericEmbedded>(v: _.Value<_embedded>): undefined | Simple {
|
||||
let _tmp0: (Field) | undefined;
|
||||
let result: undefined | Simple;
|
||||
_tmp0 = toField(v);
|
||||
|
@ -478,20 +466,20 @@ export function toSimple<_embedded extends _.Embeddable = _.GenericEmbedded>(v:
|
|||
|
||||
export namespace Simple {export const __from_preserve__ = toSimple;}
|
||||
|
||||
export function fromSimple<_embedded extends _.Embeddable = _.GenericEmbedded>(_v: Simple): _.Value<_embedded> {
|
||||
export function fromSimple<_embedded = _.GenericEmbedded>(_v: Simple): _.Value<_embedded> {
|
||||
switch (_v._variant) {
|
||||
case "Field": {return fromField<_embedded>(_v.value);};
|
||||
case "Record": {return fromRecord<_embedded>(_v.value);};
|
||||
};
|
||||
}
|
||||
|
||||
export function asRecord<_embedded extends _.Embeddable = _.GenericEmbedded>(v: _.Value<_embedded>): Record {
|
||||
export function asRecord<_embedded = _.GenericEmbedded>(v: _.Value<_embedded>): Record {
|
||||
let result = toRecord(v);
|
||||
if (result === void 0) throw new TypeError(`Invalid Record: ${_.stringify(v)}`);
|
||||
return result;
|
||||
}
|
||||
|
||||
export function toRecord<_embedded extends _.Embeddable = _.GenericEmbedded>(v: _.Value<_embedded>): undefined | Record {
|
||||
export function toRecord<_embedded = _.GenericEmbedded>(v: _.Value<_embedded>): undefined | Record {
|
||||
let result: undefined | Record;
|
||||
if (_.Record.isRecord<_.Value<_embedded>, _.Tuple<_.Value<_embedded>>, _embedded>(v)) {
|
||||
let _tmp0: ({}) | undefined;
|
||||
|
@ -524,17 +512,17 @@ export function toRecord<_embedded extends _.Embeddable = _.GenericEmbedded>(v:
|
|||
|
||||
Record.__from_preserve__ = toRecord;
|
||||
|
||||
export function fromRecord<_embedded extends _.Embeddable = _.GenericEmbedded>(_v: Record): _.Value<_embedded> {return _.Record($rec, [_v["fields"].map(v => fromNamedField<_embedded>(v))]);}
|
||||
export function fromRecord<_embedded = _.GenericEmbedded>(_v: Record): _.Value<_embedded> {return _.Record($rec, [_v["fields"].map(v => fromNamedField<_embedded>(v))]);}
|
||||
|
||||
export function asNamedField<_embedded extends _.Embeddable = _.GenericEmbedded>(v: _.Value<_embedded>): NamedField {
|
||||
export function asNamedField<_embedded = _.GenericEmbedded>(v: _.Value<_embedded>): NamedField {
|
||||
let result = toNamedField(v);
|
||||
if (result === void 0) throw new TypeError(`Invalid NamedField: ${_.stringify(v)}`);
|
||||
return result;
|
||||
}
|
||||
|
||||
export function toNamedField<_embedded extends _.Embeddable = _.GenericEmbedded>(v: _.Value<_embedded>): undefined | NamedField {
|
||||
export function toNamedField<_embedded = _.GenericEmbedded>(v: _.Value<_embedded>): undefined | NamedField {
|
||||
let result: undefined | NamedField;
|
||||
if (_.isSequence(v) && v.length >= 2) {
|
||||
if (_.isSequence(v) && v.length === 2) {
|
||||
let _tmp0: (symbol) | undefined;
|
||||
_tmp0 = typeof v[0] === 'symbol' ? v[0] : void 0;
|
||||
if (_tmp0 !== void 0) {
|
||||
|
@ -556,15 +544,15 @@ export function toNamedField<_embedded extends _.Embeddable = _.GenericEmbedded>
|
|||
|
||||
NamedField.__from_preserve__ = toNamedField;
|
||||
|
||||
export function fromNamedField<_embedded extends _.Embeddable = _.GenericEmbedded>(_v: NamedField): _.Value<_embedded> {return [_v["name"], fromField<_embedded>(_v["type"])];}
|
||||
export function fromNamedField<_embedded = _.GenericEmbedded>(_v: NamedField): _.Value<_embedded> {return [_v["name"], fromField<_embedded>(_v["type"])];}
|
||||
|
||||
export function asField<_embedded extends _.Embeddable = _.GenericEmbedded>(v: _.Value<_embedded>): Field {
|
||||
export function asField<_embedded = _.GenericEmbedded>(v: _.Value<_embedded>): Field {
|
||||
let result = toField(v);
|
||||
if (result === void 0) throw new TypeError(`Invalid Field: ${_.stringify(v)}`);
|
||||
return result;
|
||||
}
|
||||
|
||||
export function toField<_embedded extends _.Embeddable = _.GenericEmbedded>(v: _.Value<_embedded>): undefined | Field {
|
||||
export function toField<_embedded = _.GenericEmbedded>(v: _.Value<_embedded>): undefined | Field {
|
||||
let _tmp0: ({}) | undefined;
|
||||
let result: undefined | Field;
|
||||
_tmp0 = _.is(v, $unit) ? {} : void 0;
|
||||
|
@ -699,7 +687,7 @@ export function toField<_embedded extends _.Embeddable = _.GenericEmbedded>(v: _
|
|||
|
||||
export namespace Field {export const __from_preserve__ = toField;}
|
||||
|
||||
export function fromField<_embedded extends _.Embeddable = _.GenericEmbedded>(_v: Field): _.Value<_embedded> {
|
||||
export function fromField<_embedded = _.GenericEmbedded>(_v: Field): _.Value<_embedded> {
|
||||
switch (_v._variant) {
|
||||
case "unit": {return $unit;};
|
||||
case "any": {return $any;};
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -1,11 +1,11 @@
|
|||
import { compare, Embeddable } from '@preserves/core';
|
||||
import { compare } from '@preserves/core';
|
||||
|
||||
import * as M from './meta';
|
||||
import * as H from './gen/host';
|
||||
|
||||
export * from './gen/host';
|
||||
|
||||
export function definitionType<V extends Embeddable>(p: M.Definition<V>): H.Definition {
|
||||
export function definitionType<V>(p: M.Definition<V>): H.Definition {
|
||||
switch (p._variant) {
|
||||
case 'or': return H.Definition.union([p.pattern0, p.pattern1, ... p.patternN].map(p =>
|
||||
H.Variant({ label: Symbol.for(p.variantLabel), type: patternType(p.pattern) })));
|
||||
|
@ -16,7 +16,7 @@ export function definitionType<V extends Embeddable>(p: M.Definition<V>): H.Defi
|
|||
}
|
||||
}
|
||||
|
||||
export function patternType<V extends Embeddable>(p: M.Pattern<V>): H.Simple {
|
||||
export function patternType<V>(p: M.Pattern<V>): H.Simple {
|
||||
switch (p._variant) {
|
||||
case 'SimplePattern':
|
||||
return H.Simple.Field(fieldType(p.value));
|
||||
|
@ -25,7 +25,7 @@ export function patternType<V extends Embeddable>(p: M.Pattern<V>): H.Simple {
|
|||
}
|
||||
}
|
||||
|
||||
export function fieldType<V extends Embeddable>(p: M.SimplePattern<V>): H.Field {
|
||||
export function fieldType<V>(p: M.SimplePattern<V>): H.Field {
|
||||
switch (p._variant) {
|
||||
case 'any': return H.Field.any();
|
||||
case 'atom': return H.Field.AtomKind(p.atomKind);
|
||||
|
@ -38,19 +38,19 @@ export function fieldType<V extends Embeddable>(p: M.SimplePattern<V>): H.Field
|
|||
}
|
||||
}
|
||||
|
||||
export function productType<V extends Embeddable>(ps: M.NamedPattern<V>[]): H.Simple {
|
||||
export function productType<V>(ps: M.NamedPattern<V>[]): H.Simple {
|
||||
const gathered: H.NamedField[] = [];
|
||||
ps.forEach(p => gather(p, gathered));
|
||||
if (gathered.length === 0) return H.Simple.Field(H.Field.unit());
|
||||
return H.Simple.Record(H.Record(gathered));
|
||||
}
|
||||
|
||||
function promote<V extends Embeddable>(p: M.NamedSimplePattern<V>): M.NamedPattern<V> {
|
||||
function promote<V>(p: M.NamedSimplePattern<V>): M.NamedPattern<V> {
|
||||
if (p._variant === 'named') return p;
|
||||
return M.NamedPattern.anonymous(M.Pattern.SimplePattern(p.value));
|
||||
}
|
||||
|
||||
function gather<V extends Embeddable>(p: M.NamedPattern<V>, into: H.NamedField[]) {
|
||||
function gather<V>(p: M.NamedPattern<V>, into: H.NamedField[]) {
|
||||
switch (p._variant) {
|
||||
case 'named': {
|
||||
const t = fieldType(p.value.pattern);
|
||||
|
|
|
@ -1,38 +1,37 @@
|
|||
import { EncodableDictionary, KeyedDictionary, Dictionary, Value, is, Record, Float, Bytes, isEmbedded, isSequence, Set, Atom, merge as plainMerge, Preservable, PreserveWritable, _iterMap, stringify, fromJS, Embeddable, DictionaryMap, JsDictionary } from '@preserves/core';
|
||||
import { SchemaDefinition } from './reflection';
|
||||
import { EncodableDictionary, KeyedDictionary, Dictionary, Value, is, Record, Float, Bytes, isEmbedded, isSequence, Set, Atom, Embedded, merge as plainMerge, Preservable, PreserveWritable, _iterMap, stringify } from '@preserves/core';
|
||||
import * as M from './meta';
|
||||
import * as H from './host';
|
||||
|
||||
export const UNIT: true = true;
|
||||
|
||||
export type Parsed<V extends Embeddable> = Atom | V | Parsed<V>[] | DictOf<V> | Bindings<V>;
|
||||
export type TopParsed<V extends Embeddable> = Atom | V | Parsed<V>[] | DictOf<V> | TopBindings<V>;
|
||||
export type Parsed<V> = Atom | V | Parsed<V>[] | DictOf<V> | Bindings<V>;
|
||||
export type TopParsed<V> = Atom | V | Parsed<V>[] | DictOf<V> | TopBindings<V>;
|
||||
|
||||
export type Top<V extends Embeddable> =
|
||||
& Preservable<V>
|
||||
& PreserveWritable<V>
|
||||
& { __as_preserve__(): Value<V> };
|
||||
export type Top<V> = Preservable<V> & PreserveWritable<V> & { __as_preserve__(): Value<V> };
|
||||
|
||||
export type DictOf<V extends Embeddable> = EncodableDictionary<V, Parsed<V>, Parsed<V>>;
|
||||
export type DictOf<V> = EncodableDictionary<Parsed<V>, Parsed<V>, V>;
|
||||
|
||||
export type BindingName = string;
|
||||
export type Bindings<V extends Embeddable> = { [key: BindingName]: Parsed<V> };
|
||||
export type TopBindings<V extends Embeddable> = Bindings<V> & Top<V>;
|
||||
export type Bindings<V> = { [key: BindingName]: Parsed<V> };
|
||||
export type TopBindings<V> = Bindings<V> & Top<V>;
|
||||
|
||||
export type SingleConstructor<V extends Embeddable> = ((input: any) => Parsed<V>) & { schema(): SchemaDefinition };
|
||||
export type MultipleConstructors<V extends Embeddable> = { [key: string]: SingleConstructor<V> };
|
||||
export type DefinitionConstructors<V extends Embeddable> = SingleConstructor<V> | MultipleConstructors<V>;
|
||||
export type SingleConstructor<V> = (input: any) => Parsed<V>;
|
||||
export type MultipleConstructors<V> = { [key: string]: SingleConstructor<V> };
|
||||
export type DefinitionConstructors<V> =
|
||||
| { single: SingleConstructor<V> }
|
||||
| { multiple: MultipleConstructors<V> }
|
||||
;
|
||||
|
||||
export namespace Bindings {
|
||||
export function empty<V extends Embeddable>(): Bindings<V> {
|
||||
export function empty<V>(): Bindings<V> {
|
||||
return {};
|
||||
}
|
||||
export function single<V extends Embeddable>(k: BindingName, v: Parsed<V>): Bindings<V> {
|
||||
export function single<V>(k: BindingName, v: Parsed<V>): Bindings<V> {
|
||||
const bs = empty<V>();
|
||||
bs[k] = v;
|
||||
return bs;
|
||||
}
|
||||
export function merge<V extends Embeddable>(... vs: Bindings<V>[]): Bindings<V> {
|
||||
export function merge<V>(... vs: Bindings<V>[]): Bindings<V> {
|
||||
const acc = empty<V>();
|
||||
for (const v of vs) {
|
||||
Object.entries(v).forEach(([kw, vw]) => acc[kw] = vw);
|
||||
|
@ -41,29 +40,29 @@ export namespace Bindings {
|
|||
}
|
||||
}
|
||||
|
||||
export type DynField<V extends Embeddable> =
|
||||
export type DynField<V> =
|
||||
| { type: 'simple', value: Parsed<V> }
|
||||
| { type: 'compound', values: Bindings<V> }
|
||||
;
|
||||
export namespace DynField {
|
||||
export function unwrap<V extends Embeddable>(f: DynField<V>): Parsed<V> {
|
||||
export function unwrap<V>(f: DynField<V>): Parsed<V> {
|
||||
if (f.type === 'simple') return f.value;
|
||||
return f.values;
|
||||
}
|
||||
export function unwrap_compound<V extends Embeddable>(f: DynField<V>): Bindings<V> {
|
||||
export function unwrap_compound<V>(f: DynField<V>): Bindings<V> {
|
||||
if (f.type === 'simple') throw new Error("Cannot unwrap DynField.simple to compound fields");
|
||||
return f.values;
|
||||
}
|
||||
export function simple<V extends Embeddable>(value: Parsed<V>): DynField<V> {
|
||||
export function simple<V>(value: Parsed<V>): DynField<V> {
|
||||
return { type: 'simple', value };
|
||||
}
|
||||
export function maybeSimple<V extends Embeddable>(value: Parsed<V> | null): DynField<V> {
|
||||
export function maybeSimple<V>(value: Parsed<V> | null): DynField<V> {
|
||||
return value === null ? compound(Bindings.empty()) : simple(value);
|
||||
}
|
||||
export function compound<V extends Embeddable>(values: Bindings<V>): DynField<V> {
|
||||
export function compound<V>(values: Bindings<V>): DynField<V> {
|
||||
return { type: 'compound', values };
|
||||
}
|
||||
export function promote<V extends Embeddable>(f: DynField<V>, key?: symbol): Bindings<V> {
|
||||
export function promote<V>(f: DynField<V>, key?: symbol): Bindings<V> {
|
||||
if (f.type === 'compound') return f.values;
|
||||
return key ? Bindings.single(M.jsId(key.description!), f.value) : Bindings.empty();
|
||||
}
|
||||
|
@ -74,27 +73,11 @@ function optmap<A,B>(a: A | undefined, f: (a: A) => B): B | undefined {
|
|||
return f(a);
|
||||
}
|
||||
|
||||
export type Unparseable<V extends Embeddable> = TopParsed<V>;
|
||||
export type Unparser<V extends Embeddable> = (v: Parsed<V>) => Value<V>;
|
||||
export type UnparserCompound<V extends Embeddable> = (v: Bindings<V>) => Value<V>;
|
||||
export type Unparseable<V> = TopParsed<V>;
|
||||
export type Unparser<V> = (v: Parsed<V>) => Value<V>;
|
||||
export type UnparserCompound<V> = (v: Bindings<V>) => Value<V>;
|
||||
|
||||
function attachSchema<V extends Embeddable>(
|
||||
schema: M.Schema<V>,
|
||||
name: symbol,
|
||||
f: (input: any) => Parsed<V>,
|
||||
variant?: symbol,
|
||||
): SingleConstructor<V> {
|
||||
const g = f as SingleConstructor<V>;
|
||||
g.schema = () => ({
|
||||
schema: fromJS(schema),
|
||||
imports: {}, // TODO
|
||||
definitionName: name,
|
||||
variant,
|
||||
});
|
||||
return g;
|
||||
}
|
||||
|
||||
export class SchemaInterpreter<V extends Embeddable> {
|
||||
export class SchemaInterpreter<V> {
|
||||
activeModule: M.ModulePath = [];
|
||||
unparserCache: { [key: string]: [Unparser<V>] } = {};
|
||||
|
||||
|
@ -128,18 +111,14 @@ export class SchemaInterpreter<V extends Embeddable> {
|
|||
}
|
||||
}
|
||||
|
||||
_lookup<R>(
|
||||
modulePath: M.ModulePath,
|
||||
name: symbol,
|
||||
f: (d: M.Definition<V>, schema: M.Schema<V>) => R,
|
||||
): R {
|
||||
_lookup<R>(modulePath: M.ModulePath, name: symbol, f: (d: M.Definition<V>) => R): R {
|
||||
const { resolved, schema } = this._findModule(modulePath);
|
||||
return this._withModule(resolved, () => {
|
||||
const definition = JsDictionary.get(schema.definitions, name);
|
||||
const definition = schema.definitions.get(name);
|
||||
if (definition === void 0) {
|
||||
throw new Error(`No such preserves-schema definition: ${[... modulePath, name].map(s => s.description!).join('.')}`);
|
||||
}
|
||||
return f(definition, schema);
|
||||
return f(definition);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -151,62 +130,41 @@ export class SchemaInterpreter<V extends Embeddable> {
|
|||
return result;
|
||||
}
|
||||
|
||||
buildConstructor(
|
||||
modulePath: M.ModulePath,
|
||||
name: symbol,
|
||||
schema: M.Schema<V>,
|
||||
ty: H.Simple,
|
||||
variant?: symbol,
|
||||
): SingleConstructor<V> {
|
||||
const flatName = M.formatModulePath([
|
||||
... modulePath, name, ... (variant === void 0 ? [] : [variant])]);
|
||||
const mkBase = (variant === void 0)
|
||||
? () => ({})
|
||||
: () => ({ _variant: variant.description! });
|
||||
switch (ty._variant) {
|
||||
case 'Field': {
|
||||
const tmp =
|
||||
ty.value._variant === 'unit'
|
||||
? { [flatName]: () => this.makeTop(modulePath, name, mkBase()) }
|
||||
: (variant === void 0
|
||||
? { [flatName]: (value: any) => value }
|
||||
: { [flatName]: (value: any) => this.makeTop(
|
||||
modulePath, name, { ... mkBase(), value }) });
|
||||
return attachSchema(schema, name, tmp[flatName], variant);
|
||||
}
|
||||
case 'Record': {
|
||||
const rec = ty.value;
|
||||
if (rec.fields.length > 1) {
|
||||
const tmp = { [flatName]: (fields: Bindings<V>) =>
|
||||
this.makeTop(modulePath, name, { ... mkBase(), ... fields }) };
|
||||
return attachSchema(schema, name, tmp[flatName], variant);
|
||||
} else {
|
||||
const tmp = { [flatName]: (field: Parsed<V>) =>
|
||||
this.makeTop(modulePath, name, {
|
||||
... mkBase(),
|
||||
[M.jsId(rec.fields[0].name.description!)]: field,
|
||||
}) };
|
||||
return attachSchema(schema, name, tmp[flatName], variant);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
definitionConstructor(
|
||||
modulePath: M.ModulePath,
|
||||
name: symbol,
|
||||
): DefinitionConstructors<V> {
|
||||
return this._lookup(modulePath, name, (definition, schema): DefinitionConstructors<V> => {
|
||||
return this._lookup(modulePath, name, (definition): DefinitionConstructors<V> => {
|
||||
const ty = H.definitionType(definition);
|
||||
if (ty._variant === 'union') {
|
||||
const multiple: MultipleConstructors<V> = {};
|
||||
ty.variants.forEach(v => {
|
||||
multiple[M.jsId(v.label.description!)] = this.buildConstructor(
|
||||
modulePath, name, schema, v.type, v.label);
|
||||
const _variant = v.label.description!;
|
||||
switch (v.type._variant) {
|
||||
case 'Field':
|
||||
multiple[_variant] = (value: any) =>
|
||||
this.makeTop(modulePath, name, { _variant, value });
|
||||
break;
|
||||
case 'Record':
|
||||
multiple[_variant] = (fields: object) =>
|
||||
this.makeTop(modulePath, name, { _variant, ... fields });
|
||||
break;
|
||||
}
|
||||
});
|
||||
return multiple;
|
||||
return { multiple };
|
||||
} else {
|
||||
return this.buildConstructor(modulePath, name, schema, ty.value);
|
||||
const flatName = M.formatModulePath([... modulePath, name]);
|
||||
switch (ty.value._variant) {
|
||||
case 'Field': {
|
||||
const tmp = { [flatName]: (value: any) => value };
|
||||
return { single: tmp[flatName] };
|
||||
}
|
||||
case 'Record': {
|
||||
const tmp = { [flatName]: (fields: Bindings<V>) =>
|
||||
this.makeTop(modulePath, name, fields) };
|
||||
return { single: tmp[flatName] };
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -234,13 +192,9 @@ export class SchemaInterpreter<V extends Embeddable> {
|
|||
const ty = H.definitionType(definition);
|
||||
if (ty._variant === 'union' || ty.value._variant === 'Record') {
|
||||
return this.makeTop(modulePath, name, result0 as Bindings<V>);
|
||||
} else {
|
||||
if (ty.value.value._variant === 'unit') {
|
||||
return this.makeTop(modulePath, name, {});
|
||||
} else {
|
||||
return result0 as Exclude<Parsed<V>, Bindings<V>>;
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
|
@ -305,13 +259,14 @@ export class SchemaInterpreter<V extends Embeddable> {
|
|||
case 'any': return input;
|
||||
case 'atom': switch (p.atomKind._variant) {
|
||||
case 'Boolean': return inputIf(typeof input === 'boolean');
|
||||
case 'Float': return inputIf(Float.isFloat(input));
|
||||
case 'Double': return inputIf(Float.isDouble(input));
|
||||
case 'SignedInteger': return inputIf(typeof input === 'number' || typeof input === 'bigint');
|
||||
case 'String': return inputIf(typeof input === 'string');
|
||||
case 'ByteString': return inputIf(Bytes.isBytes(input));
|
||||
case 'Symbol': return inputIf(typeof input === 'symbol');
|
||||
}
|
||||
case 'embedded': return isEmbedded(input) ? input : void 0;
|
||||
case 'embedded': return isEmbedded(input) ? input.embeddedValue : void 0;
|
||||
case 'lit': return is(input, p.value) ? null : void 0;
|
||||
case 'seqof': {
|
||||
if (!isSequence(input)) return void 0;
|
||||
|
@ -335,21 +290,10 @@ export class SchemaInterpreter<V extends Embeddable> {
|
|||
}
|
||||
case 'dictof': {
|
||||
if (!Dictionary.isDictionary<V>(input)) return void 0;
|
||||
if (M.isSymbolPattern(p.key)) {
|
||||
const result: Bindings<V> = {};
|
||||
for (const [k, v] of Dictionary.asMap<V>(input)) {
|
||||
const kw = this.parseSimplePattern(p.key, k);
|
||||
if (kw === void 0 || typeof kw !== 'symbol') return void 0;
|
||||
const vw = this.parseSimplePattern(p.value, v);
|
||||
if (vw === void 0) return void 0;
|
||||
result[kw.description!] = vw === null ? UNIT : vw;
|
||||
}
|
||||
return result;
|
||||
} else {
|
||||
const result: DictOf<V> = new EncodableDictionary(
|
||||
this.unparserSimplePattern(p.key),
|
||||
this.unparserSimplePattern(p.value));
|
||||
for (const [k, v] of Dictionary.asMap<V>(input)) {
|
||||
for (const [k, v] of input) {
|
||||
const kw = this.parseSimplePattern(p.key, k);
|
||||
if (kw === void 0) return void 0;
|
||||
const vw = this.parseSimplePattern(p.value, v);
|
||||
|
@ -358,7 +302,6 @@ export class SchemaInterpreter<V extends Embeddable> {
|
|||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
case 'Ref': return this.tryParse(p.value.module, p.value.name, input);
|
||||
}
|
||||
}
|
||||
|
@ -372,7 +315,7 @@ export class SchemaInterpreter<V extends Embeddable> {
|
|||
fsw => Bindings.merge(lw, fsw)));
|
||||
case 'tuple': {
|
||||
if (!isSequence(input)) return void 0;
|
||||
if (input.length < p.patterns.length) return void 0;
|
||||
if (input.length !== p.patterns.length) return void 0;
|
||||
let results: Bindings<V>[] = [];
|
||||
for (let i = 0; i < p.patterns.length; i++) {
|
||||
const w = this.parseNamedPattern(p.patterns[i], input[i]);
|
||||
|
@ -397,11 +340,10 @@ export class SchemaInterpreter<V extends Embeddable> {
|
|||
});
|
||||
}
|
||||
case 'dict': {
|
||||
const inputMap = Dictionary.asMap<V>(input);
|
||||
if (!inputMap) return void 0;
|
||||
if (!Dictionary.isDictionary<V>(input)) return void 0;
|
||||
const results: Bindings<V>[] = [];
|
||||
for (const [key, vp] of p.entries) {
|
||||
const v = inputMap.get(key);
|
||||
const v = input.get(key);
|
||||
if (v === void 0) return void 0;
|
||||
const vw = this.parseNamedSimplePattern(vp, v);
|
||||
if (vw === void 0) return void 0;
|
||||
|
@ -500,7 +442,7 @@ export class SchemaInterpreter<V extends Embeddable> {
|
|||
switch (p._variant) {
|
||||
case 'any': return v => v as Value<V>; // ?!
|
||||
case 'atom': return v => v as Atom;
|
||||
case 'embedded': return v => v as V;
|
||||
case 'embedded': return v => new Embedded(v as V);
|
||||
case 'lit': return _v => p.value;
|
||||
case 'seqof': {
|
||||
const up = this.unparserSimplePattern(p.pattern);
|
||||
|
@ -513,17 +455,8 @@ export class SchemaInterpreter<V extends Embeddable> {
|
|||
case 'dictof': {
|
||||
const kp = this.unparserSimplePattern(p.key);
|
||||
const vp = this.unparserSimplePattern(p.value);
|
||||
return vs => {
|
||||
const d = new DictionaryMap<V>();
|
||||
for (const [k, v] of
|
||||
(Map.isMap(vs)
|
||||
? vs.entries()
|
||||
: JsDictionary.entries(vs as Bindings<V>)))
|
||||
{
|
||||
d.set(kp(k), vp(v));
|
||||
}
|
||||
return M.isSymbolPattern(p.key) ? d.asJsDictionary() : d.asKeyedDictionary();
|
||||
};
|
||||
return vs => new Dictionary<V>(_iterMap(
|
||||
(vs as DictOf<V>).entries(), ([k, v]) => [kp(k), vp(v)]));
|
||||
}
|
||||
case 'Ref': {
|
||||
const up = this._unparser(p.value.module, p.value.name);
|
||||
|
@ -552,11 +485,11 @@ export class SchemaInterpreter<V extends Embeddable> {
|
|||
const ups: [Value<V>, Unparser<V>][] = Array.from(p.entries.entries()).map(
|
||||
([key, vp]) => [key, this.unparserNamedSimplePattern(vp)]);
|
||||
return bs => {
|
||||
const result = new DictionaryMap<V>();
|
||||
const result = new Dictionary<V>();
|
||||
for (const [key, up] of ups) {
|
||||
result.set(key, up(bs));
|
||||
}
|
||||
return result.simplifiedValue();
|
||||
return result;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -573,32 +506,4 @@ export class SchemaInterpreter<V extends Embeddable> {
|
|||
return this.unparserSimplePattern(p.value);
|
||||
}
|
||||
}
|
||||
|
||||
moduleFor(modulePath: M.ModulePath): { [key: string]: any } | undefined {
|
||||
const schema = this.env.get(modulePath);
|
||||
if (schema === void 0) return void 0;
|
||||
const mod: { [key: string]: any } = {};
|
||||
JsDictionary.forEach(schema.definitions, (_d, n) => {
|
||||
const definitionName = n.description!;
|
||||
const definitionId = M.jsId(definitionName);
|
||||
mod[`${definitionId}`] = this.definitionConstructor(modulePath, n);
|
||||
mod[`from${definitionId}`] = this.unparser(modulePath, n);
|
||||
mod[`to${definitionId}`] = (v: Value<V>) => this.tryParse(modulePath, n, v);
|
||||
mod[`as${definitionId}`] = (v: Value<V>) => this.parse(modulePath, n, v);
|
||||
});
|
||||
return mod;
|
||||
}
|
||||
|
||||
moduleTree(tree: { [key: string]: any } = {}): { [key: string]: any } {
|
||||
for (const modulePath of this.env.keys()) {
|
||||
let container = tree;
|
||||
modulePath.slice(0, -1).forEach(n => {
|
||||
if (!(n.description! in container)) container[n.description!] = {};
|
||||
container = container[n.description!];
|
||||
});
|
||||
container[modulePath[modulePath.length - 1].description!] =
|
||||
this.moduleFor(modulePath)!;
|
||||
}
|
||||
return tree;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { Embeddable, GenericEmbedded, is, Value } from '@preserves/core';
|
||||
import { GenericEmbedded, is, Value } from '@preserves/core';
|
||||
import * as M from './gen/schema';
|
||||
import { isJsKeyword } from './compiler/jskw';
|
||||
|
||||
|
@ -98,8 +98,3 @@ export function namelike(x: Input): string | undefined {
|
|||
if (typeof x === 'boolean') return x ? 'true' : 'false';
|
||||
return void 0;
|
||||
}
|
||||
|
||||
export function isSymbolPattern<T extends Embeddable>(p: M.SimplePattern<T>): boolean {
|
||||
return p._variant === 'atom'
|
||||
&& p.atomKind._variant === 'Symbol';
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { Reader, Annotated, Dictionary, is, peel, preserves, Record, strip, Tuple, Position, position, stringify, isCompound, EncodableDictionary, annotate, annotations, isEmbedded, GenericEmbedded, genericEmbeddedTypeDecode, JsDictionary, KeyedDictionary } from '@preserves/core';
|
||||
import { Reader, Annotated, Dictionary, is, peel, preserves, Record, strip, Tuple, Position, position, stringify, isCompound, EncodableDictionary, annotate, annotations, isEmbedded, GenericEmbedded, genericEmbeddedTypeDecode } from '@preserves/core';
|
||||
import { Input, Pattern, Schema, Definition, CompoundPattern, SimplePattern } from './meta';
|
||||
import * as M from './meta';
|
||||
import { SchemaSyntaxError } from './error';
|
||||
|
@ -70,7 +70,7 @@ export function parseSchema(toplevelTokens: Array<Input>, options: SchemaReaderO
|
|||
{
|
||||
let version: M.Version | undefined = void 0;
|
||||
let embeddedType: M.EmbeddedTypeName = M.EmbeddedTypeName.$false();
|
||||
let definitions: M.Definitions = {};
|
||||
let definitions: M.Definitions = new EncodableDictionary(k => k, M.fromDefinition);
|
||||
|
||||
function process(toplevelTokens: Array<Input>): void {
|
||||
const toplevelClauses = splitBy(peel(toplevelTokens) as Array<Input>, M.DOT);
|
||||
|
@ -82,10 +82,10 @@ export function parseSchema(toplevelTokens: Array<Input>, options: SchemaReaderO
|
|||
if (!M.isValidToken(name.description!)) {
|
||||
throw new SchemaSyntaxError(preserves`Invalid definition name: ${name}`, pos);
|
||||
}
|
||||
if (JsDictionary.has(definitions, name)) {
|
||||
if (definitions.has(name)) {
|
||||
throw new SchemaSyntaxError(preserves`Duplicate definition: ${clause}`, pos);
|
||||
}
|
||||
JsDictionary.set(definitions, name, parseDefinition(name, pos, clause.slice(2)));
|
||||
definitions.set(name, parseDefinition(name, pos, clause.slice(2)));
|
||||
} else if (clause.length === 2 && is(clause[0], M.$version)) {
|
||||
version = M.asVersion(peel(clause[1]));
|
||||
} else if (clause.length === 2 && is(clause[0], M.$embeddedType)) {
|
||||
|
@ -225,6 +225,7 @@ function parsePattern(name: symbol, body0: Array<Input>): Pattern {
|
|||
switch (str) {
|
||||
case 'any': return ks(M.SimplePattern.any());
|
||||
case 'bool': return ks(M.SimplePattern.atom(M.AtomKind.Boolean()));
|
||||
case 'float': return ks(M.SimplePattern.atom(M.AtomKind.Float()));
|
||||
case 'double': return ks(M.SimplePattern.atom(M.AtomKind.Double()));
|
||||
case 'int': return ks(M.SimplePattern.atom(M.AtomKind.SignedInteger()));
|
||||
case 'string': return ks(M.SimplePattern.atom(M.AtomKind.String()));
|
||||
|
@ -232,7 +233,7 @@ function parsePattern(name: symbol, body0: Array<Input>): Pattern {
|
|||
case 'symbol': return ks(M.SimplePattern.atom(M.AtomKind.Symbol()));
|
||||
default: {
|
||||
if (str[0] === '=') {
|
||||
return ks(M.SimplePattern.lit<GenericEmbedded>(Symbol.for(str.slice(1))));
|
||||
return ks(M.SimplePattern.lit(Symbol.for(str.slice(1))));
|
||||
} else if (M.isValidQid(str)) {
|
||||
return ks(M.SimplePattern.Ref(parseRef(str, pos)));
|
||||
} else {
|
||||
|
@ -260,22 +261,22 @@ function parsePattern(name: symbol, body0: Array<Input>): Pattern {
|
|||
if (item.size !== 1) complain();
|
||||
const [vp] = item.values();
|
||||
return ks(M.SimplePattern.setof(walkSimple(vp)));
|
||||
} else {
|
||||
const itemMap = Dictionary.asMap<M.InputEmbedded, Input>(item);
|
||||
if (itemMap && itemMap.size === 2 && itemMap.has(M.DOTDOTDOT)) {
|
||||
const v = itemMap.clone();
|
||||
} else if (Dictionary.isDictionary<M.InputEmbedded, Input>(item)
|
||||
&& item.size === 2
|
||||
&& item.has(M.DOTDOTDOT))
|
||||
{
|
||||
const v = item.clone();
|
||||
v.delete(M.DOTDOTDOT);
|
||||
const [[kp, vp]] = v.entries();
|
||||
return ks(M.SimplePattern.dictof({ key: walkSimple(kp), value: walkSimple(vp) }));
|
||||
} else if (isCompound(item)) {
|
||||
return kf();
|
||||
} else if (isEmbedded(item)) {
|
||||
return ks(M.SimplePattern.embedded(walkSimple(item.generic)));
|
||||
return ks(M.SimplePattern.embedded(walkSimple(item.embeddedValue.generic)));
|
||||
} else {
|
||||
return ks(M.SimplePattern.lit(strip(item)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function parseCompound(item0: Input): CompoundPattern {
|
||||
const pos = position(item0);
|
||||
|
@ -312,22 +313,21 @@ function parsePattern(name: symbol, body0: Array<Input>): Pattern {
|
|||
});
|
||||
} else if (Array.isArray(item)) {
|
||||
return M.CompoundPattern.tuple(item.map(maybeNamed));
|
||||
} else {
|
||||
const itemMap = Dictionary.asMap<M.InputEmbedded, Input>(item);
|
||||
if (itemMap && !itemMap.has(M.DOTDOTDOT)) {
|
||||
const entries = new KeyedDictionary<M.InputEmbedded, Input, M.NamedSimplePattern>();
|
||||
itemMap.forEach((vp, k) => entries.set(
|
||||
} else if (Dictionary.isDictionary<M.InputEmbedded, Input>(item) && !item.has(M.DOTDOTDOT)) {
|
||||
return M.CompoundPattern.dict(
|
||||
M.DictionaryEntries(item.mapEntries<M.NamedSimplePattern, Input, M.InputEmbedded>(
|
||||
([k, vp]) => [
|
||||
strip(k),
|
||||
_maybeNamed(M.NamedSimplePattern.named,
|
||||
_maybeNamed(
|
||||
M.NamedSimplePattern.named,
|
||||
M.NamedSimplePattern.anonymous,
|
||||
walkSimple,
|
||||
strip(k))(vp)));
|
||||
return M.CompoundPattern.dict(M.DictionaryEntries(entries));
|
||||
strip(k))(vp)
|
||||
])));
|
||||
} else {
|
||||
complain();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const walk = (b: Input): Pattern => parsePattern(name, [b]);
|
||||
const walkSimple = (b: Input): SimplePattern => parseSimple(b, p => p, () => {
|
||||
|
|
|
@ -1,29 +0,0 @@
|
|||
import { } from '@preserves/core';
|
||||
import { compile, Meta as M, readSchema } from '../src/index';
|
||||
import './test-utils';
|
||||
import { js as format } from 'js-beautify';
|
||||
|
||||
function compileSingleSchema(source: string): string {
|
||||
return format(compile([], [Symbol.for('test')], readSchema(`version 1 . ${source}`), {}));
|
||||
}
|
||||
|
||||
describe('compiler', () => {
|
||||
it('basics', () => {
|
||||
expect(compileSingleSchema(`X = int .`)).toContain(`\nexport type X = number;\n`);
|
||||
});
|
||||
|
||||
it('symbol-keyed dictionary', () => {
|
||||
const s = compileSingleSchema(`D = { symbol:any ...:... } .`);
|
||||
expect(s).toMatch(/^export type D.*= _.JsDictionary < _.Value < _embedded >> ;$/m);
|
||||
});
|
||||
|
||||
it('string-keyed dictionary', () => {
|
||||
const s = compileSingleSchema(`D = { string:any ...:... } .`);
|
||||
expect(s).toMatch(/^export type D.*= _.EncodableDictionary < _embedded, string, _.Value < _embedded >> ;$/m);
|
||||
});
|
||||
|
||||
it('any-keyed dictionary', () => {
|
||||
const s = compileSingleSchema(`D = { any:any ...:... } .`);
|
||||
expect(s).toMatch(/^export type D.*= _.EncodableDictionary < _embedded, _.Value < _embedded > , _.Value < _embedded >> ;$/m);
|
||||
});
|
||||
});
|
|
@ -1,111 +0,0 @@
|
|||
import { GenericEmbedded, stringify, fromJS, parse, EncodableDictionary, KeyedDictionary } from '@preserves/core';
|
||||
import { SchemaInterpreter, readSchema } from '../src/index';
|
||||
import './test-utils';
|
||||
|
||||
describe('interpreter', () => {
|
||||
const I = new SchemaInterpreter<GenericEmbedded>();
|
||||
|
||||
const X_schema = readSchema(`
|
||||
version 1 .
|
||||
A = <foo> .
|
||||
B = <bar @v int> .
|
||||
C = <zot @v int @w int> .
|
||||
N = int .
|
||||
|
||||
U = <class @a int @b int> / <true @c int @d int> .
|
||||
V = <public @n int> / <private @n int> .
|
||||
W = <const> / <let> .
|
||||
|
||||
D1 = { symbol:A ...:... } .
|
||||
D2 = { any:A ...:... } .
|
||||
D3 = { string:A ...:... } .
|
||||
`);
|
||||
const X = Symbol.for('X');
|
||||
I.env.set([X], X_schema);
|
||||
|
||||
const E = I.moduleTree();
|
||||
|
||||
it('basically works', () => {
|
||||
const a = E.X.A();
|
||||
const b = E.X.B(22);
|
||||
const c = E.X.C({v: 33, w: 44});
|
||||
const n = E.X.N(22);
|
||||
expect(stringify(a)).toBe('<foo>');
|
||||
expect(stringify(b)).toBe('<bar 22>');
|
||||
expect(stringify(c)).toBe('<zot 33 44>');
|
||||
expect(stringify(n)).toBe('22');
|
||||
expect(parse('<foo>')).is(fromJS(a));
|
||||
expect(parse('<bar 22>')).is(fromJS(b));
|
||||
expect(parse('<zot 33 44>')).is(fromJS(c));
|
||||
expect(parse('22')).is(fromJS(n));
|
||||
expect(stringify(E.X.asA(fromJS(a)))).toBe('<foo>');
|
||||
expect(stringify(E.X.asB(fromJS(b)))).toBe('<bar 22>');
|
||||
expect(stringify(E.X.asC(fromJS(c)))).toBe('<zot 33 44>');
|
||||
expect(stringify(E.X.asN(fromJS(n)))).toBe('22');
|
||||
});
|
||||
|
||||
it('escapes JS keywords', () => {
|
||||
expect(Object.keys(E.X.U)).toEqual(['$class', '$true']);
|
||||
expect(Object.keys(E.X.V)).toEqual(['$public', '$private']);
|
||||
expect(Object.keys(E.X.W)).toEqual(['$const', '$let']);
|
||||
});
|
||||
|
||||
const STANDARD_METHODS = ['__as_preserve__', '__preserve_on__', '__preserve_text_on__'];
|
||||
|
||||
it('accepts correct arguments to n-ary variant ctors', () => {
|
||||
expect(stringify(E.X.U.$class({ a: 123, b: 234 }))).toBe('<class 123 234>');
|
||||
expect(stringify(E.X.U.$true({ c: 123, d: 234 }))).toBe('<true 123 234>');
|
||||
expect(Object.keys(E.X.U.$class({ a: 123, b: 234 }))).toEqual([
|
||||
'_variant', 'a', 'b', ... STANDARD_METHODS]);
|
||||
});
|
||||
|
||||
it('accepts correct arguments to unary variant ctors', () => {
|
||||
expect(stringify(E.X.V.$public(123))).toBe('<public 123>');
|
||||
expect(stringify(E.X.V.$private(123))).toBe('<private 123>');
|
||||
expect(Object.keys(E.X.V.$public(123))).toEqual([
|
||||
'_variant', 'n', ... STANDARD_METHODS]);
|
||||
});
|
||||
|
||||
it('accepts correct arguments to nullary variant ctors', () => {
|
||||
expect(stringify(E.X.W.$const())).toBe('<const>');
|
||||
expect(stringify(E.X.W.$let())).toBe('<let>');
|
||||
expect(Object.keys(E.X.W.$const())).toEqual([
|
||||
'_variant', ... STANDARD_METHODS]);
|
||||
});
|
||||
|
||||
it('produces JsDictionary for symbol-keyed dicts', () => {
|
||||
const v = E.X.asD1(parse('{ a: <foo>, b: <foo>}'));
|
||||
expect(Object.keys(v)).toEqual(['a', 'b']);
|
||||
expect(stringify(E.X.fromA(v.a))).toBe('<foo>');
|
||||
expect(stringify(E.X.fromA(v.b))).toBe('<foo>');
|
||||
});
|
||||
|
||||
it('produces EncodableDictionary for any-keyed dicts', () => {
|
||||
const d2 = E.X.asD2(parse('{ a: <foo>, b: <foo>}'));
|
||||
expect(d2 instanceof EncodableDictionary).toBe(true);
|
||||
expect(Array.from(d2.keys())).toEqual([Symbol.for('a'), Symbol.for('b')]);
|
||||
expect(fromJS(d2.get(Symbol.for('a')))).is(fromJS(E.X.A()));
|
||||
});
|
||||
|
||||
it('accepts either kind of dictionary for symbol-keyed dicts', () => {
|
||||
const v = { a: E.X.A(), b: E.X.A() };
|
||||
expect(stringify(v)).toBe('{a: <foo> b: <foo>}');
|
||||
expect(E.X.fromD1(v)).is(parse('{a: <foo> b: <foo>}'));
|
||||
expect(E.X.fromD1(E.X.D1(v))).is(parse('{a: <foo> b: <foo>}'));
|
||||
expect(E.X.fromD1(E.X.D1(new KeyedDictionary([
|
||||
[parse('a'), parse('<foo>')],
|
||||
[parse('b'), parse('<foo>')],
|
||||
])))).is(parse('{a: <foo> b: <foo>}'));
|
||||
});
|
||||
|
||||
it('accepts either kind of dictionary for any-keyed dicts', () => {
|
||||
const v = { a: E.X.A(), b: E.X.A() };
|
||||
expect(stringify(v)).toBe('{a: <foo> b: <foo>}');
|
||||
expect(E.X.fromD2(v)).is(parse('{a: <foo> b: <foo>}'));
|
||||
expect(E.X.fromD2(E.X.D2(v))).is(parse('{a: <foo> b: <foo>}'));
|
||||
expect(E.X.fromD2(E.X.D2(new KeyedDictionary([
|
||||
[parse('a'), parse('<foo>')],
|
||||
[parse('b'), parse('<foo>')],
|
||||
])))).is(parse('{a: <foo> b: <foo>}'));
|
||||
});
|
||||
});
|
|
@ -1,4 +1,3 @@
|
|||
import { JsDictionary } from '@preserves/core';
|
||||
import { readSchema, Meta } from '../src/index';
|
||||
|
||||
describe('reader schema', () => {
|
||||
|
@ -15,12 +14,12 @@ describe('reader schema', () => {
|
|||
'__preserve_on__',
|
||||
'__preserve_text_on__',
|
||||
]);
|
||||
expect(JsDictionary.size(s.definitions)).toBe(0);
|
||||
expect(s.definitions.size).toBe(0);
|
||||
expect(s.embeddedType._variant).toBe('false');
|
||||
});
|
||||
it('understands patterns under embed', () => {
|
||||
const s = readSchema('version 1 . X = #:0 .');
|
||||
const def: Meta.Definition = JsDictionary.get(s.definitions, Symbol.for('X'))!;
|
||||
const s = readSchema('version 1 . X = #!0 .');
|
||||
const def: Meta.Definition = s.definitions.get(Symbol.for('X'))!;
|
||||
if (def._variant !== 'Pattern') fail('bad definition 1');
|
||||
if (def.value._variant !== 'SimplePattern') fail ('bad definition 2');
|
||||
if (def.value.value._variant !== 'embedded') fail('bad definition 3');
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import { Value, is, preserves, Embeddable } from '@preserves/core';
|
||||
import { Value, is, preserves } from '@preserves/core';
|
||||
|
||||
declare global {
|
||||
namespace jest {
|
||||
interface Matchers<R> {
|
||||
is<T extends Embeddable>(expected: Value<T>): R;
|
||||
is<T>(expected: Value<T>): R;
|
||||
toThrowFilter(f: (e: Error) => boolean): R;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,5 +21,6 @@ open "cd packages/schema; yarn run compile:watch"
|
|||
open "cd packages/schema; yarn run rollup:watch"
|
||||
open "cd packages/schema; yarn run test:watch"
|
||||
open "cd packages/schema-cli; yarn run compile:watch"
|
||||
open "cd packages/schema-cli; yarn run rollup:watch"
|
||||
|
||||
tmux select-layout even-vertical
|
||||
|
|
|
@ -308,18 +308,6 @@
|
|||
resolved "https://registry.yarnpkg.com/@hutson/parse-repository-url/-/parse-repository-url-3.0.2.tgz#98c23c950a3d9b6c8f0daed06da6c3af06981340"
|
||||
integrity sha512-H9XAx3hc0BQHY6l+IFSWHDySypcXsvsuLhgYLUGywmJ5pswRVQJUHpOsobnLYp2ZUaUlKiKDrgWWhosOwAEM8Q==
|
||||
|
||||
"@isaacs/cliui@^8.0.2":
|
||||
version "8.0.2"
|
||||
resolved "https://registry.yarnpkg.com/@isaacs/cliui/-/cliui-8.0.2.tgz#b37667b7bc181c168782259bab42474fbf52b550"
|
||||
integrity sha512-O8jcjabXaleOG9DQ0+ARXWZBTfnP4WNAqzuiJK7ll44AmxGKv/J2M4TPjxjY3znBCfvBXFzucm1twdyFybFqEA==
|
||||
dependencies:
|
||||
string-width "^5.1.2"
|
||||
string-width-cjs "npm:string-width@^4.2.0"
|
||||
strip-ansi "^7.0.1"
|
||||
strip-ansi-cjs "npm:strip-ansi@^6.0.1"
|
||||
wrap-ansi "^8.1.0"
|
||||
wrap-ansi-cjs "npm:wrap-ansi@^7.0.0"
|
||||
|
||||
"@istanbuljs/load-nyc-config@^1.0.0":
|
||||
version "1.1.0"
|
||||
resolved "https://registry.yarnpkg.com/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz#fd3db1d59ecf7cf121e80650bb86712f9b55eced"
|
||||
|
@ -1429,16 +1417,6 @@
|
|||
dependencies:
|
||||
"@octokit/openapi-types" "^12.11.0"
|
||||
|
||||
"@one-ini/wasm@0.1.1":
|
||||
version "0.1.1"
|
||||
resolved "https://registry.yarnpkg.com/@one-ini/wasm/-/wasm-0.1.1.tgz#6013659736c9dbfccc96e8a9c2b3de317df39323"
|
||||
integrity sha512-XuySG1E38YScSJoMlqovLru4KTUNSjgVTIjyh7qMX6aNN5HY5Ct5LhRJdxO79JtTzKfzV/bnWpz+zquYrISsvw==
|
||||
|
||||
"@pkgjs/parseargs@^0.11.0":
|
||||
version "0.11.0"
|
||||
resolved "https://registry.yarnpkg.com/@pkgjs/parseargs/-/parseargs-0.11.0.tgz#a77ea742fab25775145434eb1d2328cf5013ac33"
|
||||
integrity sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg==
|
||||
|
||||
"@rollup/plugin-terser@^0.4":
|
||||
version "0.4.0"
|
||||
resolved "https://registry.yarnpkg.com/@rollup/plugin-terser/-/plugin-terser-0.4.0.tgz#4c76249ad337f3eb04ab409332f23717af2c1fbf"
|
||||
|
@ -1562,11 +1540,6 @@
|
|||
jest-matcher-utils "^27.0.0"
|
||||
pretty-format "^27.0.0"
|
||||
|
||||
"@types/js-beautify@1.14":
|
||||
version "1.14.3"
|
||||
resolved "https://registry.yarnpkg.com/@types/js-beautify/-/js-beautify-1.14.3.tgz#6ced76f79935e37e0d613110dea369881d93c1ff"
|
||||
integrity sha512-FMbQHz+qd9DoGvgLHxeqqVPaNRffpIu5ZjozwV8hf9JAGpIOzuAf4wGbRSo8LNITHqGjmmVjaMggTT5P4v4IHg==
|
||||
|
||||
"@types/minimatch@*":
|
||||
version "5.1.2"
|
||||
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-5.1.2.tgz#07508b45797cb81ec3f273011b054cd0755eddca"
|
||||
|
@ -1654,11 +1627,6 @@ abbrev@1:
|
|||
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8"
|
||||
integrity sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q==
|
||||
|
||||
abbrev@^2.0.0:
|
||||
version "2.0.0"
|
||||
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-2.0.0.tgz#cf59829b8b4f03f89dda2771cb7f3653828c89bf"
|
||||
integrity sha512-6/mh1E2u2YgEsCHdY0Yx5oW+61gZU+1vXaoiHHrpKeuRNNgFvS+/jrwHiQhB5apAf5oB7UB7E19ol2R2LKH8hQ==
|
||||
|
||||
acorn-globals@^6.0.0:
|
||||
version "6.0.0"
|
||||
resolved "https://registry.yarnpkg.com/acorn-globals/-/acorn-globals-6.0.0.tgz#46cdd39f0f8ff08a876619b55f5ac8a6dc770b45"
|
||||
|
@ -1748,11 +1716,6 @@ ansi-regex@^5.0.1:
|
|||
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-5.0.1.tgz#082cb2c89c9fe8659a311a53bd6a4dc5301db304"
|
||||
integrity sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==
|
||||
|
||||
ansi-regex@^6.0.1:
|
||||
version "6.0.1"
|
||||
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-6.0.1.tgz#3183e38fae9a65d7cb5e53945cd5897d0260a06a"
|
||||
integrity sha512-n5M855fKb2SsfMIiFFoVrABHJC8QtHwVx+mHWP3QcEqBHYienj5dHSgjbxtC0WEZXYt4wcD6zrQElDPhFuZgfA==
|
||||
|
||||
ansi-styles@^3.2.1:
|
||||
version "3.2.1"
|
||||
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-3.2.1.tgz#41fbb20243e50b12be0f04b8dedbf07520ce841d"
|
||||
|
@ -1772,11 +1735,6 @@ ansi-styles@^5.0.0:
|
|||
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-5.2.0.tgz#07449690ad45777d1924ac2abb2fc8895dba836b"
|
||||
integrity sha512-Cxwpt2SfTzTtXcfOlzGEee8O+c+MmUgGrNiBcXnuWxuFJHe6a5Hz7qwhwe5OgaSYI0IJvkLqWX1ASG+cJOkEiA==
|
||||
|
||||
ansi-styles@^6.1.0:
|
||||
version "6.2.1"
|
||||
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-6.2.1.tgz#0e62320cf99c21afff3b3012192546aacbfb05c5"
|
||||
integrity sha512-bN798gFfQX+viw3R7yrGWRqnrN2oRkEkUjjl4JNn4E8GxxbjtG3FbrEIIY3l8/hrwUwIeCZvi4QuOTP4MErVug==
|
||||
|
||||
anymatch@^3.0.3, anymatch@~3.1.2:
|
||||
version "3.1.3"
|
||||
resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-3.1.3.tgz#790c58b19ba1720a84205b57c618d5ad8524973e"
|
||||
|
@ -1984,13 +1942,6 @@ brace-expansion@^1.1.7:
|
|||
balanced-match "^1.0.0"
|
||||
concat-map "0.0.1"
|
||||
|
||||
brace-expansion@^2.0.1:
|
||||
version "2.0.1"
|
||||
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-2.0.1.tgz#1edc459e0f0c548486ecf9fc99f2221364b9a0ae"
|
||||
integrity sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==
|
||||
dependencies:
|
||||
balanced-match "^1.0.0"
|
||||
|
||||
braces@^3.0.2, braces@~3.0.2:
|
||||
version "3.0.2"
|
||||
resolved "https://registry.yarnpkg.com/braces/-/braces-3.0.2.tgz#3454e1a462ee8d599e236df336cd9ea4f8afe107"
|
||||
|
@ -2281,11 +2232,6 @@ combined-stream@^1.0.6, combined-stream@^1.0.8, combined-stream@~1.0.6:
|
|||
dependencies:
|
||||
delayed-stream "~1.0.0"
|
||||
|
||||
commander@^10.0.0:
|
||||
version "10.0.1"
|
||||
resolved "https://registry.yarnpkg.com/commander/-/commander-10.0.1.tgz#881ee46b4f77d1c1dccc5823433aa39b022cbe06"
|
||||
integrity sha512-y4Mg2tXshplEbSGzx7amzPwKKOCGuoSRP/CjEdwwk0FOGlUbq6lKuoyDZTNZkmxHdJtp54hdfY/JUrdL7Xfdug==
|
||||
|
||||
commander@^2.20.0:
|
||||
version "2.20.3"
|
||||
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33"
|
||||
|
@ -2319,7 +2265,7 @@ concat-stream@^2.0.0:
|
|||
readable-stream "^3.0.2"
|
||||
typedarray "^0.0.6"
|
||||
|
||||
config-chain@^1.1.12, config-chain@^1.1.13:
|
||||
config-chain@^1.1.12:
|
||||
version "1.1.13"
|
||||
resolved "https://registry.yarnpkg.com/config-chain/-/config-chain-1.1.13.tgz#fad0795aa6a6cdaff9ed1b68e9dff94372c232f4"
|
||||
integrity sha512-qj+f8APARXHrM0hraqXYb2/bOVSV4PvJQlNZ/DVj0QrmNM2q2euizkeuVckQ57J+W0mRH6Hvi+k50M4Jul2VRQ==
|
||||
|
@ -2445,7 +2391,7 @@ create-require@^1.1.0:
|
|||
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
|
||||
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==
|
||||
|
||||
cross-spawn@^7.0.0, cross-spawn@^7.0.3:
|
||||
cross-spawn@^7.0.3:
|
||||
version "7.0.3"
|
||||
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
|
||||
integrity sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==
|
||||
|
@ -2655,11 +2601,6 @@ dynamic-dedupe@^0.3.0:
|
|||
dependencies:
|
||||
xtend "^4.0.0"
|
||||
|
||||
eastasianwidth@^0.2.0:
|
||||
version "0.2.0"
|
||||
resolved "https://registry.yarnpkg.com/eastasianwidth/-/eastasianwidth-0.2.0.tgz#696ce2ec0aa0e6ea93a397ffcf24aa7840c827cb"
|
||||
integrity sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==
|
||||
|
||||
ecc-jsbn@~0.1.1:
|
||||
version "0.1.2"
|
||||
resolved "https://registry.yarnpkg.com/ecc-jsbn/-/ecc-jsbn-0.1.2.tgz#3a83a904e54353287874c564b7549386849a98c9"
|
||||
|
@ -2668,16 +2609,6 @@ ecc-jsbn@~0.1.1:
|
|||
jsbn "~0.1.0"
|
||||
safer-buffer "^2.1.0"
|
||||
|
||||
editorconfig@^1.0.4:
|
||||
version "1.0.4"
|
||||
resolved "https://registry.yarnpkg.com/editorconfig/-/editorconfig-1.0.4.tgz#040c9a8e9a6c5288388b87c2db07028aa89f53a3"
|
||||
integrity sha512-L9Qe08KWTlqYMVvMcTIvMAdl1cDUubzRNYL+WfA4bLDMHe4nemKkpmYzkznE1FwLKu0EEmy6obgQKzMJrg4x9Q==
|
||||
dependencies:
|
||||
"@one-ini/wasm" "0.1.1"
|
||||
commander "^10.0.0"
|
||||
minimatch "9.0.1"
|
||||
semver "^7.5.3"
|
||||
|
||||
electron-to-chromium@^1.4.284:
|
||||
version "1.4.286"
|
||||
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.286.tgz#0e039de59135f44ab9a8ec9025e53a9135eba11f"
|
||||
|
@ -2693,11 +2624,6 @@ emoji-regex@^8.0.0:
|
|||
resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-8.0.0.tgz#e818fd69ce5ccfcb404594f842963bf53164cc37"
|
||||
integrity sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==
|
||||
|
||||
emoji-regex@^9.2.2:
|
||||
version "9.2.2"
|
||||
resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-9.2.2.tgz#840c8803b0d8047f4ff0cf963176b32d4ef3ed72"
|
||||
integrity sha512-L18DaJsXSUk2+42pv8mLs5jJT2hqFkFE4j21wOmgbUqsZ2hL72NsUU785g9RXgo3s0ZNgVl42TiHp3ZtOv/Vyg==
|
||||
|
||||
encoding@^0.1.12:
|
||||
version "0.1.13"
|
||||
resolved "https://registry.yarnpkg.com/encoding/-/encoding-0.1.13.tgz#56574afdd791f54a8e9b2785c0582a2d26210fa9"
|
||||
|
@ -2971,14 +2897,6 @@ for-each@^0.3.3:
|
|||
dependencies:
|
||||
is-callable "^1.1.3"
|
||||
|
||||
foreground-child@^3.1.0:
|
||||
version "3.1.1"
|
||||
resolved "https://registry.yarnpkg.com/foreground-child/-/foreground-child-3.1.1.tgz#1d173e776d75d2772fed08efe4a0de1ea1b12d0d"
|
||||
integrity sha512-TMKDUnIte6bfb5nWv7V/caI169OHgvwjb7V4WkeUvbQQdjr5rWKqHFiKWb/fcOwB+CzBT+qbWjvj+DVwRskpIg==
|
||||
dependencies:
|
||||
cross-spawn "^7.0.0"
|
||||
signal-exit "^4.0.1"
|
||||
|
||||
forever-agent@~0.6.1:
|
||||
version "0.6.1"
|
||||
resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91"
|
||||
|
@ -3185,17 +3103,6 @@ glob-parent@^5.1.1, glob-parent@^5.1.2, glob-parent@~5.1.2:
|
|||
dependencies:
|
||||
is-glob "^4.0.1"
|
||||
|
||||
glob@^10.3.3:
|
||||
version "10.3.10"
|
||||
resolved "https://registry.yarnpkg.com/glob/-/glob-10.3.10.tgz#0351ebb809fd187fe421ab96af83d3a70715df4b"
|
||||
integrity sha512-fa46+tv1Ak0UPK1TOy/pZrIybNNt4HCv7SDzwyfiOZkvZLEbjsZkJBPtDHVshZjbecAoAGSC20MjLDG/qr679g==
|
||||
dependencies:
|
||||
foreground-child "^3.1.0"
|
||||
jackspeak "^2.3.5"
|
||||
minimatch "^9.0.1"
|
||||
minipass "^5.0.0 || ^6.0.2 || ^7.0.0"
|
||||
path-scurry "^1.10.1"
|
||||
|
||||
glob@^7.1, glob@^7.1.1, glob@^7.1.2, glob@^7.1.3, glob@^7.1.4, glob@^7.1.6:
|
||||
version "7.2.3"
|
||||
resolved "https://registry.yarnpkg.com/glob/-/glob-7.2.3.tgz#b8df0fb802bbfa8e89bd1d938b4e16578ed44f2b"
|
||||
|
@ -3791,15 +3698,6 @@ istanbul-reports@^3.1.3:
|
|||
html-escaper "^2.0.0"
|
||||
istanbul-lib-report "^3.0.0"
|
||||
|
||||
jackspeak@^2.3.5:
|
||||
version "2.3.6"
|
||||
resolved "https://registry.yarnpkg.com/jackspeak/-/jackspeak-2.3.6.tgz#647ecc472238aee4b06ac0e461acc21a8c505ca8"
|
||||
integrity sha512-N3yCS/NegsOBokc8GAdM8UcmfsKiSS8cipheD/nivzr700H+nsMOxJjQnvwOcRYVuFkdH0wGUvW2WbXGmrZGbQ==
|
||||
dependencies:
|
||||
"@isaacs/cliui" "^8.0.2"
|
||||
optionalDependencies:
|
||||
"@pkgjs/parseargs" "^0.11.0"
|
||||
|
||||
jest-changed-files@^27.5.1:
|
||||
version "27.5.1"
|
||||
resolved "https://registry.yarnpkg.com/jest-changed-files/-/jest-changed-files-27.5.1.tgz#a348aed00ec9bf671cc58a66fcbe7c3dfd6a68f5"
|
||||
|
@ -4205,22 +4103,6 @@ jest@^27.4:
|
|||
import-local "^3.0.2"
|
||||
jest-cli "^27.5.1"
|
||||
|
||||
js-beautify@1.15:
|
||||
version "1.15.1"
|
||||
resolved "https://registry.yarnpkg.com/js-beautify/-/js-beautify-1.15.1.tgz#4695afb508c324e1084ee0b952a102023fc65b64"
|
||||
integrity sha512-ESjNzSlt/sWE8sciZH8kBF8BPlwXPwhR6pWKAw8bw4Bwj+iZcnKW6ONWUutJ7eObuBZQpiIb8S7OYspWrKt7rA==
|
||||
dependencies:
|
||||
config-chain "^1.1.13"
|
||||
editorconfig "^1.0.4"
|
||||
glob "^10.3.3"
|
||||
js-cookie "^3.0.5"
|
||||
nopt "^7.2.0"
|
||||
|
||||
js-cookie@^3.0.5:
|
||||
version "3.0.5"
|
||||
resolved "https://registry.yarnpkg.com/js-cookie/-/js-cookie-3.0.5.tgz#0b7e2fd0c01552c58ba86e0841f94dc2557dcdbc"
|
||||
integrity sha512-cEiJEAEoIbWfCZYKWhVwFuvPX1gETRYPw6LlaTKoxD3s2AkXzkCjnp6h0V77ozyqj0jakteJ4YqDJT830+lVGw==
|
||||
|
||||
js-tokens@^4.0.0:
|
||||
version "4.0.0"
|
||||
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
|
||||
|
@ -4488,11 +4370,6 @@ lru-cache@^6.0.0:
|
|||
dependencies:
|
||||
yallist "^4.0.0"
|
||||
|
||||
"lru-cache@^9.1.1 || ^10.0.0":
|
||||
version "10.2.0"
|
||||
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-10.2.0.tgz#0bd445ca57363465900f4d1f9bd8db343a4d95c3"
|
||||
integrity sha512-2bIM8x+VAf6JT4bKAljS1qUWgMsqZRPGJS6FSahIMPVvctcNhyVp7AJu7quxOW9jwkryBReKZY5tY5JYv2n/7Q==
|
||||
|
||||
make-dir@^2.1.0:
|
||||
version "2.1.0"
|
||||
resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-2.1.0.tgz#5f0310e18b8be898cc07009295a30ae41e91e6f5"
|
||||
|
@ -4630,13 +4507,6 @@ min-indent@^1.0.0:
|
|||
resolved "https://registry.yarnpkg.com/min-indent/-/min-indent-1.0.1.tgz#a63f681673b30571fbe8bc25686ae746eefa9869"
|
||||
integrity sha512-I9jwMn07Sy/IwOj3zVkVik2JTvgpaykDZEigL6Rx6N9LbMywwUSMtxET+7lVoDLLd3O3IXwJwvuuns8UB/HeAg==
|
||||
|
||||
minimatch@9.0.1:
|
||||
version "9.0.1"
|
||||
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-9.0.1.tgz#8a555f541cf976c622daf078bb28f29fb927c253"
|
||||
integrity sha512-0jWhJpD/MdhPXwPuiRkCbfYfSKp2qnn2eOc279qI7f+osl/l+prKSrvhg157zSYvx/1nmgn2NqdT6k2Z7zSH9w==
|
||||
dependencies:
|
||||
brace-expansion "^2.0.1"
|
||||
|
||||
minimatch@^3.0, minimatch@^3.0.4, minimatch@^3.1.1:
|
||||
version "3.1.2"
|
||||
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.1.2.tgz#19cd194bfd3e428f049a70817c038d89ab4be35b"
|
||||
|
@ -4644,13 +4514,6 @@ minimatch@^3.0, minimatch@^3.0.4, minimatch@^3.1.1:
|
|||
dependencies:
|
||||
brace-expansion "^1.1.7"
|
||||
|
||||
minimatch@^9.0.1:
|
||||
version "9.0.3"
|
||||
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-9.0.3.tgz#a6e00c3de44c3a542bfaae70abfc22420a6da825"
|
||||
integrity sha512-RHiac9mvaRw0x3AYRgDC1CxAP7HTcNrrECeA8YYJeWnpo+2Q5CegtZjaotWTWxDG3UeGA1coE05iH1mPjT/2mg==
|
||||
dependencies:
|
||||
brace-expansion "^2.0.1"
|
||||
|
||||
minimist-options@4.1.0:
|
||||
version "4.1.0"
|
||||
resolved "https://registry.yarnpkg.com/minimist-options/-/minimist-options-4.1.0.tgz#c0655713c53a8a2ebd77ffa247d342c40f010619"
|
||||
|
@ -4732,11 +4595,6 @@ minipass@^4.0.0:
|
|||
resolved "https://registry.yarnpkg.com/minipass/-/minipass-4.0.1.tgz#2b9408c6e81bb8b338d600fb3685e375a370a057"
|
||||
integrity sha512-V9esFpNbK0arbN3fm2sxDKqMYgIp7XtVdE4Esj+PE4Qaaxdg1wIw48ITQIOn1sc8xXSmUviVL3cyjMqPlrVkiA==
|
||||
|
||||
"minipass@^5.0.0 || ^6.0.2 || ^7.0.0":
|
||||
version "7.0.4"
|
||||
resolved "https://registry.yarnpkg.com/minipass/-/minipass-7.0.4.tgz#dbce03740f50a4786ba994c1fb908844d27b038c"
|
||||
integrity sha512-jYofLM5Dam9279rdkWzqHozUo4ybjdZmCsDHePy5V/PbBcVMiSZR97gmAy45aqi8CK1lG2ECd356FU86avfwUQ==
|
||||
|
||||
minizlib@^1.3.3:
|
||||
version "1.3.3"
|
||||
resolved "https://registry.yarnpkg.com/minizlib/-/minizlib-1.3.3.tgz#2290de96818a34c29551c8a8d301216bd65a861d"
|
||||
|
@ -4884,13 +4742,6 @@ nopt@^5.0.0:
|
|||
dependencies:
|
||||
abbrev "1"
|
||||
|
||||
nopt@^7.2.0:
|
||||
version "7.2.0"
|
||||
resolved "https://registry.yarnpkg.com/nopt/-/nopt-7.2.0.tgz#067378c68116f602f552876194fd11f1292503d7"
|
||||
integrity sha512-CVDtwCdhYIvnAzFoJ6NJ6dX3oga9/HyciQDnG1vQDjSLMeKLJ4A93ZqYKDrgYSr1FBY5/hMYC+2VCi24pgpkGA==
|
||||
dependencies:
|
||||
abbrev "^2.0.0"
|
||||
|
||||
normalize-package-data@^2.0.0, normalize-package-data@^2.3.2, normalize-package-data@^2.5.0:
|
||||
version "2.5.0"
|
||||
resolved "https://registry.yarnpkg.com/normalize-package-data/-/normalize-package-data-2.5.0.tgz#e66db1838b200c1dfc233225d12cb36520e234a8"
|
||||
|
@ -5307,14 +5158,6 @@ path-parse@^1.0.7:
|
|||
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.7.tgz#fbc114b60ca42b30d9daf5858e4bd68bbedb6735"
|
||||
integrity sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==
|
||||
|
||||
path-scurry@^1.10.1:
|
||||
version "1.10.1"
|
||||
resolved "https://registry.yarnpkg.com/path-scurry/-/path-scurry-1.10.1.tgz#9ba6bf5aa8500fe9fd67df4f0d9483b2b0bfc698"
|
||||
integrity sha512-MkhCqzzBEpPvxxQ71Md0b1Kk51W01lrYvlMzSUaIzNsODdd7mqhiimSZlr+VegAz5Z6Vzt9Xg2ttE//XBhH3EQ==
|
||||
dependencies:
|
||||
lru-cache "^9.1.1 || ^10.0.0"
|
||||
minipass "^5.0.0 || ^6.0.2 || ^7.0.0"
|
||||
|
||||
path-type@^3.0.0:
|
||||
version "3.0.0"
|
||||
resolved "https://registry.yarnpkg.com/path-type/-/path-type-3.0.0.tgz#cef31dc8e0a1a3bb0d105c0cd97cf3bf47f4e36f"
|
||||
|
@ -5824,13 +5667,6 @@ semver@^6.0.0, semver@^6.3.0:
|
|||
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
|
||||
integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==
|
||||
|
||||
semver@^7.5.3:
|
||||
version "7.6.0"
|
||||
resolved "https://registry.yarnpkg.com/semver/-/semver-7.6.0.tgz#1a46a4db4bffcccd97b743b5005c8325f23d4e2d"
|
||||
integrity sha512-EnwXhrlwXMk9gKu5/flx5sv/an57AkRplG3hTK68W7FRDN+k+OWBj65M7719OkA82XLBxrcX0KSHj+X5COhOVg==
|
||||
dependencies:
|
||||
lru-cache "^6.0.0"
|
||||
|
||||
serialize-javascript@^6.0.0:
|
||||
version "6.0.1"
|
||||
resolved "https://registry.yarnpkg.com/serialize-javascript/-/serialize-javascript-6.0.1.tgz#b206efb27c3da0b0ab6b52f48d170b7996458e5c"
|
||||
|
@ -5876,11 +5712,6 @@ signal-exit@^3.0.0, signal-exit@^3.0.2, signal-exit@^3.0.3:
|
|||
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.7.tgz#a9a1767f8af84155114eaabd73f99273c8f59ad9"
|
||||
integrity sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==
|
||||
|
||||
signal-exit@^4.0.1:
|
||||
version "4.1.0"
|
||||
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-4.1.0.tgz#952188c1cbd546070e2dd20d0f41c0ae0530cb04"
|
||||
integrity sha512-bzyZ1e88w9O1iNJbKnOlvYTrWPDl46O1bG0D3XInv+9tkPrxrN8jUUTiFlDkkmKWgn1M6CfIA13SuGqOa9Korw==
|
||||
|
||||
sisteransi@^1.0.5:
|
||||
version "1.0.5"
|
||||
resolved "https://registry.yarnpkg.com/sisteransi/-/sisteransi-1.0.5.tgz#134d681297756437cc05ca01370d3a7a571075ed"
|
||||
|
@ -6056,15 +5887,6 @@ string-length@^4.0.1:
|
|||
char-regex "^1.0.2"
|
||||
strip-ansi "^6.0.0"
|
||||
|
||||
"string-width-cjs@npm:string-width@^4.2.0":
|
||||
version "4.2.3"
|
||||
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
|
||||
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
|
||||
dependencies:
|
||||
emoji-regex "^8.0.0"
|
||||
is-fullwidth-code-point "^3.0.0"
|
||||
strip-ansi "^6.0.1"
|
||||
|
||||
string-width@^1.0.1:
|
||||
version "1.0.2"
|
||||
resolved "https://registry.yarnpkg.com/string-width/-/string-width-1.0.2.tgz#118bdf5b8cdc51a2a7e70d211e07e2b0b9b107d3"
|
||||
|
@ -6083,15 +5905,6 @@ string-width@^1.0.1:
|
|||
is-fullwidth-code-point "^3.0.0"
|
||||
strip-ansi "^6.0.1"
|
||||
|
||||
string-width@^5.0.1, string-width@^5.1.2:
|
||||
version "5.1.2"
|
||||
resolved "https://registry.yarnpkg.com/string-width/-/string-width-5.1.2.tgz#14f8daec6d81e7221d2a357e668cab73bdbca794"
|
||||
integrity sha512-HnLOCR3vjcY8beoNLtcjZ5/nxn2afmME6lhrDrebokqMap+XbeW8n9TXpPDOqdGK5qcI3oT0GKTW6wC7EMiVqA==
|
||||
dependencies:
|
||||
eastasianwidth "^0.2.0"
|
||||
emoji-regex "^9.2.2"
|
||||
strip-ansi "^7.0.1"
|
||||
|
||||
string.prototype.trimend@^1.0.6:
|
||||
version "1.0.6"
|
||||
resolved "https://registry.yarnpkg.com/string.prototype.trimend/-/string.prototype.trimend-1.0.6.tgz#c4a27fa026d979d79c04f17397f250a462944533"
|
||||
|
@ -6124,13 +5937,6 @@ string_decoder@~1.1.1:
|
|||
dependencies:
|
||||
safe-buffer "~5.1.0"
|
||||
|
||||
"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
|
||||
version "6.0.1"
|
||||
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
|
||||
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
|
||||
dependencies:
|
||||
ansi-regex "^5.0.1"
|
||||
|
||||
strip-ansi@^3.0.0, strip-ansi@^3.0.1:
|
||||
version "3.0.1"
|
||||
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-3.0.1.tgz#6a385fb8853d952d5ff05d0e8aaf94278dc63dcf"
|
||||
|
@ -6145,13 +5951,6 @@ strip-ansi@^6.0.0, strip-ansi@^6.0.1:
|
|||
dependencies:
|
||||
ansi-regex "^5.0.1"
|
||||
|
||||
strip-ansi@^7.0.1:
|
||||
version "7.1.0"
|
||||
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.1.0.tgz#d5b6568ca689d8561370b0707685d22434faff45"
|
||||
integrity sha512-iq6eVVI64nQQTRYq2KtEg2d2uU7LElhTJwsH4YzIHZshxlgZms/wIc4VoDQTlG/IvVIrBKG06CrZnp0qv7hkcQ==
|
||||
dependencies:
|
||||
ansi-regex "^6.0.1"
|
||||
|
||||
strip-bom@^3.0.0:
|
||||
version "3.0.0"
|
||||
resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-3.0.0.tgz#2334c18e9c759f7bdd56fdef7e9ae3d588e68ed3"
|
||||
|
@ -6806,15 +6605,6 @@ wordwrap@^1.0.0:
|
|||
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-1.0.0.tgz#27584810891456a4171c8d0226441ade90cbcaeb"
|
||||
integrity sha512-gvVzJFlPycKc5dZN4yPkP8w7Dc37BtP1yczEneOb4uq34pXZcvrtRTmWV8W+Ume+XCxKgbjM+nevkyFPMybd4Q==
|
||||
|
||||
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
|
||||
version "7.0.0"
|
||||
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
|
||||
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
|
||||
dependencies:
|
||||
ansi-styles "^4.0.0"
|
||||
string-width "^4.1.0"
|
||||
strip-ansi "^6.0.0"
|
||||
|
||||
wrap-ansi@^7.0.0:
|
||||
version "7.0.0"
|
||||
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
|
||||
|
@ -6824,15 +6614,6 @@ wrap-ansi@^7.0.0:
|
|||
string-width "^4.1.0"
|
||||
strip-ansi "^6.0.0"
|
||||
|
||||
wrap-ansi@^8.1.0:
|
||||
version "8.1.0"
|
||||
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"
|
||||
integrity sha512-si7QWI6zUMq56bESFvagtmzMdGOtoxfR+Sez11Mobfc7tm+VkUckk9bW2UeffTGVUbOksxmSw0AA2gs8g71NCQ==
|
||||
dependencies:
|
||||
ansi-styles "^6.1.0"
|
||||
string-width "^5.0.1"
|
||||
strip-ansi "^7.0.1"
|
||||
|
||||
wrappy@1:
|
||||
version "1.0.2"
|
||||
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
if ! [ -d .venv ]
|
||||
then
|
||||
python3 -m venv .venv
|
||||
python -m venv .venv
|
||||
. .venv/bin/activate
|
||||
pip install -e '.[dev]'
|
||||
else
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
# This used to just be
|
||||
# PACKAGEVERSION := "`python3 setup.py --version`"
|
||||
PACKAGEVERSION := $(shell ./print-package-version)
|
||||
PACKAGEVERSION := $(shell python -c 'import tomllib; print(tomllib.load(open("pyproject.toml", "rb"))["project"]["version"])')
|
||||
|
||||
all: test build-docs
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ The main package re-exports a subset of the exports of its constituent modules:
|
|||
- From [preserves.values][]:
|
||||
- [Annotated][preserves.values.Annotated]
|
||||
- [Embedded][preserves.values.Embedded]
|
||||
- [Float][preserves.values.Float]
|
||||
- [ImmutableDict][preserves.values.ImmutableDict]
|
||||
- [Record][preserves.values.Record]
|
||||
- [Symbol][preserves.values.Symbol]
|
||||
|
@ -55,7 +56,7 @@ Finally, it provides a few utility aliases for common tasks:
|
|||
|
||||
'''
|
||||
|
||||
from .values import Symbol, Record, ImmutableDict, Embedded, preserve
|
||||
from .values import Float, Symbol, Record, ImmutableDict, Embedded, preserve
|
||||
|
||||
from .values import Annotated, is_annotated, strip_annotations, annotate
|
||||
|
||||
|
|
|
@ -206,6 +206,7 @@ class Decoder(BinaryCodec):
|
|||
return self.wrap(Embedded(self.decode_embedded(self.next())))
|
||||
if tag == 0x87:
|
||||
count = self.nextbyte()
|
||||
if count == 4: return self.wrap(Float.from_bytes(self.nextbytes(4)))
|
||||
if count == 8: return self.wrap(struct.unpack('>d', self.nextbytes(8))[0])
|
||||
raise DecodeError('Invalid IEEE754 size')
|
||||
if tag == 0xb0: return self.wrap(self.nextint(self.varint()))
|
||||
|
@ -217,11 +218,7 @@ class Decoder(BinaryCodec):
|
|||
if not vs: raise DecodeError('Too few elements in encoded record')
|
||||
return self.wrap(Record(vs[0], vs[1:]))
|
||||
if tag == 0xb5: return self.wrap(tuple(self.nextvalues()))
|
||||
if tag == 0xb6:
|
||||
vs = self.nextvalues()
|
||||
s = frozenset(vs)
|
||||
if len(s) != len(vs): raise DecodeError('Duplicate value')
|
||||
return self.wrap(s)
|
||||
if tag == 0xb6: return self.wrap(frozenset(self.nextvalues()))
|
||||
if tag == 0xb7: return self.wrap(ImmutableDict.from_kvs(self.nextvalues()))
|
||||
raise DecodeError('Invalid tag: ' + hex(tag))
|
||||
|
||||
|
|
|
@ -33,12 +33,12 @@ import numbers
|
|||
from enum import Enum
|
||||
from functools import cmp_to_key
|
||||
|
||||
from .values import preserve, Embedded, Record, Symbol, cmp_floats, _unwrap
|
||||
from .values import preserve, Float, Embedded, Record, Symbol, cmp_floats, _unwrap
|
||||
from .compat import basestring_
|
||||
|
||||
class TypeNumber(Enum):
|
||||
BOOL = 0
|
||||
# FLOAT = 1 # single-precision
|
||||
FLOAT = 1
|
||||
DOUBLE = 2
|
||||
SIGNED_INTEGER = 3
|
||||
STRING = 4
|
||||
|
@ -57,6 +57,7 @@ def type_number(v):
|
|||
raise ValueError('type_number expects Preserves value; use preserve()')
|
||||
|
||||
if isinstance(v, bool): return TypeNumber.BOOL
|
||||
if isinstance(v, Float): return TypeNumber.FLOAT
|
||||
if isinstance(v, float): return TypeNumber.DOUBLE
|
||||
if isinstance(v, numbers.Number): return TypeNumber.SIGNED_INTEGER
|
||||
if isinstance(v, basestring_): return TypeNumber.STRING
|
||||
|
|
|
@ -9,7 +9,7 @@ def map_embeddeds(f, v):
|
|||
|
||||
```python
|
||||
>>> map_embeddeds(lambda w: Embedded(f'w={w}'), ['a', Embedded(123), {'z': 6.0}])
|
||||
('a', #:'w=123', {'z': 6.0})
|
||||
('a', #!'w=123', {'z': 6.0})
|
||||
|
||||
```
|
||||
"""
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
´³schema·³version°³definitions·³Axis´³orµµ±values´³rec´³lit³values„´³tupleµ„„„„µ±descendants´³rec´³lit³descendants„´³tupleµ„„„„µ±at´³rec´³lit³at„´³tupleµ´³named³key³any„„„„„µ±label´³rec´³lit³label„´³tupleµ„„„„µ±keys´³rec´³lit³keys„´³tupleµ„„„„µ±length´³rec´³lit³length„´³tupleµ„„„„µ±annotations´³rec´³lit³annotations„´³tupleµ„„„„µ±embedded´³rec´³lit³embedded„´³tupleµ„„„„µ±parse´³rec´³lit³parse„´³tupleµ´³named³module´³seqof´³atom³Symbol„„„´³named³name´³atom³Symbol„„„„„„µ±unparse´³rec´³lit³unparse„´³tupleµ´³named³module´³seqof´³atom³Symbol„„„´³named³name´³atom³Symbol„„„„„„„„³Step´³orµµ±Axis´³refµ„³Axis„„µ±Filter´³refµ„³Filter„„µ±Function´³refµ„³Function„„„„³Filter´³orµµ±nop´³rec´³lit³nop„´³tupleµ„„„„µ±compare´³rec´³lit³compare„´³tupleµ´³named³op´³refµ„³
|
||||
Comparison„„´³named³literal³any„„„„„µ±regex´³rec´³lit³regex„´³tupleµ´³named³regex´³atom³String„„„„„„µ±test´³rec´³lit³test„´³tupleµ´³named³pred´³refµ„³ Predicate„„„„„„µ±real´³rec´³lit³real„´³tupleµ„„„„µ±int´³rec´³lit³int„´³tupleµ„„„„µ±kind´³rec´³lit³kind„´³tupleµ´³named³kind´³refµ„³ ValueKind„„„„„„„„³Function´³rec´³lit³count„´³tupleµ´³named³selector´³refµ„³Selector„„„„„³Selector´³seqof´³refµ„³Step„„³ Predicate´³orµµ±Selector´³refµ„³Selector„„µ±not´³rec´³lit³not„´³tupleµ´³named³pred´³refµ„³ Predicate„„„„„„µ±or´³rec´³lit³or„´³tupleµ´³named³preds´³seqof´³refµ„³ Predicate„„„„„„„µ±and´³rec´³lit³and„´³tupleµ´³named³preds´³seqof´³refµ„³ Predicate„„„„„„„„„³ ValueKind´³orµµ±Boolean´³lit³Boolean„„µ±Double´³lit³Double„„µ±
SignedInteger´³lit³
SignedInteger„„µ±String´³lit³String„„µ±
|
||||
Comparison„„´³named³literal³any„„„„„µ±regex´³rec´³lit³regex„´³tupleµ´³named³regex´³atom³String„„„„„„µ±test´³rec´³lit³test„´³tupleµ´³named³pred´³refµ„³ Predicate„„„„„„µ±real´³rec´³lit³real„´³tupleµ„„„„µ±int´³rec´³lit³int„´³tupleµ„„„„µ±kind´³rec´³lit³kind„´³tupleµ´³named³kind´³refµ„³ ValueKind„„„„„„„„³Function´³rec´³lit³count„´³tupleµ´³named³selector´³refµ„³Selector„„„„„³Selector´³seqof´³refµ„³Step„„³ Predicate´³orµµ±Selector´³refµ„³Selector„„µ±not´³rec´³lit³not„´³tupleµ´³named³pred´³refµ„³ Predicate„„„„„„µ±or´³rec´³lit³or„´³tupleµ´³named³preds´³seqof´³refµ„³ Predicate„„„„„„„µ±and´³rec´³lit³and„´³tupleµ´³named³preds´³seqof´³refµ„³ Predicate„„„„„„„„„³ ValueKind´³orµµ±Boolean´³lit³Boolean„„µ±Float´³lit³Float„„µ±Double´³lit³Double„„µ±
SignedInteger´³lit³
SignedInteger„„µ±String´³lit³String„„µ±
|
||||
ByteString´³lit³
|
||||
ByteString„„µ±Symbol´³lit³Symbol„„µ±Record´³lit³Record„„µ±Sequence´³lit³Sequence„„µ±Set´³lit³Set„„µ±
|
||||
Dictionary´³lit³
|
||||
|
|
|
@ -59,16 +59,13 @@ The result of evaluating it on `testdata` is as follows:
|
|||
"In each test, let stripped = strip(annotatedValue),"
|
||||
" encodeBinary(·) produce canonical ordering and no annotations,"
|
||||
" looseEncodeBinary(·) produce any ordering, but with annotations,"
|
||||
" annotatedBinary(·) produce “canonical ordering”, but with annotations,"
|
||||
" annotatedBinary(·) produce canonical ordering, but with annotations,"
|
||||
" decodeBinary(·) include annotations,"
|
||||
" encodeText(·) include annotations,"
|
||||
" decodeText(·) include annotations,"
|
||||
"and check the following numbered expectations according to the table above:"
|
||||
"Implementations may vary in their treatment of the difference between expectations"
|
||||
"21/22 and 31/32, depending on how they wish to treat end-of-stream conditions."
|
||||
"The idea of canonical-ordering-with-annotations is to encode, say, sets with their elements"
|
||||
"in sorted order of their canonical annotationless binary encoding, but then actually"
|
||||
"*serialized* with the annotations present."
|
||||
|
||||
```
|
||||
|
||||
|
@ -173,6 +170,7 @@ FILTER_RE2 = Symbol('=r')
|
|||
FILTER_LABEL = Symbol('^')
|
||||
|
||||
FILTER_BOOL = Symbol('bool')
|
||||
FILTER_FLOAT = Symbol('float')
|
||||
FILTER_DOUBLE = Symbol('double')
|
||||
FILTER_INT = Symbol('int')
|
||||
FILTER_STRING = Symbol('string')
|
||||
|
@ -228,6 +226,7 @@ def parse_step(tokens):
|
|||
if t == TRANSFORM_REAL: return syntax.Step.Filter(syntax.Filter.real)
|
||||
if t == TRANSFORM_INT: return syntax.Step.Filter(syntax.Filter.int)
|
||||
if t == FILTER_BOOL: return kind_filter(syntax.ValueKind.Boolean())
|
||||
if t == FILTER_FLOAT: return kind_filter(syntax.ValueKind.Float())
|
||||
if t == FILTER_DOUBLE: return kind_filter(syntax.ValueKind.Double())
|
||||
if t == FILTER_INT: return kind_filter(syntax.ValueKind.SignedInteger())
|
||||
if t == FILTER_STRING: return kind_filter(syntax.ValueKind.String())
|
||||
|
@ -449,6 +448,8 @@ def exec(self, v):
|
|||
@extend(syntax.Filter.real)
|
||||
def exec(self, v):
|
||||
v = preserve(_unwrap(v))
|
||||
if isinstance(v, Float):
|
||||
return (v.value,)
|
||||
if type(v) == float:
|
||||
return (v,)
|
||||
if type(v) == int:
|
||||
|
@ -458,6 +459,8 @@ def exec(self, v):
|
|||
@extend(syntax.Filter.int)
|
||||
def exec(self, v):
|
||||
v = preserve(_unwrap(v))
|
||||
if isinstance(v, Float):
|
||||
return (int(v.value()),)
|
||||
if type(v) == float:
|
||||
return (int(v),)
|
||||
if type(v) == int:
|
||||
|
@ -473,6 +476,10 @@ def exec(self, v):
|
|||
def exec(self, v):
|
||||
return (v,) if type(v) == bool else ()
|
||||
|
||||
@extend(syntax.ValueKind.Float)
|
||||
def exec(self, v):
|
||||
return (v,) if isinstance(v, Float) else ()
|
||||
|
||||
@extend(syntax.ValueKind.Double)
|
||||
def exec(self, v):
|
||||
return (v,) if type(v) == float else ()
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
´³schema·³version°³definitions·³Ref´³rec´³lit³ref„´³tupleµ´³named³module´³refµ„³
|
||||
ModulePath„„´³named³name´³atom³Symbol„„„„„³Bundle´³rec´³lit³bundle„´³tupleµ´³named³modules´³refµ„³Modules„„„„„³Schema´³rec´³lit³schema„´³tupleµ´³dict·³version´³named³version´³refµ„³Version„„³definitions´³named³definitions´³refµ„³Definitions„„³embeddedType´³named³embeddedType´³refµ„³EmbeddedTypeName„„„„„„„³Binding´³rec´³lit³named„´³tupleµ´³named³name´³atom³Symbol„„´³named³pattern´³refµ„³
SimplePattern„„„„„³Modules´³dictof´³refµ„³
|
||||
ModulePath„´³refµ„³Schema„„³Pattern´³orµµ±
SimplePattern´³refµ„³
SimplePattern„„µ±CompoundPattern´³refµ„³CompoundPattern„„„„³Version´³lit°„³AtomKind´³orµµ±Boolean´³lit³Boolean„„µ±Double´³lit³Double„„µ±
SignedInteger´³lit³
SignedInteger„„µ±String´³lit³String„„µ±
|
||||
ModulePath„´³refµ„³Schema„„³Pattern´³orµµ±
SimplePattern´³refµ„³
SimplePattern„„µ±CompoundPattern´³refµ„³CompoundPattern„„„„³Version´³lit°„³AtomKind´³orµµ±Boolean´³lit³Boolean„„µ±Float´³lit³Float„„µ±Double´³lit³Double„„µ±
SignedInteger´³lit³
SignedInteger„„µ±String´³lit³String„„µ±
|
||||
ByteString´³lit³
|
||||
ByteString„„µ±Symbol´³lit³Symbol„„„„³
|
||||
Definition´³orµµ±or´³rec´³lit³or„´³tupleµ´³tuplePrefixµ´³named³pattern0´³refµ„³NamedAlternative„„´³named³pattern1´³refµ„³NamedAlternative„„„´³named³patternN´³seqof´³refµ„³NamedAlternative„„„„„„„„µ±and´³rec´³lit³and„´³tupleµ´³tuplePrefixµ´³named³pattern0´³refµ„³NamedPattern„„´³named³pattern1´³refµ„³NamedPattern„„„´³named³patternN´³seqof´³refµ„³NamedPattern„„„„„„„„µ±Pattern´³refµ„³Pattern„„„„³
|
||||
|
|
|
@ -279,6 +279,7 @@ DICT = Symbol('dict')
|
|||
DICTOF = Symbol('dictof')
|
||||
DOUBLE = Symbol('Double')
|
||||
EMBEDDED = Symbol('embedded')
|
||||
FLOAT = Symbol('Float')
|
||||
LIT = Symbol('lit')
|
||||
NAMED = Symbol('named')
|
||||
OR = Symbol('or')
|
||||
|
@ -468,6 +469,7 @@ class SchemaObject:
|
|||
if p.key == ATOM:
|
||||
k = p[0]
|
||||
if k == BOOLEAN and isinstance(v, bool): return v
|
||||
if k == FLOAT and isinstance(v, Float): return v
|
||||
if k == DOUBLE and isinstance(v, float): return v
|
||||
if k == SIGNED_INTEGER and isinstance(v, int): return v
|
||||
if k == STRING and isinstance(v, str): return v
|
||||
|
@ -505,7 +507,7 @@ class SchemaObject:
|
|||
return ()
|
||||
if p.key == TUPLE:
|
||||
if not sequenceish(v): raise SchemaDecodeFailed(cls, p, v)
|
||||
if len(v) < len(p[0]): raise SchemaDecodeFailed(cls, p, v)
|
||||
if len(v) != len(p[0]): raise SchemaDecodeFailed(cls, p, v)
|
||||
i = 0
|
||||
for pp in p[0]:
|
||||
cls.parse(pp, v[i], args)
|
||||
|
|
|
@ -28,7 +28,7 @@ from .binary import Decoder
|
|||
class TextCodec(object):
|
||||
pass
|
||||
|
||||
NUMBER_RE = re.compile(r'^([-+]?\d+)((\.\d+([eE][-+]?\d+)?)|([eE][-+]?\d+))?$')
|
||||
NUMBER_RE = re.compile(r'^([-+]?\d+)(((\.\d+([eE][-+]?\d+)?)|([eE][-+]?\d+))([fF]?))?$')
|
||||
|
||||
class Parser(TextCodec):
|
||||
"""Parser for the human-readable Preserves text syntax.
|
||||
|
@ -251,13 +251,15 @@ class Parser(TextCodec):
|
|||
if c == '=': continue
|
||||
acc.append(c)
|
||||
|
||||
def read_hex_float(self):
|
||||
def read_hex_float(self, bytecount):
|
||||
if self.nextchar() != '"':
|
||||
raise DecodeError('Missing open-double-quote in hex-encoded floating-point number')
|
||||
bs = self.read_hex_binary()
|
||||
if len(bs) != 8:
|
||||
if len(bs) != bytecount:
|
||||
raise DecodeError('Incorrect number of bytes in hex-encoded floating-point number')
|
||||
return struct.unpack('>d', bs)[0]
|
||||
if bytecount == 4: return Float.from_bytes(bs)
|
||||
if bytecount == 8: return struct.unpack('>d', bs)[0]
|
||||
raise DecodeError('Unsupported byte count in hex-encoded floating-point number')
|
||||
|
||||
def upto(self, delimiter, skip_commas):
|
||||
vs = []
|
||||
|
@ -306,8 +308,10 @@ class Parser(TextCodec):
|
|||
if m:
|
||||
if m[2] is None:
|
||||
return int(m[1])
|
||||
elif m[7] == '':
|
||||
return float(m[1] + m[3])
|
||||
else:
|
||||
return float(acc)
|
||||
return Float(float(m[1] + m[3]))
|
||||
else:
|
||||
return Symbol(acc)
|
||||
|
||||
|
@ -346,10 +350,6 @@ class Parser(TextCodec):
|
|||
c = self.nextchar()
|
||||
if c in ' \t': return self.unshift_annotation(self.comment_line(), self.next())
|
||||
if c in '\n\r': return self.unshift_annotation('', self.next())
|
||||
if c == '!':
|
||||
return self.unshift_annotation(
|
||||
Record(Symbol('interpreter'), [self.comment_line()]),
|
||||
self.next())
|
||||
if c == 'f': self.require_delimiter('#f'); return self.wrap(False)
|
||||
if c == 't': self.require_delimiter('#t'); return self.wrap(True)
|
||||
if c == '{': return self.wrap(self.read_set())
|
||||
|
@ -357,10 +357,11 @@ class Parser(TextCodec):
|
|||
if c == 'x':
|
||||
c = self.nextchar()
|
||||
if c == '"': return self.wrap(self.read_hex_binary())
|
||||
if c == 'd': return self.wrap(self.read_hex_float())
|
||||
if c == 'f': return self.wrap(self.read_hex_float(4))
|
||||
if c == 'd': return self.wrap(self.read_hex_float(8))
|
||||
raise DecodeError('Invalid #x syntax')
|
||||
if c == '[': return self.wrap(self.read_base64_binary())
|
||||
if c == ':':
|
||||
if c == '!':
|
||||
if self.parse_embedded is None:
|
||||
raise DecodeError('No parse_embedded function supplied')
|
||||
return self.wrap(Embedded(self.parse_embedded(self.next())))
|
||||
|
|
|
@ -39,6 +39,135 @@ def cmp_floats(a, b):
|
|||
if b & 0x8000000000000000: b = b ^ 0x7fffffffffffffff
|
||||
return a - b
|
||||
|
||||
class Float(object):
|
||||
"""Wrapper for treating a Python double-precision floating-point value as a
|
||||
single-precision (32-bit) float, from Preserves' perspective. (Python lacks native
|
||||
single-precision floating point support.)
|
||||
|
||||
```python
|
||||
>>> Float(3.45)
|
||||
Float(3.45)
|
||||
>>> import preserves
|
||||
>>> preserves.stringify(Float(3.45))
|
||||
'3.45f'
|
||||
>>> preserves.stringify(3.45)
|
||||
'3.45'
|
||||
>>> preserves.parse('3.45f')
|
||||
Float(3.45)
|
||||
>>> preserves.parse('3.45')
|
||||
3.45
|
||||
>>> preserves.encode(Float(3.45))
|
||||
b'\\x87\\x04@\\\\\\xcc\\xcd'
|
||||
>>> preserves.encode(3.45)
|
||||
b'\\x87\\x08@\\x0b\\x99\\x99\\x99\\x99\\x99\\x9a'
|
||||
|
||||
```
|
||||
|
||||
Attributes:
|
||||
value (float): the double-precision representation of intended single-precision value
|
||||
"""
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
def __eq__(self, other):
|
||||
other = _unwrap(other)
|
||||
if other.__class__ is self.__class__:
|
||||
return cmp_floats(self.value, other.value) == 0
|
||||
|
||||
def __lt__(self, other):
|
||||
other = _unwrap(other)
|
||||
if other.__class__ is self.__class__:
|
||||
return cmp_floats(self.value, other.value) < 0
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self.__eq__(other)
|
||||
|
||||
def __hash__(self):
|
||||
return hash(self.value)
|
||||
|
||||
def __repr__(self):
|
||||
return 'Float(' + repr(self.value) + ')'
|
||||
|
||||
def to_bytes(self):
|
||||
"""Converts this 32-bit single-precision floating point value to its binary32 format,
|
||||
taking care to preserve the quiet/signalling bit-pattern of NaN values, unlike its
|
||||
`struct.pack('>f', ...)` equivalent.
|
||||
|
||||
```python
|
||||
>>> Float.from_bytes(b'\\x7f\\x80\\x00{')
|
||||
Float(nan)
|
||||
>>> Float.from_bytes(b'\\x7f\\x80\\x00{').to_bytes()
|
||||
b'\\x7f\\x80\\x00{'
|
||||
|
||||
>>> struct.unpack('>f', b'\\x7f\\x80\\x00{')[0]
|
||||
nan
|
||||
>>> Float(struct.unpack('>f', b'\\x7f\\x80\\x00{')[0]).to_bytes()
|
||||
b'\\x7f\\xc0\\x00{'
|
||||
>>> struct.pack('>f', struct.unpack('>f', b'\\x7f\\x80\\x00{')[0])
|
||||
b'\\x7f\\xc0\\x00{'
|
||||
|
||||
```
|
||||
|
||||
(Note well the difference between `7f80007b` and `7fc0007b`!)
|
||||
|
||||
"""
|
||||
|
||||
if math.isnan(self.value) or math.isinf(self.value):
|
||||
dbs = struct.pack('>d', self.value)
|
||||
vd = struct.unpack('>Q', dbs)[0]
|
||||
sign = vd >> 63
|
||||
payload = (vd >> 29) & 0x007fffff
|
||||
vf = (sign << 31) | 0x7f800000 | payload
|
||||
return struct.pack('>I', vf)
|
||||
else:
|
||||
return struct.pack('>f', self.value)
|
||||
|
||||
def __preserve_write_binary__(self, encoder):
|
||||
encoder.buffer.append(0x87)
|
||||
encoder.buffer.append(4)
|
||||
encoder.buffer.extend(self.to_bytes())
|
||||
|
||||
def __preserve_write_text__(self, formatter):
|
||||
if math.isnan(self.value) or math.isinf(self.value):
|
||||
formatter.chunks.append('#xf"' + self.to_bytes().hex() + '"')
|
||||
else:
|
||||
formatter.chunks.append(repr(self.value) + 'f')
|
||||
|
||||
@staticmethod
|
||||
def from_bytes(bs):
|
||||
"""Converts a 4-byte-long byte string to a 32-bit single-precision floating point value
|
||||
wrapped in a [Float][preserves.values.Float] instance. Takes care to preserve the
|
||||
quiet/signalling bit-pattern of NaN values, unlike its `struct.unpack('>f', ...)`
|
||||
equivalent.
|
||||
|
||||
```python
|
||||
>>> Float.from_bytes(b'\\x7f\\x80\\x00{')
|
||||
Float(nan)
|
||||
>>> Float.from_bytes(b'\\x7f\\x80\\x00{').to_bytes()
|
||||
b'\\x7f\\x80\\x00{'
|
||||
|
||||
>>> struct.unpack('>f', b'\\x7f\\x80\\x00{')[0]
|
||||
nan
|
||||
>>> Float(struct.unpack('>f', b'\\x7f\\x80\\x00{')[0]).to_bytes()
|
||||
b'\\x7f\\xc0\\x00{'
|
||||
>>> struct.pack('>f', struct.unpack('>f', b'\\x7f\\x80\\x00{')[0])
|
||||
b'\\x7f\\xc0\\x00{'
|
||||
|
||||
```
|
||||
|
||||
(Note well the difference between `7f80007b` and `7fc0007b`!)
|
||||
|
||||
"""
|
||||
vf = struct.unpack('>I', bs)[0]
|
||||
if (vf & 0x7f800000) == 0x7f800000:
|
||||
# NaN or inf. Preserve quiet/signalling bit by manually expanding to double-precision.
|
||||
sign = vf >> 31
|
||||
payload = vf & 0x007fffff
|
||||
dbs = struct.pack('>Q', (sign << 63) | 0x7ff0000000000000 | (payload << 29))
|
||||
return Float(struct.unpack('>d', dbs)[0])
|
||||
else:
|
||||
return Float(struct.unpack('>f', bs)[0])
|
||||
|
||||
# FIXME: This regular expression is conservatively correct, but Anglo-chauvinistic.
|
||||
RAW_SYMBOL_RE = re.compile(r'^[-a-zA-Z0-9~!$%^&*?_=+/.]+$')
|
||||
|
||||
|
@ -575,7 +704,7 @@ class Embedded:
|
|||
>>> import io
|
||||
>>> e = Embedded(io.StringIO('some text'))
|
||||
>>> e # doctest: +ELLIPSIS
|
||||
#:<_io.StringIO object at ...>
|
||||
#!<_io.StringIO object at ...>
|
||||
>>> e.embeddedValue # doctest: +ELLIPSIS
|
||||
<_io.StringIO object at ...>
|
||||
|
||||
|
@ -588,7 +717,7 @@ class Embedded:
|
|||
...
|
||||
TypeError: Cannot preserves-format: None
|
||||
>>> print(preserves.stringify(Embedded(None), format_embedded=lambda x: 'abcdef'))
|
||||
#:"abcdef"
|
||||
#!"abcdef"
|
||||
|
||||
```
|
||||
|
||||
|
@ -610,12 +739,12 @@ class Embedded:
|
|||
return hash(self.embeddedValue)
|
||||
|
||||
def __repr__(self):
|
||||
return '#:%r' % (self.embeddedValue,)
|
||||
return '#!%r' % (self.embeddedValue,)
|
||||
|
||||
def __preserve_write_binary__(self, encoder):
|
||||
encoder.buffer.append(0x86)
|
||||
encoder.append(encoder.encode_embedded(self.embeddedValue))
|
||||
|
||||
def __preserve_write_text__(self, formatter):
|
||||
formatter.chunks.append('#:')
|
||||
formatter.chunks.append('#!')
|
||||
formatter.append(formatter.format_embedded(self.embeddedValue))
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
#!/bin/sh
|
||||
cd "$(dirname "$0")"
|
||||
. ./.envrc
|
||||
exec python -c \
|
||||
'import tomllib; print(tomllib.load(open("pyproject.toml", "rb"))["project"]["version"])'
|
|
@ -1,6 +1,6 @@
|
|||
[project]
|
||||
name = "preserves"
|
||||
version = "0.995.1"
|
||||
version = "0.992.2"
|
||||
description = "Data serialization format"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.6, <4"
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue