# 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
    appendRule('RdxSeq',      'term', 'parseAUTO', 'a number')
    appendRule('alphaNumSeq', 'term', 'parseAUTO', "an alpha-numeric string")
    appendRule('ws',          'term', 'parseAUTO', 'white space')
    appendRule('wsc',         'term', 'parseAUTO', 'white space or C comment')
    appendRule('quotedStr',   'term', 'parseAUTO', "a quoted string with single or double quotes and escapes")
    appendRule('quotedStr1',  'term', 'parseAUTO', "a single quoted string with escapes")
    appendRule('quotedStr2',  'term', 'parseAUTO', "a double quoted string with escapes")
    appendRule('HexNum_str',  'term', 'parseAUTO', "a hexadecimal number")
    appendRule('BinNum_str',  'term', 'parseAUTO', "a binary number")
    appendRule('BigInt',      'term', 'parseAUTO', "an integer")
    appendRule('FlexNum',     'term', 'parseAUTO', "a rational number")
    appendRule('CID',         'term', 'parseAUTO', 'a C-like identifier')
    appendRule('UniID',       'term', 'parseAUTO', 'a unicode identifier')
    appendRule('printables',  'term', 'parseAUTO', "a seqence of printable chars")
    appendRule('toEOL',       'term', 'parseAUTO', "read to End Of Line, including EOL.")
    # TODO: delimited List, keyWord



nextParseNameID=0 # Global used to uniquify sub-seqs and sub-alts in a struct parse. E.g.: ruleName: parse_myStruct_sub1
def fetchOrWriteTerminalParseRule(modelName, field, logLvl):
    global nextParseNameID
    #print "FIELD_IN:", modelName, field
    fieldName='N/A'
    fieldValue=''
    if 'value' in field: fieldValue =field['value']
    typeSpec   = field['typeSpec']
    fieldType  = progSpec.getFieldType(typeSpec)
    fieldOwner = typeSpec['owner']
    if 'fieldName' in field: fieldName  =field['fieldName']

# 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

    appendRule('RdxSeq',      'term', 'parseAUTO', 'a number')
    appendRule('alphaNumSeq', 'term', 'parseAUTO', "an alpha-numeric string")
    appendRule('ws',          'term', 'parseAUTO', 'white space')
    appendRule('wsc',         'term', 'parseAUTO', 'white space or C comment')
    appendRule('quotedStr',   'term', 'parseAUTO', "a quoted string with single or double quotes and escapes")
    appendRule('quotedStr1',  'term', 'parseAUTO', "a single quoted string with escapes")
    appendRule('quotedStr2',  'term', 'parseAUTO', "a double quoted string with escapes")
    appendRule('HexNum_str',  'term', 'parseAUTO', "a hexadecimal number")
    appendRule('BinNum_str',  'term', 'parseAUTO', "a binary number")
    appendRule('BigInt',      'term', 'parseAUTO', "an integer")
    appendRule('FlexNum',     'term', 'parseAUTO', "a rational number")
    appendRule('CID',         'term', 'parseAUTO', 'a C-like identifier')
    appendRule('UniID',       'term', 'parseAUTO', 'a unicode identifier')
    appendRule('printables',  'term', 'parseAUTO', "a seqence of printable chars")
    appendRule('toEOL',       'term', 'parseAUTO', "read to End Of Line, including EOL.")
    # TODO: delimited List, keyWord



nextParseNameID=0 # Global used to uniquify sub-seqs and sub-alts in a struct parse. E.g.: ruleName: parse_myStruct_sub1
def fetchOrWriteTerminalParseRule(modelName, field, logLvl):
    global nextParseNameID
    #print "FIELD_IN:", modelName, field
    fieldName='N/A'
    fieldValue=''
    if 'value' in field: fieldValue =field['value']
    typeSpec   = field['typeSpec']
    fieldType  = progSpec.getFieldType(typeSpec)
    fieldOwner = typeSpec['owner']
    if 'fieldName' in field: fieldName  =field['fieldName']

    cdlog(logLvl, "WRITING PARSE RULE for: {}.{}".format(modelName, fieldName))