Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grammar railroad diagram #798

Open
mingodad opened this issue Mar 4, 2025 · 1 comment
Open

Grammar railroad diagram #798

mingodad opened this issue Mar 4, 2025 · 1 comment

Comments

@mingodad
Copy link

mingodad commented Mar 4, 2025

I've just added this project grammar to https://mingodad.github.io/parsertl-playground/playground/ an Yacc/Lex like online interpreter/editor (select Mgmt-config parser from Examples then click Parse to see a parse tree for the content in Input source) and also from there generated an EBNF understood by https://github.com/GuntherRademacher/rr that generates a nice navigable railroad diagram (see bellow with instructions at the top).

I hope this can help document/develop/debug this project.

//
// 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.
//

prog::=
	  /*%empty*/
	| prog stmt

stmt::=
	  bind
	| panic
	| resource
	| edge
	| IF expr OPEN_CURLY prog CLOSE_CURLY
	| IF expr OPEN_CURLY prog CLOSE_CURLY ELSE OPEN_CURLY prog CLOSE_CURLY
	| FUNC_IDENTIFIER IDENTIFIER OPEN_PAREN args CLOSE_PAREN OPEN_CURLY expr CLOSE_CURLY
	| FUNC_IDENTIFIER IDENTIFIER OPEN_PAREN args CLOSE_PAREN type OPEN_CURLY expr CLOSE_CURLY
	| CLASS_IDENTIFIER colon_identifier OPEN_CURLY prog CLOSE_CURLY
	| CLASS_IDENTIFIER colon_identifier OPEN_PAREN args CLOSE_PAREN OPEN_CURLY prog CLOSE_CURLY
	| INCLUDE_IDENTIFIER dotted_identifier
	| INCLUDE_IDENTIFIER dotted_identifier OPEN_PAREN call_args CLOSE_PAREN
	| INCLUDE_IDENTIFIER dotted_identifier AS_IDENTIFIER IDENTIFIER
	| INCLUDE_IDENTIFIER dotted_identifier OPEN_PAREN call_args CLOSE_PAREN AS_IDENTIFIER IDENTIFIER
	| IMPORT_IDENTIFIER STRING
	| IMPORT_IDENTIFIER STRING AS_IDENTIFIER IDENTIFIER
	| IMPORT_IDENTIFIER STRING AS_IDENTIFIER MULTIPLY

expr::=
	  BOOL
	| STRING
	| INTEGER
	| FLOAT
	| list
	| map
	| struct
	| call
	| var
	| func
	| IF expr OPEN_CURLY expr CLOSE_CURLY ELSE OPEN_CURLY expr CLOSE_CURLY
	| OPEN_PAREN expr CLOSE_PAREN

list::=
	  OPEN_BRACK list_elements CLOSE_BRACK

list_elements::=
	  /*%empty*/
	| list_elements list_element

list_element::=
	  expr COMMA

map::=
	  OPEN_CURLY map_kvs CLOSE_CURLY

map_kvs::=
	  /*%empty*/
	| map_kvs map_kv

map_kv::=
	  expr ROCKET expr COMMA

struct::=
	  STRUCT_IDENTIFIER OPEN_CURLY struct_fields CLOSE_CURLY

struct_fields::=
	  /*%empty*/
	| struct_fields struct_field

struct_field::=
	  IDENTIFIER ROCKET expr COMMA

call::=
	  dotted_identifier OPEN_PAREN call_args CLOSE_PAREN
	| dotted_var_identifier OPEN_PAREN call_args CLOSE_PAREN
	| func OPEN_PAREN call_args CLOSE_PAREN
	| expr PLUS expr
	| expr MINUS expr
	| expr MULTIPLY expr
	| expr DIVIDE expr
	| expr EQ expr
	| expr NEQ expr
	| expr LT expr
	| expr GT expr
	| expr LTE expr
	| expr GTE expr
	| expr AND expr
	| expr OR expr
	| NOT expr
	| expr OPEN_BRACK expr CLOSE_BRACK
	| expr OPEN_BRACK expr CLOSE_BRACK DEFAULT expr
	| expr ARROW IDENTIFIER
	| expr ARROW IDENTIFIER DEFAULT expr
	| expr IN expr

call_args::=
	  /*%empty*/
	| call_args COMMA expr
	| expr

var::=
	  dotted_var_identifier

func::=
	  FUNC_IDENTIFIER OPEN_PAREN args CLOSE_PAREN OPEN_CURLY expr CLOSE_CURLY
	| FUNC_IDENTIFIER OPEN_PAREN args CLOSE_PAREN type OPEN_CURLY expr CLOSE_CURLY

args::=
	  /*%empty*/
	| args COMMA arg
	| arg

arg::=
	  var_identifier
	| var_identifier type

bind::=
	  var_identifier EQUALS expr
	| var_identifier type EQUALS expr

panic::=
	  PANIC_IDENTIFIER OPEN_PAREN call_args CLOSE_PAREN

resource::=
	  colon_identifier expr OPEN_CURLY resource_body CLOSE_CURLY

resource_body::=
	  /*%empty*/
	| resource_body resource_field
	| resource_body conditional_resource_field
	| resource_body resource_edge
	| resource_body conditional_resource_edge
	| resource_body resource_meta
	| resource_body conditional_resource_meta
	| resource_body resource_meta_struct
	| resource_body conditional_resource_meta_struct

resource_field::=
	  IDENTIFIER ROCKET expr COMMA

conditional_resource_field::=
	  IDENTIFIER ROCKET expr ELVIS expr COMMA

resource_edge::=
	  CAPITALIZED_IDENTIFIER ROCKET edge_half COMMA

conditional_resource_edge::=
	  CAPITALIZED_IDENTIFIER ROCKET expr ELVIS edge_half COMMA

resource_meta::=
	  CAPITALIZED_IDENTIFIER COLON IDENTIFIER ROCKET expr COMMA

conditional_resource_meta::=
	  CAPITALIZED_IDENTIFIER COLON IDENTIFIER ROCKET expr ELVIS expr COMMA

resource_meta_struct::=
	  CAPITALIZED_IDENTIFIER ROCKET expr COMMA

conditional_resource_meta_struct::=
	  CAPITALIZED_IDENTIFIER ROCKET expr ELVIS expr COMMA

edge::=
	  edge_half_list
	| edge_half_sendrecv ARROW edge_half_sendrecv

edge_half_list::=
	  edge_half
	| edge_half_list ARROW edge_half

edge_half::=
	  capitalized_res_identifier OPEN_BRACK expr CLOSE_BRACK

edge_half_sendrecv::=
	  capitalized_res_identifier OPEN_BRACK expr CLOSE_BRACK DOT IDENTIFIER

type::=
	  BOOL_IDENTIFIER
	| STR_IDENTIFIER
	| INT_IDENTIFIER
	| FLOAT_IDENTIFIER
	| OPEN_BRACK CLOSE_BRACK type
	| MAP_IDENTIFIER OPEN_CURLY type COLON type CLOSE_CURLY
	| STRUCT_IDENTIFIER OPEN_CURLY type_struct_fields CLOSE_CURLY
	| FUNC_IDENTIFIER OPEN_PAREN type_func_args CLOSE_PAREN type
	| VARIANT_IDENTIFIER

type_struct_fields::=
	  /*%empty*/
	| type_struct_fields SEMICOLON type_struct_field
	| type_struct_field

type_struct_field::=
	  IDENTIFIER type

type_func_args::=
	  /*%empty*/
	| type_func_args COMMA type_func_arg
	| type_func_arg

type_func_arg::=
	  type
	| var_identifier type

undotted_identifier::=
	  IDENTIFIER
	| MAP_IDENTIFIER

var_identifier::=
	  DOLLAR undotted_identifier

colon_identifier::=
	  IDENTIFIER
	| colon_identifier COLON IDENTIFIER

dotted_identifier::=
	  undotted_identifier
	| dotted_identifier DOT undotted_identifier

dotted_var_identifier::=
	  DOLLAR dotted_identifier

capitalized_res_identifier::=
	  CAPITALIZED_IDENTIFIER
	| capitalized_res_identifier COLON CAPITALIZED_IDENTIFIER

//Tokens

AND ::= "and"
ARROW ::= "->"
AS_IDENTIFIER ::= "as"
BOOL ::= "true"|"false"
BOOL_IDENTIFIER ::= "bool"
CLASS_IDENTIFIER ::= "class"
CLOSE_BRACK ::= "]"
CLOSE_CURLY ::= "}"
CLOSE_PAREN ::= ")"
COLON ::= ":"
COMMA ::= ","
DEFAULT ::= "||"
DIVIDE ::= "/"
DOLLAR ::= "$"
DOT ::= "."
ELSE ::= "else"
ELVIS ::= "?:"
EQ ::= "=="
EQUALS ::= "="
FLOAT_IDENTIFIER ::= "float"
FUNC_IDENTIFIER ::= "func"
GT ::= ">"
GTE ::= ">="
IF ::= "if"
IMPORT_IDENTIFIER ::= "import"
IN ::= "in"
INCLUDE_IDENTIFIER ::= "include"
INT_IDENTIFIER ::= "int"
LT ::= "<"
LTE ::= "<="
MAP_IDENTIFIER ::= "map"
MINUS ::= "-"
MULTIPLY ::= "*"
NEQ ::= "!="
NOT ::= "not"
OPEN_BRACK ::= "["
OPEN_CURLY ::= "{"
OPEN_PAREN ::= "("
OR ::= "or"
PANIC_IDENTIFIER ::= "panic"
PLUS ::= "+"
ROCKET ::= "=>"
SEMICOLON ::= ";"
STRUCT_IDENTIFIER ::= "struct"
STR_IDENTIFIER ::= "str"
VARIANT_IDENTIFIER ::= "variant"
@purpleidea
Copy link
Owner

Neat! I think you're probably for more of an expert in parsers than I am! Do you think your work could either:

  1. Help us build a formatting tool for mcl kind of like gofmt does for golang?

  2. Help us build a language server for mcl that makes it easier for others to use it in their IDE?

If so please let us know and maybe we can get you sending some patches.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants