mirror of
https://github.com/freedoom/freedoom.git
synced 2025-09-01 13:25:46 -04:00
simplecpp: keep blank lines
This commit is contained in:
parent
6b8397dc0d
commit
d4c5b3ab48
1 changed files with 95 additions and 82 deletions
|
@ -35,26 +35,31 @@ debug = False
|
|||
defines = {}
|
||||
|
||||
command_re = re.compile("\#(\w+)(\s+(.*))?")
|
||||
include_re = re.compile("\s*\"(.*)\"\s*")
|
||||
include_re = re.compile('\s*"(.*)"\s*')
|
||||
|
||||
|
||||
def debug_msg(message):
|
||||
if debug:
|
||||
sys.stderr.write(message)
|
||||
|
||||
|
||||
# Parse command line options
|
||||
|
||||
|
||||
def parse_cmdline():
|
||||
for arg in sys.argv[1:]:
|
||||
if arg.startswith("-D"):
|
||||
name = arg[2:]
|
||||
defines[name] = True
|
||||
|
||||
|
||||
def parse_stream(stream):
|
||||
result = read_block(stream, False)
|
||||
|
||||
if result is not None:
|
||||
raise Exception("Mismatched #if in '%s'" % stream.name)
|
||||
|
||||
|
||||
def parse_file(filename):
|
||||
f = open(filename)
|
||||
|
||||
|
@ -63,8 +68,10 @@ def parse_file(filename):
|
|||
finally:
|
||||
f.close()
|
||||
|
||||
|
||||
# #include
|
||||
|
||||
|
||||
def cmd_include(arg):
|
||||
# Extract the filename
|
||||
|
||||
|
@ -79,19 +86,25 @@ def cmd_include(arg):
|
|||
|
||||
parse_file(filename)
|
||||
|
||||
|
||||
# #define
|
||||
|
||||
|
||||
def cmd_define(arg):
|
||||
defines[arg] = True
|
||||
|
||||
|
||||
# #undef
|
||||
|
||||
|
||||
def cmd_undef(arg):
|
||||
if arg in defines:
|
||||
del defines[arg]
|
||||
|
||||
|
||||
# #ifdef/#ifndef
|
||||
|
||||
|
||||
def cmd_ifdef(arg, command, stream, ignore):
|
||||
|
||||
# Get the define name
|
||||
|
@ -101,7 +114,7 @@ def cmd_ifdef(arg, command, stream, ignore):
|
|||
|
||||
# Should we ignore the contents of this block?
|
||||
|
||||
sub_ignore = (name not in defines)
|
||||
sub_ignore = name not in defines
|
||||
|
||||
if "n" in command:
|
||||
sub_ignore = not sub_ignore
|
||||
|
@ -124,6 +137,7 @@ def cmd_ifdef(arg, command, stream, ignore):
|
|||
if result != "endif":
|
||||
raise Exception("'if' block did not end in an 'endif'")
|
||||
|
||||
|
||||
commands = {
|
||||
"include": cmd_include,
|
||||
"define": cmd_define,
|
||||
|
@ -137,6 +151,7 @@ commands = {
|
|||
# Recursive block reading function
|
||||
# if 'ignore' argument is 1, contents are ignored
|
||||
|
||||
|
||||
def read_block(stream, ignore):
|
||||
|
||||
for line in stream:
|
||||
|
@ -145,9 +160,10 @@ def read_block(stream, ignore):
|
|||
|
||||
line = line[0:-1]
|
||||
|
||||
# Ignore empty lines
|
||||
# Ignore, but keep empty lines
|
||||
|
||||
if line == " " * len(line):
|
||||
print(line)
|
||||
continue
|
||||
|
||||
# Check if this line has a command
|
||||
|
@ -161,8 +177,7 @@ def read_block(stream, ignore):
|
|||
if command == "else" or command == "endif":
|
||||
return command
|
||||
elif command not in commands:
|
||||
raise Exception("Unknown command: '%s'" % \
|
||||
command)
|
||||
raise Exception("Unknown command: '%s'" % command)
|
||||
|
||||
# Get the callback function.
|
||||
|
||||
|
@ -174,15 +189,13 @@ def read_block(stream, ignore):
|
|||
# ignoring this block.
|
||||
|
||||
if func == cmd_ifdef:
|
||||
cmd_ifdef(arg, command=command,
|
||||
stream=stream,
|
||||
ignore=ignore)
|
||||
cmd_ifdef(arg, command=command, stream=stream, ignore=ignore)
|
||||
elif not ignore:
|
||||
func(arg)
|
||||
else:
|
||||
if not ignore:
|
||||
print(line)
|
||||
|
||||
|
||||
parse_cmdline()
|
||||
parse_stream(sys.stdin)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue