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 #41

Open
mingodad opened this issue May 27, 2024 · 0 comments
Open

Grammar railroad diagram #41

mingodad opened this issue May 27, 2024 · 0 comments

Comments

@mingodad
Copy link

Using https://mingodad.github.io/plgh/json2ebnf.html to convert https://raw.githubusercontent.com/moonbitlang/tree-sitter-moonbit/main/src/grammar.json to an EBNF understood by (IPV6) https://www.bottlecaps.de/rr/ui or (IPV4) https://rr.red-dove.com/ui to generate a navigable railroad diagram (after some small manual fixes).

//
// EBNF to generate railroad diagram at
//      (IPV6) https://www.bottlecaps.de/rr/ui
//      (IPV4) https://rr.red-dove.com/ui
//
// From https://raw.githubusercontent.com/moonbitlang/tree-sitter-moonbit/main/src/grammar.json
//

structure ::=
	 structure_item ( ( '\n'  | ';' | '\0'  ) structure_item )* ( '\n'  | ';' | '\0'  )?

structure_item ::=
	 type_definition
	| struct_definition
	| enum_definition
	| value_definition
	| function_definition
	| test_definition
	| trait_definition
	| impl_definition

visibility ::=
	 'priv'
	| 'pub' pub_attribute?

pub_attribute ::=
	 '(' 'readonly' ')'

derive_item ::=
	 qualified_type_identifier

derive ::=
	 'derive'

derive_directive ::=
	 derive '(' ( derive_item ( ',' derive_item )* ','? )? ')'

type_definition ::=
	 visibility? 'type' identifier type_parameters? type? derive_directive?

struct_definition ::=
	 visibility? 'struct' identifier type_parameters? '{' ( struct_field_declaration ( ( '\n'  | ';' | '\0'  ) struct_field_declaration )* ( '\n'  | ';' | '\0'  )? )? '}' derive_directive?

struct_field_declaration ::=
	 visibility? mutability? lowercase_identifier colon type

enum_definition ::=
	 visibility? 'enum' identifier type_parameters? '{' ( enum_constructor ( ( '\n'  | ';' | '\0'  ) enum_constructor )* ( '\n'  | ';' | '\0'  )? )? '}' derive_directive?

enum_constructor ::=
	 uppercase_identifier ( '(' type ( ',' type )* ','? ')' )?

value_definition ::=
	 pub? 'let' lowercase_identifier type_annotation? '=' expression

function_definition ::=
	 pub? 'fn' function_identifier type_parameters? parameters? return_type? block_expression

test_definition ::=
	 'test' string_literal? block_expression

trait_definition ::=
	 pub? 'trait' identifier super_trait_declaration? '{' ( trait_method_declaration ( ( '\n'  | ';' | '\0'  ) trait_method_declaration )* ( '\n'  | ';' | '\0'  )? )? '}'

super_trait_declaration ::=
	 colon ( identifier ( '+' identifier )* )?

trait_method_declaration ::=
	 function_identifier '(' ( type ( ',' type )* ','? )? ')' return_type

impl_definition ::=
	 'impl' qualified_type_identifier colon_colon function_identifier parameters return_type? block_expression

expression ::=
	 simple_expression
	| if_expression
	| loop_expression
	| match_expression
	| for_expression

unwrap_expression ::=
	 simple_expression question_operator

simple_expression ::=
	 atomic_expression
	| qualified_identifier
	| unary_expression
	| binary_expression
	| struct_expression
	| nonempty_block_expression
	| anonymous_lambda_expression
	| anonymous_matrix_lambda_expression
	| constructor_expression
	| apply_expression
	| array_access_expression
	| dot_apply_expression
	| access_expression
	| method_expression
	| unit_expression
	| tuple_expression
	| constraint_expression
	| array_expression
	| unwrap_expression
	| '_'

atomic_expression ::=
	 string_interpolation
	| literal

string_interpolation ::=
	 '"'  string_fragment* interpolator ( string_fragment | interpolator )* '"' 

interpolator ::=
	 '\(' expression ')'

literal ::=
	 boolean_literal
	| float_literal
	| integer_literal
	| byte_literal
	| char_literal
	| string_literal
	| multiline_string_literal

boolean_literal ::=
	 'true'
	| 'false'

integer_literal ::=
	 ( [0-9][0-9_]*'L'? | '0'[xX][0-9a-fA-F_]+'L'? | '0'[oO][0-7_]+'L'? | '0'[bB][01_]+'L'? )

