Advertisement
Guest User

python_mini_C_Parser

a guest
Apr 9th, 2012
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.45 KB | None | 0 0
  1. import sys
  2. from operator import itemgetter, attrgetter
  3. from xml.etree.ElementTree import ElementTree
  4.  
  5. # parsing config
  6. CONFIG_FILE = "cparser.xml"
  7.  
  8. tr = ElementTree()
  9. m=tr.parse(CONFIG_FILE)
  10. path = m.find("includeDir").text
  11. startXML = ""
  12. sXML = m.find("startXML")
  13. oXML = m.find("outputXML").text
  14. if sXML.attrib["active"].lower() == "true":
  15.     startXML = sXML.text
  16. # parsing config
  17.  
  18.  
  19. arr = []
  20. defineID = 2
  21. funcID = 1
  22.  
  23. def trim(l):
  24.     nl = []
  25.     for i in l:
  26.         if len(i) > 0:
  27.             nl.append(i)
  28.     return nl
  29.  
  30. def stripList(l):
  31.     for i in range(0,len(l)):
  32.         l[i] = l[i].strip()
  33.     return l
  34.  
  35. def contains(lst, srch):
  36.     for x in lst:
  37.         if x == srch:return True
  38.     return False
  39.  
  40. def filter_existant(ar):
  41.     tmp = []
  42.     apTmp = []
  43.     for x in range(0,len(ar)):
  44.         if not contains(tmp, ar[x][1]):
  45.             apTmp.append(ar[x])
  46.             tmp.append(ar[x][1])
  47.     return apTmp
  48.    
  49.  
  50.  
  51.  
  52. tree = ElementTree()
  53. try:
  54.     if len(startXML) != 0:
  55.         m=tree.parse(startXML)
  56.         m=m.find("NotepadPlus")
  57.         l=list(m.iter("KeyWord"))
  58.         for x in l:
  59.             name = x.attrib["name"]
  60.             func = funcID if x.attrib.__contains__("func") else defineID
  61.             retval = ""
  62.             parm = []
  63.             if func:
  64.                 ov = x.find("Overload")
  65.                 if ov is not None:
  66.                     retval = ov.attrib["retVal"]
  67.                     p = list(ov.iter("Param"))
  68.                     for i in p:
  69.                         parm.append(i.attrib["name"])
  70.             arr.append((func, name, retval, parm))
  71. except:
  72.     pass
  73.    
  74. def parseDefine(s):
  75.     global arr, defineID, funcID
  76.     ls = trim(s.split(" "))
  77.     arr.append((defineID, ls[1], '' if len(ls)<3 else ls[2], []))
  78. def parseFunc(s):
  79.     global arr, defineID, funcID
  80.     l = s.split("(")
  81.     ps = l[1].split(")")
  82.     ps = ')'.join(ps[:len(ps)-1]).split(",")
  83.     stripList(ps)
  84.     l[0] = l[0].replace(chr(0x9), ' ')
  85.     tl = trim(l[0].split(" "))
  86.     try:
  87.         arr.append( (funcID, tl[len(tl)-1], ' '.join(tl[0:len(tl)-1]), ps) )
  88.     except:
  89.         if len(s) == 2:
  90.             arr.append( (funcID, tl[len(tl)-1], ' ', []) )
  91.  
  92. def parseC(file):
  93.     global arr
  94.     nav = False
  95.     ncom = 0
  96.     stInd = 0
  97.     ndef = False
  98.     nusl = 0
  99.     s = ""
  100.     zag = 0
  101.     bTyp = False
  102.     nZag2 = 0
  103.     bInc = False
  104.    
  105.     try:
  106.         fl = open(file, "r")
  107.         print("parsing file: "+file)
  108.     except:
  109.         return
  110.     f = fl.read()
  111.     fl.close()
  112.    
  113.     for i in range(0,len(f)):
  114.            
  115.         if f[i:i+2] == "/*":
  116.             ncom = 1
  117.             i+=1
  118.             continue
  119.            
  120.         if f[i:i+2] == "*/":
  121.             ncom = 0
  122.             i+=1
  123.             stInd = i+1
  124.             continue
  125.  
  126.         if ncom == 1:
  127.             continue
  128.            
  129.         if f[i]=="{" and f[stInd:i].find("extern")==-1:
  130.             nZag2 += 1
  131.         if f[i]=="}":
  132.             nZag2 -= 1
  133.            
  134.         if nZag2 > 0:
  135.             continue
  136.            
  137.         if f[i]!="\"" and nav:
  138.             continue
  139.            
  140.         if f[i:i+7] == "#define":
  141.             stInd = i
  142.             ndef = True
  143.            
  144.         if f[i:i+8] == "#include":
  145.             bInc = True
  146.             stInd = i
  147.            
  148.         if f[i]=="#":
  149.             nusl = 1
  150.            
  151.         if f[i]==";":
  152.             if zag and not bTyp:
  153.                 s = f[stInd:i]
  154.                 parseFunc(s)
  155.                 zag = False
  156.             else:
  157.                 bTyp = False
  158.             stInd = i+2
  159.            
  160.         if f[i]==")" and zag>0 and not ndef and not nusl and not bTyp:
  161.             zag -= 1
  162.             if zag == 0:
  163.                 s = f[stInd:i+1]
  164.                 parseFunc(s)
  165.                 zag = False
  166.                 stInd = i+1
  167.            
  168.         if f[i]=="\"":
  169.             nav^=True
  170.            
  171.         if (f[i]=="\r" or f[i]=="\n"):
  172.             nusl = 0
  173.             if bInc:
  174.                 s = f[stInd:i]
  175.                 s1 = s.split("<")
  176.                 if len(s1) > 1:
  177.                     parseC(path+s1[1].split(">")[0].strip())
  178.                 else:
  179.                     spl = s.split("\"")
  180.                     if len(spl) == 3:
  181.                         parseC(spl[1])
  182.             if ndef:
  183.                 ndef = False
  184.                 s = f[stInd:i]
  185.                 try:
  186.                     parseDefine(s)
  187.                 except:
  188.                     pass
  189.                 s = ""
  190.                 continue
  191.             stInd = i+1
  192.            
  193.        
  194.         if f[i] == "(" and not ndef and not nusl:
  195.             zag += 1
  196.            
  197.         if f[i:i+7] == "typedef" and not nusl and not ndef:
  198.             stInd = i
  199.             bTyp = True
  200.    
  201. parseC(sys.argv[1])
  202.  
  203. arr = filter_existant(arr)
  204. arr = sorted(arr, key=lambda x: x[1].upper())
  205.  
  206. # export
  207. fl = open(oXML, "w")
  208. fl.write("<?xml version=\"1.0\" encoding=\"Windows-1252\" ?>\n")
  209. fl.write("<NotepadPlus>\n")
  210. fl.write("\t<AutoComplete language=\"C\">\n")
  211. fl.write("\t\t<Environment ignoreCase=\"yes\" startFunc=\"(\" stopFunc=\")\" paramSeparator=\",\" terminal=\";\" />\n")
  212. for i in arr:
  213.     if(i[0] == defineID):
  214.         fl.write("\t\t\t<KeyWord name=\""+i[1]+"\"/>\n")
  215.     if(i[0] == funcID):
  216.         fl.write("\t\t\t<KeyWord name=\""+i[1]+"\" func=\"yes\">\n")
  217.         fl.write("\t\t\t\t<Overload retVal=\""+i[2].replace('\n', ' ')+"\">\n")
  218.         for j in i[3]:
  219.             fl.write("\t\t\t\t\t<Param name=\""+j.strip()+"\"/>\n")
  220.            
  221.         fl.write("\t\t\t\t</Overload>\n")
  222.         fl.write("\t\t\t</KeyWord>\n")
  223.        
  224. fl.write("\t</AutoComplete>\n")
  225. fl.write("</NotepadPlus>\n")
  226. fl.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement