fixing lark for js-files and its transformer

This commit is contained in:
Martin Karkowski 2022-07-25 07:37:31 +02:00
parent 46b0a49241
commit ba752f4142
6 changed files with 945 additions and 448 deletions

View File

@ -49,16 +49,10 @@ def parse(parser, path_to_file):
if __name__ == "__main__":
# "dist-py\dispatcher\baseServices\connectivy.js"
# dist-py/dispatcher/InstanceManager/InstanceManager.js
input_path = Path(__file__).parent.joinpath(
'..', "..", "dist-py", "helpers")
path_to_file = os.path.join(input_path, "jsonSchemaMethods.js")
input_path = Path(__file__).parent.joinpath(
'..', "..", "..", "dist-py", "dispatcher", "baseServices")
path_to_file = os.path.join(input_path, "connectivy.js")
'..', "..", "..", "dist-py", "dispatcher", "InstanceManager")
path_to_file = os.path.join(input_path, "InstanceManager.js")
parse(
get_parser(),

View File

@ -25,8 +25,10 @@ STR: /(`.*?`)|(".*?")|(\'.*?\')/
// %ignore "export" "default" /w+/ ";"
// %ignore "export" "*" "from" STR ";"
// %ignore "export" "*" "as" /w+/ "from" STR ";"
// %ignore "export" /(?<=export)(.|\n)+?(?=})/ "}" "from" STR ";"
// %ignore "export" /(?<=export)(.|\n)+?(?=})/ "}" ";"
// %ignore "export" "{" /(?<=export {)(.|\n)+?(?=})/ "}" "from" STR ";"
// %ignore "export" "{" /(?<=export {)(.|\n)+?(?=})/ "}" ";"
skip: export "{" (id [","])+ "}" terminator
// --------------------------------------------------------------------------
@ -51,7 +53,6 @@ ret_expr: id
| invert
| list
| dict
| descruct_dict
| reassign
| await_stmt
| delete_stmt
@ -73,7 +74,7 @@ ret_expr: id
// Now we ar able to provide this expressions wiht a terminator.
ret_expr_with_terminator: ret_expr terminator
ret_expr_with_terminator.10: ret_expr terminator
return_statement: "return" [ret_expr]
@ -87,116 +88,126 @@ statement: ret_expr_with_terminator
| do_while
| if_statement
| switch
| class
| decorated_class
| class_statement
| decorated_class_statement
| function
| try_catch
| skip
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
// Default Terminator:
terminator: ";"
terminator.100: ";"
// Default ID:
id: /[a-zA-Z_$][a-zA-Z0-9_$]*/ -> identifier
id: /[a-zA-Z_$][a-zA-Z0-9_$]*/ -> identifier
// We define valid import statements:
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
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)* [","]
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)*?`)/
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
num: INT ["." INT] | "." INT
// Define a boolean;
bool: "false" | "true"
bool: bool_false | bool_true
bool_false: "false"
bool_true: "true"
null: "null"
undefined: "undefined"
null: "null"
undefined: "undefined"
increment: accessor "++"
| "++" accessor
decrement: accessor "--"
| "--" accessor
increment: accessor "++"
| "++" accessor
decrement: accessor "--"
| "--" accessor
invert: "!" ret_expr
invert: "!" ret_expr
instanceof: id "instanceof" id
instanceof: id "instanceof" id
typeof: "typeof" accessor
typeof: "typeof" accessor
delete_stmt: "delete" accessor
delete_stmt: "delete" accessor
await_stmt: "await" ret_expr
await_stmt: "await" ret_expr
reg_ex: "/" /(?<=\/).+(?=\/\w)/ "/" [/\w/]
reg_ex: "/" /(?<=\/).+(?=\/\w)/ "/" [/\w/]
sum: product
| sum "+" product -> add
| sum "-" product -> sub
| accessor "+=" product -> assigned_add
| accessor "-=" product -> assigned_sub
sum: product
| sum "+" product -> add
| sum "-" product -> sub
| accessor "+=" product -> assigned_add
| accessor "-=" product -> assigned_sub
product: atom
| product "*" atom -> mult
| product "/" atom -> div
| accessor "*=" atom -> assigned_mult
| accessor "/=" atom -> assigned_div
product: atom
| product "*" atom -> mult
| product "/" atom -> div
| accessor "*=" atom -> assigned_mult
| accessor "/=" atom -> assigned_div
boolean_operation: boolean_input boolean_operator boolean_input
boolean_operation: boolean_input boolean_operator boolean_input
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_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
boolean_input: ret_expr
atom: ret_expr
| id
| "-" atom
| "(" sum ")"
atom: ret_expr
| id
| "-" atom
| "(" sum ")"
// Define Lists.
// Define Lists.
list: "[" [list_items] "]"
list_items: (list_item [","])+
list_item: ret_expr
| "..." ret_expr -> list_item_rest
descruct_list: "[" ((id | (rest_accessor)) [","])* "]" "=" ret_expr
declare_descruct_list_var: declare_var_type "[" (destruct_list_items [","])* "]" "=" ret_expr terminator
destruct_list_items: destruct_list_item | destruct_list_rest
destruct_list_rest: "..." id
destruct_list_item: id
// Define Objects
dict: "{" [dict_items] "}"
dict_items: (dict_item [","] )+
dict_item: (id | num | str) ":" ret_expr -> dict_item_default
| id "(" [func_args] ")" func_body -> dict_item_func
| id "(" [func_args] ")" body -> dict_item_func
| "..." ret_expr -> dict_item_rest
| id -> dict_item_short
descruct_dict: "{" ((id | (id ":" id) | (rest_accessor)) [","])* "}" "=" ret_expr
declare_descruct_dict_var: declare_var_type "{" (descruct_dict_items [","])* "}" "=" ret_expr terminator
descruct_dict_items: (destruct_dict_single_id | destruct_dict_renamed | destruct_dict_rest)
destruct_dict_single_id: id
destruct_dict_renamed: (id | num | str) ":" id
destruct_dict_rest: "..." id
// --------------------------------------------------------------------------
@ -204,13 +215,12 @@ descruct_dict: "{" ((id | (id ":" id) | (rest_accessor)) [","])
// --------------------------------------------------------------------------
export: "export"
declare_var: [export] declare_var_type id "=" ret_expr_with_terminator
declare_var.10: [export] declare_var_type id "=" ret_expr_with_terminator
| [export] declare_var_type id "=" function terminator
| [export] declare_var_type id "=" arrow_function terminator
declare_var_not_initialized: [export] declare_var_type id terminator
declare_var_descructed: declare_var_type descruct_dict terminator
| declare_var_type descruct_list terminator
declare_var_not_initialized.10: [export] declare_var_type id terminator
declare_var_descructed.10: declare_descruct_dict_var | declare_descruct_list_var
// Valid defintions of variables.
@ -251,10 +261,14 @@ accessor: id
| "(" await_stmt ")"
| accessor ["?"] ("." accessor)+ -> access_dot
| accessor ["?"] ("[" bracket_accessor "]")+ -> access_bracket
| function_call -> simple_access
| function_call
| accessor "." "length" -> access_len
| accessor "." "size" -> access_len
| accessor "." "filter" "(" call_args ")" -> access_filter
| accessor "." "map" "(" call_args ")" -> access_map
rest_accessor: "..." id
rest_accessor: "..." id
// --------------------------------------------------------------------------
// Reassignment:
@ -262,7 +276,7 @@ rest_accessor: "..." id
// lets define a reassingment:
reassign: accessor "=" ret_expr
reassign: accessor "=" ret_expr
// --------------------------------------------------------------------------
@ -277,11 +291,11 @@ reassign: accessor "=" ret_expr
// function body;
// --------------------------------------------------------------------------
function: [export] "function" [id] "(" [func_args] ")" func_body -> function
| [export] "async" "function" [id] "(" [func_args] ")" func_body -> async_function
function: [export] "function" [id] "(" [func_args] ")" body -> function
| [export] "async" "function" [id] "(" [func_args] ")" body -> async_function
arrow_function: "(" [func_args] ")" "=>" func_body -> arrow_function
| "async" "(" [func_args] ")" "=>" func_body -> async_arrow_function
arrow_function: "(" [func_args] ")" "=>" body -> arrow_function
| "async" "(" [func_args] ")" "=>" body -> async_arrow_function
// Now we have to define the valid arguments:
// The function may receives multiple arguments
@ -297,22 +311,6 @@ func_arg: id -> default_func_arg
// we can not parse it more or less :(
// | dict | list
// 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
// 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.
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
@ -333,21 +331,20 @@ call_arg: ret_expr -> call_arg
// --------------------------------------------------------------------------
// Define a For - Statement
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: "for" "(" declare_var_type id for_iter_type accessor ")" body_or_expr_with_terminator -> default_for
| "for" "(" declare_var_type id "=" ret_expr ";" ret_expr ";" ret_expr ")" body_or_expr_with_terminator -> ranged_for
| "for" "(" declare_var_type "[" (id [","])+ "]" for_iter_type ret_expr ")" body_or_expr_with_terminator -> multi_for
for_iter_type: "in" | "of"
for_iter_var: id | dict | list
while_statement: "while" "(" ret_expr ")" body_or_expr_with_terminator
while_statement: "while" "(" ret_expr ")" iter_body
do_while: "do" body "while" "(" ret_expr ")" terminator
do_while: "do" iter_body "while" "(" ret_expr ")" terminator
body: "{" statement* "}"
iter_body: "{" iter_statements "}" | iter_statement
iter_statements: iter_statement*
iter_statement: statement
body_or_expr_with_terminator: body
| ret_expr_with_terminator
continue_statement: "continue"
break_statement: "break"
@ -358,39 +355,37 @@ break_statement: "break"
// Define a If - Statement
// We have to consider "if" "else if" and "else"
if_statement: "if" "(" ret_expr ")" if_body [else_if_statements] [else_statement]
if_statement: "if" "(" ret_expr ")" body_or_expr_with_terminator [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
else_if_statement: "else" "if" "(" ret_expr ")" body_or_expr_with_terminator
else_statement: "else" body_or_expr_with_terminator
if_body: "{" statement* "}"
| ret_expr_with_terminator -> if_body_single
inline_if: ret_expr "?" ret_expr ":" ret_expr
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_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) break_statement
switch_case_body: ("{" switch_case_statements "}")
| switch_case_statements
switch_case_statements: switch_case_statement*
switch_case_statement: statement
switch_case_statements: statement* switch_case_body_end
switch_case_body_end: break_statement terminator
| return_statement terminator
| throw_statement terminator
| throw_error_statement terminator
// --------------------------------------------------------------------------
// Error Handling
// --------------------------------------------------------------------------
try_catch: "try" try_catch_body "catch" "(" id ")" try_catch_body ["finally" try_catch_body]
try_catch_body: "{" statement* "}"
try_catch: "try" body "catch" "(" id ")" body ["finally" body]
throw_statement: "throw" ret_expr
throw_error_statement: "throw" "Error" "(" ret_expr ")"
@ -399,9 +394,9 @@ throw_error_statement: "throw" "Error" "(" ret_expr ")"
// --------------------------------------------------------------------------
class: [export] "class" id ["extends" id] class_body
decorated_class: "@" function_call class
| /let \w+ = (?=class)/ class terminator // Version compiled by tsc
class_statement: [export] "class" id ["extends" id] class_body
decorated_class_statement: "@" function_call class_statement
| /let \w+ = (?=class)/ class_statement terminator // Version compiled by tsc
class_body: "{" class_declarations* "}"
@ -413,18 +408,15 @@ class_declarations: constructor
| decorated_method
| decorated_async_method
constructor: "constructor" "(" constructor_args? ")" func_body
constructor_args: constructor_arg ("," constructor_arg)*
constructor_arg: ["@" function_call] func_arg
constructor: "constructor" "(" [func_args] ")" body
getter: "get" id "(" ")" body
setter: "set" id "(" func_arg ")" body
getter: "get" id "(" ")" func_body
setter: "set" id "(" func_arg ")" func_body
method: id "(" [func_args] ")" func_body
async_method: "async" id "(" [func_args] ")" func_body
method: id "(" [func_args] ")" body
async_method: "async" id "(" [func_args] ")" body
decorated_method: ("@" function_call) method
decorated_async_method: ("@" function_call) async_method
// Types for a new Class
new_class: "new" id "(" [call_args] ")"
new_class: "new" ret_expr "(" [call_args] ")"

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,4 @@
import argparse
from msilib.schema import Error
import os
import re
import multiprocessing as mp
@ -194,9 +193,9 @@ def main():
typescript_files.append((file_name, path_to_file, dir_path))
else:
raise Error("Failed to load the file")
raise Exception("Failed to load the file")
typescript_files = sorted(typescript_files,key=lambda item: item[1])
# Define the Destination
output_path = os.path.join(os.getcwd(), args.outputFolder)
@ -238,6 +237,12 @@ def main():
else:
failed.append((org_file_name, err))
if len(success):
print("\n"*2)
print(f"Created the following files ({len(success)}):")
for file_name in success:
print("\t- ", file_name)
if len(failed):
logger.warn(f"The following files failed ({len(failed)}):")
@ -245,11 +250,7 @@ def main():
print("\t", idx+1, ".\t", file_name)
print("\t\t\t->",str(err).split("\n")[0])
if (args.debug):
print("\n"*2)
print(f"Created the following files ({len(success)}):")
for file_name in success:
print("\t- ", file_name)
print("\n"*2)
logger.info(f"Parsed {len(success)} of {len(typescript_files)} files ({(len(success)/len(typescript_files))*100:.2f} %).")

View File

@ -7,7 +7,20 @@ replacers = {
"true": "True",
"false": "False",
"JSON.stringify": "json.dumps",
"JSON.parse": "json.loads"
"JSON.parse": "json.loads",
"const _this = this;": "",
"_this": "self",
"this": "self",
"Set": "set",
"Map": "dict",
"toLowerCase": "lower",
"toUpperCase": "upper",
".push(": ".append(",
"Array.from": "list",
"null": "None",
'"null"': "None",
'"undefined"': "None"
}
def post_process(code: str) -> str:

View File

@ -21,7 +21,17 @@
"declaration": true
},
"include": [
"lib",
"lib/communication",
"lib/decorators",
"lib/dispatcher",
"lib/eventEmitter",
"lib/helpers",
"lib/loader",
"lib/logger",
"lib/module",
"lib/observables",
"lib/promise",
"lib/pubSub"
],
"exclude": [
"node_modules",