ZQuestClassic / ZQuestClassic

ZQuest Classic is a game engine for creating games similar to the original NES Zelda
https://zquestclassic.com
GNU General Public License v3.0
453 stars 79 forks source link

Grammar railroad diagram #931

Open mingodad opened 1 month ago

mingodad commented 1 month ago

I've just added the ffscript grammar to https://mingodad.github.io/parsertl-playground/playground/ (select Ffscript parser from Examples then click Parse to see a parse tree for the content in Input source) and also generated 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 (see instructions on the EBNF shown bellow).

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

Init::=
      Global_List

Global_List::=
      Global_List Global_Statement
    | Global_List Option
    | /*%empty*/

Global_Statement::=
      Import
    | IncludePath
    | Namespace
    | DataTypeDef SEMICOLON
    | ScriptTypeDef SEMICOLON
    | Data SEMICOLON
    | Function
    | Script
    | Annotated_Script
    | Class
    | DataEnum SEMICOLON
    | Using SEMICOLON
    | AlwaysUsing SEMICOLON
    | Statement_Assert SEMICOLON
    | EXPECTERROR LPAREN Expression_Constant RPAREN Global_Statement

Namespace::=
      NAMESPACE Scoperes_Identifier_List LBRACE RBRACE
    | NAMESPACE Scoperes_Identifier_List LBRACE Namespace_Block_List RBRACE

Namespace_Block_List::=
      Namespace_Block_List Namespace_Statement
    | Namespace_Block_List Option
    | Namespace_Statement
    | Option

Namespace_Statement::=
      Namespace
    | DataTypeDef SEMICOLON
    | ScriptTypeDef SEMICOLON
    | Data SEMICOLON
    | Function
    | Script
    | Annotated_Script
    | Class
    | DataEnum SEMICOLON
    | Using SEMICOLON
    | Statement_Assert SEMICOLON
    | EXPECTERROR LPAREN Expression_Constant RPAREN Namespace_Statement

Using::=
      USING NAMESPACE Scoperes_Identifier_List

AlwaysUsing::=
      ALWAYS USING NAMESPACE Scoperes_Identifier_List

Import::=
      IMPORT QUOTEDSTRING
    | HASH INCLUDE IMPORTSTRING
    | HASH INCLUDEIF LPAREN Expression_Constant COMMA IMPORTSTRING RPAREN

IncludePath::=
      HASH INCLUDEPATH IMPORTSTRING

Option::=
      HASH OPTION IDENTIFIER Expression_Constant ENDLINE
    | HASH OPTION IDENTIFIER Expression_Constant NEWLINE
    | HASH OPTION IDENTIFIER INHERIT ENDLINE
    | HASH OPTION IDENTIFIER INHERIT NEWLINE
    | HASH OPTION IDENTIFIER DEFAULT ENDLINE
    | HASH OPTION IDENTIFIER DEFAULT NEWLINE
    | HASH OPTION DEFAULT Expression_Constant ENDLINE
    | HASH OPTION DEFAULT Expression_Constant NEWLINE
    | HASH OPTION DEFAULT INHERIT ENDLINE
    | HASH OPTION DEFAULT INHERIT NEWLINE
    | HASH OPTION DEFAULT DEFAULT ENDLINE
    | HASH OPTION DEFAULT DEFAULT NEWLINE

Statement_Assert::=
      CASSERT LPAREN Expression_Constant RPAREN
    | CASSERT LPAREN Expression_Constant COMMA QuotedString RPAREN

DataTypeDef::=
      StandardDataTypedef
    | EnumDataTypedef

StandardDataTypedef::=
      TYPEDEF DataType IDENTIFIER

EnumDataTypedef::=
      ENUM IDENTIFIER LBRACE Enum_Block RBRACE

DataType::=
      DataType EMPTYBRACKETS
    | DataType_Mods

DataType_Mods::=
      ZCONST DataType_Base
    | DataType_Base

DataType_Base::=
      ZAUTO
    | ZVOID
    | UNTYPED
    | ZBOOL
    | ZFLOAT
    | ZCHAR
    | ZLONG
    | ZRGB
    | Identifier_List

