Skip to content

Grammar railroad diagram #22

@mingodad

Description

@mingodad

Asking gemini to extract an EBNF to generate a nice navigable railroad diagram gave me this one shown bellow with instructions at the top, I hope it can be a starting point to help document/develop/debug the grammar.

//
// EBNF to be viewd at https://www.bottlecaps.de/rr/ui
//
// Copy and paste this at one of the urls shown above in the 'Edit Grammar' tab
// then click the 'View Diagram' tab.
//
/* Top level entry point */
Program          ::= (GlobalDeclaration | Statement)*

GlobalDeclaration ::= FunctionDef 
                    | StructDef 
                    | EnumDef 
                    | TraitDef 
                    | ImplDef 
                    | IncludeStmt 
                    | ImportStmt 
                    | ComptimeBlock 
                    | TestBlock

/* Statements */
Statement        ::= Block
                   | IfStmt
                   | WhileStmt
                   | ForStmt
                   | LoopStmt
                   | RepeatStmt
                   | UnlessStmt
                   | GuardStmt
                   | MatchStmt
                   | ReturnStmt
                   | VarDecl
                   | ConstDecl
                   | TypeAlias
                   | AssertStmt
                   | DeferStmt
                   | AsmStmt
                   | PluginStmt
                   | Expression ";"

Block            ::= "{" Statement* "}"

IfStmt           ::= "if" Expression Block ("else" (IfStmt | Block))?
WhileStmt        ::= "while" Expression Block
ForStmt          ::= "for" "(" (VarDecl | Expression)? ";" Expression? ";" Expression? ")" Block
LoopStmt         ::= "loop" Block
RepeatStmt       ::= "repeat" Block "while" Expression ";"
UnlessStmt       ::= "unless" Expression Block
GuardStmt        ::= "guard" Expression "else" Block
ReturnStmt       ::= "return" Expression? ";"

MatchStmt        ::= "match" Expression "{" MatchArm* "}"
MatchArm         ::= Expression "=>" (Statement | Block)

/* Declarations */
VarDecl          ::= "var" Identifier (":" Type)? ("=" Expression)? ";"
ConstDecl        ::= "const" Identifier (":" Type)? "=" Expression ";"
TypeAlias        ::= "type" Identifier "=" Type ";"

FunctionDef      ::= "async"? "fn" Identifier "(" ParameterList? ")" ("->" Type)? Block
ParameterList    ::= Parameter ("," Parameter)*
Parameter        ::= Identifier ":" Type ("=" Expression)?

StructDef        ::= ("struct" | "union") Identifier "{" Field* "}"
Field            ::= Identifier ":" Type ";"

EnumDef          ::= "enum" Identifier "{" EnumVariant* "}"
EnumVariant      ::= Identifier ("=" Expression)? ("," | ";")?

TraitDef         ::= "trait" Identifier "{" FunctionDef* "}"
ImplDef          ::= ("impl" Identifier "for")? Identifier "{" FunctionDef* "}"

/* Expressions (simplified precedence levels) */
Expression       ::= Assignment
Assignment       ::= Ternary (("=" | "+=" | "-=" | "*=" | "/=") Assignment)?
Ternary          ::= Or ("?" Expression ":" Expression)?
Or               ::= And ("||" Or)?
And              ::= Equality ("&&" And)?
Equality         ::= Comparison (("==" | "!=") Equality)?
Comparison       ::= Term (("<" | ">" | "<=" | ">=") Comparison)?
Term             ::= Factor (("+" | "-") Term)?
Factor           ::= Unary (("*" | "/" | "%") Factor)?
Unary            ::= ("!" | "-" | "~" | "*" | "&")? Call
Call             ::= Primary ( "(" ArgumentList? ")" | "." Identifier | "[" Expression "]" )*

Primary          ::= Identifier 
                   | Literal 
                   | "(" Expression ")" 
                   | ArrayLiteral 
                   | TupleLiteral 
                   | Lambda 
                   | MacroCall 
                   | EmbedStmt

Literal          ::= Integer | Float | String | "true" | "false" | "null"
ArgumentList     ::= Expression ("," Expression)*
ArrayLiteral     ::= "[" (Expression ("," Expression)*)? "]"
TupleLiteral     ::= "(" Expression "," (Expression ("," Expression)*)? ")"

Lambda           ::= ("|" ParameterList? "|" | "fn" "(" ParameterList? ")") ("->" Type)? (Expression | Block)
MacroCall        ::= Identifier "!" "(" ArgumentList? ")"
EmbedStmt        ::= "embed" "(" String ")"

/* Types */
Type             ::= ("*" | "[]")* (Identifier | "void" | "int" | "float" | "bool" | "string")

/* Miscellaneous */
IncludeStmt      ::= "include" (String | "<" Identifier ">")
ImportStmt       ::= "import" String ("as" Identifier)?
AssertStmt       ::= "assert" Expression ("," String)? ";"
DeferStmt        ::= "defer" Statement
AsmStmt          ::= "asm" "{" String* "}"
PluginStmt       ::= "plugin" Identifier (Block | ";")
ComptimeBlock    ::= "comptime" Block
TestBlock        ::= "test" String? Block

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions