# Path: progSpec.py
# def cdlog(lvl, mesg):
#     global MaxLogLevelToShow
#     global lastLogMesgs
#     global highestLvl
#     highestLvl=lvl
#     resizeLogArray(lvl)
#     lastLogMesgs[lvl]=mesg
#     for i in range(highestLvl+1, len(lastLogMesgs)):
#         lastLogMesgs[i]=''
#     if(lvl<=MaxLogLevelToShow):
#         if(lvl==0): print('')
#         printAtLvl(lvl, mesg, '|    ')
# 
# def cdErr(mesg):
#     global lastLogMesgs
#     global highestLvl
#     highestLvl+=1
#     lastLogMesgs[highestLvl]="\n\nError: "+mesg
#     exit(1)


import re
import progSpec
import codeDogParser
from progSpec import cdlog, cdErr
    nameForFunc=getFunctionName(FromStructName, ToStructName)
    cdlog(logLvl, "WRITING function {}() to extract struct {} from parse tree: stage 1...".format(nameForFunc, ToStructName))
    if nameForFunc in alreadyWrittenFunctions: return
    alreadyWrittenFunctions[nameForFunc]=True
    S=''
    ObjectDef = progSpec.findSpecOf(classes[0], FromStructName, 'string')
    fields=ObjectDef["fields"]
    configType=ObjectDef['configType']
    SeqOrAlt=''
    if configType=='SEQ': SeqOrAlt='parseSEQ'
    elif configType=='ALT': SeqOrAlt='parseALT'
    cdlog(logLvl, "WRITING function {}() to extract struct {} from parse tree: stage 2...".format(nameForFunc, ToStructName))
    if configType=='SEQ':
        S+=Write_structExtracter(classes, ToStructName, FromStructName, fields, nameForFunc, logLvl)
    elif configType=='ALT':
        S+=Write_ALT_Extracter(classes, ToStructName, fields, 'SRec', '', 'tmpStr', '    ', -1, logLvl)

    seqExtracter =  "\n    void: "+nameForFunc+"(our stateRec: SRec0, their "+ToStructName+": memStruct, their EParser: EP) <- {\n" + S + "    }\n"
    extracterFunctionAccumulator += seqExtracter
    #print "########################## extracterFunctionAccumulator\n",extracterFunctionAccumulator,"\n#####################################\n"

def writeParserWrapperFunction(classes, className):
    S='''
struct EParser{
    me uint64: sourceID

    our <CLASSNAME>: Parse_<CLASSNAME>(me string: textIn) <- {
        our <CLASSNAME>: result
        initParseFromString("<CLASSNAME>", textIn)
        doParse()

# Path: progSpec.py
# def cdlog(lvl, mesg):
#     global MaxLogLevelToShow
#     global lastLogMesgs
#     global highestLvl
#     highestLvl=lvl
#     resizeLogArray(lvl)
#     lastLogMesgs[lvl]=mesg
#     for i in range(highestLvl+1, len(lastLogMesgs)):
#         lastLogMesgs[i]=''
#     if(lvl<=MaxLogLevelToShow):
#         if(lvl==0): print('')
#         printAtLvl(lvl, mesg, '|    ')
# 
# def cdErr(mesg):
#     global lastLogMesgs
#     global highestLvl
#     highestLvl+=1
#     lastLogMesgs[highestLvl]="\n\nError: "+mesg
#     exit(1)


# Path: stringStructs.py
import re
import progSpec
import codeDogParser
from progSpec import cdlog, cdErr

    nameForFunc=getFunctionName(FromStructName, ToStructName)
    cdlog(logLvl, "WRITING function {}() to extract struct {} from parse tree: stage 1...".format(nameForFunc, ToStructName))
    if nameForFunc in alreadyWrittenFunctions: return
    alreadyWrittenFunctions[nameForFunc]=True
    S=''
    ObjectDef = progSpec.findSpecOf(classes[0], FromStructName, 'string')
    fields=ObjectDef["fields"]
    configType=ObjectDef['configType']
    SeqOrAlt=''
    if configType=='SEQ': SeqOrAlt='parseSEQ'
    elif configType=='ALT': SeqOrAlt='parseALT'
    cdlog(logLvl, "WRITING function {}() to extract struct {} from parse tree: stage 2...".format(nameForFunc, ToStructName))
    if configType=='SEQ':
        S+=Write_structExtracter(classes, ToStructName, FromStructName, fields, nameForFunc, logLvl)
    elif configType=='ALT':
        S+=Write_ALT_Extracter(classes, ToStructName, fields, 'SRec', '', 'tmpStr', '    ', -1, logLvl)

    seqExtracter =  "\n    void: "+nameForFunc+"(our stateRec: SRec0, their "+ToStructName+": memStruct, their EParser: EP) <- {\n" + S + "    }\n"
    extracterFunctionAccumulator += seqExtracter
    #print "########################## extracterFunctionAccumulator\n",extracterFunctionAccumulator,"\n#####################################\n"

def writeParserWrapperFunction(classes, className):
    S='''
struct EParser{
    me uint64: sourceID

    our <CLASSNAME>: Parse_<CLASSNAME>(me string: textIn) <- {
        our <CLASSNAME>: result
        initParseFromString("<CLASSNAME>", textIn)
        doParse()

        if (doesParseHaveError()) {