Open qwertie opened 4 years ago
Also, in general, Loyc trees are DAGs (directed acyclic graphs) so I would also propose the following JSON representation for tree definitions and backreferences.
LES3 | JSON | Comment |
---|---|---|
@.id tree(a.b.c) |
["*","id", ["tree",["'.",["'.","a","b"],"c"]]] |
A subtree that also has a name. |
@@id |
["*","id"] |
Backreference to a previously defined subtree. |
@.id2 @x tree2() |
["*","id2", ["@","x", ["tree2"]]] |
Define a subtree with an attribute. |
@x @@id2 |
["@","x", ["*","id2"]] |
Refer to a previously defined subtree and attach an attribute. |
`*`(x) | `["+*","x"] |
As mentioned above, certain one-character identifiers such as * must have + prepended to avoid ambiguity. |
Edited Jan 2021 to make the notation more brief. The representation of @.id tree(a.b)
changed from {"@@":"id","":["tree",["'.","a","b"]]}
to ["*","id", ["tree",["'.","a","b"]]]
; the representation of @@id
changed from {"@@":"id","":[]}
to ["*","id"]
. The name *
is intended to remind you of pointer notation in C/C#/Rust, since shared subtrees involve duplicate pointers.
Just as in LES3, a subtree definition must appear lexically before any references to it.
@vladimir-vg mentioned he wanted to store syntax as "JSON in text file... file tree in Git or tree in IPFS". I'm not sure about those last two, but it would made sense to standardize a JSON representation of Loyc trees, so I'm sketching out a proposal. This proposal will mainly take the form of a series of examples showing how a given bit of LES3 code will be represented in JSON.
Identifiers
Hello
"Hello"
``
|""
`\t\0\n\u1234`
|"\t\0\n\u1234"
`\u01F4A9`.\u10FFFF`
|"\uD83D\uDCA9.\uDBFF\uDFFF"
`\xFF.\uD800`
|"\xFF.\uD800"
#
|"+#"
| Single-character strings with an ASCII code of 64 or less are reserved for special purposes. Use a+
prefix (#98) to define a single-character identifier with one of these values.#if
|"#if"
`'+`
|"'+"
_
|"_"
| This rule does not affect normal identifiers such as_
(ASCII 95)Literals
x"hi!"
{"x": "hi!"}
"hi!"
{"": "hi!"}
`@`"hi!"
|{"@": 123}
123
123
123.0
{"_": "123.0"}
1234f
{"_f":"1234"}
_
for all numbers1234f
{"_f":1234}
123
{"_":123}
true
true
bool
)true
{"bool":"true"}
null
null
null
{"null":""}
json"{\"x\":123}"
{"json":{ "x": 123 }}
json"[\"x", 123]"
{"json":["x", 123]}
json"{\"x\":123}"
{"json":"{\"x\":123}"}
json"{x:123}"
{"json":"{x:123}"}
Note that general JSON objects like
{ "x":1, "y":2 }
have no interpretation above, and serve as an indicator that the JSON file does not represent a Loyc tree.Calls
foo()
["foo"]
1234(z)
[1234, "z"]
x"hi!"(z)
[{"x":"hi!"}, "z"]
foo(x, 2, null)
["foo", "x", 2, null]
x + 2
["'+", "x", 2]
{ }
["'{}"]
'{}
#foo(42)
["#foo", 42]
#
.foo 42
["#foo", 42]
#
{ "x": 123 }
["'{}", ["':", {"":"x"}, 123]]
["x", 123]
["'[]", {"":"x"}, 123]
foo(x)(y)
[["foo", "x"], "y"]
Attributes
@Foo X
["@","Foo","x"]
@x foo()
["@","x",["foo"]]
@x @y(z) foo
["@","x",["y", "z"],"foo"]
@123 X
["@",123,"x"]
/*comment*/ X
["@",["%MLComment","comment"],"X"]
foo
["@","foo"]
["@"]
"@"
Edited Jan 21, 2021: since no one has reported interest in using the JSON encoding, I've changed parts of the proposal without notice. Most notably, backreferences and attributes now use a more compact encoding. Previously,
@a @b foo()
would be represented as{"@":["a","b"], "":["foo"]}
, but now it's["@","a","b", ["foo"]]
.