dynin / ideal

Experimental metaprogramming framework
BSD 3-Clause "New" or "Revised" License
26 stars 12 forks source link

Grammar railroad diagram #2

Open mingodad opened 9 months ago

mingodad commented 9 months ago

Using a script to convert (with some manual fixes) base_parser.cup to an EBNF understood by (IPV6) https://www.bottlecaps.de/rr/ui or (IPV4) https://rr.red-dove.com/ui to generate a nice navigable railroad diagram that can be used to document/develop/debug this project grammar.

Follow the instructions shown 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 url shown above in the 'Edit Grammar' tab
// then click the 'View Diagram' tab.
//

/* The grammar */
compilation_unit ::=
                statements_opt

statements_opt ::=
                statement statements_opt
        |       /* epsilon */

statements ::=
                statement statements_opt

statement ::=
                block_statement
        |       annotations_opt block
        |       annotations_opt expression post_annotations_opt2 procedure_body
        |       SEMICOLON
        |       variable_declaration SEMICOLON
        |       procedure_declaration
        |       type_declaration
        |       type_announcement
        |       supertype_declaration
        |       annotations_opt IMPORT expression SEMICOLON
        |       USE name SEMICOLON
        |       TARGET name COLON expression SEMICOLON
        |       grammar_declaration

type_declaration ::=
                annotations_opt KIND name parameters_opt
                    OPEN_BRACE statements_opt CLOSE_BRACE
                // TODO fragment origin

type_announcement ::=
                annotations_opt KIND name SEMICOLON
                // TODO fragment origin

procedure_declaration ::=
                annotations_opt expression name parameters post_annotations_opt
                    procedure_body
        |       annotations_opt expression name post_annotations_opt
                    EQUALS_GREATER_THAN expression SEMICOLON

variable_declaration ::=
                annotations_opt expression name post_annotations_opt COLON expression
        |       annotations_opt expression name post_annotations_opt
        |       annotations_opt name post_annotations_opt COLON expression

supertype_declaration ::=
                annotations_opt SUBTYPE_TAG types SEMICOLON
        |       annotations_opt FLAVOR SUBTYPE_TAG types SEMICOLON

block ::=       OPEN_BRACE statements_opt CLOSE_BRACE

procedure_body ::=
                block
        |       EQUALS_GREATER_THAN expression SEMICOLON
        |       SEMICOLON
        |       template

block_statement ::=
                RETURN expression_opt SEMICOLON
        |       JUMP SEMICOLON
        |       CONSTRAINT expression SEMICOLON
        |       IF expression_in_parens statement
        |       IF expression_in_parens statement ELSE statement
        |       LOOP statement
        |       WHILE expression_in_parens statement
        |       FOR OPEN_PARENTHESIS variable_or_expression_opt SEMICOLON
                      expression_opt SEMICOLON expression_opt CLOSE_PARENTHESIS
                      statement
        |       FOR OPEN_PARENTHESIS variable_declaration CLOSE_PARENTHESIS statement
        |       SWITCH expression_in_parens OPEN_BRACE case_clauses CLOSE_BRACE
        |       PLEASE statement

case_clauses ::=
                case_clause case_clauses_opt

case_clauses_opt ::=
                case_clause case_clauses_opt
        |       /* epsilon */

case_clause ::=
                case_expressions statements

case_expressions ::=
                case_expression case_expressions_opt

case_expressions_opt ::=
                case_expression case_expressions_opt
        |       /* epsilon */

case_expression ::=
                CASE expression COLON
        |       DEFAULT COLON

variable_or_expression_opt ::=
                annotations_opt expression_opt
        |       variable_declaration

annotation ::=  MODIFIER_KIND parameters_opt
        |       COMMENT

annotations_opt ::=
                /* epsilon */
        |       annotation annotations_opt

post_annotation ::=     MODIFIER_KIND parameters_opt
        |       FLAVOR

post_annotations_opt ::=
                /* epsilon */
        |       post_annotation post_annotations_opt

// TODO with post_annotations_opt
post_annotations_opt2 ::=
                /* epsilon */
        |       FLAVOR

expression_opt ::=
                expression
        |       /* epsilon */

expression_in_parens ::=
                OPEN_PARENTHESIS expression CLOSE_PARENTHESIS

types ::=       expression
        |       types COMMA expression

parameters ::=
                OPEN_PARENTHESIS parameter_list_opt CLOSE_PARENTHESIS
        |       OPEN_BRACKET parameter_list_opt CLOSE_BRACKET

parameters_opt ::=
                parameters
        |       /* epsilon */

// Trailing comma is allowed in parameters for declaring singleton lists:
// [1] is an integer and [1] is a one-element list.
parameter_list_opt ::=
                /* epsilon */
        |       parameter
        |       parameter COMMA parameter_list_opt

parameter ::=
                annotations_opt expression
        |       annotations_opt expression name post_annotations_opt COLON expression
        |       annotations_opt expression name post_annotations_opt
        |       annotations_opt expression post_annotations_opt COLON expression

expression ::=  name
        |       literal_value
        |       parameters
        |       expression DOT name
        |       expression parameters
        |       EXCLAMATION_MARK expression
        |       MINUS expression
        |       expression ASTERISK expression
        |       expression SLASH expression
        |       expression PERCENT expression
        |       expression PLUS expression
        |       expression PLUS_PLUS expression
        |       expression MINUS expression
        |       expression EQUALS_EQUALS expression
        |       expression EXCLAMATION_MARK_EQUALS expression
        |       expression LESS_THAN expression
        |       expression GREATER_THAN expression
        |       expression LESS_THAN_EQUALS expression
        |       expression LESS_THAN_EQUALS_GREATER_THAN expression
        |       expression GREATER_THAN_EQUALS expression
        |       expression DOT_GREATER_THAN expression
        |       expression EXCLAMATION_GREATER_THAN expression
        |       expression IS expression
        |       expression IS_NOT expression
        |       expression AMPERSAND expression
        |       expression CARET expression
        |       expression VERTICAL_BAR expression
        |       expression AMPERSAND_AMPERSAND expression
        |       expression VERTICAL_BAR_VERTICAL_BAR expression
        |       expression OR expression
        |       expression QUESTION_MARK expression COLON expression
        |       expression EQUALS expression
        |       expression PLUS_EQUALS expression
        |       expression MINUS_EQUALS expression
        |       expression ASTERISK_EQUALS expression
        |       expression PLUS_PLUS_EQUALS expression
        |       FLAVOR expression

literal_value ::=
                LITERAL

name ::=        SIMPLE_NAME
        |       SPECIAL_NAME

template ::=    HASH sexpression

sexpression ::= OPEN_PARENTHESIS sexpression_elements CLOSE_PARENTHESIS

sexpression_elements ::=
                /* epsilon */
        |       sexpression_element sexpression_elements

sexpression_element ::=
                sexpression_atom
        |       literal_value
        |       sexpression
        |       OPEN_BRACE expression CLOSE_BRACE

sexpression_atom ::=
                name
        |       FOR

grammar_declaration ::=
                annotations_opt GRAMMAR name
                    OPEN_BRACE grammar_statements_opt CLOSE_BRACE

grammar_statements_opt ::=
                grammar_statement grammar_statements_opt
        |       /* epsilon */

grammar_statement ::=
                terminal_statement
        |       nonterminal_statement
        |       production_statement

terminal_statement ::=
                TERMINAL expression name_list SEMICOLON

nonterminal_statement ::=
                NONTERMINAL expression name_list SEMICOLON

name_list ::=
                term_name name_list_tail

name_list_tail ::=
                COMMA term_name name_list_tail
        |       /* epsilon */

production_statement ::=
                term_name COLON_COLON_EQUALS rule_lists SEMICOLON

term_name ::=
                SIMPLE_NAME

rule ::=
                term_list_opt action_opt

action_opt ::=
                block
        |       /* epsilon */

rule_lists ::=
                rule rule_lists_tail

rule_lists_tail ::=
                VERTICAL_BAR rule rule_lists_tail
        |       /* epsilon */

term ::=
                term_name variable_name_opt
        |       term_name variable_name_opt ASTERISK
        |       term_name variable_name_opt PLUS

variable_name_opt ::=
                COLON term_name
        |       /* epsilon */

term_list_opt ::=
                term term_list_opt
        |       /* epsilon */

//Tokens

HASH  ::= "#"
ELLIPSIS  ::= "..."
OPEN_PARENTHESIS  ::= "("
CLOSE_PARENTHESIS  ::= ")"
OPEN_BRACKET  ::= "["
CLOSE_BRACKET  ::= "]"
OPEN_BRACE  ::= "{"
CLOSE_BRACE  ::= "}"
DOT  ::= "."
COMMA  ::= ","
COLON  ::= ":"
SEMICOLON  ::= ";"
EQUALS_GREATER_THAN  ::= "=>"
QUESTION_MARK  ::= "?"
EQUALS  ::= "="
ASTERISK  ::= "*"
SLASH  ::= "/"
PERCENT  ::= "%"
PLUS  ::= "+"
PLUS_PLUS  ::= "++"
PLUS_PLUS_EQUALS  ::= "++="
MINUS  ::= "-"
EQUALS_EQUALS  ::= "=="
EXCLAMATION_MARK  ::= "!"
EXCLAMATION_MARK_EQUALS  ::= "!="
LESS_THAN  ::= "<"
GREATER_THAN  ::= ">"
LESS_THAN_EQUALS  ::= "<="
GREATER_THAN_EQUALS  ::= ">="
LESS_THAN_EQUALS_GREATER_THAN  ::= "<=>"
DOT_GREATER_THAN  ::= ".>"
EXCLAMATION_GREATER_THAN  ::= "!>"
QUESTION_MARK_GREATER_THAN  ::= "?>"
MINUS_MINUS  ::= "--"
AMPERSAND  ::= "&"
CARET  ::= "^"
VERTICAL_BAR  ::= "|"
AMPERSAND_AMPERSAND  ::= "&&"
VERTICAL_BAR_VERTICAL_BAR  ::= "||"
PLUS_EQUALS  ::= "+="
MINUS_EQUALS  ::= "-="
ASTERISK_EQUALS  ::= "*="
MINUS_MINUS_MINUS  ::= "---"
COLON_COLON_EQUALS  ::= "::="

RESERVED ::= "reserved"
THIS ::= "this"
SUPER ::= "super"
NEW ::= "new"
OR ::= "or"
IS ::= "is"
IS_NOT ::= "is_not"
RETURN ::= "return"
IF ::= "if"
ELSE ::= "else"
LOOP ::= "loop"
WHILE ::= "while"
FOR ::= "for"
IMPORT ::= "import"
SWITCH ::= "switch"
CASE ::= "case"
DEFAULT ::= "default"
USE ::= "use"
TARGET ::= "target"
PLEASE ::= "please"
GRAMMAR ::= "grammar"
TERMINAL ::= "terminal"
NONTERMINAL ::= "nonterminal"

FLAVOR ::= "nameonly"
FLAVOR ::= "any"
FLAVOR ::= "readonly"
FLAVOR ::= "writeonly"
FLAVOR ::= "mutable"
FLAVOR ::= "immutable"
FLAVOR ::= "deeply_immutable"
FLAVOR ::= "raw"

KIND ::= "block"
KIND ::= "class"
KIND ::= "concept"
KIND ::= "datatype"
KIND ::= "enum"
KIND ::= "interface"
KIND ::= "module"
KIND ::= "namespace"
KIND ::= "package"
KIND ::= "program"
KIND ::= "html_content"
KIND ::= "singleton"
KIND ::= "project"
KIND ::= "service"
KIND ::= "world"
KIND ::= "test_suite"
KIND ::= "reference_kind"
KIND ::= "procedure_kind"
KIND ::= "union_kind"
KIND ::= "type_alias_kind"
mishadynin commented 9 months ago

This is awesome! Thank you @mingodad!