Skip to content

Commit

Permalink
tfpp: Fix f-string string nesting and backslash problems with other P…
Browse files Browse the repository at this point in the history
…ython
  • Loading branch information
photron committed Nov 2, 2024
1 parent 7fb1431 commit 1d833c4
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions software/tfpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))

Expand All @@ -58,37 +59,37 @@ 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)
value = int(m.group(2))
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)
elif verb == 'ifdef':
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
Expand All @@ -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
Expand All @@ -121,31 +122,31 @@ 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'

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()

Expand All @@ -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 = []
Expand Down

0 comments on commit 1d833c4

Please sign in to comment.