Making Working TS

This commit is contained in:
Martin Karkowski 2022-07-21 22:13:56 +02:00
parent 9870c095ce
commit 6870b27829

View File

@ -22,11 +22,13 @@ STR: /(`.*?`)|(".*?")|(\'.*?\')/
%ignore COMMENT
%ignore WS
// Ignore Exports
%ignore "export" "default" /w+/ ";"
%ignore "export" "*" "from" STR ";"
%ignore "export" "*" "as" /w+/ "from" STR ";"
%ignore "export" /(?<=export)(.|\n)+?(?=})/ "}" "from" STR ";"
%ignore "export" /(?<=export)(.|\n)+?(?=})/ "}" ";"
// --------------------------------------------------------------------------
// Summary:
// --------------------------------------------------------------------------
@ -45,16 +47,16 @@ ret_expr: id
| instanceof
| typeof
| increment
| decrease
| decrement
| invert
| list
| dict
| descruct_dict
| reassign
| await
| delete
| await_stmt
| delete_stmt
| reg_ex
| throw
| throw_statement
| sum
| product
| boolean_operation
@ -64,19 +66,17 @@ ret_expr: id
| function_call
| inline_if
| new_class
| break
| continue
| break_statement
| continue_statement
| ret_expr ("as" type)+
| "(" ret_expr ")"
| continue
| break
| return
| return_statement
// Now we ar able to provide this expressions wiht a terminator.
ret_expr_with_terminator: ret_expr terminator
return: "return" [ret_expr]
return_statement: "return" [ret_expr]
statement: ret_expr_with_terminator
| declare
@ -86,9 +86,9 @@ statement: ret_expr_with_terminator
| declare_var_descructed
| import_stmt
| for
| while
| while_statement
| do_while
| if
| if_statement
| switch
| interface
| class
@ -106,12 +106,13 @@ 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
import_stmt: "import" str terminator -> import_stmt_all
| "import" id "from" str terminator -> import_stmt_id
| "import" "*" "as" id "from" str terminator -> import_stmt_as
| "import" "{" import_names "}" "from" str terminator -> import_stmt_from
// we may import multiple items:
import_names: import_name ("," import_name)* [","]
@ -135,43 +136,46 @@ undefined: "undefined"
increment: accessor "++"
| "++" accessor
decrease: accessor "--"
decrement: accessor "--"
| "--" accessor
invert: "!" ret_expr
instanceof: id "instanceof" id_typed
typeof: "typeof" accessor
delete: "delete" accessor
delete_stmt: "delete" accessor
await: "await" ret_expr
await_stmt: "await" ret_expr
reg_ex: "/" /(?<=\/).+(?=\/\w)/ "/" [/\w/]
?sum: product
| sum "+" product -> add
| sum "-" product -> min
| id "+=" product
| id "-=" product
| sum "-" product -> sub
| accessor "+=" product -> assigned_add
| accessor "-=" product -> assigned_sub
?product: atom
| product "*" atom -> mul
| product "*" atom -> mult
| product "/" atom -> div
| id "*=" atom
| id "/=" atom
| accessor "*=" atom -> assigned_mult
| accessor "/=" atom -> assigned_div
boolean_operation: boolean_input boolean_operator boolean_input
boolean_operator: ">"
| "<"
| "<="
| ">="
| "=="
| "==="
| "!="
| "!=="
| "&&"
| "||"
| "in"
boolean_operator: ">" -> bool_op_gt
| "<" -> bool_op_lt
| "<=" -> bool_op_lte
| ">=" -> bool_op_gte
| "==" -> bool_op_eq
| "===" -> bool_op_eq
| "!=" -> bool_op_not_eq
| "!==" -> bool_op_not_eq
| "&&" -> bool_op_and
| "||" -> bool_op_or
| "in" -> bool_op_in
boolean_input: ret_expr
@ -184,19 +188,19 @@ boolean_input: ret_expr
list: "[" [list_items] "]"
list_items: (list_item [","])+
list_item: ret_expr
| "..." ret_expr
| "..." ret_expr -> list_item_rest
descruct_list: "[" ((id | | ("..." id)) [","])* "]" "=" ret_expr
descruct_list: "[" ((id | (rest_accessor)) [","])* "]" "=" ret_expr
// Define Objects
dict: "{" dict_items? "}"
dict: "{" [dict_items] "}"
dict_items: (dict_item [","] )+
dict_item: (id | num | str) ":" ret_expr
| id "(" func_args? ")" func_body
| "..." ret_expr
| id
dict_item: (id | num | str) ":" ret_expr -> dict_item_default
| id "(" func_args? ")" func_body -> dict_item_func
| "..." ret_expr -> dict_item_rest
| id -> dict_item_short
descruct_dict: "{" ((id | (id ":" id) | ("..." id)) [","])* "}" "=" ret_expr
descruct_dict: "{" ((id | (id ":" id) | (rest_accessor)) [","])* "}" "=" ret_expr
// --------------------------------------------------------------------------
@ -225,14 +229,37 @@ declare_var_type: "let"
// 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 "]"
bracket_accessor: id
| str
| str_multi_line
| num
| bool
| instanceof
| typeof
| await_stmt
| sum
| product
| boolean_operation
| function_call
| inline_if
| bracket_accessor ("as" type)+ -> simple_access
| "(" bracket_accessor ")"
accessor: id -> var_based_access
| str
| num
| list
| dict
| await_stmt
// | ret_expr
| accessor ("as" type)+ -> simple_access
| "(" accessor ["as" type]+ ")" -> simple_access
| accessor ["?"] ("." accessor)+ -> access_dot
| accessor ["?"] ("[" bracket_accessor "]")+ -> access_bracket
| function_call -> simple_access
rest_accessor: "..." id
// --------------------------------------------------------------------------
// Reassignment:
@ -257,9 +284,11 @@ reassign: accessor "=" ret_expr
function_helper: ["<" type ("," type)* ">"] "(" func_args? ")" [":" type] func_body
function: [export] ["async"] "function" [id] ["<" type ("," type)* ">"] "(" func_args? ")" [":" type] func_body
function: [export] "function" [id] ["<" type ("," type)* ">"] "(" func_args? ")" [":" type] func_body -> function
| [export] "async" "function" [id] ["<" type ("," type)* ">"] "(" func_args? ")" [":" type] func_body -> async_function
arrow_function: [export] ["async"] ["<" type ("," type)* ">"] (( "(" func_args? ")" [":" type] ) | func_arg ) "=>" arrow_func_body
arrow_function: ["<" type ("," type)* ">"] (( "(" func_args? ")" [":" type] ) | func_arg ) "=>" func_body -> arrow_function
| "async" ["<" type ("," type)* ">"] (( "(" func_args? ")" [":" type] ) | func_arg ) "=>" func_body -> async_arrow_function
arrow_function_type: ["<" type ("," type)* ">"] "(" func_args? ")" [":" type] "=>" type
@ -269,10 +298,13 @@ arrow_function_type: ["<" type ("," type)* ">"] "(" func_args? "
func_args: func_arg ("," func_arg)*
func_arg: implicit_or_typed
| "..." implicit_or_typed
| implicit_or_typed "=" ret_expr
| dict | list
func_arg: implicit_or_typed -> default_func_arg
| "..." implicit_or_typed -> rest_func_arg
| implicit_or_typed "=" ret_expr -> assigend_func_arg
// We dont want to enable list | dict destruction as a function arg, because
// we can not parse it more or less :(
// | dict | list
implicit_or_typed: id
| id ["?"] ":" type
@ -299,14 +331,14 @@ arrow_func_body: func_body
// 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? ")"
function_call: accessor ["<" type ("," type)* ">"] "(" call_args? ")"
// We define our call args:
call_args: call_arg ("," call_arg)*
call_arg: ret_expr
| "..." ret_expr
call_arg: ret_expr -> call_arg
| "..." ret_expr -> rest_call_arg
type: basic_type
| enhanced_type
@ -344,13 +376,15 @@ id_typed: id | enhanced_type
// --------------------------------------------------------------------------
// 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: "for" "(" declare_var_type for_iter_var for_iter_type ret_expr ")" iter_body -> default_for
| "for" "(" declare_var_type "[" (for_iter_var [","])+ "]" for_iter_type ret_expr ")" iter_body -> mutli_for
| "for" "(" declare_var_type id "=" ret_expr ";" ret_expr ";" ret_expr ")" iter_body -> ranged_for
for_iter_type: "in" | "of"
for_iter_var: id | dict | list
while: "while" "(" ret_expr ")" iter_body
while_statement: "while" "(" ret_expr ")" iter_body
do_while: "do" iter_body "while" "(" ret_expr ")" terminator
@ -358,8 +392,8 @@ iter_body: "{" iter_statements "}" | iter_statement
iter_statements: iter_statement*
iter_statement: statement
continue: "continue"
break: "break"
continue_statement: "continue"
break_statement: "break"
// --------------------------------------------------------------------------
// IF-Statements
@ -367,10 +401,14 @@ break: "break"
// 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_statement: "if" "(" ret_expr ")" if_body [else_if_statements] [else_statement]
else_if_statements: else_if_statement+
else_if_statement: "else" "if" "(" ret_expr ")" if_body
else_statement: "else" if_body
if_body: "{" statement* "}"
| ret_expr_with_terminator
| ret_expr_with_terminator -> if_body_single
inline_if: ret_expr "?" ret_expr ":" ret_expr
@ -383,11 +421,11 @@ 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_body: (("{" switch_case_statements "}") | switch_case_statements) break_statement
switch_case_statements: switch_case_statement*
switch_case_statement: statement
| break
// --------------------------------------------------------------------------
// Error Handling
@ -397,7 +435,7 @@ try_catch: "try" try_catch_body "catch" "(" id ")" try_catch_
try_catch_body: "{" statement* "}"
throw: "throw" ret_expr
throw_statement: "throw" ret_expr
// --------------------------------------------------------------------------
// interfaces