Changed existential conditionals to be IFALL, IFANY, IFNONE.
Added code to force check for extraneous text on an existential conditional.
1 parent d8d7392 commit 8769dfe2c752ad8c08c2cc93df84dfb9de7b14bd
@tundra tundra authored on 14 Mar 2004
Showing 1 changed file
View
208
tconfpy.py
 
# Program Information
 
PROGNAME = "tconfpy"
RCSID = "$Id: tconfpy.py,v 1.116 2004/03/14 21:03:36 tundra Exp $"
RCSID = "$Id: tconfpy.py,v 1.117 2004/03/15 00:19:56 tundra Exp $"
VERSION = RCSID.split()[2]
 
# Copyright Information
 
CONDINTRO = '.' # Conditional introducer token
INCLUDE = CONDINTRO + "include"
ENDIF = CONDINTRO + "endif"
IF = CONDINTRO + "if"
IFNOT = CONDINTRO + "ifnot"
IFALL = IF + "all"
IFANY = IF + "any"
IFNONE = IF + "none"
 
Reserved = ["DELIML", "DELIMR", "DOLLAR", "EQUAL", "EQUIV", "NOTEQUIV",
"HASH", "INCLUDE", "ENDIF", "IF", "IFNOT"]
"HASH", "INCLUDE", "ENDIF", "IF", "IFALL", "IFANY", "IFNONE"]
 
 
# Regular Expressions
 
###########
# Literals
###########
 
 
TRUE = str(True)
FALSE = str(False)
# String Representations Of Booleans
 
sFALSE = "False"
sNO = "No"
sOFF = "Off"
sON = "On"
sTRUE = "True"
sYES = "Yes"
 
 
#----------------------------------------------------------#
# Global Variables & Data Structures #
###########
 
wWARNING = "WARNING"
 
wEXTRATEXT = FILENUM + " '%s' Statements Only Process Variables. Extra Text Ignored"
wTRAILING = FILENUM + " Trailing Text After '%s' Statement Ignored"
 
 
#--------------------------- Code Begins Here ---------------------------------#
if reporterr:
ErrorMsg(eVARUNDEF % (cfgfile, linenum, sym))
 
ref_ok = False
 
return line, ref_ok
 
# End of 'DerefVar()'
 
 
if line.startswith(INCLUDE):
ParseFile(line.split(INCLUDE)[1].strip())
 
#####
# .if Processing
 
#####
# Conditional Processing
#
# Must be one of the following forms -
#
# .if string
# .ifnot string
# .if string == string
# .if string != string
# IFALL [var] ...
# IFANY [var] ...
# IFNONE [var] ...
# IF string == string
# IF string != string
#
# where string can be any concatentation of text and variable references
#
#####
 
if line.startswith(IFNOT) or line.startswith(IF):
 
# NOTE: This next bit of logic will fail if you do not
# test the longest of the 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 Forms ".if/.ifnot string"
else:
if line.startswith(IF):
 
# Since all conditional lines start with IF,
# test for this case last - i.e. test the for
# longest tokens first.
 
#####
# Existential Conditionals
#####
 
if line.startswith(IFALL) or line.startswith(IFANY) or line.startswith(IFNONE):
 
if line.startswith(IFALL):
IFTYPE = IFALL
line = line.split(IFALL)[1].strip()
 
elif line.startswith(IFANY):
IFTYPE = IFANY
line = line.split(IFANY)[1].strip()
 
else:
IFTYPE = IFNONE
line = line.split(IFNONE)[1].strip()
 
# 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, reporterr=False)
 
# 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:
line = FALSE
CondStack.append(False)
 
vars = VarRef.findall(line)
if len(vars) > 0:
 
# Only variable references are significant - warn on other text.
 
varlen = 0
plain = line
for v in vars:
varlen += len(v)
plain.replace(v, "")
 
if len(plain.strip()):
WarningMsg(wEXTRATEXT % (cfgfile, linenum, IFTYPE))
 
# Go see how many references actually resolve
resolved = 0
for v in vars:
v, ref_ok = DerefVar(v, cfgfile, linenum, reporterr=False)
if ref_ok:
resolved += 1
 
# And set state accordingly
 
state = sTRUE
if IFTYPE == IFALL and len(vars) != resolved:
state = sFALSE
 
if IFTYPE == IFANY and not resolved:
state = sFALSE
 
if IFTYPE == IFNONE and resolved:
state = sFALSE
 
CondStack.append(state)
line = state
 
# Bogus conditional syntax
else:
ErrorMsg(eBADCOND % (cfgfile, linenum, eNOVARREF, orig))
 
 
line, ref_ok = DerefVar(line, cfgfile, linenum)
 
#####
# (In)Equality Conditionals
#####
else:
line = line.split(IF)[1].strip()
# Handle Equality Form: ".if string == string"
if line.count(EQUIV):
pass
 
# Handle InEquality Form: ".if string != string"
elif line.count(NOTEQUIV):
pass
 
#####
# Handle New Variable Declaration/Assignment
#####
 
else:
line, ref_ok = DerefVar(line, cfgfile, linenum)
 
##########
# End Of Line Parser
# Note blank lines for debug purposes
if not line:
DebugMsg(dLINEIGNORE % (cfgfile, linenum, orig, dBLANKLINE))
 
# All non-blank lines noted here
else:
DebugMsg(dPARSEDLINE % (cfgfile, linenum, orig, line))