Return formatted code from the parser so it can be used in error messages.
parent
12dd5f50d6
commit
f9f454625b
50
lambdaV.py
50
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<len(code)+4 and code[parse_position:parse_position+4]=="else":
|
||||
|
@ -385,10 +385,10 @@ def parse_code(code, allowed_commands, allowed_conditions, unformatted_code=True
|
|||
else_code_length = get_length_of_contained_code(code[parse_position:])
|
||||
if else_code_length == -1:
|
||||
debug_message(" Missing (")
|
||||
return [[], [], parse_position, "Syntax error: Cannot not find '(' for else-code"]
|
||||
return [[], [], parse_position, "Syntax error: Cannot not find '(' for else-code", code]
|
||||
if else_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]
|
||||
else_code = code[parse_position:parse_position+else_code_length]
|
||||
debug_message(" Else-code: "+else_code)
|
||||
parsed_else_code = parse_code(else_code[1:-1], allowed_commands, allowed_conditions, unformatted_code=False)
|
||||
|
@ -403,26 +403,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)
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue