handle comments and conditionals in preprogs.src

This commit is contained in:
Bill Currie 2007-04-06 09:27:56 +00:00 committed by Jeff Teunissen
parent 04765083aa
commit 26e15db4ba

View file

@ -65,43 +65,13 @@ def parse_pragma (pragma, args):
keep_newlines = parse_on_off (args)
elif pragma == 'CHECK_REDEFINES':
check_redefines = parse_on_off (args)
elif pragma == 'CHECK_UNUSED_ON':
pass
elif pragma == 'CHECK_UNUSED_OFF':
pass
else:
print "ignoring " + pragma
def do_preprogs_src ():
global current_file
current_file = []
append_file ("preprogs.src")
preprogs = current_file
for p in preprogs:
m = directive_re.match (p)
if (m):
g = m.groups()
directive = g[0]
arg1 = string.strip (g[1])
margs = string.strip (g[4])
#pprint ((directive, arg1, margs))
if directive == 'pragma':
parse_pragma (arg1, margs)
elif directive == 'includelist':
fname = parse_filename (arg1)
if fname:
source_list.append (fname)
elif directive == 'endlist':
pass
else:
print "ignoring " + p
else:
fname = parse_filename (p)
if fname:
source_list.append (fname)
def include_file (fname):
global current_file
fname = parse_filename (fname)
if fname:
append_file (fname)
def comment_string (string_list, start, end):
i = 0
while i < len (string_list):
@ -111,6 +81,90 @@ def comment_string (string_list, start, end):
i += 1
return string_list
def parse_strings_and_comments (l, incomment):
#print source_file + ":" + `i` + ":" + l
string_list = []
s = string_re.search (l)
while s:
string_list.append ((l[s.start():s.end()], s.start(), s.end()))
l = l[:s.start()] + ((s.end() - s.start()) * ' ') + l[s.end():]
s = string_re.search (l)
if incomment:
s = comment_end.search (l)
if s:
l = (s.end() * ' ') + l[s.end():]
incomment = 0
string_list = comment_string (string_list, s.start(), s.end())
else:
string_list = comment_string (string_list, 0, len(l))
l = ""
s = comment_whole.search (l)
if s:
l = l[:s.start()] + ((s.end() - s.start()) * ' ') + l[s.end():]
string_list = comment_string (string_list, s.start(), s.end())
s = comment_start.search (l)
if s:
l = l[:s.start()] + ((s.end() - s.start()) * ' ')
string_list = comment_string (string_list, s.start(), s.end())
incomment = 1
for str in string_list:
l = l[:str[1]] + str[0] + l[str[2]:]
#print l
return l, incomment
def do_preprogs_src ():
global current_file
current_file = []
condition = [1]
incomment = 0
append_file ("preprogs.src")
preprogs = current_file
for p in preprogs:
p, incomment = parse_strings_and_comments (p, incomment)
m = directive_re.match (p)
if (m):
g = m.groups()
directive = g[0]
arg1 = string.strip (g[1])
margs = string.strip (g[4])
#pprint ((directive, arg1, margs))
if directive == 'pragma':
if condition[-1]:
parse_pragma (arg1, margs)
elif directive == 'includelist':
if condition[-1]:
fname = parse_filename (arg1)
if fname:
source_list.append (fname)
elif directive == 'endlist':
pass
elif directive == 'ifdef':
condition.append (condition[-1])
if not defines.has_key (arg1):
condition[-1]=0
elif directive == 'ifndef':
condition.append (condition[-1])
if defines.has_key (arg1):
condition[-1]=0
elif directive == 'else':
condition[-1] = condition [-2] and not condition[-1]
elif directive == 'endif':
del condition[-1]
else:
if condition[-1]:
print "ignoring " + p
else:
if condition[-1]:
fname = parse_filename (p)
if fname:
source_list.append (fname)
def include_file (fname):
global current_file
fname = parse_filename (fname)
if fname:
append_file (fname)
def process_source (source_file):
global compile_this_file, current_file
if verbose:
@ -125,34 +179,7 @@ def process_source (source_file):
i = 0
while i < len (current_file):
l = current_file[i]
#print source_file + ":" + `i` + ":" + l
string_list = []
s = string_re.search (l)
while s:
string_list.append ((l[s.start():s.end()], s.start(), s.end()))
l = l[:s.start()] + ((s.end() - s.start()) * ' ') + l[s.end():]
s = string_re.search (l)
if incomment:
s = comment_end.search (l)
if s:
l = (s.end() * ' ') + l[s.end():]
incomment = 0
string_list = comment_string (string_list, s.start(), s.end())
else:
string_list = comment_string (string_list, 0, len(l))
l = ""
s = comment_whole.search (l)
if s:
l = l[:s.start()] + ((s.end() - s.start()) * ' ') + l[s.end():]
string_list = comment_string (string_list, s.start(), s.end())
s = comment_start.search (l)
if s:
l = l[:s.start()] + ((s.end() - s.start()) * ' ')
string_list = comment_string (string_list, s.start(), s.end())
incomment = 1
for str in string_list:
l = l[:str[1]] + str[0] + l[str[2]:]
#print l
l, incomment = parse_strings_and_comments (l, incomment)
m = directive_re.match (l)
if (m):
g = m.groups()