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__": if __name__ == "__main__":
# dist-py/dispatcher/InstanceManager/InstanceManager.js
# "dist-py\dispatcher\baseServices\connectivy.js"
input_path = Path(__file__).parent.joinpath( input_path = Path(__file__).parent.joinpath(
'..', "..", "dist-py", "helpers") '..', "..", "..", "dist-py", "dispatcher", "InstanceManager")
path_to_file = os.path.join(input_path, "jsonSchemaMethods.js") path_to_file = os.path.join(input_path, "InstanceManager.js")
input_path = Path(__file__).parent.joinpath(
'..', "..", "..", "dist-py", "dispatcher", "baseServices")
path_to_file = os.path.join(input_path, "connectivy.js")
parse( parse(
get_parser(), get_parser(),

View File

@ -25,8 +25,10 @@ STR: /(`.*?`)|(".*?")|(\'.*?\')/
// %ignore "export" "default" /w+/ ";" // %ignore "export" "default" /w+/ ";"
// %ignore "export" "*" "from" STR ";" // %ignore "export" "*" "from" STR ";"
// %ignore "export" "*" "as" /w+/ "from" STR ";" // %ignore "export" "*" "as" /w+/ "from" STR ";"
// %ignore "export" /(?<=export)(.|\n)+?(?=})/ "}" "from" STR ";" // %ignore "export" "{" /(?<=export {)(.|\n)+?(?=})/ "}" "from" STR ";"
// %ignore "export" /(?<=export)(.|\n)+?(?=})/ "}" ";" // %ignore "export" "{" /(?<=export {)(.|\n)+?(?=})/ "}" ";"
skip: export "{" (id [","])+ "}" terminator
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -51,7 +53,6 @@ ret_expr: id
| invert | invert
| list | list
| dict | dict
| descruct_dict
| reassign | reassign
| await_stmt | await_stmt
| delete_stmt | delete_stmt
@ -73,7 +74,7 @@ ret_expr: id
// Now we ar able to provide this expressions wiht a terminator. // 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] return_statement: "return" [ret_expr]
@ -87,17 +88,18 @@ statement: ret_expr_with_terminator
| do_while | do_while
| if_statement | if_statement
| switch | switch
| class | class_statement
| decorated_class | decorated_class_statement
| function | function
| try_catch | try_catch
| skip
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// Default Terminator: // Default Terminator:
terminator: ";" terminator.100: ";"
// Default ID: // Default ID:
id: /[a-zA-Z_$][a-zA-Z0-9_$]*/ -> identifier id: /[a-zA-Z_$][a-zA-Z0-9_$]*/ -> identifier
@ -125,7 +127,9 @@ str_multi_line: /(`(\\`|.|\n)*?`)/
num: INT ["." INT] | "." INT num: INT ["." INT] | "." INT
// Define a boolean; // Define a boolean;
bool: "false" | "true" bool: bool_false | bool_true
bool_false: "false"
bool_true: "true"
null: "null" null: "null"
undefined: "undefined" undefined: "undefined"
@ -186,17 +190,24 @@ list_items: (list_item [","])+
list_item: ret_expr list_item: ret_expr
| "..." ret_expr -> list_item_rest | "..." 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 // Define Objects
dict: "{" [dict_items] "}" dict: "{" [dict_items] "}"
dict_items: (dict_item [","] )+ dict_items: (dict_item [","] )+
dict_item: (id | num | str) ":" ret_expr -> dict_item_default 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 | "..." ret_expr -> dict_item_rest
| id -> dict_item_short | 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" 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 "=" function terminator
| [export] declare_var_type id "=" arrow_function terminator | [export] declare_var_type id "=" arrow_function terminator
declare_var_not_initialized: [export] declare_var_type id terminator declare_var_not_initialized.10: [export] declare_var_type id terminator
declare_var_descructed: declare_var_type descruct_dict terminator declare_var_descructed.10: declare_descruct_dict_var | declare_descruct_list_var
| declare_var_type descruct_list terminator
// Valid defintions of variables. // Valid defintions of variables.
@ -251,7 +261,11 @@ accessor: id
| "(" await_stmt ")" | "(" await_stmt ")"
| accessor ["?"] ("." accessor)+ -> access_dot | accessor ["?"] ("." accessor)+ -> access_dot
| accessor ["?"] ("[" bracket_accessor "]")+ -> access_bracket | 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
@ -277,11 +291,11 @@ reassign: accessor "=" ret_expr
// function body; // function body;
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
function: [export] "function" [id] "(" [func_args] ")" func_body -> function function: [export] "function" [id] "(" [func_args] ")" body -> function
| [export] "async" "function" [id] "(" [func_args] ")" func_body -> async_function | [export] "async" "function" [id] "(" [func_args] ")" body -> async_function
arrow_function: "(" [func_args] ")" "=>" func_body -> arrow_function arrow_function: "(" [func_args] ")" "=>" body -> arrow_function
| "async" "(" [func_args] ")" "=>" func_body -> async_arrow_function | "async" "(" [func_args] ")" "=>" body -> async_arrow_function
// Now we have to define the valid arguments: // Now we have to define the valid arguments:
// The function may receives multiple 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 :( // we can not parse it more or less :(
// | dict | list // | 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 // Define a For - Statement
for: "for" "(" declare_var_type for_iter_var for_iter_type ret_expr ")" iter_body -> default_for for: "for" "(" declare_var_type id for_iter_type accessor ")" body_or_expr_with_terminator -> 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 ")" body_or_expr_with_terminator -> ranged_for
| "for" "(" declare_var_type id "=" ret_expr ";" ret_expr ";" ret_expr ")" iter_body -> 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_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 body_or_expr_with_terminator: body
iter_statements: iter_statement* | ret_expr_with_terminator
iter_statement: statement
continue_statement: "continue" continue_statement: "continue"
break_statement: "break" break_statement: "break"
@ -358,14 +355,10 @@ break_statement: "break"
// Define a If - Statement // Define a If - Statement
// We have to consider "if" "else if" and "else" // 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_statements: else_if_statement+
else_if_statement: "else" "if" "(" ret_expr ")" if_body else_if_statement: "else" "if" "(" ret_expr ")" body_or_expr_with_terminator
else_statement: "else" if_body 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
@ -374,23 +367,25 @@ inline_if: ret_expr "?" ret_expr ":" ret_expr
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
switch: "switch" "(" ret_expr ")" switch_body switch: "switch" "(" ret_expr ")" switch_body
switch_body: "{" ((switch_case)* [switch_default])* "}" switch_body: "{" (switch_case* [switch_default])* "}"
switch_case: "case" ret_expr ":" [switch_case_body] switch_case: "case" ret_expr ":" switch_case_body
switch_default: "default" ":" [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_statements: statement* switch_case_body_end
switch_case_statement: statement
switch_case_body_end: break_statement terminator
| return_statement terminator
| throw_statement terminator
| throw_error_statement terminator
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// Error Handling // Error Handling
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
try_catch: "try" try_catch_body "catch" "(" id ")" try_catch_body ["finally" try_catch_body] try_catch: "try" body "catch" "(" id ")" body ["finally" body]
try_catch_body: "{" statement* "}"
throw_statement: "throw" ret_expr throw_statement: "throw" ret_expr
throw_error_statement: "throw" "Error" "(" 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 class_statement: [export] "class" id ["extends" id] class_body
decorated_class: "@" function_call class decorated_class_statement: "@" function_call class_statement
| /let \w+ = (?=class)/ class terminator // Version compiled by tsc | /let \w+ = (?=class)/ class_statement terminator // Version compiled by tsc
class_body: "{" class_declarations* "}" class_body: "{" class_declarations* "}"
@ -413,18 +408,15 @@ class_declarations: constructor
| decorated_method | decorated_method
| decorated_async_method | decorated_async_method
constructor: "constructor" "(" constructor_args? ")" func_body constructor: "constructor" "(" [func_args] ")" body
constructor_args: constructor_arg ("," constructor_arg)*
constructor_arg: ["@" function_call] func_arg
getter: "get" id "(" ")" body
setter: "set" id "(" func_arg ")" body
getter: "get" id "(" ")" func_body method: id "(" [func_args] ")" body
setter: "set" id "(" func_arg ")" func_body async_method: "async" id "(" [func_args] ")" body
method: id "(" [func_args] ")" func_body
async_method: "async" id "(" [func_args] ")" func_body
decorated_method: ("@" function_call) method decorated_method: ("@" function_call) method
decorated_async_method: ("@" function_call) async_method decorated_async_method: ("@" function_call) async_method
// Types for a new Class // 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 import argparse
from msilib.schema import Error
import os import os
import re import re
import multiprocessing as mp import multiprocessing as mp
@ -194,9 +193,9 @@ def main():
typescript_files.append((file_name, path_to_file, dir_path)) typescript_files.append((file_name, path_to_file, dir_path))
else: 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 # Define the Destination
output_path = os.path.join(os.getcwd(), args.outputFolder) output_path = os.path.join(os.getcwd(), args.outputFolder)
@ -239,17 +238,19 @@ def main():
else: else:
failed.append((org_file_name, err)) 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): if len(failed):
logger.warn(f"The following files failed ({len(failed)}):") logger.warn(f"The following files failed ({len(failed)}):")
for (idx, (file_name, err)) in enumerate(failed): for (idx, (file_name, err)) in enumerate(failed):
print("\t", idx+1, ".\t", file_name) print("\t", idx+1, ".\t", file_name)
print("\t\t\t->",str(err).split("\n")[0]) 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) print("\n"*2)
logger.info(f"Parsed {len(success)} of {len(typescript_files)} files ({(len(success)/len(typescript_files))*100:.2f} %).") 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", "true": "True",
"false": "False", "false": "False",
"JSON.stringify": "json.dumps", "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: def post_process(code: str) -> str:

View File

@ -21,7 +21,17 @@
"declaration": true "declaration": true
}, },
"include": [ "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": [ "exclude": [
"node_modules", "node_modules",