2
0

CppHeaderParser.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. #!/usr/bin/python
  2. #
  3. # Author: Jashua R. Cloutier (contact via sourceforge username:senexcanis)
  4. #
  5. # Copyright (C) 2010, Jashua R. Cloutier
  6. # All rights reserved.
  7. #
  8. # Redistribution and use in source and binary forms, with or without
  9. # modification, are permitted provided that the following conditions
  10. # are met:
  11. #
  12. # * Redistributions of source code must retain the above copyright
  13. # notice, this list of conditions and the following disclaimer.
  14. #
  15. # * Redistributions in binary form must reproduce the above copyright
  16. # notice, this list of conditions and the following disclaimer in
  17. # the documentation and/or other materials provided with the
  18. # distribution.
  19. #
  20. # * Neither the name of Jashua R. Cloutier nor the names of its
  21. # contributors may be used to endorse or promote products derived from
  22. # this software without specific prior written permission.
  23. #
  24. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. # POSSIBILITY OF SUCH DAMAGE.
  36. #
  37. #
  38. # The CppHeaderParser.py script is written in Python 2.4 and released to
  39. # the open source community for continuous improvements under the BSD
  40. # 2.0 new license, which can be found at:
  41. #
  42. # http://www.opensource.org/licenses/bsd-license.php
  43. #
  44. """Parse C++ header files and generate a data structure
  45. representing the class
  46. """
  47. import ply.lex as lex
  48. import os
  49. import sys
  50. import re
  51. import inspect
  52. def lineno():
  53. """Returns the current line number in our program."""
  54. return inspect.currentframe().f_back.f_lineno
  55. __version__ = "1.9"
  56. version = "1.9"
  57. tokens = [
  58. 'NUMBER',
  59. 'NAME',
  60. 'OPEN_PAREN',
  61. 'CLOSE_PAREN',
  62. 'OPEN_BRACE',
  63. 'CLOSE_BRACE',
  64. 'COLON',
  65. 'SEMI_COLON',
  66. 'COMMA',
  67. 'COMMENT_SINGLELINE',
  68. 'COMMENT_MULTILINE',
  69. 'PRECOMP_MACRO',
  70. 'PRECOMP_MACRO_CONT',
  71. 'ASTERISK',
  72. 'AMPERSTAND',
  73. 'EQUALS',
  74. 'MINUS',
  75. 'PLUS',
  76. 'DIVIDE',
  77. 'CHAR_LITERAL',
  78. 'STRING_LITERAL',
  79. 'OPERATOR_DIVIDE_OVERLOAD',
  80. 'NEW_LINE',
  81. ]
  82. t_ignore = " \t\r[].|!?%@"
  83. t_NUMBER = r'[0-9][0-9XxA-Fa-f]*'
  84. t_NAME = r'[<>A-Za-z_~][A-Za-z0-9_]*'
  85. t_OPERATOR_DIVIDE_OVERLOAD = r'/='
  86. t_OPEN_PAREN = r'\('
  87. t_CLOSE_PAREN = r'\)'
  88. t_OPEN_BRACE = r'{'
  89. t_CLOSE_BRACE = r'}'
  90. t_SEMI_COLON = r';'
  91. t_COLON = r':'
  92. t_COMMA = r','
  93. t_PRECOMP_MACRO = r'\#.*'
  94. t_PRECOMP_MACRO_CONT = r'.*\\\n'
  95. def t_COMMENT_SINGLELINE(t):
  96. r'\/\/.*\n'
  97. global doxygenCommentCache
  98. if t.value.startswith("///") or t.value.startswith("//!"):
  99. if doxygenCommentCache:
  100. doxygenCommentCache += "\n"
  101. if t.value.endswith("\n"):
  102. doxygenCommentCache += t.value[:-1]
  103. else:
  104. doxygenCommentCache += t.value
  105. t_ASTERISK = r'\*'
  106. t_MINUS = r'\-'
  107. t_PLUS = r'\+'
  108. t_DIVIDE = r'/[^/]'
  109. t_AMPERSTAND = r'&'
  110. t_EQUALS = r'='
  111. t_CHAR_LITERAL = "'.'"
  112. #found at http://wordaligned.org/articles/string-literals-and-regular-expressions
  113. #TODO: This does not work with the string "bla \" bla"
  114. t_STRING_LITERAL = r'"([^"\\]|\\.)*"'
  115. #Found at http://ostermiller.org/findcomment.html
  116. def t_COMMENT_MULTILINE(t):
  117. r'/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/'
  118. global doxygenCommentCache
  119. if t.value.startswith("/**") or t.value.startswith("/*!"):
  120. #not sure why, but get double new lines
  121. v = t.value.replace("\n\n", "\n")
  122. #strip prefixing whitespace
  123. v = re.sub("\n[\s]+\*", "\n*", v)
  124. doxygenCommentCache += v
  125. def t_NEWLINE(t):
  126. r'\n+'
  127. t.lexer.lineno += len(t.value)
  128. def t_error(v):
  129. print "Lex error: ", v
  130. lex.lex()
  131. debug = 0
  132. supportedAccessSpecifier = [
  133. 'public',
  134. 'protected',
  135. 'private'
  136. ]
  137. doxygenCommentCache = ""
  138. def is_namespace(nameStack):
  139. """Determines if a namespace is being specified"""
  140. if len(nameStack) == 0:
  141. return False
  142. if nameStack[0] == "namespace":
  143. return True
  144. return False
  145. def is_enum_namestack(nameStack):
  146. """Determines if a namestack is an enum namestack"""
  147. if len(nameStack) == 0:
  148. return False
  149. if nameStack[0] == "enum":
  150. return True
  151. if len(nameStack) > 1 and nameStack[0] == "typedef" and nameStack[1] == "enum":
  152. return True
  153. return False
  154. class CppParseError(Exception): pass
  155. class CppClass(dict):
  156. """Takes a name stack and turns it into a class
  157. Contains the following Keys:
  158. self['name'] - Name of the class
  159. self['doxygen'] - Doxygen comments associated with the class if they exist
  160. self['inherits'] - List of Classes that this one inherits where the values
  161. are of the form {"access": Anything in supportedAccessSpecifier
  162. "class": Name of the class
  163. self['methods'] - Dictionary where keys are from supportedAccessSpecifier
  164. and values are a lists of CppMethod's
  165. self['properties'] - Dictionary where keys are from supportedAccessSpecifier
  166. and values are lists of CppVariable's
  167. self['enums'] - Dictionary where keys are from supportedAccessSpecifier and
  168. values are lists of CppEnum's
  169. An example of how this could look is as follows:
  170. #self =
  171. {
  172. 'name': ""
  173. 'inherits':[]
  174. 'methods':
  175. {
  176. 'public':[],
  177. 'protected':[],
  178. 'private':[]
  179. },
  180. 'properties':
  181. {
  182. 'public':[],
  183. 'protected':[],
  184. 'private':[]
  185. },
  186. 'enums':
  187. {
  188. 'public':[],
  189. 'protected':[],
  190. 'private':[]
  191. }
  192. }
  193. """
  194. def __init__(self, nameStack):
  195. if (debug): print "Class: ", nameStack
  196. if (len(nameStack) < 2):
  197. print "Error detecting class"
  198. return
  199. global doxygenCommentCache
  200. if len(doxygenCommentCache):
  201. self["doxygen"] = doxygenCommentCache
  202. doxygenCommentCache = ""
  203. self["name"] = nameStack[1]
  204. inheritList = []
  205. if ":" in nameStack:
  206. nameStack = nameStack[nameStack.index(":") + 1:]
  207. while len(nameStack):
  208. tmpStack = []
  209. tmpInheritClass = {"access":"private"}
  210. if "," in nameStack:
  211. tmpStack = nameStack[:nameStack.index(",")]
  212. nameStack = nameStack[nameStack.index(",") + 1:]
  213. else:
  214. tmpStack = nameStack
  215. nameStack = []
  216. if len(tmpStack) == 0:
  217. break;
  218. elif len(tmpStack) == 1:
  219. tmpInheritClass["class"] = tmpStack[0]
  220. elif len(tmpStack) == 2:
  221. tmpInheritClass["access"] = tmpStack[0]
  222. tmpInheritClass["class"] = tmpStack[1]
  223. else:
  224. print "Warning: Cant figure out class inheriting %s\n"%(" ".join(tmpStack))
  225. continue
  226. inheritList.append(tmpInheritClass)
  227. methodAccessSpecificList = {}
  228. propertyAccessSpecificList = {}
  229. enumAccessSpecificList = {}
  230. for accessSpecifier in supportedAccessSpecifier:
  231. methodAccessSpecificList[accessSpecifier] = []
  232. propertyAccessSpecificList[accessSpecifier] = []
  233. enumAccessSpecificList[accessSpecifier] = []
  234. self['inherits'] = inheritList
  235. self['methods'] = methodAccessSpecificList
  236. self['properties'] = propertyAccessSpecificList
  237. self['enums'] = enumAccessSpecificList
  238. self['namespace'] = ""
  239. def __repr__(self):
  240. """Convert class to a string"""
  241. namespace_prefix = ""
  242. if self["namespace"]: namespace_prefix = self["namespace"] + "::"
  243. rtn = "class %s\n"%(namespace_prefix + self["name"])
  244. try:
  245. print self["doxygen"],
  246. except: pass
  247. if "inherits" in self.keys():
  248. rtn += "Inherits: "
  249. for inheritClass in self["inherits"]:
  250. rtn += "%s %s, "%(inheritClass["access"], inheritClass["class"])
  251. rtn += "\n"
  252. rtn += "{\n"
  253. for accessSpecifier in supportedAccessSpecifier:
  254. rtn += "%s\n"%(accessSpecifier)
  255. #Enums
  256. if (len(self["enums"][accessSpecifier])):
  257. rtn += " // Enums\n"
  258. for enum in self["enums"][accessSpecifier]:
  259. rtn += " %s\n"%(repr(enum))
  260. #Properties
  261. if (len(self["properties"][accessSpecifier])):
  262. rtn += " // Properties\n"
  263. for property in self["properties"][accessSpecifier]:
  264. rtn += " %s\n"%(repr(property))
  265. #Methods
  266. if (len(self["methods"][accessSpecifier])):
  267. rtn += " // Method\n"
  268. for method in self["methods"][accessSpecifier]:
  269. rtn += " %s\n"%(repr(method))
  270. rtn += "}\n"
  271. return rtn
  272. class CppMethod(dict):
  273. """Takes a name stack and turns it into a method
  274. Contains the following Keys:
  275. self['rtnType'] - Return type of the method (ex. "int")
  276. self['name'] - Name of the method (ex. "getSize")
  277. self['doxygen'] - Doxygen comments associated with the method if they exist
  278. self['parameters'] - List of CppVariables
  279. """
  280. def __init__(self, nameStack, curClass):
  281. if (debug): print "Method: ", nameStack
  282. global doxygenCommentCache
  283. if len(doxygenCommentCache):
  284. self["doxygen"] = doxygenCommentCache
  285. doxygenCommentCache = ""
  286. if "operator" in nameStack:
  287. self["rtnType"] = " ".join(nameStack[:nameStack.index('operator')])
  288. self["name"] = "".join(nameStack[nameStack.index('operator'):nameStack.index('(')])
  289. else:
  290. self["rtnType"] = " ".join(nameStack[:nameStack.index('(') - 1])
  291. self["name"] = " ".join(nameStack[nameStack.index('(') - 1:nameStack.index('(')])
  292. if len(self["rtnType"]) == 0 or self["name"] == curClass:
  293. self["rtnType"] = "void"
  294. paramsStack = nameStack[nameStack.index('(') + 1: ]
  295. #Remove things from the stack till we hit the last paren, this helps handle abstract and normal methods
  296. while (paramsStack[-1] != ")"):
  297. paramsStack.pop()
  298. paramsStack.pop()
  299. params = []
  300. #See if there is a doxygen comment for the variable
  301. doxyVarDesc = {}
  302. #TODO: Put this into a class
  303. if self.has_key("doxygen"):
  304. doxyLines = self["doxygen"].split("\n")
  305. lastParamDesc = ""
  306. for doxyLine in doxyLines:
  307. if " @param " in doxyLine or " \param " in doxyLine:
  308. try:
  309. #Strip out the param
  310. doxyLine = doxyLine[doxyLine.find("param ") + 6:]
  311. (var, desc) = doxyLine.split(" ", 1)
  312. doxyVarDesc[var] = desc.strip()
  313. lastParamDesc = var
  314. except: pass
  315. elif " @return " in doxyLine or " \return " in doxyLine:
  316. lastParamDesc = ""
  317. # not handled for now
  318. elif lastParamDesc:
  319. try:
  320. doxyLine = doxyLine.strip()
  321. if " " not in doxyLine:
  322. lastParamDesc = ""
  323. continue
  324. doxyLine = doxyLine[doxyLine.find(" ") + 1:]
  325. doxyVarDesc[lastParamDesc] += " " + doxyLine
  326. except: pass
  327. #Create the variable now
  328. while (len(paramsStack)):
  329. if (',' in paramsStack):
  330. params.append(CppVariable(paramsStack[0:paramsStack.index(',')], doxyVarDesc=doxyVarDesc))
  331. paramsStack = paramsStack[paramsStack.index(',') + 1:]
  332. else:
  333. param = CppVariable(paramsStack, doxyVarDesc=doxyVarDesc)
  334. if len(param.keys()):
  335. params.append(param)
  336. break
  337. self["parameters"] = params
  338. class CppVariable(dict):
  339. """Takes a name stack and turns it into a method
  340. Contains the following Keys:
  341. self['type'] - Type for the variable (ex. "const string &")
  342. self['name'] - Name of the variable (ex. "numItems")
  343. self['namespace'] - Namespace containing the enum
  344. self['desc'] - Description of the variable if part of a method (optional)
  345. self['doxygen'] - Doxygen comments associated with the method if they exist
  346. self['defaltValue'] - Default value of the variable, this key will only
  347. exist if there is a default value
  348. """
  349. def __init__(self, nameStack, **kwargs):
  350. if (debug): print "Variable: ", nameStack
  351. if (len(nameStack) < 2):
  352. return
  353. global doxygenCommentCache
  354. if len(doxygenCommentCache):
  355. self["doxygen"] = doxygenCommentCache
  356. doxygenCommentCache = ""
  357. if ("=" in nameStack):
  358. self["type"] = " ".join(nameStack[:nameStack.index("=") - 1])
  359. self["name"] = nameStack[nameStack.index("=") - 1]
  360. self["defaltValue"] = " ".join(nameStack[nameStack.index("=") + 1:])
  361. else:
  362. self["type"] = " ".join(nameStack[:-1])
  363. self["name"] = nameStack[-1]
  364. self["type"] = self["type"].replace(" :",":")
  365. self["type"] = self["type"].replace(": ",":")
  366. self["type"] = self["type"].replace(" <","<")
  367. self["type"] = self["type"].replace(" >",">")
  368. #Optional doxygen description
  369. try:
  370. self["desc"] = kwargs["doxyVarDesc"][self["name"]]
  371. except: pass
  372. class CppEnum(dict):
  373. """Takes a name stack and turns it into an Enum
  374. Contains the following Keys:
  375. self['name'] - Name of the enum (ex. "ItemState")
  376. self['namespace'] - Namespace containing the enum
  377. self['values'] - List of values where the values are a dictionary of the
  378. form {"name": name of the key (ex. "PARSING_HEADER"),
  379. "value": Specified value of the enum, this key will only exist
  380. if a value for a given enum value was defined
  381. }
  382. """
  383. def __init__(self, nameStack):
  384. if len(nameStack) < 4 or "{" not in nameStack or "}" not in nameStack:
  385. #Not enough stuff for an enum
  386. return
  387. global doxygenCommentCache
  388. if len(doxygenCommentCache):
  389. self["doxygen"] = doxygenCommentCache
  390. doxygenCommentCache = ""
  391. valueList = []
  392. #Figure out what values it has
  393. valueStack = nameStack[nameStack.index('{') + 1: nameStack.index('}')]
  394. while len(valueStack):
  395. tmpStack = []
  396. if "," in valueStack:
  397. tmpStack = valueStack[:valueStack.index(",")]
  398. valueStack = valueStack[valueStack.index(",") + 1:]
  399. else:
  400. tmpStack = valueStack
  401. valueStack = []
  402. if len(tmpStack) == 1:
  403. valueList.append({"name": tmpStack[0]})
  404. elif len(tmpStack) >= 3 and tmpStack[1] == "=":
  405. valueList.append({"name": tmpStack[0], "value": " ".join(tmpStack[2:])})
  406. elif len(tmpStack) == 2 and tmpStack[1] == "=":
  407. if (debug): print "Missed value for %s"%tmpStack[0]
  408. valueList.append({"name": tmpStack[0]})
  409. if len(valueList):
  410. self["values"] = valueList
  411. else:
  412. #An enum without any values is useless, dont bother existing
  413. return
  414. #Figure out if it has a name
  415. preBraceStack = nameStack[:nameStack.index("{")]
  416. postBraceStack = nameStack[nameStack.index("}") + 1:]
  417. if (len(preBraceStack) == 2 and "typedef" not in nameStack):
  418. self["name"] = preBraceStack[1]
  419. elif len(postBraceStack) and "typedef" in nameStack:
  420. self["name"] = " ".join(postBraceStack)
  421. #See if there are instances of this
  422. if "typedef" not in nameStack and len(postBraceStack):
  423. self["instances"] = []
  424. for var in postBraceStack:
  425. if "," in var:
  426. continue
  427. self["instances"].append(var)
  428. self["namespace"] = ""
  429. class CppHeader:
  430. """Parsed C++ class header
  431. Variables produced:
  432. self.classes - Dictionary of classes found in a given header file where the
  433. key is the name of the class
  434. """
  435. def __init__(self, headerFileName, argType = "file"):
  436. if (argType == "file"):
  437. self.headerFileName = os.path.expandvars(headerFileName)
  438. self.mainClass = os.path.split(self.headerFileName)[1][:-2]
  439. headerFileStr = ""
  440. # if headerFileName[-2:] != ".h":
  441. # raise Exception("file must be a header file and end with .h")
  442. elif argType == "string":
  443. self.headerFileName = ""
  444. self.mainClass = "???"
  445. headerFileStr = headerFileName
  446. else:
  447. raise Exception("Arg type must be either file or string")
  448. self.curClass = ""
  449. self.classes = {}
  450. self.enums = []
  451. self.nameStack = []
  452. self.nameSpaces = []
  453. self.curAccessSpecifier = 'private'
  454. if (len(self.headerFileName)):
  455. headerFileStr = "\n".join(open(self.headerFileName).readlines())
  456. self.braceDepth = 0
  457. lex.input(headerFileStr)
  458. curLine = 0
  459. curChar = 0
  460. try:
  461. while True:
  462. tok = lex.token()
  463. # Example: LexToken(COLON,';',1,373)
  464. # where (tok.name, tok.value, ?, ?)
  465. if not tok:
  466. break
  467. curLine = tok.lineno
  468. curChar = tok.lexpos
  469. if (tok.type == 'OPEN_BRACE'):
  470. if len(self.nameStack) and is_namespace(self.nameStack):
  471. self.nameSpaces.append(self.nameStack[1])
  472. if len(self.nameStack) and not is_enum_namestack(self.nameStack):
  473. self.evaluate_stack()
  474. else:
  475. self.nameStack.append(tok.value)
  476. self.braceDepth += 1
  477. elif (tok.type == 'CLOSE_BRACE'):
  478. if self.braceDepth == 0:
  479. continue
  480. if (self.braceDepth == len(self.nameSpaces)):
  481. tmp = self.nameSpaces.pop()
  482. if len(self.nameStack) and is_enum_namestack(self.nameStack):
  483. self.nameStack.append(tok.value)
  484. elif self.braceDepth < 10:
  485. self.evaluate_stack()
  486. else:
  487. self.nameStack = []
  488. self.braceDepth -= 1
  489. if (self.braceDepth == 0):
  490. self.curClass = ""
  491. if (tok.type == 'OPEN_PAREN'):
  492. self.nameStack.append(tok.value)
  493. elif (tok.type == 'CLOSE_PAREN'):
  494. self.nameStack.append(tok.value)
  495. elif (tok.type == 'EQUALS'):
  496. self.nameStack.append(tok.value)
  497. elif (tok.type == 'COMMA'):
  498. self.nameStack.append(tok.value)
  499. elif (tok.type == 'NUMBER'):
  500. self.nameStack.append(tok.value)
  501. elif (tok.type == 'MINUS'):
  502. self.nameStack.append(tok.value)
  503. elif (tok.type == 'PLUS'):
  504. self.nameStack.append(tok.value)
  505. elif (tok.type == 'STRING_LITERAL'):
  506. self.nameStack.append(tok.value)
  507. elif (tok.type == 'NAME' or tok.type == 'AMPERSTAND' or tok.type == 'ASTERISK'):
  508. if (tok.value == 'class'):
  509. self.nameStack.append(tok.value)
  510. elif (tok.value in supportedAccessSpecifier and self.braceDepth == len(self.nameSpaces) + 1):
  511. self.curAccessSpecifier = tok.value
  512. else:
  513. self.nameStack.append(tok.value)
  514. elif (tok.type == 'COLON'):
  515. #Dont want colon to be first in stack
  516. if len(self.nameStack) == 0:
  517. continue
  518. self.nameStack.append(tok.value)
  519. elif (tok.type == 'SEMI_COLON'):
  520. if (self.braceDepth < 10):
  521. self.evaluate_stack()
  522. except:
  523. raise CppParseError("Not able to parse %s on line %d evaluating \"%s\"\nError around: %s"
  524. % (self.headerFileName, tok.lineno, tok.value, " ".join(self.nameStack)))
  525. def evaluate_stack(self):
  526. """Evaluates the current name stack"""
  527. global doxygenCommentCache
  528. if (debug): print "Evaluating stack %s at..."%self.nameStack
  529. if (len(self.curClass)):
  530. if (debug): print "%s (%s) "%(self.curClass, self.curAccessSpecifier),
  531. if (len(self.nameStack) == 0):
  532. if (debug): print "line ",lineno()
  533. if (debug): print "(Empty Stack)"
  534. return
  535. elif (self.nameStack[0] == "namespace"):
  536. #Taken care of outside of here
  537. pass
  538. elif (self.nameStack[0] == "class"):
  539. if (debug): print "line ",lineno()
  540. self.evaluate_class_stack()
  541. elif (self.nameStack[0] == "struct"):
  542. if (debug): print "line ",lineno()
  543. self.curAccessSpecifier = "public"
  544. self.evaluate_class_stack()
  545. elif (len(self.curClass) == 0):
  546. if (debug): print "line ",lineno()
  547. if is_enum_namestack(self.nameStack):
  548. self.evaluate_enum_stack()
  549. self.nameStack = []
  550. doxygenCommentCache = ""
  551. return
  552. elif (self.braceDepth < 1):
  553. if (debug): print "line ",lineno()
  554. #Ignore global stuff for now
  555. if (debug): print "Global stuff: ", self.nameStack
  556. self.nameStack = []
  557. doxygenCommentCache = ""
  558. return
  559. elif (self.braceDepth > len(self.nameSpaces) + 1):
  560. if (debug): print "line ",lineno()
  561. self.nameStack = []
  562. doxygenCommentCache = ""
  563. return
  564. elif is_enum_namestack(self.nameStack):
  565. if (debug): print "line ",lineno()
  566. #elif self.nameStack[0] == "enum":
  567. self.evaluate_enum_stack()
  568. elif ('(' in self.nameStack):
  569. if (debug): print "line ",lineno()
  570. self.evaluate_method_stack()
  571. else:
  572. if (debug): print "line ",lineno()
  573. self.evaluate_property_stack()
  574. self.nameStack = []
  575. doxygenCommentCache = ""
  576. def evaluate_class_stack(self):
  577. """Create a Class out of the name stack (but not its parts)"""
  578. #dont support sub classes today
  579. if self.braceDepth != len(self.nameSpaces):
  580. return
  581. newClass = CppClass(self.nameStack)
  582. if len(newClass.keys()):
  583. self.curClass = newClass["name"]
  584. self.classes[self.curClass] = newClass
  585. else:
  586. self.curClass = ""
  587. newClass["namespace"] = self.cur_namespace()
  588. def evaluate_method_stack(self):
  589. """Create a method out of the name stack"""
  590. newMethod = CppMethod(self.nameStack, self.curClass)
  591. if len(newMethod.keys()):
  592. self.classes[self.curClass]["methods"][self.curAccessSpecifier].append(newMethod)
  593. def evaluate_property_stack(self):
  594. """Create a Property out of the name stack"""
  595. newVar = CppVariable(self.nameStack)
  596. if len(newVar.keys()):
  597. self.classes[self.curClass]["properties"][self.curAccessSpecifier].append(newVar)
  598. def evaluate_enum_stack(self):
  599. """Create an Enum out of the name stack"""
  600. newEnum = CppEnum(self.nameStack)
  601. if len(newEnum.keys()):
  602. if len(self.curClass):
  603. newEnum["namespace"] = self.cur_namespace()
  604. self.classes[self.curClass]["enums"][self.curAccessSpecifier].append(newEnum)
  605. else:
  606. newEnum["namespace"] = self.cur_namespace()
  607. # print "Adding global enum"
  608. self.enums.append(newEnum)
  609. #This enum has instances, turn them into properties
  610. if newEnum.has_key("instances"):
  611. instanceType = "enum"
  612. if newEnum.has_key("name"):
  613. instanceType = newEnum["name"]
  614. for instance in newEnum["instances"]:
  615. self.nameStack = [instanceType, instance]
  616. self.evaluate_property_stack()
  617. del newEnum["instances"]
  618. def cur_namespace(self, add_double_colon = False):
  619. rtn = ""
  620. i = 0
  621. while i < len(self.nameSpaces):
  622. rtn += self.nameSpaces[i]
  623. if add_double_colon or i < len(self.nameSpaces) - 1:
  624. rtn += "::"
  625. i+=1
  626. return rtn
  627. def __repr__(self):
  628. rtn = ""
  629. for className in self.classes.keys():
  630. rtn += repr(self.classes[className])
  631. return rtn