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