From 39e66ac37c7ae421bbc1fd763ae5a58842484466 Mon Sep 17 00:00:00 2001 From: Ash Ranjan Date: Mon, 26 Apr 2021 13:46:09 -0700 Subject: [PATCH 1/4] Hotfix v1.2.3 --- python/syntax.lark | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/syntax.lark b/python/syntax.lark index abaa2235..7a738b0b 100644 --- a/python/syntax.lark +++ b/python/syntax.lark @@ -40,7 +40,7 @@ name_type: pattern [":" types] function_dec_call: NAME (" " [name_type (" " name_type)*])? function_call: literal "(" [expression ("," expression)*] ")" pipe_function_call: literal ["(" [expression ("," expression)*] ")"] -code_block: "{" [instruction (("\n" | ";") instruction)*] "}" +code_block: "{" [instruction*] "}" function_def: arguments ["->" types] code_block arguments: "[" generic_declaration? arg_name_type* "]" arg_name_type: definite_pattern [":" types] From 5293df0b0947a4c94a572052298782b5be3715e5 Mon Sep 17 00:00:00 2001 From: Ash Ranjan Date: Mon, 26 Apr 2021 14:21:42 -0700 Subject: [PATCH 2/4] Why wont this work --- python/parse.py | 2 +- python/scope.py | 6 +++++- python/syntax.lark | 40 ++++++++++++++++++++++++---------------- python/syntax_error.py | 6 +++++- 4 files changed, 35 insertions(+), 19 deletions(-) diff --git a/python/parse.py b/python/parse.py index a008c967..c8735e35 100644 --- a/python/parse.py +++ b/python/parse.py @@ -13,6 +13,6 @@ syntaxpath = path.join(basepath, "syntax.lark") with open(syntaxpath, "r") as f: - parse = f.read() + parse = f.read() + "\n" n_parser = Lark(parse, start="start", propagate_positions=True) diff --git a/python/scope.py b/python/scope.py index 8dbfbb77..5ac706bc 100644 --- a/python/scope.py +++ b/python/scope.py @@ -54,7 +54,7 @@ def parse_file(file_path, base_path): native_functions.add_funcs(import_scope) with open(syntaxpath, "r") as f: - parse = f.read() + parse = f.read() + "\n" n_parser = Lark(parse, start="start", propagate_positions=True) with open(file_path, "r", encoding="utf-8") as f: @@ -967,6 +967,8 @@ async def eval_expr(self, expr): """ async def eval_command(self, tree): + if tree.data == "main_instruction" or tree.data == "last_instruction": + tree = lark.tree.Tree("instruction", tree.children[0].children) if tree.data == "if" or tree.data == "ifelse": tree = lark.tree.Tree("instruction", [tree]) elif tree.data == "code_block": @@ -1662,6 +1664,8 @@ def type_check_expr(self, expr): """ def type_check_command(self, tree): + if tree.data == "main_instruction" or tree.data == "last_instruction": + tree = lark.tree.Tree("instruction", tree.children[0].children) if tree.data == "if" or tree.data == "ifelse": tree = lark.tree.Tree("instruction", [tree]) elif tree.data == "code_block": diff --git a/python/syntax.lark b/python/syntax.lark index 7a738b0b..53b54310 100644 --- a/python/syntax.lark +++ b/python/syntax.lark @@ -1,18 +1,26 @@ -start: [instruction*] - -instruction: declare (";" | "\n") - | function_callback (";" | "\n") - | function_callback_pipe (";" | "\n") - | for (";" | "\n")? - | for_legacy (";" | "\n")? - | imp (";" | "\n") - | return (";" | "\n") - | if (";" | "\n")? - | ifelse (";" | "\n")? - | vary (";" | "\n") - | enum_definition (";" | "\n") - | alias_definition (";" | "\n") - | class_definition (";" | "\n") +start: [main_instruction* last_instruction] + +main_instruction: instruction (";" | "\n") + | if (";" | "\n")? + | ifelse (";" | "\n")? + | for (";" | "\n")? + | for_legacy (";" | "\n")? + +last_instruction: instruction (";" | "\n")? + | if (";" | "\n")? + | ifelse (";" | "\n")? + | for (";" | "\n")? + | for_legacy (";" | "\n")? + +instruction: declare + | function_callback + | function_callback_pipe + | imp + | return + | vary + | enum_definition + | alias_definition + | class_definition // functions vary: "var " NAME "=" expression @@ -40,7 +48,7 @@ name_type: pattern [":" types] function_dec_call: NAME (" " [name_type (" " name_type)*])? function_call: literal "(" [expression ("," expression)*] ")" pipe_function_call: literal ["(" [expression ("," expression)*] ")"] -code_block: "{" [instruction*] "}" +code_block: "{" [main_instruction* last_instruction] "}" function_def: arguments ["->" types] code_block arguments: "[" generic_declaration? arg_name_type* "]" arg_name_type: definite_pattern [":" types] diff --git a/python/syntax_error.py b/python/syntax_error.py index aa4ea543..cb76078c 100644 --- a/python/syntax_error.py +++ b/python/syntax_error.py @@ -7,7 +7,11 @@ def format_error(e, file): if isinstance(e, lark.exceptions.UnexpectedEOF): spaces = " " * (len(str(len(file.lines) + 1) + " |") + 1) + " " * (-1) spaces_arrow = " " * (len(str(len(file.lines) + 1) + " |") - 3) - formatted_chars = ", ".join(e.expected) + formatted_chars = "" + try: + formatted_chars = ", ".join(e.expected) + except: + formatted_chars = ", ".join([term.name for term in e.expected]) print( f"{Fore.RED}{Style.BRIGHT}Error{Style.RESET_ALL}: Unexpected end of file, expected: [{formatted_chars}]" ) From 152bb07d416b0708dede8c5b16aa92e3c4d7d5bf Mon Sep 17 00:00:00 2001 From: Ash Ranjan Date: Mon, 26 Apr 2021 14:29:11 -0700 Subject: [PATCH 3/4] It appears to work --- python/scope.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python/scope.py b/python/scope.py index 5ac706bc..f174be8a 100644 --- a/python/scope.py +++ b/python/scope.py @@ -968,8 +968,8 @@ async def eval_expr(self, expr): async def eval_command(self, tree): if tree.data == "main_instruction" or tree.data == "last_instruction": - tree = lark.tree.Tree("instruction", tree.children[0].children) - if tree.data == "if" or tree.data == "ifelse": + tree = tree.children[0] + if tree.data == "if" or tree.data == "ifelse" or tree.data == "for" or tree.data == "for_legacy": tree = lark.tree.Tree("instruction", [tree]) elif tree.data == "code_block": exit, value = (False, None) @@ -1665,8 +1665,8 @@ def type_check_expr(self, expr): def type_check_command(self, tree): if tree.data == "main_instruction" or tree.data == "last_instruction": - tree = lark.tree.Tree("instruction", tree.children[0].children) - if tree.data == "if" or tree.data == "ifelse": + tree = tree.children[0] + if tree.data == "if" or tree.data == "ifelse" or tree.data == "for" or tree.data == "for_legacy": tree = lark.tree.Tree("instruction", [tree]) elif tree.data == "code_block": exit_point = None From 40e9d2c42617ee4802089c3a56ae221c2e317851 Mon Sep 17 00:00:00 2001 From: Ash Ranjan Date: Mon, 26 Apr 2021 14:30:12 -0700 Subject: [PATCH 4/4] cleaned up some code --- python/parse.py | 2 +- python/scope.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/python/parse.py b/python/parse.py index c8735e35..a008c967 100644 --- a/python/parse.py +++ b/python/parse.py @@ -13,6 +13,6 @@ syntaxpath = path.join(basepath, "syntax.lark") with open(syntaxpath, "r") as f: - parse = f.read() + "\n" + parse = f.read() n_parser = Lark(parse, start="start", propagate_positions=True) diff --git a/python/scope.py b/python/scope.py index f174be8a..eaa02160 100644 --- a/python/scope.py +++ b/python/scope.py @@ -54,7 +54,7 @@ def parse_file(file_path, base_path): native_functions.add_funcs(import_scope) with open(syntaxpath, "r") as f: - parse = f.read() + "\n" + parse = f.read() n_parser = Lark(parse, start="start", propagate_positions=True) with open(file_path, "r", encoding="utf-8") as f: