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

Open
mingodad opened this issue Mar 2, 2025 · 0 comments
Open

Grammar railroad diagram #4

mingodad opened this issue Mar 2, 2025 · 0 comments

Comments

@mingodad
Copy link

mingodad commented Mar 2, 2025

Using https://mingodad.github.io/plgh/json2ebnf.html to convert the content in src/grammar.json to an EBNF understood by https://github.com/GuntherRademacher/rr that create a nice navigable railroad diagram (see bellow with instructions at the top).

//
// EBNF to generate railroad diagram 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://raw.githubusercontent.com/SerenityOS/tree-sitter-jakt/refs/heads/main/src/grammar.json
//

source_file ::=
	 ( _statement ( '\n'  | ';' )? )*

_statement ::=
	 declaration
	| _expression
	| block
	| if_statement
	| return_statement
	| while_statement
	| continue_statement
	| throw_statement
	| defer_statement
	| loop_statement
	| try_statement
	| unsafe_block
	| cpp_block
	| yield_statement
	| import_statement
	| guard_statement

declaration ::=
	 let_declaration
	| mutable_declaration
	| enum_declaration
	| struct_declaration
	| trait_declaration
	| class_declaration
	| generic_class_declaration
	| namespace_declaration
	| function_declaration
	| comptime_function_declaration
	| generic_function_declaration

logical_not_expression ::=
	 'not' _expression

_expression ::=
	 identifier
	| _literal
	| logical_not_expression
	| this_reference
	| this_reference_shorthand
	| unary_expression
	| bitwisenot_expression
	| binary_expression
	| optional_expression
	| optional_value_expression
	| call_expression
	| range_expression
	| for_expression
	| field_expression
	| static_call_expression
	| namespace_call_expression
	| namespace_scope_expression
	| type_conversion_expression
	| assignment_expression
	| none_expression
	| update_expression
	| match_expression
	| match_default_binding_expression
	| pointer_expression
	| parenthesized_expression
	| try_expression
	| array_expression
	| reflect_expression
	| closure_function_expression

parenthesized_expression ::=
	 '(' _expression ')'

while_statement ::=
	 'while' _expression block

loop_statement ::=
	 'loop' block

continue_statement ::=
	 'continue'

throw_statement ::=
	 'throw' _expression

defer_statement ::=
	 'defer' ( block | _expression )

try_statement ::=
	 'try' ( block | _expression ) 'catch' identifier? ( block | _expression )

try_expression ::=
	 'try' _expression

unsafe_block ::=
	 'unsafe' '{' _statement* '}'

cpp_block ::=
	 'cpp' '{' ( '"'  _string_content '"'  ( '\n'  | ';' )? )* '}'

yield_statement ::=
	 'yield' _expression

c_header_identfier ::=
	 ( '"'  '.'* '.h"' )

cpp_header_identfier ::=
	 ( '"'  '.'* '"'  )

import_as_clause ::=
	 'as'

import_statement ::=
	 'import' ( extern_specifier 'c' c_header_identfier | extern_specifier cpp_header_identfier | identifier ( import_as_clause identifier )? | ( identifier '::' _field_identifier ( '::' _field_identifier )* ) )? import_block?

import_block ::=
	 '{' ( identifier ( '\n'  | ';' )? ( ',' identifier ( '\n'  | ';' )? )* )? ( function_declaration ( '\n'  | ';' )? | class_declaration ( '\n'  | ';' )? )*? namespace_declaration? '}'

guard_statement ::=
	 'guard' ( _expression ) 'else' block

namespace_declaration ::=
	 'namespace' _pattern block

for_expression ::=
	 'for' _pattern 'in' _expression block

call_expression ::=
	 ( _expression | generic_type ) arguments

range_expression ::=
	 _expression '..' _expression? | '..'

field_expression ::=
	 ( this_reference | _expression ) '.' ( '['? ( _field_identifier | integer_literal ) ']'? )

namespace_call_expression ::=
	 identifier '::' ( identifier '::' )+ ( _field_identifier | generic_type ) arguments

static_call_expression ::=
	 identifier '::' ( _field_identifier | generic_type ) arguments

type_conversion_expression ::=
	 _expression ( 'as?' | 'as!' ) identifier | _primitive_types

assignment_expression ::=
	 _expression ( '=' | '+=' | '-=' | '&=' | '|=' | '^=' | '*=' | '/=' | '%=' ) _expression

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

reflect_expression ::=
	 'reflect' identifier

pointer_expression ::=
	 ( '&' | '*' ) ( 'raw' | mutable_specifier )? identifier

pointer_type ::=
	 ( 'raw' ) identifier

optional_expression ::=
	 _expression ( '!!' | '??' ) _expression

optional_type ::=
	 _type ( '!' | '?' )

optional_value_expression ::=
	 _expression ( '!' | '?' )

none_expression ::=
	 'None'

arguments ::=
	 '(' ( ( argument ) ','? )*? ')'

argument ::=
	 _pattern ':' _expression | ( identifier ) ( '\n'  | ';' ) | ( _expression )

_type ::=
	 _primitive_types
	| _simple_type

_simple_type ::=
	 _type_identifier
	| generic_type
	| dictionary_type
	| set_type
	| array_type
	| function_return_type
	| namespace_scope_type
	| reference_type
	| closure_function_type
	| tuple_type
	| optional_type
	| pointer_type

namespace_scope_type ::=
	 ( [_\p{XID_Start}][_\p{XID_Continue}]* '::' )+

namespace_scope_expression ::=
	 identifier ( '::' identifier )+ ( '\n'  | ';' )?

array_type ::=
	 '[' _type ']'

dictionary_type ::=
	 '[' _type ':' _type ']'

set_type ::=
	 '{' _type '}'

function_return_type ::=
	 ( [_\p{XID_Start}][_\p{XID_Continue}]* ( '::' [_\p{XID_Start}][_\p{XID_Continue}]* )+ )

reference_type ::=
	 '&' mutable_specifier? ( _primitive_types | identifier )

let_declaration ::=
	 'let' _pattern ( ':' _type )? ( '=' _expression )?

mutable_declaration ::=
	 mutable_specifier _pattern ( ':' weak_specifier? _type )? ( '=' _expression )? ';'?

boxed_specifier ::=
	 'boxed'

enum_declaration ::=
	 boxed_specifier? 'enum' ( _type | enum_integral_type ) _implements? enum_variant_list

enum_integral_type ::=
	 identifier ':' _type

enum_variant_list ::=
	 '{' ( ( enum_variant | enum_tuple_variant | enum_struct_variant | enum_field_declaration | function_declaration )* ( '\n'  ( enum_variant | enum_tuple_variant | enum_struct_variant | enum_field_declaration | function_declaration )* )* )? '}'

enum_field_declaration ::=
	 _field_identifier ':' _type ( '=' _expression )?

enum_variant ::=
	 identifier ( '=' _expression )?

enum_tuple_variant ::=
	 identifier '(' ( _type ) ')'

enum_struct_variant ::=
	 identifier '(' ( field_declaration ( '\n'  | ',' )? )+? ')'

field_declaration_list ::=
	 '{' ( function_declaration | field_declaration ( '\n'  | ',' )? | generic_function_declaration )+? '}'

field_declaration ::=
	 visibility_specifier? _field_identifier ':' _type ( '=' _expression )?

_field_identifier ::=
	 identifier

_implements ::=
	 'implements' '(' ( trait_identifier ( ','? trait_identifier )* | generic_type ) ')'

struct_declaration ::=
	 ( attributes | extern_specifier )? 'struct' ( _type_identifier | generic_type ) _implements? field_declaration_list

trait_declaration ::=
	 'trait' ( identifier | generic_type ) field_declaration_list

class_declaration ::=
	 extern_specifier? 'class' _type_identifier ( ':' _type_identifier )? field_declaration_list

generic_class_declaration ::=
	 'class' generic_type ( ':' _type_identifier )? field_declaration_list

visibility_specifier ::=
	 'public'
	| 'private'

mutable_specifier ::=
	 'mut'

weak_specifier ::=
	 'weak'

restricted_specifier ::=
	 'restricted' '(' ( identifier ( ',' identifier )* )? ')'

unary_expression ::=
	 '-' _expression

bitwisenot_expression ::=
	 '~' _expression

return_statement ::=
	 'return' _expression?

binary_expression ::=
	 _expression 'and' _expression
	| _expression 'or' _expression
	| _expression 'is' _expression
	| _expression '&' _expression
	| _expression '|' _expression
	| _expression '^' _expression
	| _expression ( '==' | '!=' | '<' | '<=' | '>' | '>=' ) _expression
	| _expression ( '<<' | '<<<' | '>>' | '>>>' | '<<=' | '>>=' ) _expression
	| _expression ( '+' | '-' ) _expression
	| _expression ( '*' | '/' | '%' ) _expression

update_expression ::=
	 ( '--' | '++' ) ( identifier | field_expression | this_reference_shorthand ) | ( identifier | field_expression | this_reference_shorthand ) ( '--' | '++' )

match_expression ::=
	 'match' _expression match_block

match_block ::=
	 '{' ( ',' | match_arm )* last_match_arm '}'

match_arm ::=
	 ( match_pattern ) '=>' ( _expression | block )

match_default_binding_expression ::=
	 identifier 'default' '(' mutable_specifier? identifier '=' _literal ')'

last_match_arm ::=
	 ( match_else | match_else_binding | match_pattern ) '=>' ( _expression | block )

match_pattern ::=
	 _expression

match_else ::=
	 'else'

match_else_binding ::=
	 'else' '(' identifier ')'

_literal ::=
	 string_literal | char_literal | byte_literal | boolean_literal | integer_literal | binary_literal | float_literal | array_literal | dictionary_literal | tuple_literal | set_literal | destructuring_literal

_pattern ::=
	 _literal
	| ( 'u8' | 'i8' | 'u16' | 'i16' | 'u32' | 'i32' | 'u64' | 'i64' | 'u128' | 'i128' | 'isize' | 'usize' | 'uz' | 'f32' | 'f64' | 'c_int' | 'bool' | 'String' | 'c_char' | 'void' )
	| identifier

destructuring_literal ::=
	 '(' ( identifier ','? )+ ')'

negative_literal ::=
	 '-' ( integer_literal | float_literal )

integer_literal ::=
	 ( ( [0-9][0-9_]* | '0x'[0-9a-fA-F_]+ | '0o'[0-7_]+ ) ( 'u8' | 'i8' | 'u16' | 'i16' | 'u32' | 'i32' | 'u64' | 'i64' | 'u128' | 'i128' | 'isize' | 'usize' | 'uz' | 'f32' | 'f64' | 'c_int' )? )

binary_literal ::=
	 ( ( '0b'[01_]+ ) )

string_literal ::=
	 'b'?'"' ( escape_sequence | _string_content )* '"' 

char_literal ::=
	 ( "'"  ( '\' ( [^xu] | 'u'[0-9a-fA-F]'{4}' | 'u{'[0-9a-fA-F]+'}' | 'x'[0-9a-fA-F]'{2}' ) | [^\'] )? "'"  )

byte_literal ::=
	 ( 'b' "'"  ( '\' ( [^xu] | 'u'[0-9a-fA-F]'{4}' | 'u{'[0-9a-fA-F]+'}' | 'x'[0-9a-fA-F]'{2}' ) | [^\'] )? "'"  )

array_literal ::=
	 '[' ( _expression ( ','? _expression )* | _literal ';' _expression )? ']'

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

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

tuple_literal ::=
	 '(' _expression ( ',' _expression )* ')'

dictionary_literal ::=
	 '[' ( ':' | ( dictionary_element ','? )+ ) ']'

set_literal ::=
	 '{' ( _expression ','? )+? '}'

dictionary_element ::=
	 _literal ':' _expression

escape_sequence ::=
	 ( '\' ( [^xu] | 'u'[0-9a-fA-F]'{4}' | 'u{'[0-9a-fA-F]+'}' | 'x'[0-9a-fA-F]'{2}' ) )

trait_requirement ::=
	 identifier ( '<' trait_identifier 'requires' '(' trait_identifier ( ','? trait_identifier )* ')' '>' )

attributes ::=
	 '[[' ( call_expression | 'name' '=' identifier ) ']]'

function_declaration ::=
	 ( attributes | restricted_specifier | visibility_specifier | extern_specifier )? 'fn' ( identifier | trait_requirement ) parameters throws_specifier? ( '->' _type )? ( return_expression | block )?

comptime_function_declaration ::=
	 ( 'comptime' ) ( identifier | generic_type ) parameters throws_specifier? ( '->' _type )? ( return_expression | block )?

generic_type ::=
	 identifier generic_arguments

generic_arguments ::=
	 '<' ( _type_identifier | _primitive_types ) ( ',' ( _type_identifier | _primitive_types ) )* '>'

generic_function_declaration ::=
	 'fn' generic_type parameters throws_specifier? ( '->' ( _type_identifier | generic_type | _type ) )? ( return_expression | block )?

extern_specifier ::=
	 'extern'

throws_specifier ::=
	 'throws'

return_expression ::=
	 '=>' _expression

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

this_reference ::=
	 'this'

this_parameter ::=
	 mutable_specifier? this_reference

this_reference_shorthand ::=
	 '.' identifier

parameter ::=
	 anonymous_specifier? mutable_specifier? ( _pattern ) ':' _type ( '=' _expression )?

closure_function_type ::=
	 ( '&' )? 'fn' parameters throws_specifier? ( '->' _type )?

_closure_capture_reference ::=
	 '[' ( ( identifier | pointer_expression ) | comptime_specifier identifier ( ',' comptime_specifier identifier )* ) ']'

closure_function_expression ::=
	 ( restricted_specifier | visibility_specifier )? 'fn' _closure_capture_reference? parameters throws_specifier? ( '->' _type )? ( return_expression | block )

anonymous_specifier ::=
	 'anon'

comptime_specifier ::=
	 'comptime'

block ::=
	 '{' ( _statement ( '\n'  | ';' )? )* '}'

if_statement ::=
	 'if' _expression block else_clause?

else_clause ::=
	 'else' ( block | if_statement )

boolean_literal ::=
	 'true'
	| 'false'

comment ::=
	 line_comment

line_comment ::=
	 ( '//' '.'* )

identifier ::=
	 [_\p{XID_Start}][_\p{XID_Continue}]*

_type_identifier ::=
	 identifier

trait_identifier ::=
	 identifier

_primitive_types ::=
	 ( 'u8' | 'i8' | 'u16' | 'i16' | 'u32' | 'i32' | 'u64' | 'i64' | 'u128' | 'i128' | 'isize' | 'usize' | 'uz' | 'f32' | 'f64' | 'c_int' | 'bool' | 'String' | 'c_char' | 'void' )
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