diff --git a/tconfpy.py b/tconfpy.py index 97aaaf3..9d29857 100755 --- a/tconfpy.py +++ b/tconfpy.py @@ -6,7 +6,7 @@ # Program Information PROGNAME = "tconfpy" -RCSID = "$Id: tconfpy.py,v 1.119 2004/03/16 10:35:47 tundra Exp $" +RCSID = "$Id: tconfpy.py,v 1.120 2004/03/19 10:16:16 tundra Exp $" VERSION = RCSID.split()[2] # Copyright Information @@ -63,8 +63,8 @@ # Formatting Constants MSGPROMPT = "%s>" -FILENUM = "[File: %s Line: %s]" # Display filename and linenum -PTR = " ---> " # Textual pointer for debug output +FILENUM = "[File: %s Line: %s] " # Display filename and linenum +PTR = " ---> " # Textual pointer for debug output # Special And Reserved Symbols @@ -202,9 +202,10 @@ # Literals dDEBUG = "DEBUG" +dLINEIGNORE = "Line Ignored" -dBLANKLINE = "Parsed To Blank Line - Ignored" -dNOTINCLUDE = "Current Conditional Block False: Line Not Included" +dBLANKLINE = "Parsed To Blank Line. %s" % dLINEIGNORE +dNOTINCLUDE = "Current Conditional Block False. %s" % dLINEIGNORE # Messages @@ -220,20 +221,18 @@ # Literals eERROR = "ERROR" -eSYNTAX = "Incorrect '%s' Syntax" -eENDIFBAD = eSYNTAX % ENDIF -eIFBAD = eSYNTAX % IF -eINCLBAD = eSYNTAX % INCLUDE +eIFBAD = "'%s' Or '%s' Missing" % (EQUIV, NOTEQUIV) eNOVARREF = "Must Have At Least One Variable Reference" # Messages -Messages["eBADCOND"] = FILENUM + " Bad Directive - %s %s" +Messages["eBADCOND"] = FILENUM + "Bad '%s' Directive. %s" +Messages["eBADSYNTAX"] = FILENUM + "Syntax Error. Statement Not In Known Form" Messages["eCONFOPEN"] = "Cannot Open The File '%s'" -Messages["eENDIFEXTRA"] = FILENUM + " '" + ENDIF + "' Without Matching Condition" -Messages["eENDIFMISS"] = FILENUM + " Missing %d '" + ENDIF + "' Statement(s)" -Messages["eVARUNDEF"] = FILENUM + " " + "Attempt To Reference Undefined Variable '%s'" +Messages["eENDIFEXTRA"] = FILENUM + "'" + ENDIF + "' Without Matching Condition" +Messages["eENDIFMISS"] = FILENUM + "Missing %d '" + ENDIF + "' Statement(s)" +Messages["eVARUNDEF"] = FILENUM + "Attempt To Reference Undefined Variable '%s'" ########### @@ -478,8 +477,9 @@ global CondStack, MsgList, SymTable - orig = line # May need copy of original for debug output + orig = line # May need copy of original for debug output line = ConditionLine(line) + condstate = True # Results of conditional tests kept here ########## # Beginning Of Line Parser @@ -492,21 +492,17 @@ FIRSTTOK = line.split()[0] ##### - # .endif Processing - Must be done before state check - # because .endif can change parser state + # ENDIF Processing - Must be done before state check + # because ENDIF can change parser state ##### - if line.startswith(ENDIF): - - # Check for space after conditional - if line.split()[0] != ENDIF: - ErrorMsg("eBADCOND", (cfgfile, linenum, eENDIFBAD, "'%s'" % orig)) + if FIRSTTOK == ENDIF: # This should be the only thing on the line - elif line != ENDIF: + if line != ENDIF: WarningMsg("wTRAILING", (cfgfile, linenum, ENDIF)) - # Remove one level of nesting + # Remove one level of conditional nesting CondStack.pop() # Error, if there are more .endifs than conditionals @@ -525,17 +521,14 @@ return ##### - # .include Processing + # INCLUDE Processing ##### - if line.startswith(INCLUDE): + if FIRSTTOK == INCLUDE: + line, ref_ok = DerefVar(line.split(INCLUDE)[1].strip(), cfgfile, linenum) - # Make sure a space follows the directive - if line.split()[0] != INCLUDE: - ErrorMsg("eBADCOND", (cfgfile, linenum, eINCLBAD, "'%s'" % orig)) - - else: - ParseFile(line.split(INCLUDE)[1].strip()) + if ref_ok: + ParseFile(line) ##### @@ -555,20 +548,18 @@ # Is it any of the IF forms? - if FIRSTTOK.startswith(IF): + elif FIRSTTOK in (IF, IFALL, IFANY, IFNONE): - IFTYPE = FIRSTTOK - ##### # Existential Conditionals ##### - if IFTYPE in (IFALL, IFANY, IFNONE): + if FIRSTTOK in (IFALL, IFANY, IFNONE): - if IFTYPE == IFALL: + if FIRSTTOK == IFALL: line = line.split(IFALL)[1].strip() - elif IFTYPE == IFANY: + elif FIRSTTOK == IFANY: line = line.split(IFANY)[1].strip() else: @@ -589,7 +580,7 @@ plain=plain.replace(v, "") if len(plain.strip()): - WarningMsg("wEXTRATEXT", (cfgfile, linenum, IFTYPE)) + WarningMsg("wEXTRATEXT", (cfgfile, linenum, FIRSTTOK)) if vars: @@ -601,61 +592,84 @@ if ref_ok: resolved += 1 - # And set state accordingly + # And set the conditional state accordingly - state = True - if IFTYPE == IFALL and len(vars) != resolved: - state = False + if FIRSTTOK == IFALL and len(vars) != resolved: + condstate = False - if IFTYPE == IFANY and not resolved: - state = False + if FIRSTTOK == IFANY and not resolved: + condstate = False - if IFTYPE == IFNONE and resolved: - state = False - - CondStack.append(state) - - # Now reflect this in the parsed line - line = sTRUE - if not state: - line = sFALSE + if FIRSTTOK == IFNONE and resolved: + condstate = False # Bogus conditional syntax - no variable refs found else: - ErrorMsg("eBADCOND", (cfgfile, linenum, "'%s'" % orig, eNOVARREF)) + ErrorMsg("eBADCOND", (cfgfile, linenum, FIRSTTOK, eNOVARREF)) + + # Force parse state to False on an error + condstate = False ##### - # (In)Equality Conditionals + # (In)Equality Conditionals - IF string ==/!= string forms ##### - # Check for IF followed by mandatory whitespace - elif line.split()[0] == IF: + else: line = line.split(IF)[1].strip() - # Handle Equality Form: ".if string == string" - if line.count(EQUIV): + # Handle Equality Form: ".IF string == string" + if EQUIV in line: pass - # Handle InEquality Form: ".if string != string" - elif line.count(NOTEQUIV): + # Handle InEquality Form: "IF string != string" + elif NOTEQUIV in line: pass - ##### - # Line Started With IF, But Was Not In Any Known IF Form - ##### + # Conditional Syntax Error + else: + ErrorMsg("eBADCOND", (cfgfile, linenum, FIRSTTOK, eIFBAD)) - else: - ErrorMsg("eBADCOND", (cfgfile, linenum, eIFBAD, "'%s'" % orig)) + # Force parse state to False on an error + condstate = False + + # Set parser state based on a successful conditional test + + CondStack.append(condstate) + + # Now reflect this in the parsed line + line = sTRUE + if not condstate: + line = sFALSE + + ##### # Handle New Variable Declaration/Assignment ##### - else: + # If we got here it means that none of the conditional forms + # were found, so the only thing left might be a variable + # definition/assignment. + + elif EQUAL in line: line, ref_ok = DerefVar(line, cfgfile, linenum) + + + ##### + # Line Format Is Not In One Of The Recognized Forms - Syntax Error + ##### + + # To keep the code structure clean, the ENDIF and INCLUDE + # processing above falls through to here as well. We ignore + # it because any problems with these directives have already been + # handled. + + elif FIRSTTOK not in (ENDIF, INCLUDE): + ErrorMsg("eBADSYNTAX", (cfgfile, linenum)) + ########## # End Of Line Parser ##########