A little serailization format based on a minimal syntax derived from and compatible with Jevko (Jevko minus digraphs). Implemented in JavaScript.
Parsing happens in three steps:
- Transform source text into a tree according to an extremely simple formal grammar.
- Transform the tree obtained in the previous step into another tree, removing comments, trimming whitespace, and expanding escapes.
- Finally, transform the tree obtained in the previous step into a JavaScript value: either a string, an array, or an object. Arrays and objects can have arbitrarily nested values of these three types.
Serializing works for strings, an arrays, and objects, where arrays and objects can have arbitrarily nested values of only these three types.
We start with the following formal grammar (ABNF + RegExp):
tree = *sub text
sub = text '[' tree ']'
text = /[^\[\]`]*/
Meaning:
- a
tree
is zero or moresub
s followed bytext
- a
sub
istext
followed bytree
enclosed in square brackets[]
text
is zero or more characters which are not square brackets[]
or backticks`
Instead of handling comments, whitespace, or escaping on the level of the formal grammar, we do that in a preprocessing step which transforms the parse tree obtained from parsing the above grammar into another tree which may contain square brackets in text.
If a sub's text spans multiple lines, all lines except for the last one are discarded (treated as single-line comments) during preprocessing.
comment
text [text]
Also, all subs which have text that begins with ;
are discarded (treated as multi-line comments) during preprocessing.
;[comment]
text [ text ]
becomes
text[ text ]
To supress removing comments, trimming whitespace, and to have square brackets or backticks as part of text we need some sort of an escape mechanism.
This is done as follows.
If we want to have a sub like ~}{text}{~[...]
, except with square brackets instead of curly brackets and backticks instead of tildes as part of the text (so the actual text we want is literally `][text][`
) we should write that like so:
\[[~][}][{]text[}][{][~]][...]
If we want square brackets or backticks in the text of a tree, e.g.:
sub1[...] sub2[...] ~}{text}{~
we should write:
sub1[...] sub2[...] \[[~][}][{]text with brackets[}][{][~]]
node --test