schibsted / jslt

JSON query and transformation language
Apache License 2.0
618 stars 119 forks source link

Grammar railroad diagram #361

Open mingodad opened 5 days ago

mingodad commented 5 days ago

Based on https://github.com/schibsted/jslt/blob/master/core/src/main/jjtree/jslt.jjt with a bit of manual changes we can have a nice navigable railroad diagram:

//
// EBNF to be viewd at
//    (IPV6) https://www.bottlecaps.de/rr/ui
//    (IPV4) https://rr.red-dove.com/ui
//
// Copy and paste this at one of the urls shown above in the 'Edit Grammar' tab
// then click the 'View Diagram' tab.
//

/** Root production. */
Start ::=
    Import* (Let | FunctionDecl)* Expr //<EOF>

///** Root production for module files */
//Module ::=
//  Import* (Let | FunctionDecl)* Expr? //<EOF>
//

Expr ::=
    OrExpr (PipeOperator OrExpr)*

OrExpr ::=
    AndExpr (OR OrExpr)?

AndExpr ::=
    ComparativeExpr (AND AndExpr)?

ComparativeExpr ::=
    AdditiveExpr (Comparator AdditiveExpr)?

// not necessary, but makes the tree easier to traverse
Comparator ::=
    EQUALS
    | UNEQUALS
    | BIGOREQ
    | BIGGER
    | SMALLER
    | SMALLOREQ

PipeOperator ::=
    PIPE

AdditiveExpr ::=
    MultiplicativeExpr (AdditiveOperator MultiplicativeExpr)*

// not necessary, but makes the tree easier to traverse
AdditiveOperator ::=
    PLUS
    | MINUS

MultiplicativeExpr ::=
    BaseExpr (MultiplicativeOperator BaseExpr)*

// not necessary, but makes the tree easier to traverse
MultiplicativeOperator ::=
    STAR
    | SLASH

BaseExpr ::=
    //(LOOKAHEAD(2)
    NULL
    | INTEGER
    | DECIMAL
    | STRING
    | TRUE
    | FALSE
    | Chainable
    | Parenthesis
    | IfStatement
    | Array
    //|(LOOKAHEAD(2)
    | Object
    | ObjectComprehension

Chainable ::=
    (FunctionCall | VARIABLE | DOT (IDENT | STRING)?) ChainLink?

ChainLink ::=
    (DotKey | ArraySlicing) ChainLink?

Parenthesis ::=
    LPAREN Expr RPAREN

DotKey ::=
    DOT (IDENT | STRING)

ArraySlicing ::=
    LBRACKET
    (
        Expr (Colon Expr?)?
        | Colon Expr
    )
    RBRACKET

Colon ::=
    COLON // need this to make parse tree manageable

ArrayElem /*#void*/  ::=
    Expr (COMMA ArrayElem?)?

Array ::=
    LBRACKET
    (
        FOR LPAREN Expr RPAREN Let* Expr
            (IF LPAREN Expr RPAREN)?
        | ArrayElem?
    )
    RBRACKET

Object ::=
    LCURLY Let* (Pair | Matcher)? RCURLY

Matcher ::=
    STAR MatcherMinus? COLON Expr

MatcherMinus ::=
    MINUS (IDENT | STRING) (COMMA (IDENT | STRING))*

Pair ::=
    Expr COLON Expr (COMMA (Pair | Matcher)?)?

ObjectComprehension ::=
    LCURLY FOR LPAREN Expr RPAREN Let* Expr COLON Expr
        (IF LPAREN Expr RPAREN)?
    RCURLY

IfStatement ::=
    IF LPAREN Expr RPAREN Let* Expr ElseBranch?

// not necessary, but makes it easier to walk the parse tree
ElseBranch ::=
    ELSE Let* Expr

FunctionCall ::=
    (IDENT | PIDENT) LPAREN (Expr (COMMA Expr)*)? RPAREN

Let ::=
    LET IDENT ASSIGN Expr

FunctionDecl ::=
    DEF IDENT LPAREN (IDENT (COMMA IDENT)*)? RPAREN Let* Expr

Import ::=
    IMPORT STRING AS IDENT

//Tokens

NULL ::= "null"
LBRACKET ::= "["
RBRACKET ::= "]"
COMMA ::= ","
COLON ::= ":"
LCURLY ::= "{"
RCURLY ::= "}"
TRUE ::= "true"
FALSE ::= "false"
OR ::= "or"
AND ::= "and"
DOT ::= "."
IF ::= "if"
ELSE ::= "else"
LPAREN ::= "("
RPAREN ::= ")"
LET ::= "let"
ASSIGN ::= "="
EQUALS ::= "=="
UNEQUALS ::= "!="
BIGOREQ ::= ">="
BIGGER ::= ">"
SMALLER ::= "<"
SMALLOREQ ::= "<="
PLUS ::= "+"
MINUS ::= "-"
STAR ::= "*"
SLASH ::= "/"
PIPE ::= "|"
FOR ::= "for"
DEF ::= "def"
IMPORT ::= "import"
AS ::= "as"
larsga commented 4 days ago

That sounds like it could be interesting. Is this something you're interested in contributing?

mingodad commented 4 days ago

It's already here.

larsga commented 4 days ago

I don't follow. Where is it?

catull commented 4 days ago

Perhaps @mingodad can explain what he intends or expects with the link above.

mingodad commented 4 days ago

Sorry ! it seems that people here is not ready for new things !

larsga commented 4 days ago

Sorry. I didn't fully understand your meaning at first. Now I generated some railroad diagrams. Nice!

I need to think a little about how to use them, but it may indeed be that this could be useful.