# 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)
# 
# def logLvl():
#     return highestLvl+1


import re
import progSpec
from progSpec import cdlog, cdErr, logLvl
from pyparsing import *
#######################################   B A S I C   T Y P E S
expr = Forward()
CID = identifier("CID")
CIDList = Group(delimitedList(CID, ','))("CIDList")
objectName = CID("objectName")
classSpec = Forward()
cppType = Keyword("void") | Keyword("bool") | Keyword("int32") | Keyword("int64") | Keyword("double") | Keyword("char") | Keyword("uint32") | Keyword("uint64") | Keyword("string") | Keyword("int")
HexNums = Combine((Literal("0X") | Literal("0x")) + Word(hexnums))
BinNums = Combine((Literal("0B") | Literal("0b")) + Word("01"))
intNum = HexNums | BinNums | Word(nums)
numRange = Group(intNum + ".." + intNum)("numRange")
varType = classSpec | cppType | numRange
boolValue = Keyword("true") | Keyword("false")
floatNum = Combine(intNum + "." + intNum)("floatNum")
value = Forward()
listVal = "[" + delimitedList(expr, ",") + "]"
strMapVal = "{" + delimitedList(quotedString + ":" + expr, ",")  + "}"
value <<= boolValue | floatNum | intNum | quotedString | listVal | strMapVal
comment = Suppress(r'//') + restOfLine('comment')

#######################################   E X P R E S S I O N S
parameters = Forward()
owners = Forward()
varSpec = Group(Optional(owners)("owner") + varType("varType") )("varSpec")
varSpecList = Group(Optional(delimitedList(varSpec, ',')))("varSpecList")
typeArgList = Group(Literal("<") + CIDList + Literal(">"))("typeArgList")
reqTagList = Group(Suppress(Literal("<")) + varSpecList + Optional(Literal(":")("optionalTag") + tagDefList) + Suppress(Literal(">")))("reqTagList")
classSpec <<= Group(objectName + Optional(reqTagList('reqTagList')))("classSpec")
classDef = Group(objectName + Optional(typeArgList))("classDef")
arrayRef = Group('[' + expr('startOffset') + Optional(( ':' + expr('endOffset')) | ('..' + expr('itemLength'))) + ']')

# 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)
# 
# def logLvl():
#     return highestLvl+1


# Path: codeDogParser.py
import re
import progSpec
from progSpec import cdlog, cdErr, logLvl
from pyparsing import *

#######################################   B A S I C   T Y P E S
expr = Forward()
CID = identifier("CID")
CIDList = Group(delimitedList(CID, ','))("CIDList")
objectName = CID("objectName")
classSpec = Forward()
cppType = Keyword("void") | Keyword("bool") | Keyword("int32") | Keyword("int64") | Keyword("double") | Keyword("char") | Keyword("uint32") | Keyword("uint64") | Keyword("string") | Keyword("int")
HexNums = Combine((Literal("0X") | Literal("0x")) + Word(hexnums))
BinNums = Combine((Literal("0B") | Literal("0b")) + Word("01"))
intNum = HexNums | BinNums | Word(nums)
numRange = Group(intNum + ".." + intNum)("numRange")
varType = classSpec | cppType | numRange
boolValue = Keyword("true") | Keyword("false")
floatNum = Combine(intNum + "." + intNum)("floatNum")
value = Forward()
listVal = "[" + delimitedList(expr, ",") + "]"
strMapVal = "{" + delimitedList(quotedString + ":" + expr, ",")  + "}"
value <<= boolValue | floatNum | intNum | quotedString | listVal | strMapVal
comment = Suppress(r'//') + restOfLine('comment')

#######################################   E X P R E S S I O N S
parameters = Forward()
owners = Forward()
varSpec = Group(Optional(owners)("owner") + varType("varType") )("varSpec")
varSpecList = Group(Optional(delimitedList(varSpec, ',')))("varSpecList")
typeArgList = Group(Literal("<") + CIDList + Literal(">"))("typeArgList")
reqTagList = Group(Suppress(Literal("<")) + varSpecList + Optional(Literal(":")("optionalTag") + tagDefList) + Suppress(Literal(">")))("reqTagList")
classSpec <<= Group(objectName + Optional(reqTagList('reqTagList')))("classSpec")
classDef = Group(objectName + Optional(typeArgList))("classDef")
arrayRef = Group('[' + expr('startOffset') + Optional(( ':' + expr('endOffset')) | ('..' + expr('itemLength'))) + ']')

firstRefSegment = NotAny(owners) + Group((CID | arrayRef) + Optional(parameters))