From 1d833c4c4ffa370e9c1e7e2996985b9e0a0eeaeb Mon Sep 17 00:00:00 2001 From: Matthias Bolte Date: Sat, 2 Nov 2024 13:34:05 +0100 Subject: [PATCH] tfpp: Fix f-string string nesting and backslash problems with other Python --- software/tfpp.py | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/software/tfpp.py b/software/tfpp.py index 1a6267809..0079cfb74 100644 --- a/software/tfpp.py +++ b/software/tfpp.py @@ -32,11 +32,12 @@ def parse_file(input_path, defines, ifs_elses): m = re.match(r'^[/\s]*//\s*#\s*(.*)$', line.strip()) if m != None: + line_rstripped = line.rstrip("\r\n") directive = m.group(1) m = re.match(r'^(include |define |undef |ifdef |if |else|endif)\s*(.*)$', directive) if m == None: - raise Exception(f'Malformed directive at {input_path}:{i + 1}: {line.rstrip('\r\n')}') + raise Exception(f'Malformed directive at {input_path}:{i + 1}: {line_rstripped}') verb = m.group(1).strip() arguments = m.group(2) @@ -45,7 +46,7 @@ def parse_file(input_path, defines, ifs_elses): m = re.match(r'^(?:"([^"]+)"|\'([^\']+)\')$', arguments) if m == None: - raise Exception(f'Malformed path in #include directive at {input_path}:{i + 1}: {line.rstrip('\r\n')}') + raise Exception(f'Malformed path in #include directive at {input_path}:{i + 1}: {line_rstripped}') include_path = input_path.parent / pathlib.Path(m.group(1)) @@ -58,7 +59,7 @@ def parse_file(input_path, defines, ifs_elses): m = re.match(r'^([A-Za-z_-][A-Za-z0-9_-]*)\s+(0|1)$', arguments) if m == None: - raise Exception(f'Malformed arguments in #define directive at {input_path}:{i + 1}: {line.rstrip('\r\n')}') + raise Exception(f'Malformed arguments in #define directive at {input_path}:{i + 1}: {line_rstripped}') if not any_zero(ifs_elses): symbol = m.group(1) @@ -66,21 +67,21 @@ def parse_file(input_path, defines, ifs_elses): define = defines.get(symbol) if define != None: - raise Exception(f'Symbol {symbol} in #define directive at {input_path}:{i + 1} is already defined as {define.value} at {define.location}: {line.rstrip('\r\n')}') + raise Exception(f'Symbol {symbol} in #define directive at {input_path}:{i + 1} is already defined as {define.value} at {define.location}: {line_rstripped}') else: defines[symbol] = Define(value, f'{input_path}:{i + 1}') elif verb == 'undef': m = re.match(r'^([A-Za-z_-][A-Za-z0-9_-]*)$', arguments) if m == None: - raise Exception(f'Malformed arguments in #undef directive at {input_path}:{i + 1}: {line.rstrip('\r\n')}') + raise Exception(f'Malformed arguments in #undef directive at {input_path}:{i + 1}: {line_rstripped}') if not any_zero(ifs_elses): symbol = m.group(1) define = defines.get(symbol) if define == None: - raise Exception(f'Symbol {symbol} in #undef directive at {input_path}:{i + 1} is not defined: {line.rstrip('\r\n')}') + raise Exception(f'Symbol {symbol} in #undef directive at {input_path}:{i + 1} is not defined: {line_rstripped}') line = line.rstrip() + f' [defined at {define.location}]\n' defines.pop(symbol) @@ -88,7 +89,7 @@ def parse_file(input_path, defines, ifs_elses): m = re.match(r'^([A-Za-z_-][A-Za-z0-9_-]*)$', arguments) if m == None: - raise Exception(f'Malformed arguments in #ifdef directive at {input_path}:{i + 1}: {line.rstrip('\r\n')}') + raise Exception(f'Malformed arguments in #ifdef directive at {input_path}:{i + 1}: {line_rstripped}') symbol = m.group(1) value = None @@ -108,7 +109,7 @@ def parse_file(input_path, defines, ifs_elses): m = re.match(r'^(1|0|[A-Za-z_-][A-Za-z0-9_-]*)$', arguments) if m == None: - raise Exception(f'Malformed arguments in #if directive at {input_path}:{i + 1}: {line.rstrip('\r\n')}') + raise Exception(f'Malformed arguments in #if directive at {input_path}:{i + 1}: {line_rstripped}') value_or_symbol = m.group(1) value = None @@ -121,7 +122,7 @@ def parse_file(input_path, defines, ifs_elses): define = defines.get(symbol) if define == None: - raise Exception(f'Symbol {symbol} in #if directive at {input_path}:{i + 1} is not defined: {line.rstrip('\r\n')}') + raise Exception(f'Symbol {symbol} in #if directive at {input_path}:{i + 1} is not defined: {line_rstripped}') value = define.value line = line.rstrip() + f' [defined as {value} at {define.location}]\n' @@ -129,23 +130,23 @@ def parse_file(input_path, defines, ifs_elses): ifs_elses.append(If(value, f'{input_path}:{i + 1}')) elif verb == 'else': if len(arguments) > 0: - raise Exception(f'Unexpected arguments in #else directive at {input_path}:{i + 1}: {line.rstrip('\r\n')}') + raise Exception(f'Unexpected arguments in #else directive at {input_path}:{i + 1}: {line_rstripped}') if len(ifs_elses) == 0: - raise Exception(f'Missing #if directive for #else directive at {input_path}:{i + 1}: {line.rstrip('\r\n')}') + raise Exception(f'Missing #if directive for #else directive at {input_path}:{i + 1}: {line_rstripped}') if_else = ifs_elses.pop() if isinstance(if_else, Else): - raise Exception(f'Duplicate #else directive at {input_path}:{i + 1}: {line.rstrip('\r\n')}') + raise Exception(f'Duplicate #else directive at {input_path}:{i + 1}: {line_rstripped}') ifs_elses.append(Else(1 - if_else.value, f'{input_path}:{i + 1}')) elif verb == 'endif': if len(arguments) > 0: - raise Exception(f'Unexpected arguments in #endif directive at {input_path}:{i + 1}: {line.rstrip('\r\n')}') + raise Exception(f'Unexpected arguments in #endif directive at {input_path}:{i + 1}: {line_rstripped}') if len(ifs_elses) == 0: - raise Exception(f'Missing #if directive for #endif directive at {input_path}:{i + 1}: {line.rstrip('\r\n')}') + raise Exception(f'Missing #if directive for #endif directive at {input_path}:{i + 1}: {line_rstripped}') ifs_elses.pop() @@ -161,13 +162,13 @@ def tfpp(input_path, output_path, overwrite=False): output_path = pathlib.Path(output_path) if not input_path.exists(): - raise Exception(f"Input file {input_path} is missing") + raise Exception(f'Input file {input_path} is missing') if output_path.exists() and not overwrite: - raise Exception(f"Output file {output_path} already exists") + raise Exception(f'Output file {output_path} already exists') if input_path == output_path: - raise Exception(f"Input file {input_path} and output file {output_path} are the same") + raise Exception(f'Input file {input_path} and output file {output_path} are the same') defines = {} ifs_elses = []