Skip to content

Grammar railroad diagram #155

@mingodad

Description

@mingodad

I just added kit grammar to https://mingodad.github.io/parsertl-playground/playground/ an Yacc/Lex compatible online editor/interpreter (select Kitlang parser (partially working) then click Parse to see a parse tree for the content in Input source) and with it we can also generate an EBNF understood by https://github.com/GuntherRademacher/rr to generate a nice navigable railroad diagram (see instructions bellow at the top).

//
// 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.
//
//From: https://github.com/kitlang/kit/blob/2769a7a8e51fe4466c50439d1a1ebdad0fb79710/src/Kit/Parser/Parser.y

Statements::=
	  Statements_

Statements_::=
	  /*%empty*/
	| Statements_ Statement
	| Statements_ TypeDefinition DefinitionBody
	| Statements_ TraitDefinition DefinitionBody
	| Statements_ TraitImplementation DefinitionBody

Statement::=
	  "import" ModulePath ';'
	| "import" ModulePath ".*" ';'
	| "import" ModulePath ".**" ';'
	| "include" str "=>" str ';'
	| "include" str ';'
	| "using" UsingClause ';'
	| MetaMods "typedef" upper_identifier '=' TypeSpec ';'
	| MetaMods "default" TypeSpec "as" TypeSpec ';'
	| "rules" upper_identifier '{' ShortRules '}'
	| VarDefinition
	| FunctionDefinition
	| "extend" TypePath '{' DefStatements '}'
	| "macro" FunctionDefinitionBase
	| TypePath '(' CallArgs ')' ';'

TypeDefinition::=
	  MetaMods "enum" upper_identifier TypeParams
	| MetaMods "struct" upper_identifier TypeParams
	| MetaMods "union" upper_identifier TypeParams
	| MetaMods "abstract" upper_identifier TypeParams TypeAnnotation

TraitDefinition::=
	  MetaMods "trait" upper_identifier TypeParams AssocTypeDeclarations

TraitImplementation::=
	  "implement" TypeParams TypeSpec AssocTypes "for" TypeSpec

AssocTypes::=
	  /*%empty*/
	| '(' CommaDelimitedTypes ')'

AssocTypeDeclarations::=
	  /*%empty*/
	| '(' TypeParams_ ')'

TopLevelExpr::=
	  StandaloneExpr
	| ConstOrVar Identifier TypeAnnotation OptionalStandaloneDefault
	| "return" TopLevelExpr
	| "return" ';'
	| "continue" ';'
	| "break" ';'
	| "yield" Expr ';'
	| "tokens" str

StandaloneExpr::=
	  ExprBlock
	| "using" UsingClauses StandaloneExpr
	| IfStatement
	| "static" IfStatement
	| "for" Identifier "in" Expr ExprBlock
	| "while" Expr ExprBlock
	| "do" ExprBlock "while" Expr ';'
	| "match" Expr '{' MatchCases DefaultMatchCase '}'
	| Expr ';'

IfStatement::=
	  "if" BinopTermOr ExprBlock
	| "if" BinopTermOr ExprBlock "else" StandaloneExpr

UsingClauses::=
	  UsingClause
	| UsingClauses ',' UsingClause

UsingClause::=
	  "rules" TypePath
	| "implicit" Expr

FunctionDefinition::=
	  MetaMods "function" FunctionDefinitionBase

FunctionDefinitionBase::=
	  identifier TypeParams '(' VarArgs ')' TypeAnnotation OptionalBody

MatchCases::=
	  /*%empty*/
	| MatchCases MatchCase

MatchCase::=
	  Expr "=>" TopLevelExpr

DefaultMatchCase::=
	  /*%empty*/
	| "default" "=>" TopLevelExpr

ExprBlock::=
	  '{' MacroIdentifier '}'
	| '{' TopLevelExprs '}'

TopLevelExprs::=
	  /*%empty*/
	| TopLevelExprs TopLevelExpr

ModulePath::=
	  identifier
	| ModulePath '.' identifier

CallArgs::=
	  /*%empty*/
	| Expr
	| CallArgs ',' Expr

MetaArg::=
	  Term
	| UpperOrLowerIdentifier

MetaArgs::=
	  MetaArg
	| MetaArgs ',' MetaArg

