| SEXPRS(2) | System Calls Manual | SEXPRS(2) |
Sexprs: Sexp - S-expressions
include "bufio.m";
include "sexprs.m";
sexprs := load Sexprs Sexprs->PATH;
Sexp: adt {
pick {
String =>
s: string;
hint: string;
Binary =>
data: array of byte;
hint: string;
List =>
l: cyclic list of ref Sexp;
}
read: fn(b: ref Bufio->Iobuf): (ref Sexp, string);
parse: fn(s: string): (ref Sexp, string, string);
pack: fn(e: self ref Sexp): array of byte;
packedsize: fn(e: self ref Sexp): int;
text: fn(e: self ref Sexp): string;
b64text: fn(e: self ref Sexp): string;
unpack: fn(a: array of byte): (ref Sexp, array of byte, string);
eq: fn(e: self ref Sexp, t: ref Sexp): int;
copy: fn(e: self ref Sexp): ref Sexp;
astext: fn(e: self ref Sexp): string;
asdata: fn(e: self ref Sexp): array of byte;
islist: fn(e: self ref Sexp): int;
els: fn(e: self ref Sexp): list of ref Sexp;
op: fn(e: self ref Sexp): string;
args: fn(e: self ref Sexp): list of ref Sexp;
};
init: fn();
Sexprs provides a data type and I/O for S-expressions, or `symbolic expressions', which represent complex data as trees. This implementation provides the variant defined by Rivest in Internet Draft (4 May 1997), as used for instance by the Simple Public Key Infrastructure (SPKI). It offers a basic set of operations on the internal representation, and input and output in both canonical and advanced transport encodings. Canonical form conveys binary data directly and efficiently (unlike some other schemes such as XML). Canonical encoding must be used when exchanging S-expressions between computers, and when digitally signing an expression. Advanced encoding is a more elaborate form similar to that used by Lisp interpreters, typically using only printable characters: representing any binary data in hexadecimal or base 64 encodings, and quoting strings containing special characters, using escape sequences as required. Unquoted text is called a token, restricted by the standard to a specific alphabet: it must start with a letter or a character from the set and contain only letters, digits and characters from that set. Upper- and lower-case letters are distinct. See sexprs (6) for a precise description.
Init must be called before invoking any other operation of the module.
Sexp is the internal representation of S-expression data, as lists and non-list values (atoms) that in general can form a tree structure; that is, a list may contain not just atoms but other lists as its elements, and so on recursively. The atoms are strings of text or binary. A well-formed S-expression might be a tree, but cannot contain cycles.
For convenience in processing, Sexp distinguishes three variants represented in a pick adt:
Sexp provides the following operations for input and output, using bufio (2)'s buffered channels (directly or indirectly):
All input functions accept S-expression in either canonical or advanced form, or any legal mixture of forms. Expressions can cross line boundaries. For output in canonical form, use pack; for output in advanced form (similar to Lisp's S-expressions), use text.
Sexp provides a further small collection of operations:
Two operations provide a shorthand for fetching the value of an atom, returning nil if applied to a list:
The remaining operations extract values from lists, and return nil if applied to an atom:
The following S-expression is in advanced transport form:
(snicker "abc" (#03# |YWJj|))
It represents a list of three elements: the token the token and a sub-list with two elements (a hexadecimal constant representing the byte and a base-64 constant that represents the bytes
Here is another in advanced form with two sublists:
(certificate
(issuer bob)
(subject "alice b"))
Its equivalent in canonical form (as produced by pack) is shown below:
(11:certificate(6:issuer3:bob)(7:subject7:alice b))
Nesting parentheses still mark the start and end of lists, but there is no other punctuation or white space, and the byte sequence representing each atom is preceded by a decimal count, so that binary values appear unencoded, and for instance the space in the last string is not a delimiter but part of the token.
/appl/lib/sexprs.b
bufio (2), xml (2), sexprs (6)
R. Rivest, ``S-expressions'', Network Working Group Internet Draft, (4 May 1997), reproduced in /lib/sexp.