ScriptTypeDef::=
      SCRIPT TYPEDEF Script_Type IDENTIFIER

Data::=
      INTERNAL Data
    | DataType Data_List

Data_List::=
      Data_List COMMA Data_Element
    | Data_Element

Data_Element::=
      Data_Element_Array_List ASSIGN Expression
    | Data_Element_Array_List

Data_Element_Array_List::=
      Data_Element_Array_List Data_Element_Array_Element
    | Identifier

Single_Data_req_assign::=
      DataType Identifier ASSIGN Expression

Data_Element_Array_Element::=
      LBRACKET Data_Element_Array_Element_Size_List RBRACKET
    | EMPTYBRACKETS

Data_Element_Array_Element_Size_List::=
      Data_Element_Array_Element_Size_List COMMA Expression_Constant
    | Expression_Constant

Function::=
      CONSTEXPR Function
    | STATIC Function
    | INLINE Function
    | INTERNAL Function
    | DataType Function_Typeless

Function_Typeless::=
      Function_Heading Statement_Block
    | Function_Heading SEMICOLON
    | Function_Heading COLON DEFAULT Expression_Constant SEMICOLON

Function_Heading::=
      Identifier_List LPAREN Function_Parameters_List RPAREN
    | Identifier_List LT FunctionTemplateList GT LPAREN Function_Parameters_List RPAREN

FunctionTemplateList::=
      Identifier
    | FunctionTemplateList COMMA Identifier

Function_Parameters_List::=
      Function_Parameters_Element COMMA Function_Parameters_List
    | Function_Parameters_Element
    | Function_OptParams_List
    | Function_VarArg_Element
    | /*%empty*/

Function_Parameters_Element::=
      DataType Identifier

Function_OptParams_List::=
      Function_Parameters_Element ASSIGN Expression_Constant COMMA Function_OptParams_List
    | Function_Parameters_Element ASSIGN Expression_Constant

Function_VarArg_Element::=
      RANGE Function_Parameters_Element

Class_Ident::=
      IDENTIFIER

Class::=
      ZCLASS Class_Ident Class_Block

Class_Block::=
      LBRACE Class_Block_List RBRACE
    | LBRACE RBRACE

Class_Block_List::=
      Class_Block_List Class_Block_Element
    | Class_Block_List Option
    | Class_Block_List Class_Constructor
    | Class_Block_List Class_Destructor
    | Class_Block_Element
    | Option
    | Class_Constructor
    | Class_Destructor

Class_Constructor::=
      INTERNAL Class_Constructor
    | Function_Typeless

Class_Destructor::=
      BITNOT Function_Typeless

Class_Data::=
      Data

Class_Block_Element::=
      Class_Data SEMICOLON
    | Function
    | DataTypeDef SEMICOLON
    | DataEnum SEMICOLON
    | Using SEMICOLON
    | Statement_Assert SEMICOLON
    | EXPECTERROR LPAREN Expression_Constant RPAREN Class_Block_Element

Annotated_Script::=
      Annotation_List Script

Script::=
      Script_Type SCRIPT IDENTIFIER Script_Block

Script_Type::=
      IDENTIFIER

Script_Block::=
      LBRACE Script_Block_List RBRACE
    | LBRACE RBRACE

Script_Block_List::=
      Script_Block_List Script_Block_Element
    | Script_Block_List Option
    | Script_Block_Element
    | Option

Script_Block_Element::=
      Data SEMICOLON
    | Function
    | DataTypeDef SEMICOLON
    | DataEnum SEMICOLON
    | Using SEMICOLON
    | Statement_Assert SEMICOLON
    | EXPECTERROR LPAREN Expression_Constant RPAREN Script_Block_Element

Annotation_List::=
      Annotation_List COMMA Annotation
    | Annotation

Annotation::=
      HANDLE IDENTIFIER LPAREN QuotedString RPAREN
    | HANDLE IDENTIFIER LPAREN NUMBER RPAREN
    | HANDLE IDENTIFIER LPAREN LONGNUMBER RPAREN
    | HANDLE IDENTIFIER LPAREN MINUS NUMBER RPAREN
    | HANDLE IDENTIFIER LPAREN MINUS LONGNUMBER RPAREN
    | HANDLE IDENTIFIER LPAREN RPAREN

Block_Statement::=
      Statement

Statement::=
      Data SEMICOLON
    | DataTypeDef SEMICOLON
    | DataEnum SEMICOLON
    | Using SEMICOLON
    | Statement_Expression SEMICOLON
    | Statement_Block
    | Statement_If
    | Statement_Switch
    | Statement_For
    | Annotated_Loop
    | Statement_While
    | Statement_Do
    | Statement_Repeat
    | Statement_Return SEMICOLON
    | BREAK SEMICOLON
    | BREAK NUMBER SEMICOLON
    | CONTINUE SEMICOLON
    | CONTINUE NUMBER SEMICOLON
    | SEMICOLON
    | Statement_CompileError SEMICOLON
    | Statement_Assert SEMICOLON

Statement_NoSemicolon::=
      Data
    | DataTypeDef
    | DataEnum
    | Statement_Expression
    | Statement_Block
    | Statement_If
    | Statement_Switch
    | Statement_For
    | Annotated_Loop
    | Statement_While
    | Statement_Do
    | Statement_Return
    | BREAK
    | BREAK NUMBER
    | CONTINUE
    | CONTINUE NUMBER
    | /*%empty*/
    | Statement_CompileError

Statement_Block::=
      LBRACE Statement_Block_List RBRACE
    | LBRACE RBRACE

Statement_Block_List::=
      Statement_Block_List Statement
    | Statement_Block_List Option
    | Statement
    | Option

Statement_If::=
      IF If_Body
    | UNLESS If_Body

If_Body::=
      LPAREN Single_Data_req_assign RPAREN Block_Statement
    | LPAREN Single_Data_req_assign RPAREN Block_Statement ELSE Block_Statement
    | LPAREN Expression RPAREN Block_Statement
    | LPAREN Expression RPAREN Block_Statement ELSE Block_Statement

Statement_Switch::=
      SWITCH LPAREN Expression RPAREN LBRACE Statement_Switch_Body RBRACE

Statement_Switch_Body::=
      Statement_Switch_Body Statement_Switch_Cases Statement_Block_List
    | Statement_Switch_Cases Statement_Block_List

Statement_Switch_Cases::=
      Statement_Switch_Cases CASE Expression_Constant COLON
    | Statement_Switch_Cases DEFAULT COLON
    | Statement_Switch_Cases CASE Expression_Const_Range COLON
    | Statement_Switch_Cases CASE CASESTRING COLON
    | CASE Expression_Constant COLON
    | CASE Expression_Const_Range COLON
    | CASE CASESTRING COLON
    | DEFAULT COLON

Statement_For::=
      Statement_For_Standard
    | Statement_For_Each

Statement_CommaList::=
      Statement_CommaList COMMA Statement_NoSemicolon
    | Statement_NoSemicolon

Statement_For_Standard::=
      FOR LPAREN Statement_NoSemicolon SEMICOLON Expression SEMICOLON Statement_CommaList RPAREN Block_Statement
    | FOR LPAREN Statement_NoSemicolon SEMICOLON Expression SEMICOLON Statement_CommaList RPAREN Block_Statement ELSE Block_Statement

Statement_For_Each::=
      FOR LPAREN Identifier Token_In Expression RPAREN Block_Statement
    | FOR LPAREN Identifier Token_In Expression RPAREN Block_Statement ELSE Block_Statement

Annotated_Loop::=
      Annotation_List Statement_Loop
    | Statement_Loop

Statement_Loop::=
      Statement_Loop_Inf
    | Statement_Loop_Range

Statement_Loop_Inf::=
      LOOP LPAREN RPAREN Block_Statement

Statement_Loop_Range::=
      Statement_Loop_Range_Base ELSE Block_Statement
    | Statement_Loop_Range_Base

Statement_Loop_Range_Base::=
      LOOP LPAREN DataType IDENTIFIER Token_In Expression_Range COMMA Expression RPAREN Block_Statement
    | LOOP LPAREN IDENTIFIER Token_In Expression_Range COMMA Expression RPAREN Block_Statement
    | LOOP LPAREN Expression_Range COMMA Expression RPAREN Block_Statement
    | LOOP LPAREN DataType IDENTIFIER Token_In Expression_Range RPAREN Block_Statement
    | LOOP LPAREN IDENTIFIER Token_In Expression_Range RPAREN Block_Statement
    | LOOP LPAREN Expression_Range RPAREN Block_Statement

Token_In::=
      COLON
    | IN

Statement_While::=
      WHILE LPAREN Expression RPAREN Block_Statement
    | WHILE LPAREN Expression RPAREN Block_Statement ELSE Block_Statement
    | UNTIL LPAREN Expression RPAREN Block_Statement
    | UNTIL LPAREN Expression RPAREN Block_Statement ELSE Block_Statement

Statement_Do::=
      DO Block_Statement WHILE LPAREN Expression RPAREN
    | DO Block_Statement WHILE LPAREN Expression RPAREN ELSE Block_Statement
    | DO Block_Statement UNTIL LPAREN Expression RPAREN
    | DO Block_Statement UNTIL LPAREN Expression RPAREN ELSE Block_Statement

Statement_Repeat::=
      REPEAT LPAREN Expression_Constant RPAREN Statement

Statement_Return::=
      RETURN Expression
    | RETURN

Statement_CompileError::=
      EXPECTERROR LPAREN Expression_Constant RPAREN Statement_NoSemicolon

DataEnum::=
      ENUM LBRACE Enum_Block RBRACE
    | ENUM ASSIGN DataType LBRACE Enum_Block RBRACE

Enum_Block::=
      Enum_Block COMMA Data_Element
    | Data_Element

ScopeRes::=
      SCOPERES

Identifier_List::=
      Mixed_Identifier_List
    | idlist_scopres
    | idlist_dot
    | Ambigious_Iden_List

Scoperes_Identifier_List::=
      idlist_scopres
    | Ambigious_Iden_List

Mixed_Identifier_List::=
      Mixed_Identifier_List DOT Identifier
    | idlist_scopres DOT Identifier
    | Mixed_Identifier_List ScopeRes Identifier
    | idlist_dot ScopeRes Identifier

idlist_scopres::=
      idlist_scopres ScopeRes Identifier
    | Ambigious_Iden_List ScopeRes Identifier
    | ScopeRes Ambigious_Iden_List

idlist_dot::=
      idlist_dot DOT Identifier
    | Ambigious_Iden_List DOT Identifier

Ambigious_Iden_List::=
      Identifier

Identifier::=
      IDENTIFIER

Func_Left::=
      Expr_Arrow
    | Identifier_List

Function_Call::=
      NEW Identifier_List LPAREN RPAREN
    | NEW Identifier_List LPAREN Function_Call_Parameters RPAREN
    | Func_Left LPAREN RPAREN
    | Func_Left LPAREN Function_Call_Parameters RPAREN

Function_Call_Parameters::=
      Function_Call_Parameters COMMA Expression
    | Expression

Expr_1::=
      Identifier_List
    | Literal
    | LPAREN Expression RPAREN

Expr_2::=
      Expr_1
    | LT DataType GT Expr_2

Expr_Arrow::=
      Expr_3 ARROW IDENTIFIER

Expr_3::=
      Expr_2
    | Expr_3 INCREMENT
    | Expr_3 DECREMENT
    | Function_Call
    | Expr_3 LBRACKET Expression RBRACKET
    | Expr_Arrow

Expr_4::=
      Expr_3
    | INCREMENT Expr_4
    | DECREMENT Expr_4
    | MINUS Expr_4
    | NOT Expr_4
    | BITNOT Expr_4

Expr_5::=
      Expr_4
    | Expr_5 EXPN Expr_4

Expr_6::=
      Expr_5
    | Expr_6 TIMES Expr_5
    | Expr_6 DIVIDE Expr_5
    | Expr_6 MODULO Expr_5

Expr_7::=
      Expr_6
    | Expr_7 PLUS Expr_6
    | Expr_7 MINUS Expr_6

Expr_8::=
      Expr_7
    | Expr_8 LSHIFT Expr_7
    | Expr_8 RSHIFT Expr_7

Expr_9::=
      Expr_8
    | Expr_9 LT Expr_8
    | Expr_9 LE Expr_8
    | Expr_9 GT Expr_8
    | Expr_9 GE Expr_8

Expr_10::=
      Expr_9
    | Expr_10 EQ Expr_9
    | Expr_10 NE Expr_9
    | Expr_10 APPXEQUAL Expr_9
    | Expr_10 XOR Expr_9

Expr_11::=
      Expr_10
    | Expr_11 BITAND Expr_10

Expr_12::=
      Expr_11
    | Expr_12 BITXOR Expr_11

Expr_13::=
      Expr_12
    | Expr_13 BITOR Expr_12

Expr_14::=
      Expr_13
    | Expr_14 AND Expr_13

Expr_15::=
      Expr_14
    | Expr_15 OR Expr_14

Expr_16::=
      Expr_15
    | Expr_15 QMARK Expr_16 COLON Expr_16

Expr_17::=
      Expr_16
    | DELETE Expr_17

Expr_18::=
      Expr_17
    | Expr_17 ASSIGN Expr_18
    | Expr_17 PLUSASSIGN Expr_18
    | Expr_17 MINUSASSIGN Expr_18
    | Expr_17 TIMESASSIGN Expr_18
    | Expr_17 DIVIDEASSIGN Expr_18
    | Expr_17 MODULOASSIGN Expr_18
    | Expr_17 LSHIFTASSIGN Expr_18
    | Expr_17 RSHIFTASSIGN Expr_18
    | Expr_17 BITANDASSIGN Expr_18
    | Expr_17 BITNOTASSIGN Expr_18
    | Expr_17 BITXORASSIGN Expr_18
    | Expr_17 BITORASSIGN Expr_18
    | Expr_17 ANDASSIGN Expr_18
    | Expr_17 ORASSIGN Expr_18

Expression::=
      Expr_18

Statement_Expression::=
      Expression

Expression_Constant::=
      Expression

Expression_Const_Range::=
      Expression_Range

Expression_Range::=
      Expression RANGE Expression
    | Expression RANGE_LR Expression
    | Expression RANGE_L Expression
    | Expression RANGE_R Expression
    | Expression RANGE_N Expression
    | LBRACKET Expression COMMA Expression RBRACKET
    | LBRACKET Expression COMMA Expression RPAREN
    | LPAREN Expression COMMA Expression RBRACKET
    | LPAREN Expression COMMA Expression RPAREN

Literal::=
      NUMBER
    | LONGNUMBER
    | SINGLECHAR
    | Literal_String
    | Literal_Bool
    | Literal_Array
    | OPTIONVALUE LPAREN IDENTIFIER RPAREN
    | ISINCLUDED LPAREN IMPORTSTRING RPAREN

QuotedString::=
      QuotedString QUOTEDSTRING
    | QUOTEDSTRING

Literal_String::=
      QuotedString

Literal_Bool::=
      ZTRUE
    | ZFALSE

Literal_Array::=
      LT DataType LBRACKET Expression_Constant RBRACKET GT LBRACE Literal_Array_Body RBRACE
    | LT DataType LBRACKET Expression_Constant RBRACKET GT LBRACE RBRACE
    | LBRACE Literal_Array_Body RBRACE

Literal_Array_Body::=
      Literal_Array_Body COMMA Expression
    | Expression

//Tokens

 /* Keywords */
SCRIPT ::= "script"
ZCLASS ::= "class"
FOR ::= "for"
LOOP ::= "loop"
IF ::= "if"
ELSE ::= "else"
SWITCH ::= "switch"
CASE ::= "case"
DEFAULT ::= "default"
RETURN ::= "return"
IMPORT ::= "import"
ZTRUE ::= "true"
ZFALSE ::= "false"
WHILE ::= "while"
BREAK ::= "break"
CONTINUE ::= "continue"
ZCONST ::= "const"
DO ::= "do"
TYPEDEF ::= "typedef"

EXPECTERROR ::= "catch"
OPTIONVALUE ::= "OPTION_VALUE"
ISINCLUDED ::= "IS_INCLUDED"
ENUM ::= "enum"
NAMESPACE ::= "namespace"
USING ::= "using"
ALWAYS ::= "always"
UNTIL ::= "until"
UNLESS ::= "unless"
REPEAT ::= "repeat"
INLINE ::= "inline"
INTERNAL ::= "internal"
STATIC ::= "static"
CONSTEXPR ::= "constexpr"
NEW ::= "new"
DELETE ::= "delete"
CASSERT ::= "CONST_ASSERT"

 /* Types */
ZAUTO ::= "auto"
ZVOID ::= "void"
UNTYPED ::= "untyped"
ZBOOL ::= "bool"
ZFLOAT ::= "float"
ZFLOAT ::= "int"
ZCHAR ::= "char32"
ZLONG ::= "long"
ZRGB ::= "rgb"

 /* Syntax */
COMMA ::= ","
DOT ::= "."
SEMICOLON ::= ";"
SCOPERES ::= "::"
COLON ::= ":"
IN ::= "in"
LPAREN ::= "("
RPAREN ::= ")"
EMPTYBRACKETS ::= "[]"
LBRACKET ::= "["
RBRACKET ::= "]"
LBRACE ::= "{"
RBRACE ::= "}"
QMARK ::= "?"

HASH ::= "#"
INCLUDE ::= "include"

 /* Operators (in order of operations) */
ARROW ::= "->"
INCREMENT ::= "++"
DECREMENT ::= "--"
NOT ::= "!"
NOT ::= "not"
BITNOT ::= "~"
BITNOT ::= "compl"
BITNOT ::= "bitnot"
TIMES ::= "*"
DIVIDE ::= "/"
MODULO ::= "%"
PLUS ::= "+"
MINUS ::= "-"
LSHIFT ::= "<<"
RSHIFT ::= ">>"
LT ::= "<"
LE ::= "<="
GT ::= ">"
GE ::= ">="
EQ ::= "=="
EQ ::= "equals"
NE ::= "!="
NE ::= "not_eq"
NE ::= "<>"
NE ::= "not_equal"
BITAND ::= "&"
BITAND ::= "bitand"
BITXOR ::= "^"
BITXOR ::= "bitxor"
BITOR ::= "|"
BITOR ::= "bitor"
AND ::= "&&"
AND ::= "and"
OR ::= "||"
OR ::= "or"
ASSIGN ::= "="
ASSIGN ::= ":="
PLUSASSIGN ::= "+="
MINUSASSIGN ::= "-="
TIMESASSIGN ::= "*="
DIVIDEASSIGN ::= "/="
MODULOASSIGN ::= "%="
LSHIFTASSIGN ::= "<<="
RSHIFTASSIGN ::= ">>="
BITANDASSIGN ::= "&="
BITANDASSIGN ::= "and_eq"
BITANDASSIGN ::= "and_equal"
BITXORASSIGN ::= "^="
BITXORASSIGN ::= "xor_eq"
BITXORASSIGN ::= "xor_equal"
BITORASSIGN ::= "|="
BITORASSIGN ::= "or_eq"
BITORASSIGN ::= "or_equal"
ANDASSIGN ::= "&&="
ORASSIGN ::= "||="
XOR ::= "^^"
XOR ::= "xor"
RANGE_LR ::= "=..="
RANGE_L ::= "=.."
RANGE_R ::= "..="
RANGE_N ::= ".."
RANGE ::= "..."
APPXEQUAL ::= "~~"
APPXEQUAL ::= "appx_eq"
APPXEQUAL ::= "appx_equal"
BITNOTASSIGN ::= "~="
BITNOTASSIGN ::= "bitnot_eq"
BITNOTASSIGN ::= "bitnot_equal"
HANDLE ::= "@"
EXPN ::= "^^^"
connorjclark commented 1 month ago

Thanks for sharing, this is interesting.

Would you mind renaming it to "ZScript"? ffscript is just an internal/legacy name.

mingodad commented 1 month ago

Changed FfScript to ZScript done !