Skip to content

Grammar railroad diagram #1

Description

@mingodad

Manually converting the grammar Grammar.txt (with several guesses) to an EBNF understood by https://github.com/GuntherRademacher/rr we can create a nice navigable railroad diagram (see bellow with instructions at the top).

I hope that it can help document/develop/debug the grammar/syntax.

//From: https://github.com/MichaelEstes/Spite/blob/cea71e081dc4791a3322ce664b253c992d16abe6/Grammar.txt
//
// 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.
//

lbracket ::= '['
rbracket ::= ']'

lscope ::= '{'
rscope ::= '}'

langled ::= '<'
rangled ::= '>'

lparen ::= '('
rparen ::= ')'

number ::= number_literal

string ::= string_literal

literal ::= number | string | "true" | "false" | "null"

primitive ::= "void" | "bool" | "byte" | "ubyte" | "int" | "int16" | "int64" | "uint" | "uint16" | "uint64" | "float" | "string"

expression ::= binaryExpression | unaryExpression

binaryExpression ::= expression binaryOperator expression

unaryExpression ::= unaryOperator expression | primaryExpression

primaryExpression ::= operand |
						dereference |
						primaryExpression selector |
						primaryExpression as |
						primaryExpression index |
						primaryExpression functionParams |

operand ::= literal | identifier | groupedExpression | compileTime | anonymousType

groupedExpression ::= '(' expression ')'

operator ::=   binaryOperator | unaryOperator | variationOperator | bitwiseOperator |
				relativeOperator | assignmentOperator | definitionOperator | accessOperator

binaryOperator ::= '+' | '-' | '*' | '/' | '%' | "&&" | "||" | "<<" | ">>" | '|' | '^' | '&' | "&^" |
					relativeOperator

unaryOperator ::= '!' | '-' | '^'

bitwiseOperator ::= '|' | '^' | "<<" | ">>" | '&' | "&^"

relativeOperator ::= "==" | "!=" | "<=" | ">=" | '<' | '>'

assignmentOperator ::= "+=" | "-=" | "*=" | "/=" | "%=" | "&=" | "|=" | "^=" | "<<=" | ">>=" | "&^="

definitionOperator ::= '=' | ":="

accessOperator ::= "..." | "in" | ".." | "[]" | "=>"

import ::= "import" identifier

package ::= "package" identifier

statement ::= definition | if | for | while | switch | assignment | ternary |
				delete | defer | return | compileTime | block | expression

statements ::= statement+

generics ::= langled type (',' type)* (':' where)? rangled

type ::= namedType |
			primitive |
			explicitType |
			pointerType |
			rawPointerType |
			arrayType |
			genericType |
			functionType |

namedType ::= identifier |
				// PackageName.Type
				(identifier '.' identifier)

pointerType ::= '*' type

uniquePointerType ::= "~*" type

valueType ::= '~' type

arrayType ::= lbracketrbracket type

genericType ::= namedType generics

functionType ::= "::" type lparen (type (',' type)*)? rparen

declaration ::= identifier ':' type

explicitType ::= lscope declaration (',' declaration)* rscope

implicitType ::= lscope identifier (',' identifier)* rscope

anonymousType ::= lscope definition (',' definition)* rscope |
					lscope expression (',' expression)* rscope

assignmentValue ::= expression | new | anonymousType | functionValue | fixedArray

definition ::= declaration '=' assignmentValue |
				(identifier | implicitType) ":=" assignmentValue

assignment ::= expression assignmentOperator assignmentValue

block ::= lscope statements rscope

functionSignature ::=  lparen
							((declaration | expression+) definition*)? ','
						rparen

functionDeclaration ::= functionSignature block | functionSignature "=>" expression

function ::= type identifier generics? functionDeclaration

functionParams ::= lparen (expression (',' expression)*)? rparen

functionCall ::= identifier functionParams

state ::= "state" identifier
	generics?
	lscope
		declaration+ definition+ inset+
	rscope

inset ::= lbracket ("size" | "null" | "serialized" | "noalign") rbracket

where ::= "where" functionDeclaration

constructor ::= identifier "::" functionDeclaration

destructor ::= identifier "::" "delete" lscope statements rscope

operatorOverload ::= type identifier "::" "Operator" "::" operator functionDeclaration

method ::= (type identifier "::" identifier functionDeclaration) |
			constructor |
			destructor |
			operatorOverload

compileTime ::= "#compile" type (block | "(=>" statement)

compileTimeDebug ::= "#compileDebug" (block | statement)

functionValue ::= type functionDeclaration

condition ::= expression (("==" | '<' | '>' | "!=" | "<=" | ">=") expression)?

conditions ::= condition (("||" | "&&") condition+)?

conditional ::= lparen conditions+ rparen block

for ::= "for"
	lparen
		(identifier | declaration) ("in" | "..") (identifier | expression)
	rparen
	block

while ::= "while" conditional

if ::= "if" conditional elif* else?

elif ::= "else" if

else ::= "else" block

case ::= "case" lparen expression rparen block

switch ::= "switch" lparen expression rparen
	lscope
		case+
		("default" ':' block)?
	rscope

defer ::= defer (statement | block) | defer if conditional (statement | block)

ternary ::= expression '?' expression ':' expression

new ::= "new" type functionParams at?
	| "new" anonymousType at?

delete ::= "delete" "[]"? primaryExpression

return ::= "return" expression

fixedArray ::=  fixed type index

selector ::= '.' identifier

selection ::= identifier selector

as ::= "as" type

at ::= "at" primaryExpression

index ::= lbracket expression rbracket

dereference ::= primaryExpression '~'

reference ::= primaryExpression '@'

extern ::= "extern" lscope (type identifier lparen declaration+ rparen+) rscope

program ::= package import* state* function* method* definition* compileTime* extern*

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions