Konard / LinksPlatform

Holistic system for storage and transformation of information based on associative model of data. Целостная система для хранения и обработки информации, основанная на ассоциативной модели данных.
https://linksplatform.github.io/Documentation/
GNU Lesser General Public License v3.0
57 stars 9 forks source link

Implement parser/generator for LiNo (Links Notation) (like JSON for Links) #158

Open Konard opened 8 years ago

Konard commented 8 years ago

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.

Konard commented 8 years ago

2016-03-19 05-22-16

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

Konard commented 8 years ago

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.

Konard commented 8 years ago
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]*
Konard commented 8 years ago

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

Konard commented 5 years ago

https://github.com/Konard/PegasusExample can be used to finish this.