Open Konard opened 8 years ago
LiNo (Links Notation) Grammar:
// Links Notation Grammar
Expression = expression:(_ (Link) _)*
{
var result = [];
for (var i = 0; i < expression.length; i++)
result.push(expression[i][1]);
return result;
}
IdentityOrLink = Identity / Link
Link = "(" id:(IdOrNull) _
source:(IdentityOrLink) _ target:(IdentityOrLink) _ ")"
{ return { "Id": id, "Source": source, "Target": target }; }
IdOrNull = idParts:(_ Identity _ ":" / "")
{ if(idParts.length > 1) { return idParts[1]; } else { return null; } }
Identity "identity" = [0-9a-zA-Zа-яёА-ЯЁ]+ { return text(); }
_ "whitespace" = [ \t\n\r]*
Generated using http://pegjs.org/online
https://github.com/otac0n/Pegasus (another possible solution)
May be it is better to write links as parsing goes. And not to create intermediate object notation.
3:
loves
mama
papa
3
son
3
everyone
3
3
is
loves
mama
point
point:
point
point
Could be an example of indented notation, that is exactly the same as:
(papa (3: loves mama))
(son 3)
(everyone 3)
(3 (is (loves mama)))
(point)
(point: point point)
Possible grammar:
// Links Notation Grammar
Expression = expression:(___ AnyLink ___)*
{
var result = [];
for (var i = 0; i < expression.length; i++)
result.push(expression[i][1]);
return result;
}
IdentityOrLink = Identity / AnyLink
AnyLink = LinkWithId / Link
LinkWithId = id:(Id) __ _
source:(IdentityOrLink) __ _ target:(IdentityOrLink)
{ return { "Id": id, "Source": source, "Target": target }; }
Link = source:(Identity) __ _ target:(IdentityOrLink)
{ return { "Source": source, "Target": target }; }
Id = idParts:(Identity ":") { return idParts[0]; }
Identity "identity" = [0-9a-zA-Zа-яёА-ЯЁ]+ { return text(); }
_ "space" = " "
__ "newline" = "\n" / "\n\r" / "\r\n"
___ "whitespace" = [\n\r]*
JSON:
{
"users": [
{
"id": "43",
"name": {
"first": "John",
"last": "Williams"
},
"location": "New York",
"age": 23
},
{
"id": "56",
"name": {
"first": "Igor",
"middle": "Petrovich",
"last": "Ivanov"
},
"location": "Moscow",
"age": 20
}
]
}
Indented LiNo:
users
user1
id
43
name
first
John
last
Williams
location
New York
age
23
user2
id
56
name
first
Igor
middle
Petrovich
last
Ivanov
location
Moscow
age
20
This may be useful for keeping track of the indentation level: https://gist.github.com/dmajda/04002578dd41ae8190fc https://github.com/otac0n/Pegasus/wiki/Significant-Whitespace-Parsing
https://github.com/Konard/PegasusExample can be used to finish this.
https://github.com/pegjs/pegjs can be used to actually parse Links Notation to JavaScript object, which then will be transfered to C# code to be loaded to associative memory storage.