From f9f454625ba3da4c472d78b64f013f7a1b8ca982 Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Sun, 6 Nov 2022 03:51:31 +0100 Subject: [PATCH] Return formatted code from the parser so it can be used in error messages. --- lambdaV.py | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/lambdaV.py b/lambdaV.py index 33c7703..ff56d3b 100644 --- a/lambdaV.py +++ b/lambdaV.py @@ -280,7 +280,7 @@ def parse_code(code, allowed_commands, allowed_conditions, unformatted_code=True debug_message("Formatted code: "+code) parse_position = 0 - parsed_code = [[], [], -1, ""] + parsed_code = [[], [], -1, "", code] while parse_position < len(code): # find next command next_space = code.find(' ', parse_position) @@ -305,16 +305,16 @@ def parse_code(code, allowed_commands, allowed_conditions, unformatted_code=True elif next_command == "take": parsed_code[0].append(command_take) elif next_command == "repeat": - return [[], [], next_space, "Syntax error: Condition missing"] + return [[], [], next_space, "Syntax error: Condition missing", code] elif next_command == "while": - return [[], [], next_space, "Syntax error: Condition missing"] + return [[], [], next_space, "Syntax error: Condition missing", code] elif next_command == "if": - return [[], [], next_space, "Syntax error: Condition missing"] - elif next_command == "else": - return [[], [], parse_position, "Syntax error: Else without if statement"] + return [[], [], next_space, "Syntax error: Condition missing", code] + elif next_command[0:4] == "else": + return [[], [], parse_position, "Syntax error: Else without if statement", code] else: #TODO: better error message (especially when special chars are detected inside next_command or a known command) - return [[], [], parse_position, "Unknown command: "+next_command] + return [[], [], parse_position, "Unknown command: "+next_command, code] parse_position = next_space+1 else: debug_message("Found control structure...") @@ -325,28 +325,28 @@ def parse_code(code, allowed_commands, allowed_conditions, unformatted_code=True repetitions_length = get_length_of_condition(code[parse_position:]) if repetitions_length == -2: debug_message(" Mismatched <>") - return [[], [], parse_position, "Syntax error: Cannot not find matching '>' for this '<'"] + return [[], [], parse_position, "Syntax error: Cannot not find matching '>' for this '<'", code] repetitions = code[parse_position:parse_position+repetitions_length] debug_message(" Number of repetitions: "+repetitions) repetitions_int = 0 try: repetitions_int = int(repetitions[1:-1]) except ValueError: - return [[], [], parse_position, "Not a valid number: "+repetitions[1:-1]] + return [[], [], parse_position, "Not a valid number: "+repetitions[1:-1], code] parse_position = parse_position+len(repetitions) contained_code_length = get_length_of_contained_code(code[parse_position:]) if contained_code_length == -1: debug_message(" Missing (") - return [[], [], parse_position, "Syntax error: Cannot not find '(' for contained code"] + return [[], [], parse_position, "Syntax error: Cannot not find '(' for contained code", code] if contained_code_length == -2: debug_message(" Mismatched ()") - return [[], [], parse_position, "Syntax error: Cannot not find matching ')' for this '('"] + return [[], [], parse_position, "Syntax error: Cannot not find matching ')' for this '('", code] contained_code = code[parse_position:parse_position+contained_code_length] debug_message(" Contained code: "+contained_code) parsed_contained_code = parse_code(contained_code[1:-1], allowed_commands, allowed_conditions, unformatted_code=False) if not parsed_contained_code[2]==-1: debug_message(" Error while parsing contained code") - return [[], [], parse_position+1+parsed_contained_code[2], parsed_contained_code[3]] + return [[], [], parse_position+1+parsed_contained_code[2], parsed_contained_code[3], code] parse_position = parse_position+contained_code_length+1 parsed_code[1].append((repetitions_int, parsed_contained_code)) @@ -358,26 +358,26 @@ def parse_code(code, allowed_commands, allowed_conditions, unformatted_code=True condition_length = get_length_of_condition(code[parse_position:]) if condition_length == -2: debug_message(" Mismatched <>") - return [[], [], parse_position, "Syntax error: Cannot not find matching '>' for this '<'"] + return [[], [], parse_position, "Syntax error: Cannot not find matching '>' for this '<'", code] condition = code[parse_position:parse_position+condition_length] debug_message(" Condition is: "+condition) parsed_condition = parse_condition(condition[1:-1], allowed_conditions) if not parsed_condition[2]=="": - return [[], [], parse_position, parsed_condition[2]] + return [[], [], parse_position, parsed_condition[2], code] parse_position = parse_position+len(condition) then_code_length = get_length_of_contained_code(code[parse_position:]) if then_code_length == -1: debug_message(" Missing (") - return [[], [], parse_position, "Syntax error: Cannot not find '(' for then-code"] + return [[], [], parse_position, "Syntax error: Cannot not find '(' for then-code", code] if then_code_length == -2: debug_message(" Mismatched ()") - return [[], [], parse_position, "Syntax error: Cannot not find matching ')' for this '('"] + return [[], [], parse_position, "Syntax error: Cannot not find matching ')' for this '('", code] then_code = code[parse_position:parse_position+then_code_length] debug_message(" Then-code: "+then_code) parsed_then_code = parse_code(then_code[1:-1], allowed_commands, allowed_conditions, unformatted_code=False) if not parsed_then_code[2]==-1: debug_message(" Error while parsing then-code") - return [[], [], parse_position+1+parsed_then_code[2], parsed_then_code[3]] + return [[], [], parse_position+1+parsed_then_code[2], parsed_then_code[3], code] parse_position = parse_position+then_code_length+1 parsed_else_code = None if parse_position") - return [[], [], parse_position, "Syntax error: Cannot not find matching '>' for this '<'"] + return [[], [], parse_position, "Syntax error: Cannot not find matching '>' for this '<'", code] condition = code[parse_position:parse_position+condition_length] debug_message(" Condition is: "+condition) parsed_condition = parse_condition(condition[1:-1], allowed_conditions) if not parsed_condition[2]=="": - return [[], [], parse_position, parsed_condition[2]] + return [[], [], parse_position, parsed_condition[2], code] parse_position = parse_position+len(condition) contained_code_length = get_length_of_contained_code(code[parse_position:]) if contained_code_length == -1: debug_message(" Missing (") - return [[], [], parse_position, "Syntax error: Cannot not find '(' for contained code"] + return [[], [], parse_position, "Syntax error: Cannot not find '(' for contained code", code] if contained_code_length == -2: debug_message(" Mismatched ()") - return [[], [], parse_position, "Syntax error: Cannot not find matching ')' for this '('"] + return [[], [], parse_position, "Syntax error: Cannot not find matching ')' for this '('", code] contained_code = code[parse_position:parse_position+contained_code_length] debug_message(" Contained code: "+contained_code) parsed_contained_code = parse_code(contained_code[1:-1], allowed_commands, allowed_conditions, unformatted_code=False) if not parsed_contained_code[2]==-1: debug_message(" Error while parsing contained code") - return [[], [], parse_position+1+parsed_contained_code[2], parsed_contained_code[3]] + return [[], [], parse_position+1+parsed_contained_code[2], parsed_contained_code[3], code] parse_position = parse_position+contained_code_length+1 parsed_code[1].append((parsed_condition, parsed_contained_code)) @@ -430,7 +430,7 @@ def parse_code(code, allowed_commands, allowed_conditions, unformatted_code=True else: debug_message(" Type: unknown control structure: "+control_structure) #TODO: better error message (especially when special chars are detected inside control_structure or a known command) - return [[], [], parse_position, "Syntax error: Unknown control structure: "+control_structure] + return [[], [], parse_position, "Syntax error: Unknown control structure: "+control_structure, code] return parsed_code # returns success or error message