Implemented .ifnot logic.
Relaxed requirements so that .if/not only requires one or more [var] references.
1 parent 26dc08b commit d40be7bb58e3527379832b90290b2a065823922f
@tundra tundra authored on 14 Mar 2004
Showing 1 changed file
View
87
tconfpy.py
 
# Program Information
 
PROGNAME = "tconfpy"
RCSID = "$Id: tconfpy.py,v 1.113 2004/03/14 09:04:52 tundra Exp $"
RCSID = "$Id: tconfpy.py,v 1.114 2004/03/14 09:57:13 tundra Exp $"
VERSION = RCSID.split()[2]
 
# Copyright Information
 
 
INCLUDE = ".include"
ENDIF = ".endif"
IF = ".if"
IFNOT = ".ifnot"
 
Reserved = ["DELIML", "DELIMR", "DOLLAR", "EQUAL", "EQUIV", "NOTEQUIV",
"HASH", "INCLUDE", "ENDIF", "IF"]
"HASH", "INCLUDE", "ENDIF", "IF", "IFNOT"]
 
 
# Regular Expressions
 
###########
 
eERROR = "ERROR"
 
eBADCOND = FILENUM + " Badly Formed Conditional Expression: '%s'"
eBADCOND = FILENUM + " Bad Conditional Expression - %s: '%s'"
eCONFOPEN = "Cannot Open The File '%s'"
eENDIFEXTRA = FILENUM + " '" + ENDIF + "' Without Matching Condition"
eENDIFMISS = FILENUM + " Missing %d '" + ENDIF + "' Statement(s)"
eNOVARREF = "Must Have At Least One Variable Reference"
eVARUNDEF = FILENUM + " " + "Attempt To Reference Undefined Variable '%s'"
 
 
###########
#
# Must be one of the following forms -
#
# .if string
# .ifnot string
# .if string == string
# .if string != string
#
#####
 
if line.startswith(IF):
line = line.split(IF)[1].strip()
# where string can be any concatentation of text and variable references
#
#####
 
if line.startswith(IF) or line.startswith(IFNOT):
 
# NOTE: This next bit of logic will fail if you do not
# test the longer of the two tokens first!!!!
if line.startswith(IFNOT):
line = line.split(IFNOT)[1].strip()
invert = True
else:
line = line.split(IF)[1].strip()
invert = False
 
# Handle Equality Form: ".if string == string"
if line.count(EQUIV):
pass
 
# Handle Inequality Form: ".if string != string"
elif line.count(NOTEQUIV):
pass
 
# Handle Existential Form: ".if string"
# Handle Existential Forms ".if/.ifnot string"
else:
 
# Must be in form: ".if [var]" - trailing garbage ignored
 
chk = VarRef.findall(line)
if line[0] == DELIML and len(chk) > 0:
 
# Warn if there was trailing garbage
if len(line) != len(chk[0]):
WarningMsg(wTRAILING % (cfgfile, linenum, IF + " " + chk[0]))
 
# And condition the line to only contain the variable reference
line = chk[0]
 
# Now see if the variable resolved and set state accordingly
 
# There must be at least one var reference in the condition
if len(VarRef.findall(line)) > 0:
 
# See if the variable(s) resolve
line, ref_ok = DerefVar(line, cfgfile, linenum)
 
# If we're using '.ifnot', invert the sense of the logic
if invert:
ref_ok = not ref_ok
 
# Set parser state accordingly
if ref_ok:
line = TRUE
CondStack.append(True)
else:
CondStack.append(False)
 
# Bogus conditional syntax
else:
ErrorMsg(eBADCOND % (cfgfile, linenum, orig))
 
ErrorMsg(eBADCOND % (cfgfile, linenum, eNOVARREF, orig))
 
line, ref_ok = DerefVar(line, cfgfile, linenum)