nope/py-helpers/prepare_code/grammar.lark

454 lines
19 KiB
Plaintext

%import common.WS
%import common.DIGIT
%import common.INT
MULTI_LINE_COMMENT: "/**" /(.|\n)*?/ "*/\n"
COMMENT: "//" /.*/
STR: /(`.*?`)|(".*?")|(\'.*?\')/
%ignore MULTI_LINE_COMMENT
%ignore COMMENT
%ignore WS
// Ignore Exports
%ignore "export" "*" "from" STR ";"
%ignore "export" "*" "as" /w+/ "from" STR ";"
%ignore "export" /(?<=export)(.|\n)+?(?=})/ "}" "from" STR ";"
%ignore "export" /(?<=export)(.|\n)+?(?=})/ "}" ";"
// --------------------------------------------------------------------------
// Summary:
// --------------------------------------------------------------------------
start: statement+
// we have to define, what is a valid return type
ret_expr: id
| str
| str_multi_line
| num
| bool
| null
| undefined
| instanceof
| typeof
| increment
| decrease
| invert
| list
| dict
| descruct_dict
| reassign
| await
| delete
| reg_ex
| throw
| sum
| product
| boolean_operation
| accessor
| function
| arrow_function
| function_call
| inline_if
| new_class
| break
| continue
| ret_expr ("as" type)+
| "(" ret_expr ")"
| continue
| break
| return
// Now we ar able to provide this expressions wiht a terminator.
ret_expr_with_terminator: ret_expr terminator
return: "return" [ret_expr]
statement: ret_expr_with_terminator
| declare
| declare_type
| declare_var
| declare_var_not_initialized
| declare_var_descructed
| import_stmt
| for
| while
| do_while
| if
| switch
| interface
| class
| decorated_class
| function
| try_catch
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
// Default Terminator:
terminator: ";"
// Default ID:
id: /[a-zA-Z_$][a-zA-Z0-9_$]*/ -> identifier
// We define valid import statements:
import_stmt: "import" str terminator
| "import" id "from" str terminator
| "import" "*" "as" id "from" str terminator
| "import" "{" import_names "}" "from" str terminator
// we may import multiple items:
import_names: import_name ("," import_name)*
// The import name might include multiple lines
import_name: id
| id "as" id -> import_as_name
| /\n/
// Lets define a string;
str: /(`.*?`)|(".*?")|(\'.*?\')/
str_multi_line: /(`(\\`|.|\n)*?`)/
// Lets define a number;
num: INT ["." INT] | "." INT
// Define a boolean;
bool: "false" | "true"
null: "null"
undefined: "undefined"
increment: accessor "++"
| "++" accessor
decrease: accessor "--"
| "--" accessor
invert: "!" ret_expr
instanceof: id "instanceof" id_typed
typeof: "typeof" accessor
delete: "delete" accessor
await: "await" ret_expr
reg_ex: "/" /(?<=\/).+(?=\/\w)/ "/" [/\w/]
?sum: product
| sum "+" product -> add
| sum "-" product -> min
| id "+=" product
| id "-=" product
?product: atom
| product "*" atom -> mul
| product "/" atom -> div
| id "*=" atom
| id "/=" atom
boolean_operation: boolean_input boolean_operator boolean_input
boolean_operator: ">"
| "<"
| "<="
| ">="
| "=="
| "==="
| "!="
| "!=="
| "&&"
| "||"
| "in"
boolean_input: ret_expr
?atom: ret_expr
| id
| "-" atom -> negative_types_repr
| "(" sum ")"
// Define Lists.
list: "[" [list_items] "]"
list_items: (list_item [","])+
list_item: ret_expr
| "..." ret_expr
descruct_list: "[" ((id | | ("..." id)) [","])* "]" "=" ret_expr
// Define Objects
dict: "{" dict_items? "}"
dict_items: (dict_item [","] )+
dict_item: (id | num | str) ":" ret_expr
| id "(" func_args? ")" func_body
| "..." ret_expr
| id
descruct_dict: "{" ((id | (id ":" id) | ("..." id)) [","])* "}" "=" ret_expr
// --------------------------------------------------------------------------
// Lets enable defining variables:
// --------------------------------------------------------------------------
export: "export"
declare: "declare" declare_var_type implicit_or_typed terminator
declare_type: [export] "type" id_typed "=" type terminator
declare_var: [export] declare_var_type implicit_or_typed "=" ret_expr_with_terminator
declare_var_not_initialized: [export] declare_var_type implicit_or_typed terminator
declare_var_descructed: declare_var_type descruct_dict terminator
| declare_var_type descruct_list terminator
// Valid defintions of variables.
declare_var_type: "let"
| "var"
| "const"
// --------------------------------------------------------------------------
// Acess Variables:
// --------------------------------------------------------------------------
// To access some vars
// We may want to convert them to
// specific types.
simple_access: id | str | num | ret_expr
accessor: simple_access
| accessor ["as" type]+
| "(" accessor ["as" type]+ ")"
| accessor ["?"] "." accessor
| accessor ["?"] "[" ret_expr "]"
| function_call
// --------------------------------------------------------------------------
// Reassignment:
// --------------------------------------------------------------------------
// lets define a reassingment:
reassign: accessor "=" ret_expr
// --------------------------------------------------------------------------
// Functions:
// These may contain:
// - Async Functions
// - Sync Functions
// They might be defined as `arrow_function`.
//
// In General, a function is name with an valid id, may be typed,
// may receives a custom amount of arguments and normally provides a
// function body;
// --------------------------------------------------------------------------
function_helper: ["<" type ("," type)* ">"] "(" func_args? ")" [":" type] func_body
function: [export] ["async"] "function" [id] ["<" type ("," type)* ">"] "(" func_args? ")" [":" type] func_body
arrow_function: [export] ["async"] ["<" type ("," type)* ">"] (( "(" func_args? ")" [":" type] ) | func_arg ) "=>" arrow_func_body
arrow_function_type: ["<" type ("," type)* ">"] "(" func_args? ")" [":" type] "=>" type
// Now we have to define the valid arguments:
// The function may receives multiple arguments
// which are typed or implicit (-> any)
func_args: func_arg ("," func_arg)*
func_arg: implicit_or_typed
| "..." implicit_or_typed
| implicit_or_typed "=" ret_expr
| dict | list
implicit_or_typed: id
| id ["?"] ":" type
// Define the Function Body:
// This consists of the brackets and the statements in the function
func_body: "{" [func_statements] "}"
func_statements: func_statement+
func_statement: statement
arrow_func_body: func_body
| ret_expr
// And now we define, which elements are allowed to be included
// in a function. in our case these are more or less all statements
// Additionally, we have ot make shure, that we are able to
// "return" something.
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
// The Defintion of how a function could be called.
// In here we consider sync as well as async calls.
function_call: ret_expr ["<" type ("," type)* ">"] "(" call_args? ")"
// We define our call args:
call_args: call_arg ("," call_arg)*
call_arg: ret_expr
| "..." ret_expr
type: basic_type
| enhanced_type
| "(" type ")"
basic_type: id
| "any"
| "string" -> string
| "boolean" -> boolean
| "number" -> number
| ["|"] str ("|" str)* // Typess like `const a: "hello" | "world" = "world"
| arrow_function_type // arrow functions
enhanced_type: "|" type // Leading type.
| id "in" type
| id ("<" type ("," type)* ">") // Typess like `Map<string,string>
| "[" type ("," type)* "]" // Typess like `Array<[string,boolean]>
| "{" ( id ["?"] ":" type [";"] )* "}" // Dynamic type like
| "{" "[" id (":" | "in") type "]" ":" type [";"]"}" // Dynamic type like `{ [index: string]: INopeDescriptor }`
| type "extends"? "keyof"? "typeof"? type // Types consisting of keyof , typeof etc
| type ("[" (type | num) "]")+ // Partial type of the Element
| type ("[" "]")+ // Array type
| type ("&" type)+ // Combinded type using &
| type ("|" type)+ // Types which might be
| type "=" type // Types which are assigned with a default type.
| accessor
// Definition for a Nested Type.
id_typed: id | enhanced_type
// --------------------------------------------------------------------------
// Loops
// --------------------------------------------------------------------------
// Define a For - Statement
for: "for" "(" declare_var_type for_iter_var ("of" | "in") ret_expr ")" iter_body
| "for" "(" declare_var_type "[" (for_iter_var [","])+ "]" ("of" | "in") ret_expr ")" iter_body
| "for" "(" declare_var_type id "=" ret_expr ";" ret_expr ";" ret_expr ")" iter_body
for_iter_var: id | dict | list
while: "while" "(" ret_expr ")" iter_body
do_while: "do" iter_body "while" "(" ret_expr ")" terminator
iter_body: "{" iter_statements "}" | iter_statement
iter_statements: iter_statement*
iter_statement: statement
continue: "continue"
break: "break"
// --------------------------------------------------------------------------
// IF-Statements
// --------------------------------------------------------------------------
// Define a If - Statement
// We have to consider "if" "else if" and "else"
if: "if" "(" ret_expr ")" if_body ("else" "if" "(" ret_expr ")" if_body)* ["else" if_body]
if_body: "{" statement* "}"
| ret_expr_with_terminator
inline_if: ret_expr "?" ret_expr ":" ret_expr
// --------------------------------------------------------------------------
// switch-case
// --------------------------------------------------------------------------
switch: "switch" "(" ret_expr ")" switch_body
switch_body: "{" ((switch_case)* [switch_default])* "}"
switch_case: "case" ret_expr ":" [switch_case_body]
switch_default: "default" ":" [switch_case_body]
switch_case_body: ("{" switch_case_statements "}") | switch_case_statements
switch_case_statements: switch_case_statement*
switch_case_statement: statement
| break
// --------------------------------------------------------------------------
// Error Handling
// --------------------------------------------------------------------------
try_catch: "try" try_catch_body "catch" "(" id ")" try_catch_body ["finally" try_catch_body]
try_catch_body: "{" statement* "}"
throw: "throw" ret_expr
// --------------------------------------------------------------------------
// interfaces
// --------------------------------------------------------------------------
interface: [export] "interface" id_typed ["extends" implementing_interfaces] interface_body
extending_interfaces: id_typed ("," id_typed)*
interface_body: "{" interface_declarations* "}"
interface_declarations: interface_declaration
// Interfaces contains implicit or typed attributes or methods
interface_declaration: [readonly] implicit_or_typed terminator
| id ["<" type ("," type)* ">"] "(" func_args? ")" [":" type ] terminator
// --------------------------------------------------------------------------
// classes
// --------------------------------------------------------------------------
class: [export] "class" id_typed ["extends" id_typed] ["implements" implementing_interfaces] class_body
decorated_class: ("@" function_call) class
implementing_interfaces: id_typed ("," id_typed)*
class_body: "{" class_declarations* "}"
class_declarations: constructor
| property_defintion
| property_assigned
| decorated_property_assigned
| decorated_property_defintion
| getter
| setter
| method
| async_method
| decorated_method
| decorated_async_method
constructor: ["public"] "constructor" "(" constructor_args? ")" func_body
constructor_args: constructor_arg ("," constructor_arg)*
constructor_arg: ["@" function_call] [visibility] [readonly] func_arg
visibility: "private"
| "protected"
| "public"
readonly: "readonly"
property_defintion: [visibility] [readonly] implicit_or_typed terminator
property_assigned: [visibility] [readonly] implicit_or_typed "=" ret_expr_with_terminator
decorated_property_defintion: ("@" function_call) property_defintion
decorated_property_assigned: ("@" function_call) property_assigned
getter: [visibility] "get" id "("")" [":" type] func_body
setter: [visibility] "set" id "(" func_arg ")" [":" "void"] func_body
method: [visibility] id ["<" type ("," type)* ">"] "(" func_args? ")" [":" type] func_body
async_method: [visibility] "async" id ["<" type ("," type)* ">"] "(" func_args? ")" [":" type] func_body
decorated_method: ("@" function_call) method
decorated_async_method: ("@" function_call) async_method
// Types for a new Class
new_class: "new" id_typed "(" call_args? ")"