byte_literal ::=
	 "b'" ( escape_sequence | [^'] ) "'" 

char_literal ::=
	 "'"  ( escape_sequence | [^'] ) "'" 

string_literal ::=
	 '"'  string_fragment* '"' 

string_fragment ::=
	 unescaped_string_fragment
	| escape_sequence

unescaped_string_fragment ::=
	 '\'[^ntb"\]
	| [^"\]+

escape_sequence ::=
	 '\'[ntb"\]
	| '\o'[0-7]'{1,3}'
	| '\x'[0-9a-fA-F]'{1,2}'
	| '\x{'[0-9a-fA-F]+'}'
	| '\u'[0-9a-fA-F]'{4}'
	| '\u{'[0-9a-fA-F]+'}'

multiline_string_separator ::=
	 '#\'|

multiline_string_fragment ::=
	 multiline_string_separator [^#x0A]*

multiline_string_literal ::=
	 multiline_string_fragment multiline_string_fragment*

unary_expression ::=
	 ( '-' | '+' ) expression

pipe_operator ::=
	 '|>'

binary_expression ::=
	 expression ( '*' | '/' | '%' ) expression
	| expression ( '+' | '-' ) expression
	| expression ( '>' | '>=' | '<=' | '<' | '==' | '!=' ) expression
	| expression '&&' expression
	| expression '||' expression
	| expression pipe_operator expression

struct_expression ::=
	 struct_array_expression
	| struct_brace_expression

struct_array_expression ::=
	 qualified_type_identifier colon_colon array_expression

struct_brace_expression ::=
	 ( qualified_type_identifier colon_colon )? ( '{' struct_field_expressions? '}' | '{' dot_dot expression '}' | '{' dot_dot expression ',' struct_field_expressions? '}' )

struct_field_expressions ::=
	 labeled_expression_pun ',' ( struct_field_expression ( ',' struct_field_expression )* ','? )?
	| labeled_expression ','?
	| labeled_expression ',' struct_field_expression ( ',' struct_field_expression )* ','?

struct_field_expression ::=
	 labeled_expression
	| labeled_expression_pun

labeled_expression ::=
	 lowercase_identifier colon expression

labeled_expression_pun ::=
	 lowercase_identifier

block_expression ::=
	 '{' ( statement_expression ( ( '\n'  | ';' | '\0'  ) statement_expression )* ( '\n'  | ';' | '\0'  )? )? '}'

nonempty_block_expression ::=
	 '{' statement_expression ( ( '\n'  | ';' | '\0'  ) statement_expression )* ( '\n'  | ';' | '\0'  )? '}'

anonymous_lambda_expression ::=
	 'fn' parameters return_type? block_expression

anonymous_matrix_lambda_expression ::=
	 'fn' '{' ( matrix_case_clause ( ( '\n'  | ';' | '\0'  ) matrix_case_clause )* ( '\n'  | ';' | '\0'  )? )? '}'

matrix_case_clause ::=
	 pattern ( ',' pattern )? '=>' case_clause_body

constructor_expression ::=
	 uppercase_identifier
	| qualified_type_identifier colon_colon uppercase_identifier

apply_expression ::=
	 simple_expression '(' ( ( labeled_identifier '=' )? expression ( ',' ( labeled_identifier '=' )? expression )* ','? )? ')'

array_access_expression ::=
	 simple_expression '[' expression ']'

dot_apply_expression ::=
	 simple_expression dot_identifier '(' ( ( labeled_identifier '=' )? expression ( ',' ( labeled_identifier '=' )? expression )* ','? )? ')'

access_expression ::=
	 simple_expression accessor

accessor ::=
	 dot_identifier
	| dot_operator [0-9]+

method_expression ::=
	 qualified_type_identifier colon_colon lowercase_identifier

unit_expression ::=
	 '(' ')'

tuple_expression ::=
	 '(' expression ( ',' expression )* ','? ')'

constraint_expression ::=
	 '(' expression colon type ')'

array_expression ::=
	 '[' ( expression ( ',' expression )* ','? )? ']'

match_expression ::=
	 'match' simple_expression '{' ( case_clause ( ( '\n'  | ';' | '\0'  ) case_clause )* ( '\n'  | ';' | '\0'  )? )? '}'

case_clause ::=
	 pattern '=>' case_clause_body

case_clause_body ::=
	 assign_expression
	| while_expression
	| 'break' ( expression ( ',' expression )* ','? )?
	| 'continue' ( expression ( ',' expression )* ','? )?
	| return_expression
	| expression

if_expression ::=
	 'if' simple_expression block_expression else_clause?

else_clause ::=
	 'else' ( block_expression | if_expression )

statement_expression ::=
	 let_expression
	| let_mut_expression
	| assign_expression
	| named_lambda_expression
	| named_matrix_expression
	| while_expression
	| 'break' ( expression ( ',' expression )* ','? )?
	| 'continue' ( expression ( ',' expression )* ','? )?
	| return_expression
	| expression

let_expression ::=
	 'let' pattern type_annotation? '=' expression

let_mut_expression ::=
	 'let' 'mut' lowercase_identifier type_annotation? '=' expression

assign_expression ::=
	 left_value ( '=' | '+=' | '-=' | '*=' | '/=' ) expression

left_value ::=
	 qualified_identifier
	| access_expression
	| array_access_expression

named_lambda_expression ::=
	 'fn' lowercase_identifier parameters return_type? block_expression

named_matrix_expression ::=
	 'fn' lowercase_identifier '{' ( matrix_case_clause ( ( '\n'  | ';' | '\0'  ) matrix_case_clause )* ( '\n'  | ';' | '\0'  )? )? '}'

while_expression ::=
	 'while' simple_expression block_expression

loop_expression ::=
	 'loop' simple_expression ( ',' simple_expression )? '{' ( matrix_case_clause ( ( '\n'  | ';' | '\0'  ) matrix_case_clause )* ( '\n'  | ';' | '\0'  )? )? '}'

for_binder ::=
	 lowercase_identifier '=' expression

for_expression ::=
	 'for' ( for_binder ( ',' for_binder )* )? ( ';' simple_expression? ';' ( for_binder ( ',' for_binder )* )? )? block_expression

return_expression ::=
	 'return' expression?

pattern ::=
	 simple_pattern
	| as_pattern
	| or_pattern

as_pattern ::=
	 pattern 'as' lowercase_identifier

or_pattern ::=
	 pattern '|' pattern

simple_pattern ::=
	 '_'
	| '(' pattern ')'
	| literal
	| lowercase_identifier
	| constructor_pattern
	| tuple_pattern
	| constraint_pattern
	| array_pattern
	| struct_pattern

constructor_pattern ::=
	 constructor_expression ( '(' pattern ( ',' pattern )* ','? ')' )?

tuple_pattern ::=
	 '(' pattern ',' pattern ( ',' pattern )* ','? ')'

constraint_pattern ::=
	 '(' pattern colon type ')'

array_pattern ::=
	 '[' array_sub_pattern? ']'

array_sub_pattern ::=
	 dotdot_pattern
	| pattern ( ',' pattern )* ','?
	| dotdot_pattern pattern ( ',' pattern )* ','?
	| pattern ( ',' pattern )* ','? dotdot_pattern

dotdot_pattern ::=
	 dot_dot ','?

struct_pattern ::=
	 '{' struct_field_pattern? '}'

struct_field_pattern ::=
	 field_single_pattern ( ',' field_single_pattern )* ','?
	| field_single_pattern ( ',' field_single_pattern )* ','? dotdot_pattern

field_single_pattern ::=
	 labeled_pattern
	| labeled_pattern_pun

labeled_pattern ::=
	 lowercase_identifier colon pattern

labeled_pattern_pun ::=
	 lowercase_identifier

type ::=
	 tuple_type
	| function_type
	| apply_type
	| any

tuple_type ::=
	 '(' ( type ( ',' type )* ','? )? ')'

function_type ::=
	 '(' ( type ( ',' type )* ','? )? ')' '->' type

apply_type ::=
	 qualified_type_identifier type_arguments?

type_arguments ::=
	 '[' type ( ',' type )* ','? ']'

type_parameters ::=
	 '[' type_identifier ( ',' type_identifier )* ','? ']'

type_annotation ::=
	 colon type

return_type ::=
	 '->' type

parameter ::=
	 ( lowercase_identifier | labeled_identifier ) type_annotation? ( '=' expression )?

parameters ::=
	 '(' ( parameter ( ',' parameter )* ','? )? ')'

any ::=
	 '_'

mutability ::=
	 'mut'

pub ::=
	 'pub'

uppercase_identifier ::=
	 //[\p{Uppercase_Letter}][_\p{XID_Continue}]*
	 [A-Z][A-Za-z0-9_]*

lowercase_identifier ::=
	 //[_[\p{XID_Start}--\p{Uppercase_Letter}]][_\p{XID_Continue}]*
	 [a-z_][A-Za-z0-9_]*

identifier ::=
	 uppercase_identifier
	| lowercase_identifier

dot_operator ::=
	 '.'

dot_identifier ::=
	 dot_operator [A-Za-z_][A-Za-z0-9_]* //[_\p{XID_Start}][_\p{XID_Continue}]*

package_identifier ::=
	 '@'[A-Za-z_][A-Za-z0-9_]* //[_\p{XID_Start}][_\p{XID_Continue}]*

labeled_identifier ::=
	 '~'[A-Za-z_][A-Za-z0-9_]* //[_\p{XID_Start}][_\p{XID_Continue}]*

qualified_identifier ::=
	 lowercase_identifier
	| package_identifier dot_identifier

qualified_type_identifier ::=
	 identifier
	| package_identifier dot_identifier

function_identifier ::=
	 lowercase_identifier
	| qualified_type_identifier colon_colon lowercase_identifier

type_identifier ::=
	 identifier
	| identifier colon constraints

constraints ::=
	 constraint ( '+' constraint )*

constraint ::=
	 qualified_type_identifier

comment ::=
	 '\/\/'[^/]'.'*

docstring ::=
	 '\/\/\/.'*
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

1 participant