Metadata::=
	  "#[" identifier '(' MetaArgs ')' ']'
	| "#[" identifier ']'
	| "#[" ReservedIdentifier ']'

ReservedIdentifier::=
	  "static"

MetaMods::=
	  /*%empty*/
	| MetaMods Metadata
	| MetaMods "public"
	| MetaMods "private"
	| MetaMods "inline"
	| MetaMods "static"

TypeAnnotation::=
	  /*%empty*/
	| ':' TypeSpec

TypeSpec::=
	  TypePath TypeSpecParams
	| '&' TypeSpec
	| "function" FunctionTypeSpec
	| '(' TypeSpec ',' CommaDelimitedTypes ')'
	| Term
	| "Self"
	| '_'

FunctionTypeSpec::=
	  '(' ')' "->" TypeSpec
	| '(' CommaDelimitedTypes ',' identifier "..." ')' "->" TypeSpec
	| '(' CommaDelimitedTypes ')' "->" TypeSpec

CommaDelimitedTypes::=
	  TypeSpec
	| CommaDelimitedTypes ',' TypeSpec

OptionalBody::=
	  ';'
	| "using" UsingClauses ExprBlock
	| ExprBlock

OptionalRuleBody::=
	  ';'
	| "=>" StandaloneExpr

TypeParams::=
	  /*%empty*/
	| '[' TypeParams_ ']'

TypeParams_::=
	  TypeParam
	| TypeParams_ ',' TypeParam

TypeParam::=
	  TypeParam '=' TypeSpec
	| upper_identifier TypeConstraints
	| macro_identifier

TypeSpecParams::=
	  /*%empty*/
	| '[' TypeSpecParams_ ']'

TypeSpecParams_::=
	  TypeSpec
	| TypeSpecParams_ ',' TypeSpec

TypeConstraints::=
	  /*%empty*/
	| ':' TypeSpec
	| "::" TypeConstraints_

TypeConstraints_::=
	  TypeSpec
	| TypeConstraints_ '|' TypeSpec

UpperOrLowerIdentifier::=
	  identifier
	| upper_identifier

TypePath::=
	  TypePath '.' UpperOrLowerIdentifier
	| UpperOrLowerIdentifier

ConstOrVar::=
	  "var"
	| "const"

VarDefinition::=
	  MetaMods ConstOrVar UpperOrLowerIdentifier TypeAnnotation OptionalDefault ';'

VarDefinitions::=
	  /*%empty*/
	| VarDefinitions VarDefinition

VarBlock::=
	  MetaMods '{' VarDefinitions '}'

OptionalStandaloneDefault::=
	  ';'
	| '=' "undefined" ';'
	| '=' StandaloneExpr

OptionalDefault::=
	  /*%empty*/
	| '=' "undefined"
	| '=' Expr

EnumVariant::=
	  MetaMods upper_identifier ';'
	| MetaMods upper_identifier '(' Args ')' ';'

VarArgs::=
	  Args ',' identifier "..."
	| Args

Args::=
	  /*%empty*/
	| ArgSpec
	| Args ',' ArgSpec

ArgSpec::=
	  identifier TypeAnnotation OptionalDefault

DefinitionBody::=
	  ';'
	| '{' DefStatements '}'

DefStatements::=
	  /*%empty*/
	| DefStatements RewriteRule
	| DefStatements RuleBlock
	| DefStatements FunctionDefinition
	| DefStatements VarDefinition
	| DefStatements VarBlock
	| DefStatements EnumVariant

RewriteExpr::=
	  Expr
	| StandaloneExpr

RewriteRule::=
	  "rule" '(' RewriteExpr ')' OptionalRuleBody

RuleBlock::=
	  "rules" '{' ShortRules '}'

ShortRules::=
	  /*%empty*/
	| ShortRules ShortRule

ShortRule::=
	  '(' RewriteExpr ')' OptionalRuleBody

Expr::=
	  RangeLiteral

RangeLiteral::=
	  BinopTermAssign "..." BinopTermAssign
	| BinopTermAssign

BinopTermAssign::=
	  ArrayAccessCallFieldExpr '=' BinopTermAssign
	| BinopTermCons
	| BinopTermCons '=' BinopTermAssign
	| BinopTermCons assign_op BinopTermAssign

BinopTermCons::=
	  BinopTermTernary
	| BinopTermTernary "::" BinopTermCons

BinopTermTernary::=
	  "if" BinopTermTernary "then" BinopTermTernary "else" BinopTermTernary
	| BinopTermOr

BinopTermOr::=
	  BinopTermAnd
	| BinopTermOr "||" BinopTermAnd

BinopTermAnd::=
	  BinopTermEq
	| BinopTermAnd "&&" BinopTermEq

BinopTermEq::=
	  BinopTermCompare
	| BinopTermEq "==" BinopTermCompare
	| BinopTermEq "!=" BinopTermCompare

BinopTermCompare::=
	  BinopTermBitOr
	| BinopTermCompare '>' BinopTermBitOr
	| BinopTermCompare '<' BinopTermBitOr
	| BinopTermCompare ">=" BinopTermBitOr
	| BinopTermCompare "<=" BinopTermBitOr

BinopTermBitOr::=
	  BinopTermBitXor
	| BinopTermBitOr '|' BinopTermBitXor

BinopTermBitXor::=
	  BinopTermBitAnd
	| BinopTermBitXor '^' BinopTermBitAnd

BinopTermBitAnd::=
	  BinopTermShift
	| BinopTermBitAnd '&' BinopTermShift

BinopTermShift::=
	  BinopTermAdd
	| BinopTermShift "<<" BinopTermAdd
	| BinopTermShift ">>" BinopTermAdd

BinopTermAdd::=
	  BinopTermMul
	| BinopTermAdd '+' BinopTermMul
	| BinopTermAdd '-' BinopTermMul

BinopTermMul::=
	  BinopTermCustom
	| BinopTermMul '*' BinopTermCustom
	| BinopTermMul '/' BinopTermCustom
	| BinopTermMul '%' BinopTermCustom

BinopTermCustom::=
	  CastExpr
	| BinopTermCustom custom_op CastExpr

CastExpr::=
	  CastExpr "as" TypeSpec
	| Unop

Unop::=
	  "++" VecExpr
	| "--" VecExpr
	| '!' VecExpr
	| '-' VecExpr
	| '~' VecExpr
	| '&' VecExpr
	| '*' VecExpr
	| VecExpr "++"
	| VecExpr "--"
	| VecExpr

VecExpr::=
	  '[' ArrayElems ']'
	| '[' ArrayElems ',' ']'
	| '[' ']'
	| ArrayAccessCallFieldExpr

ArrayElems::=
	  Expr
	| ArrayElems ',' Expr

ArrayAccessCallFieldExpr::=
	  ArrayAccessCallFieldExpr '[' ArrayElems ']'
	| ArrayAccessCallFieldExpr '(' CallArgs ')'
	| ArrayAccessCallFieldExpr '.' Identifier
	| TypeAnnotatedExpr

TypeAnnotatedExpr::=
	  TypeAnnotatedExpr ':' TypeSpec
	| BaseExpr

BaseExpr::=
	  Term
	| "this"
	| "Self"
	| Identifier
	| '(' identifier "..." ')'
	| "unsafe" Expr
	| "sizeof" TypeSpec
	| "sizeof" '(' TypeSpec ')'
	| "defined" Identifier
	| "defined" '(' Identifier ')'
	| '(' Expr ParenthesizedExprs ')'
	| "null"
	| "empty"
	| "struct" TypeSpec '{' StructInitFields '}'
	| "struct" TypeSpec
	| "union" TypeSpec '{' StructInitField '}'
	| "implicit" TypeSpec
	| inline_c TypeAnnotation
	| "static" Expr

ParenthesizedExprs::=
	  /*%empty*/
	| ParenthesizedExprs ',' Expr

Term::=
	  bool
	| str
	| int
	| float
	| char

StructInitFields::=
	  /*%empty*/
	| OneOrMoreStructInitFields ','
	| OneOrMoreStructInitFields

OneOrMoreStructInitFields::=
	  StructInitField
	| OneOrMoreStructInitFields ',' StructInitField

StructInitField::=
	  UpperOrLowerIdentifier ':' Expr
	| UpperOrLowerIdentifier

Identifier::=
	  UpperOrLowerIdentifier
	| MacroIdentifier
	| '_'

MacroIdentifier::=
	  macro_identifier
	| '$' '{' UpperOrLowerIdentifier TypeAnnotation '}'
	| '$' '{' '_' TypeAnnotation '}'

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