[DEV] rework a correct cpp parser

This commit is contained in:
Edouard DUPIN 2013-12-18 21:35:05 +01:00
parent f52e3e6f5e
commit c572dc8c15
25 changed files with 731 additions and 5878 deletions

38
cppParser/Class.py Normal file
View File

@ -0,0 +1,38 @@
#!/usr/bin/python
try :
# normal install module
import ply.lex as lex
except ImportError :
# local module
import lex
import os
import sys
import re
import Node
import lutinDebug as debug
import inspect
class Class(Node.Node):
def __init__(self):
# List of the class name (with their namespace : [['ewol','widget','plop'], ...])
self.parents = []
# CPP section:
self.namespaces = []
self.classes = []
# C section:
self.structs = []
self.variables = []
self.methodes = []
self.unions = []
self.types = []
def to_str(self):
return ""

File diff suppressed because it is too large Load Diff

View File

@ -1,9 +0,0 @@
# CppHeaderParser package
# Author: Jashua Cloutier (contact via sourceforge username:senexcanis)
import sys
if sys.version_info[0] == 2:
from CppHeaderParser import *
else:
from CppHeaderParser3 import *
#__all__ = ['CppHeaderParser']

File diff suppressed because it is too large Load Diff

View File

@ -1,76 +0,0 @@
#include <vector>
#include <string>
#define DEF_1 1
#define OS_NAME "Linux"
using namespace std;
class SampleClass
{
public:
SampleClass();
/*!
* Method 1
*/
string meth1();
///
/// Method 2 description
///
/// @param v1 Variable 1
///
int meth2(int v1);
/**
* Method 3 description
*
* \param v1 Variable 1
* \param v2 Variable 2
*/
void meth3(const string & v1, vector<string> & v2);
/**********************************
* Method 4 description
*
* @return Return value
*********************************/
unsigned int meth4();
private:
void * meth5(){return NULL};
/// prop1 description
string prop1;
//! prop5 description
int prop5;
};
namespace Alpha
{
class AlphaClass
{
public:
AlphaClass();
void alphaMethod();
string alphaString;
};
namespace Omega
{
class OmegaClass
{
public:
OmegaClass();
string omegaString;
};
};
}
int sampleFreeFunction(int i)
{
return i + 1;
}
int anotherFreeFunction(void);
}

View File

@ -1,63 +0,0 @@
#!/usr/bin/python
import sys
sys.path = ["../"] + sys.path
import CppHeaderParser
try:
cppHeader = CppHeaderParser.CppHeader("SampleClass.h")
except CppHeaderParser.CppParseError, e:
print e
sys.exit(1)
print "CppHeaderParser view of %s"%cppHeader
sampleClass = cppHeader.classes["SampleClass"]
print "Number of public methods %d"%(len(sampleClass["methods"]["public"]))
print "Number of private properties %d"%(len(sampleClass["properties"]["private"]))
meth3 = [m for m in sampleClass["methods"]["public"] if m["name"] == "meth3"][0] #get meth3
meth3ParamTypes = [t["type"] for t in meth3["parameters"]] #get meth3s parameters
print "Parameter Types for public method meth3 %s"%(meth3ParamTypes)
print "\nReturn type for meth1:"
print cppHeader.classes["SampleClass"]["methods"]["public"][1]["rtnType"]
print "\nDoxygen for meth2:"
print cppHeader.classes["SampleClass"]["methods"]["public"][2]["doxygen"]
print "\nParameters for meth3:"
print cppHeader.classes["SampleClass"]["methods"]["public"][3]["parameters"]
print "\nDoxygen for meth4:"
print cppHeader.classes["SampleClass"]["methods"]["public"][4]["doxygen"]
print "\nReturn type for meth5:"
print cppHeader.classes["SampleClass"]["methods"]["private"][0]["rtnType"]
print "\nDoxygen type for prop1:"
print cppHeader.classes["SampleClass"]["properties"]["private"][0]["doxygen"]
print "\nType for prop5:"
print cppHeader.classes["SampleClass"]["properties"]["private"][1]["type"]
print "\nNamespace for AlphaClass is:"
print cppHeader.classes["AlphaClass"]["namespace"]
print "\nReturn type for alphaMethod is:"
print cppHeader.classes["AlphaClass"]["methods"]["public"][0]["rtnType"]
print "\nNamespace for OmegaClass is:"
print cppHeader.classes["OmegaClass"]["namespace"]
print "\nType for omegaString is:"
print cppHeader.classes["AlphaClass"]["properties"]["public"][0]["type"]
print "\nFree functions are:"
for func in cppHeader.functions:
print " %s"%func["name"]
print "\n#includes are:"
for incl in cppHeader.includes:
print " %s"%incl
print "\n#defines are:"
for define in cppHeader.defines:
print " %s"%define

View File

@ -1,669 +0,0 @@
#!/usr/bin/python
#
# Author: Jashua R. Cloutier (contact via sourceforge username:senexcanis)
#
# Copyright (C) 2010, Jashua R. Cloutier
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# * Neither the name of Jashua R. Cloutier nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
#
# The CppHeaderParser.py script is written in Python 2.4 and released to
# the open source community for continuous improvements under the BSD
# 2.0 new license, which can be found at:
#
# http://www.opensource.org/licenses/bsd-license.php
#
"""Parse C++ header files and generate a data structure
representing the class
"""
import ply.lex as lex
import os
import sys
import re
import inspect
def lineno():
"""Returns the current line number in our program."""
return inspect.currentframe().f_back.f_lineno
__version__ = "1.9"
version = "1.9"
tokens = [
'NUMBER',
'NAME',
'OPEN_PAREN',
'CLOSE_PAREN',
'OPEN_BRACE',
'CLOSE_BRACE',
'COLON',
'SEMI_COLON',
'COMMA',
'COMMENT_SINGLELINE',
'COMMENT_MULTILINE',
'PRECOMP_MACRO',
'PRECOMP_MACRO_CONT',
'ASTERISK',
'AMPERSTAND',
'EQUALS',
'MINUS',
'PLUS',
'DIVIDE',
'CHAR_LITERAL',
'STRING_LITERAL',
'OPERATOR_DIVIDE_OVERLOAD',
'NEW_LINE',
]
t_ignore = " \t\r[].|!?%@"
t_NUMBER = r'[0-9][0-9XxA-Fa-f]*'
t_NAME = r'[<>A-Za-z_~][A-Za-z0-9_]*'
t_OPERATOR_DIVIDE_OVERLOAD = r'/='
t_OPEN_PAREN = r'\('
t_CLOSE_PAREN = r'\)'
t_OPEN_BRACE = r'{'
t_CLOSE_BRACE = r'}'
t_SEMI_COLON = r';'
t_COLON = r':'
t_COMMA = r','
t_PRECOMP_MACRO = r'\#.*'
t_PRECOMP_MACRO_CONT = r'.*\\\n'
def t_COMMENT_SINGLELINE(t):
r'\/\/.*\n'
global doxygenCommentCache
if t.value.startswith("///") or t.value.startswith("//!"):
if doxygenCommentCache:
doxygenCommentCache += "\n"
if t.value.endswith("\n"):
doxygenCommentCache += t.value[:-1]
else:
doxygenCommentCache += t.value
t_ASTERISK = r'\*'
t_MINUS = r'\-'
t_PLUS = r'\+'
t_DIVIDE = r'/[^/]'
t_AMPERSTAND = r'&'
t_EQUALS = r'='
t_CHAR_LITERAL = "'.'"
#found at http://wordaligned.org/articles/string-literals-and-regular-expressions
#TODO: This does not work with the string "bla \" bla"
t_STRING_LITERAL = r'"([^"\\]|\\.)*"'
#Found at http://ostermiller.org/findcomment.html
def t_COMMENT_MULTILINE(t):
r'/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/'
global doxygenCommentCache
if t.value.startswith("/**") or t.value.startswith("/*!"):
#not sure why, but get double new lines
v = t.value.replace("\n\n", "\n")
#strip prefixing whitespace
v = re.sub("\n[\s]+\*", "\n*", v)
doxygenCommentCache += v
def t_NEWLINE(t):
r'\n+'
t.lexer.lineno += len(t.value)
def t_error(v):
print "Lex error: ", v
lex.lex()
debug = 0
supportedAccessSpecifier = [
'public',
'protected',
'private'
]
doxygenCommentCache = ""
def is_namespace(nameStack):
"""Determines if a namespace is being specified"""
if len(nameStack) == 0:
return False
if nameStack[0] == "namespace":
return True
return False
def is_enum_namestack(nameStack):
"""Determines if a namestack is an enum namestack"""
if len(nameStack) == 0:
return False
if nameStack[0] == "enum":
return True
if len(nameStack) > 1 and nameStack[0] == "typedef" and nameStack[1] == "enum":
return True
return False
class CppParseError(Exception): pass
class CppClass(dict):
"""Takes a name stack and turns it into a class
Contains the following Keys:
self['name'] - Name of the class
self['doxygen'] - Doxygen comments associated with the class if they exist
self['inherits'] - List of Classes that this one inherits where the values
are of the form {"access": Anything in supportedAccessSpecifier
"class": Name of the class
self['methods'] - Dictionary where keys are from supportedAccessSpecifier
and values are a lists of CppMethod's
self['properties'] - Dictionary where keys are from supportedAccessSpecifier
and values are lists of CppVariable's
self['enums'] - Dictionary where keys are from supportedAccessSpecifier and
values are lists of CppEnum's
An example of how this could look is as follows:
#self =
{
'name': ""
'inherits':[]
'methods':
{
'public':[],
'protected':[],
'private':[]
},
'properties':
{
'public':[],
'protected':[],
'private':[]
},
'enums':
{
'public':[],
'protected':[],
'private':[]
}
}
"""
def __init__(self, nameStack):
if (debug): print "Class: ", nameStack
if (len(nameStack) < 2):
print "Error detecting class"
return
global doxygenCommentCache
if len(doxygenCommentCache):
self["doxygen"] = doxygenCommentCache
doxygenCommentCache = ""
self["name"] = nameStack[1]
inheritList = []
if ":" in nameStack:
nameStack = nameStack[nameStack.index(":") + 1:]
while len(nameStack):
tmpStack = []
tmpInheritClass = {"access":"private"}
if "," in nameStack:
tmpStack = nameStack[:nameStack.index(",")]
nameStack = nameStack[nameStack.index(",") + 1:]
else:
tmpStack = nameStack
nameStack = []
if len(tmpStack) == 0:
break;
elif len(tmpStack) == 1:
tmpInheritClass["class"] = tmpStack[0]
elif len(tmpStack) == 2:
tmpInheritClass["access"] = tmpStack[0]
tmpInheritClass["class"] = tmpStack[1]
else:
print "Warning: Cant figure out class inheriting %s\n"%(" ".join(tmpStack))
continue
inheritList.append(tmpInheritClass)
methodAccessSpecificList = {}
propertyAccessSpecificList = {}
enumAccessSpecificList = {}
for accessSpecifier in supportedAccessSpecifier:
methodAccessSpecificList[accessSpecifier] = []
propertyAccessSpecificList[accessSpecifier] = []
enumAccessSpecificList[accessSpecifier] = []
self['inherits'] = inheritList
self['methods'] = methodAccessSpecificList
self['properties'] = propertyAccessSpecificList
self['enums'] = enumAccessSpecificList
self['namespace'] = ""
def __repr__(self):
"""Convert class to a string"""
namespace_prefix = ""
if self["namespace"]: namespace_prefix = self["namespace"] + "::"
rtn = "class %s\n"%(namespace_prefix + self["name"])
try:
print self["doxygen"],
except: pass
if "inherits" in self.keys():
rtn += "Inherits: "
for inheritClass in self["inherits"]:
rtn += "%s %s, "%(inheritClass["access"], inheritClass["class"])
rtn += "\n"
rtn += "{\n"
for accessSpecifier in supportedAccessSpecifier:
rtn += "%s\n"%(accessSpecifier)
#Enums
if (len(self["enums"][accessSpecifier])):
rtn += " // Enums\n"
for enum in self["enums"][accessSpecifier]:
rtn += " %s\n"%(repr(enum))
#Properties
if (len(self["properties"][accessSpecifier])):
rtn += " // Properties\n"
for property in self["properties"][accessSpecifier]:
rtn += " %s\n"%(repr(property))
#Methods
if (len(self["methods"][accessSpecifier])):
rtn += " // Method\n"
for method in self["methods"][accessSpecifier]:
rtn += " %s\n"%(repr(method))
rtn += "}\n"
return rtn
class CppMethod(dict):
"""Takes a name stack and turns it into a method
Contains the following Keys:
self['rtnType'] - Return type of the method (ex. "int")
self['name'] - Name of the method (ex. "getSize")
self['doxygen'] - Doxygen comments associated with the method if they exist
self['parameters'] - List of CppVariables
"""
def __init__(self, nameStack, curClass):
if (debug): print "Method: ", nameStack
global doxygenCommentCache
if len(doxygenCommentCache):
self["doxygen"] = doxygenCommentCache
doxygenCommentCache = ""
if "operator" in nameStack:
self["rtnType"] = " ".join(nameStack[:nameStack.index('operator')])
self["name"] = "".join(nameStack[nameStack.index('operator'):nameStack.index('(')])
else:
self["rtnType"] = " ".join(nameStack[:nameStack.index('(') - 1])
self["name"] = " ".join(nameStack[nameStack.index('(') - 1:nameStack.index('(')])
if len(self["rtnType"]) == 0 or self["name"] == curClass:
self["rtnType"] = "void"
paramsStack = nameStack[nameStack.index('(') + 1: ]
#Remove things from the stack till we hit the last paren, this helps handle abstract and normal methods
while (paramsStack[-1] != ")"):
paramsStack.pop()
paramsStack.pop()
params = []
#See if there is a doxygen comment for the variable
doxyVarDesc = {}
#TODO: Put this into a class
if self.has_key("doxygen"):
doxyLines = self["doxygen"].split("\n")
lastParamDesc = ""
for doxyLine in doxyLines:
if " @param " in doxyLine or " \param " in doxyLine:
try:
#Strip out the param
doxyLine = doxyLine[doxyLine.find("param ") + 6:]
(var, desc) = doxyLine.split(" ", 1)
doxyVarDesc[var] = desc.strip()
lastParamDesc = var
except: pass
elif " @return " in doxyLine or " \return " in doxyLine:
lastParamDesc = ""
# not handled for now
elif lastParamDesc:
try:
doxyLine = doxyLine.strip()
if " " not in doxyLine:
lastParamDesc = ""
continue
doxyLine = doxyLine[doxyLine.find(" ") + 1:]
doxyVarDesc[lastParamDesc] += " " + doxyLine
except: pass
#Create the variable now
while (len(paramsStack)):
if (',' in paramsStack):
params.append(CppVariable(paramsStack[0:paramsStack.index(',')], doxyVarDesc=doxyVarDesc))
paramsStack = paramsStack[paramsStack.index(',') + 1:]
else:
param = CppVariable(paramsStack, doxyVarDesc=doxyVarDesc)
if len(param.keys()):
params.append(param)
break
self["parameters"] = params
class CppVariable(dict):
"""Takes a name stack and turns it into a method
Contains the following Keys:
self['type'] - Type for the variable (ex. "const string &")
self['name'] - Name of the variable (ex. "numItems")
self['namespace'] - Namespace containing the enum
self['desc'] - Description of the variable if part of a method (optional)
self['doxygen'] - Doxygen comments associated with the method if they exist
self['defaltValue'] - Default value of the variable, this key will only
exist if there is a default value
"""
def __init__(self, nameStack, **kwargs):
if (debug): print "Variable: ", nameStack
if (len(nameStack) < 2):
return
global doxygenCommentCache
if len(doxygenCommentCache):
self["doxygen"] = doxygenCommentCache
doxygenCommentCache = ""
if ("=" in nameStack):
self["type"] = " ".join(nameStack[:nameStack.index("=") - 1])
self["name"] = nameStack[nameStack.index("=") - 1]
self["defaltValue"] = " ".join(nameStack[nameStack.index("=") + 1:])
else:
self["type"] = " ".join(nameStack[:-1])
self["name"] = nameStack[-1]
self["type"] = self["type"].replace(" :",":")
self["type"] = self["type"].replace(": ",":")
self["type"] = self["type"].replace(" <","<")
self["type"] = self["type"].replace(" >",">")
#Optional doxygen description
try:
self["desc"] = kwargs["doxyVarDesc"][self["name"]]
except: pass
class CppEnum(dict):
"""Takes a name stack and turns it into an Enum
Contains the following Keys:
self['name'] - Name of the enum (ex. "ItemState")
self['namespace'] - Namespace containing the enum
self['values'] - List of values where the values are a dictionary of the
form {"name": name of the key (ex. "PARSING_HEADER"),
"value": Specified value of the enum, this key will only exist
if a value for a given enum value was defined
}
"""
def __init__(self, nameStack):
if len(nameStack) < 4 or "{" not in nameStack or "}" not in nameStack:
#Not enough stuff for an enum
return
global doxygenCommentCache
if len(doxygenCommentCache):
self["doxygen"] = doxygenCommentCache
doxygenCommentCache = ""
valueList = []
#Figure out what values it has
valueStack = nameStack[nameStack.index('{') + 1: nameStack.index('}')]
while len(valueStack):
tmpStack = []
if "," in valueStack:
tmpStack = valueStack[:valueStack.index(",")]
valueStack = valueStack[valueStack.index(",") + 1:]
else:
tmpStack = valueStack
valueStack = []
if len(tmpStack) == 1:
valueList.append({"name": tmpStack[0]})
elif len(tmpStack) >= 3 and tmpStack[1] == "=":
valueList.append({"name": tmpStack[0], "value": " ".join(tmpStack[2:])})
elif len(tmpStack) == 2 and tmpStack[1] == "=":
if (debug): print "Missed value for %s"%tmpStack[0]
valueList.append({"name": tmpStack[0]})
if len(valueList):
self["values"] = valueList
else:
#An enum without any values is useless, dont bother existing
return
#Figure out if it has a name
preBraceStack = nameStack[:nameStack.index("{")]
postBraceStack = nameStack[nameStack.index("}") + 1:]
if (len(preBraceStack) == 2 and "typedef" not in nameStack):
self["name"] = preBraceStack[1]
elif len(postBraceStack) and "typedef" in nameStack:
self["name"] = " ".join(postBraceStack)
#See if there are instances of this
if "typedef" not in nameStack and len(postBraceStack):
self["instances"] = []
for var in postBraceStack:
if "," in var:
continue
self["instances"].append(var)
self["namespace"] = ""
class CppHeader:
"""Parsed C++ class header
Variables produced:
self.classes - Dictionary of classes found in a given header file where the
key is the name of the class
"""
def __init__(self, headerFileName, argType = "file"):
if (argType == "file"):
self.headerFileName = os.path.expandvars(headerFileName)
self.mainClass = os.path.split(self.headerFileName)[1][:-2]
headerFileStr = ""
# if headerFileName[-2:] != ".h":
# raise Exception("file must be a header file and end with .h")
elif argType == "string":
self.headerFileName = ""
self.mainClass = "???"
headerFileStr = headerFileName
else:
raise Exception("Arg type must be either file or string")
self.curClass = ""
self.classes = {}
self.enums = []
self.nameStack = []
self.nameSpaces = []
self.curAccessSpecifier = 'private'
if (len(self.headerFileName)):
headerFileStr = "\n".join(open(self.headerFileName).readlines())
self.braceDepth = 0
lex.input(headerFileStr)
curLine = 0
curChar = 0
try:
while True:
tok = lex.token()
# Example: LexToken(COLON,';',1,373)
# where (tok.name, tok.value, ?, ?)
if not tok:
break
curLine = tok.lineno
curChar = tok.lexpos
if (tok.type == 'OPEN_BRACE'):
if len(self.nameStack) and is_namespace(self.nameStack):
self.nameSpaces.append(self.nameStack[1])
if len(self.nameStack) and not is_enum_namestack(self.nameStack):
self.evaluate_stack()
else:
self.nameStack.append(tok.value)
self.braceDepth += 1
elif (tok.type == 'CLOSE_BRACE'):
if self.braceDepth == 0:
continue
if (self.braceDepth == len(self.nameSpaces)):
tmp = self.nameSpaces.pop()
if len(self.nameStack) and is_enum_namestack(self.nameStack):
self.nameStack.append(tok.value)
elif self.braceDepth < 10:
self.evaluate_stack()
else:
self.nameStack = []
self.braceDepth -= 1
if (self.braceDepth == 0):
self.curClass = ""
if (tok.type == 'OPEN_PAREN'):
self.nameStack.append(tok.value)
elif (tok.type == 'CLOSE_PAREN'):
self.nameStack.append(tok.value)
elif (tok.type == 'EQUALS'):
self.nameStack.append(tok.value)
elif (tok.type == 'COMMA'):
self.nameStack.append(tok.value)
elif (tok.type == 'NUMBER'):
self.nameStack.append(tok.value)
elif (tok.type == 'MINUS'):
self.nameStack.append(tok.value)
elif (tok.type == 'PLUS'):
self.nameStack.append(tok.value)
elif (tok.type == 'STRING_LITERAL'):
self.nameStack.append(tok.value)
elif (tok.type == 'NAME' or tok.type == 'AMPERSTAND' or tok.type == 'ASTERISK'):
if (tok.value == 'class'):
self.nameStack.append(tok.value)
elif (tok.value in supportedAccessSpecifier and self.braceDepth == len(self.nameSpaces) + 1):
self.curAccessSpecifier = tok.value
else:
self.nameStack.append(tok.value)
elif (tok.type == 'COLON'):
#Dont want colon to be first in stack
if len(self.nameStack) == 0:
continue
self.nameStack.append(tok.value)
elif (tok.type == 'SEMI_COLON'):
if (self.braceDepth < 10):
self.evaluate_stack()
except:
raise CppParseError("Not able to parse %s on line %d evaluating \"%s\"\nError around: %s"
% (self.headerFileName, tok.lineno, tok.value, " ".join(self.nameStack)))
def evaluate_stack(self):
"""Evaluates the current name stack"""
global doxygenCommentCache
if (debug): print "Evaluating stack %s at..."%self.nameStack
if (len(self.curClass)):
if (debug): print "%s (%s) "%(self.curClass, self.curAccessSpecifier),
if (len(self.nameStack) == 0):
if (debug): print "line ",lineno()
if (debug): print "(Empty Stack)"
return
elif (self.nameStack[0] == "namespace"):
#Taken care of outside of here
pass
elif (self.nameStack[0] == "class"):
if (debug): print "line ",lineno()
self.evaluate_class_stack()
elif (self.nameStack[0] == "struct"):
if (debug): print "line ",lineno()
self.curAccessSpecifier = "public"
self.evaluate_class_stack()
elif (len(self.curClass) == 0):
if (debug): print "line ",lineno()
if is_enum_namestack(self.nameStack):
self.evaluate_enum_stack()
self.nameStack = []
doxygenCommentCache = ""
return
elif (self.braceDepth < 1):
if (debug): print "line ",lineno()
#Ignore global stuff for now
if (debug): print "Global stuff: ", self.nameStack
self.nameStack = []
doxygenCommentCache = ""
return
elif (self.braceDepth > len(self.nameSpaces) + 1):
if (debug): print "line ",lineno()
self.nameStack = []
doxygenCommentCache = ""
return
elif is_enum_namestack(self.nameStack):
if (debug): print "line ",lineno()
#elif self.nameStack[0] == "enum":
self.evaluate_enum_stack()
elif ('(' in self.nameStack):
if (debug): print "line ",lineno()
self.evaluate_method_stack()
else:
if (debug): print "line ",lineno()
self.evaluate_property_stack()
self.nameStack = []
doxygenCommentCache = ""
def evaluate_class_stack(self):
"""Create a Class out of the name stack (but not its parts)"""
#dont support sub classes today
if self.braceDepth != len(self.nameSpaces):
return
newClass = CppClass(self.nameStack)
if len(newClass.keys()):
self.curClass = newClass["name"]
self.classes[self.curClass] = newClass
else:
self.curClass = ""
newClass["namespace"] = self.cur_namespace()
def evaluate_method_stack(self):
"""Create a method out of the name stack"""
newMethod = CppMethod(self.nameStack, self.curClass)
if len(newMethod.keys()):
self.classes[self.curClass]["methods"][self.curAccessSpecifier].append(newMethod)
def evaluate_property_stack(self):
"""Create a Property out of the name stack"""
newVar = CppVariable(self.nameStack)
if len(newVar.keys()):
self.classes[self.curClass]["properties"][self.curAccessSpecifier].append(newVar)
def evaluate_enum_stack(self):
"""Create an Enum out of the name stack"""
newEnum = CppEnum(self.nameStack)
if len(newEnum.keys()):
if len(self.curClass):
newEnum["namespace"] = self.cur_namespace()
self.classes[self.curClass]["enums"][self.curAccessSpecifier].append(newEnum)
else:
newEnum["namespace"] = self.cur_namespace()
# print "Adding global enum"
self.enums.append(newEnum)
#This enum has instances, turn them into properties
if newEnum.has_key("instances"):
instanceType = "enum"
if newEnum.has_key("name"):
instanceType = newEnum["name"]
for instance in newEnum["instances"]:
self.nameStack = [instanceType, instance]
self.evaluate_property_stack()
del newEnum["instances"]
def cur_namespace(self, add_double_colon = False):
rtn = ""
i = 0
while i < len(self.nameSpaces):
rtn += self.nameSpaces[i]
if add_double_colon or i < len(self.nameSpaces) - 1:
rtn += "::"
i+=1
return rtn
def __repr__(self):
rtn = ""
for className in self.classes.keys():
rtn += repr(self.classes[className])
return rtn

View File

@ -1,12 +0,0 @@
#!/usr/bin/python
import CppHeaderParser
f = CppHeaderParser.CppHeader("test/TestSampleClass.h")
print f
print "=" * 20
#print f.classes["SampleClass"]["methods"]["public"][2]["parameters"]
print f.classes["AlphaClass"]["enums"]["protected"][0]["values"]

24
cppParser/Enum.py Normal file
View File

@ -0,0 +1,24 @@
#!/usr/bin/python
try :
# normal install module
import ply.lex as lex
except ImportError :
# local module
import lex
import os
import sys
import re
import Node
import inspect
class Enum(Node):
def __init__(self):
self.name = libName
#List is contituated of 3 element : {'name':"plop", 'value':5, 'doc'=""}, ...]
self.list = []

21
cppParser/Library.py Normal file
View File

@ -0,0 +1,21 @@
#!/usr/bin/python
import LutinDebug as debug
import os
import sys
import re
class Libray():
def __init__(self, libName):
self.name = libName
# CPP section:
self.namespaces = []
self.classes = []
# C section:
self.structs = []
self.variables = []
self.methodes = []
self.unions = []
self.types = []

40
cppParser/Methode.py Normal file
View File

@ -0,0 +1,40 @@
#!/usr/bin/python
try :
# normal install module
import ply.lex as lex
except ImportError :
# local module
import lex
import os
import sys
import re
class Methode():
def __init__(self):
self.name = ""
self.returnType = Type.TypeVoid()
self.virtual = False # only for C++
self.static = False
self.inline = False
self.doc = None
self.variable = None
self.visibility = "private" # only for C++ : "public" "protected" "private"
def to_str(self):
ret = ""
if self.virtual == True:
ret += "virtual "
if self.static == True:
ret += "static "
if self.inline == True:
ret += "inline "
ret += self.returnType.to_str()
ret += " "
ret += self.name
ret += "("
# ...
ret += ")"
return ret

29
cppParser/Namespace.py Normal file
View File

@ -0,0 +1,29 @@
#!/usr/bin/python
try :
# normal install module
import ply.lex as lex
except ImportError :
# local module
import lex
import os
import sys
import re
import inspect
class Namespace():
def __init__(self):
self.name = ""
# CPP section:
self.namespaces = []
self.classes = []
# C section:
self.structs = []
self.variables = []
self.methodes = []
self.unions = []
self.types = []
def to_str(self) :
return ""

22
cppParser/Node.py Normal file
View File

@ -0,0 +1,22 @@
#!/usr/bin/python
try :
# normal install module
import ply.lex as lex
except ImportError :
# local module
import lex
import os
import sys
import re
class Node():
def __init__(self):
self.name = ""
self.doc = None
self.fileName = ""
self.lineNumber = ""
def to_str():
return ""

View File

@ -1,290 +0,0 @@
Metadata-Version: 1.1
Name: CppHeaderParser
Version: 2.4
Summary: Parse C++ header files and generate a data structure representing the class
Home-page: http://senexcanis.com/open-source/cppheaderparser/
Author: Jashua Cloutier
Author-email: jashuac@bellsouth.net
License: BSD
Description: Python package "CppHeaderParser"
--------------------------------
**Purpose:** Parse C++ header files and generate a data structure representing the class
**Author:** Jashua Cloutier
**Licence:** BSD
**External modules required:** PLY
**Quick start**::
#include <vector>
#include <string>
#define DEF_1 1
#define OS_NAME "Linux"
using namespace std;
class SampleClass
{
public:
SampleClass();
/*!
* Method 1
*/
string meth1();
///
/// Method 2 description
///
/// @param v1 Variable 1
///
int meth2(int v1);
/**
* Method 3 description
*
* \param v1 Variable 1
* \param v2 Variable 2
*/
void meth3(const string & v1, vector<string> & v2);
/**********************************
* Method 4 description
*
* @return Return value
*********************************/
unsigned int meth4();
private:
void * meth5(){return NULL};
/// prop1 description
string prop1;
//! prop5 description
int prop5;
};
namespace Alpha
{
class AlphaClass
{
public:
AlphaClass();
void alphaMethod();
string alphaString;
};
namespace Omega
{
class OmegaClass
{
public:
OmegaClass();
string omegaString;
};
};
}
int sampleFreeFunction(int i)
{
return i + 1;
}
int anotherFreeFunction(void);
}
**Python code**::
#!/usr/bin/python
import sys
sys.path = ["../"] + sys.path
import CppHeaderParser
try:
cppHeader = CppHeaderParser.CppHeader("SampleClass.h")
except CppHeaderParser.CppParseError, e:
print e
sys.exit(1)
print "CppHeaderParser view of %s"%cppHeader
sampleClass = cppHeader.classes["SampleClass"]
print "Number of public methods %d"%(len(sampleClass["methods"]["public"]))
print "Number of private properties %d"%(len(sampleClass["properties"]["private"]))
meth3 = [m for m in sampleClass["methods"]["public"] if m["name"] == "meth3"][0] #get meth3
meth3ParamTypes = [t["type"] for t in meth3["parameters"]] #get meth3s parameters
print "Parameter Types for public method meth3 %s"%(meth3ParamTypes)
print "\nReturn type for meth1:"
print cppHeader.classes["SampleClass"]["methods"]["public"][1]["rtnType"]
print "\nDoxygen for meth2:"
print cppHeader.classes["SampleClass"]["methods"]["public"][2]["doxygen"]
print "\nParameters for meth3:"
print cppHeader.classes["SampleClass"]["methods"]["public"][3]["parameters"]
print "\nDoxygen for meth4:"
print cppHeader.classes["SampleClass"]["methods"]["public"][4]["doxygen"]
print "\nReturn type for meth5:"
print cppHeader.classes["SampleClass"]["methods"]["private"][0]["rtnType"]
print "\nDoxygen type for prop1:"
print cppHeader.classes["SampleClass"]["properties"]["private"][0]["doxygen"]
print "\nType for prop5:"
print cppHeader.classes["SampleClass"]["properties"]["private"][1]["type"]
print "\nNamespace for AlphaClass is:"
print cppHeader.classes["AlphaClass"]["namespace"]
print "\nReturn type for alphaMethod is:"
print cppHeader.classes["AlphaClass"]["methods"]["public"][0]["rtnType"]
print "\nNamespace for OmegaClass is:"
print cppHeader.classes["OmegaClass"]["namespace"]
print "\nType for omegaString is:"
print cppHeader.classes["AlphaClass"]["properties"]["public"][0]["type"]
print "\nFree functions are:"
for func in cppHeader.functions:
print " %s"%func["name"]
print "\n#includes are:"
for incl in cppHeader.includes:
print " %s"%incl
print "\n#defines are:"
for define in cppHeader.defines:
print " %s"%define
**Output**::
CppHeaderParser view of class SampleClass
{
public
// Methods
{'line_number': 11, 'static': False, 'rtnType': 'void', 'const': False, 'parameters': [], 'namespace': '', 'virtual': False, 'destructor': False, 'returns': '', 'template': False, 'friend': False, 'returns_class': False, 'extern': False, 'path': 'SampleClass', 'returns_pointer': 0, 'class': None, 'name': 'SampleClass', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': True, 'constructor': True, 'debug': 'SampleClass ( ) ;', 'inline': False}
{'line_number': 15, 'static': False, 'rtnType': 'string', 'returns_unknown': True, 'const': False, 'parameters': [], 'namespace': '', 'virtual': False, 'destructor': False, 'returns': 'string', 'template': False, 'friend': False, 'returns_class': False, 'inline': False, 'extern': False, 'path': 'SampleClass', 'class': None, 'doxygen': '/*!\n* Method 1\n*/', 'name': 'meth1', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': False, 'constructor': False, 'debug': 'string meth1 ( ) ;', 'returns_pointer': 0}
{'line_number': 22, 'static': False, 'rtnType': 'int', 'const': False, 'parameters': [{'line_number': 22, 'constant': 0, 'name': 'v1', 'reference': 0, 'type': 'int', 'static': 0, 'pointer': 0, 'desc': 'Variable 1'}], 'namespace': '', 'virtual': False, 'destructor': False, 'returns': 'int', 'template': False, 'friend': False, 'returns_class': False, 'inline': False, 'extern': False, 'path': 'SampleClass', 'class': None, 'doxygen': '///\n/// Method 2 description\n///\n/// @param v1 Variable 1\n///', 'name': 'meth2', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': True, 'constructor': False, 'debug': 'int meth2 ( int v1 ) ;', 'returns_pointer': 0}
{'line_number': 30, 'static': False, 'rtnType': 'void', 'const': False, 'parameters': [{'line_number': 30, 'constant': 1, 'name': 'v1', 'reference': 1, 'type': 'const string &', 'static': 0, 'pointer': 0, 'desc': 'Variable 1'}, {'line_number': 30, 'constant': 0, 'name': 'v2', 'reference': 1, 'type': 'vector<string> &', 'static': 0, 'pointer': 0, 'desc': 'Variable 2'}], 'namespace': '', 'virtual': False, 'destructor': False, 'returns': 'void', 'template': False, 'friend': False, 'unresolved_parameters': True, 'returns_class': False, 'inline': False, 'extern': False, 'path': 'SampleClass', 'class': None, 'doxygen': '/**\n* Method 3 description\n*\n* \\param v1 Variable 1\n* \\param v2 Variable 2\n*/', 'name': 'meth3', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': True, 'constructor': False, 'debug': 'void meth3 ( const string & v1 , vector <string> & v2 ) ;', 'returns_pointer': 0}
{'line_number': 37, 'static': False, 'rtnType': 'unsigned int', 'const': False, 'parameters': [], 'namespace': '', 'virtual': False, 'destructor': False, 'returns': 'unsigned int', 'template': False, 'friend': False, 'returns_class': False, 'inline': False, 'extern': False, 'path': 'SampleClass', 'class': None, 'doxygen': '/**********************************\n* Method 4 description\n*\n* @return Return value\n*********************************/', 'name': 'meth4', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': True, 'constructor': False, 'debug': 'unsigned int meth4 ( ) ;', 'returns_pointer': 0}
protected
private
// Properties
{'line_number': 42, 'constant': 0, 'name': 'prop1', 'reference': 0, 'type': 'string', 'static': 0, 'pointer': 0}
{'line_number': 44, 'constant': 0, 'name': 'prop5', 'reference': 0, 'type': 'int', 'static': 0, 'pointer': 0}
// Methods
{'line_number': 39, 'static': False, 'rtnType': 'void *', 'const': False, 'parameters': [], 'namespace': '', 'virtual': False, 'destructor': False, 'returns': 'void', 'template': False, 'friend': False, 'returns_class': False, 'extern': False, 'path': 'SampleClass', 'returns_pointer': 1, 'class': None, 'name': 'meth5', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': True, 'constructor': False, 'debug': 'void * meth5 ( ) {', 'inline': False}
}
class Alpha::AlphaClass
{
public
// Properties
{'line_number': 55, 'constant': 0, 'name': 'alphaString', 'reference': 0, 'type': 'string', 'static': 0, 'pointer': 0}
// Methods
{'line_number': 51, 'static': False, 'rtnType': 'void', 'const': False, 'parameters': [], 'namespace': 'Alpha::', 'virtual': False, 'destructor': False, 'returns': '', 'template': False, 'friend': False, 'returns_class': False, 'extern': False, 'path': 'Alpha::AlphaClass', 'returns_pointer': 0, 'class': None, 'name': 'AlphaClass', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': True, 'constructor': True, 'debug': 'AlphaClass ( ) ;', 'inline': False}
{'line_number': 53, 'static': False, 'rtnType': 'void', 'const': False, 'parameters': [], 'namespace': 'Alpha::', 'virtual': False, 'destructor': False, 'returns': 'void', 'template': False, 'friend': False, 'returns_class': False, 'extern': False, 'path': 'Alpha::AlphaClass', 'returns_pointer': 0, 'class': None, 'name': 'alphaMethod', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': True, 'constructor': False, 'debug': 'void alphaMethod ( ) ;', 'inline': False}
protected
private
}
class Alpha::Omega::OmegaClass
{
public
// Properties
{'line_number': 65, 'constant': 0, 'name': 'omegaString', 'reference': 0, 'type': 'string', 'static': 0, 'pointer': 0}
// Methods
{'line_number': 63, 'static': False, 'rtnType': 'void', 'const': False, 'parameters': [], 'namespace': 'Alpha::Omega::', 'virtual': False, 'destructor': False, 'returns': '', 'template': False, 'friend': False, 'returns_class': False, 'extern': False, 'path': 'Alpha::Omega::OmegaClass', 'returns_pointer': 0, 'class': None, 'name': 'OmegaClass', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': True, 'constructor': True, 'debug': 'OmegaClass ( ) ;', 'inline': False}
protected
private
}
// functions
{'line_number': 70, 'static': False, 'rtnType': 'int', 'const': False, 'parameters': [{'line_number': 70, 'constant': 0, 'name': 'i', 'reference': 0, 'type': 'int', 'static': 0, 'pointer': 0}], 'namespace': '', 'virtual': False, 'destructor': False, 'returns': 'int', 'template': False, 'friend': False, 'returns_class': False, 'extern': False, 'returns_pointer': 0, 'class': None, 'name': 'sampleFreeFunction', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': True, 'constructor': False, 'debug': 'int sampleFreeFunction ( int i ) {', 'inline': False}
{'line_number': 75, 'static': False, 'rtnType': 'int', 'const': False, 'parameters': [{'line_number': 75, 'constant': 0, 'name': '', 'reference': 0, 'type': 'void', 'static': 0, 'pointer': 0}], 'namespace': '', 'virtual': False, 'destructor': False, 'returns': 'int', 'template': False, 'friend': False, 'returns_class': False, 'extern': False, 'returns_pointer': 0, 'class': None, 'name': 'anotherFreeFunction', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': True, 'constructor': False, 'debug': 'int anotherFreeFunction ( void ) ;', 'inline': False}
Number of public methods 5
Number of private properties 2
Parameter Types for public method meth3 ['const string &', 'vector<string> &']
Return type for meth1:
string
Doxygen for meth2:
///
/// Method 2 description
///
/// @param v1 Variable 1
///
Parameters for meth3:
[{'line_number': 30, 'constant': 1, 'name': 'v1', 'reference': 1, 'type': 'const string &', 'static': 0, 'pointer': 0, 'desc': 'Variable 1'}, {'line_number': 30, 'constant': 0, 'name': 'v2', 'reference': 1, 'type': 'vector<string> &', 'static': 0, 'pointer': 0, 'desc': 'Variable 2'}]
Doxygen for meth4:
/**********************************
* Method 4 description
*
* @return Return value
*********************************/
Return type for meth5:
void *
Doxygen type for prop1:
/// prop1 description
Type for prop5:
int
Namespace for AlphaClass is:
Alpha
Return type for alphaMethod is:
void
Namespace for OmegaClass is:
Alpha::Omega
Type for omegaString is:
string
Free functions are:
sampleFreeFunction
anotherFreeFunction
#includes are:
<vector>
<string>
#defines are:
DEF_1 1
OS_NAME "Linux"
Contributors
------------
* Chris Love
* HartsAntler
Keywords: c++ header parser ply
Platform: Platform Independent
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 2
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: C++
Classifier: License :: OSI Approved :: BSD License
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development
Classifier: Topic :: Software Development :: Code Generators
Classifier: Topic :: Software Development :: Compilers
Classifier: Topic :: Software Development :: Disassemblers
Requires: ply

469
cppParser/Parse.py Normal file
View File

@ -0,0 +1,469 @@
#!/usr/bin/python
import os
import sys
import re
import ply.lex as lex
import inspect
import lutinDebug as debug
import lutinTools
import Class
tokens = [
'NUMBER',
'NAME',
'OPEN_PAREN',
'CLOSE_PAREN',
'OPEN_BRACE',
'CLOSE_BRACE',
'OPEN_SQUARE_BRACKET',
'CLOSE_SQUARE_BRACKET',
'COLON',
'SEMI_COLON',
'COMMA',
'TAB',
'BACKSLASH',
'PIPE',
'PERCENT',
'EXCLAMATION',
'CARET',
'COMMENT_SINGLELINE',
'COMMENT_MULTILINE',
'PRECOMP_MACRO',
'PRECOMP_MACRO_CONT',
'ASTERISK',
'AMPERSTAND',
'EQUALS',
'MINUS',
'PLUS',
'DIVIDE',
'CHAR_LITERAL',
'STRING_LITERAL',
'NEW_LINE',
'SQUOTE',
]
t_ignore = " \r.?@\f"
t_NUMBER = r'[0-9][0-9XxA-Fa-f]*'
t_NAME = r'[<>A-Za-z_~][A-Za-z0-9_]*'
t_OPEN_PAREN = r'\('
t_CLOSE_PAREN = r'\)'
t_OPEN_BRACE = r'{'
t_CLOSE_BRACE = r'}'
t_OPEN_SQUARE_BRACKET = r'\['
t_CLOSE_SQUARE_BRACKET = r'\]'
t_SEMI_COLON = r';'
t_COLON = r':'
t_COMMA = r','
t_TAB = r'\t'
t_BACKSLASH = r'\\'
t_PIPE = r'\|'
t_PERCENT = r'%'
t_CARET = r'\^'
t_EXCLAMATION = r'!'
t_PRECOMP_MACRO = r'\#.*'
t_PRECOMP_MACRO_CONT = r'.*\\\n'
def t_COMMENT_SINGLELINE(t):
r'\/\/.*\n'
global doxygenCommentCache
if t.value.startswith("///") or t.value.startswith("//!"):
if doxygenCommentCache:
doxygenCommentCache += "\n"
if t.value.endswith("\n"):
doxygenCommentCache += t.value[:-1]
else:
doxygenCommentCache += t.value
t.lexer.lineno += len(filter(lambda a: a=="\n", t.value))
t_ASTERISK = r'\*'
t_MINUS = r'\-'
t_PLUS = r'\+'
t_DIVIDE = r'/(?!/)'
t_AMPERSTAND = r'&'
t_EQUALS = r'='
t_CHAR_LITERAL = "'.'"
t_SQUOTE = "'"
#found at http://wordaligned.org/articles/string-literals-and-regular-expressions
#TODO: This does not work with the string "bla \" bla"
t_STRING_LITERAL = r'"([^"\\]|\\.)*"'
#Found at http://ostermiller.org/findcomment.html
def t_COMMENT_MULTILINE(t):
r'/\*([^*]|\n|(\*+([^*/]|\n)))*\*+/'
global doxygenCommentCache
if t.value.startswith("/**") or t.value.startswith("/*!"):
#not sure why, but get double new lines
v = t.value.replace("\n\n", "\n")
#strip prefixing whitespace
v = re.sub("\n[\s]+\*", "\n*", v)
doxygenCommentCache += v
t.lexer.lineno += len(filter(lambda a: a=="\n", t.value))
def t_NEWLINE(t):
r'\n+'
t.lexer.lineno += len(t.value)
def t_error(v):
print( "Lex error: ", v )
lex.lex()
class TagStr(str):
"""Wrapper for a string that allows us to store the line number associated with it"""
lineno_reg = {}
def __new__(cls,*args,**kw):
new_obj = str.__new__(cls,*args)
if "lineno" in kw:
TagStr.lineno_reg[id(new_obj)] = kw["lineno"]
return new_obj
def __del__(self):
try:
del TagStr.lineno_reg[id(self)]
except: pass
def lineno(self):
return TagStr.lineno_reg.get(id(self), -1)
doxygenCommentCache = ""
#Track what was added in what order and at what depth
parseHistory = []
def is_namespace(nameStack):
"""Determines if a namespace is being specified"""
if len(nameStack) == 0:
return False
if nameStack[0] == "namespace":
return True
return False
def is_enum_namestack(nameStack):
"""Determines if a namestack is an enum namestack"""
if len(nameStack) == 0:
return False
if nameStack[0] == "enum":
return True
if len(nameStack) > 1 \
and nameStack[0] == "typedef" \
and nameStack[1] == "enum":
return True
return False
def is_fundamental(s):
for a in s.split():
if a not in ["size_t", \
"struct", \
"union", \
"unsigned", \
"signed", \
"bool", \
"char", \
"short", \
"int", \
"float", \
"double", \
"long", \
"void", \
"*"]:
return False
return True
def is_function_pointer_stack(stack):
"""Count how many non-nested paranthesis are in the stack. Useful for determining if a stack is a function pointer"""
paren_depth = 0
paren_count = 0
star_after_first_paren = False
last_e = None
for e in stack:
if e == "(":
paren_depth += 1
elif e == ")" \
and paren_depth > 0:
paren_depth -= 1
if paren_depth == 0:
paren_count += 1
elif e == "*" \
and last_e == "(" \
and paren_count == 0 \
and paren_depth == 1:
star_after_first_paren = True
last_e = e
if star_after_first_paren and paren_count == 2:
return True
else:
return False
def is_method_namestack(stack):
r = False
if '(' not in stack:
r = False
elif stack[0] == 'typedef':
r = False # TODO deal with typedef function prototypes
#elif '=' in stack and stack.index('=') < stack.index('(') and stack[stack.index('=')-1] != 'operator': r = False #disabled July6th - allow all operators
elif 'operator' in stack:
r = True # allow all operators
elif '{' in stack \
and stack.index('{') < stack.index('('):
r = False # struct that looks like a method/class
elif '(' in stack \
and ')' in stack:
if '{' in stack \
and '}' in stack:
r = True
elif stack[-1] == ';':
if is_function_pointer_stack(stack):
r = False
else:
r = True
elif '{' in stack:
r = True # ideally we catch both braces... TODO
else:
r = False
#Test for case of property set to something with parens such as "static const int CONST_A = (1 << 7) - 1;"
if r \
and "(" in stack \
and "=" in stack \
and 'operator' not in stack:
if stack.index("=") < stack.index("("): r = False
return r
def is_property_namestack(nameStack):
r = False
if '(' not in nameStack \
and ')' not in nameStack:
r = True
elif "(" in nameStack \
and "=" in nameStack \
and nameStack.index("=") < nameStack.index("("):
r = True
#See if we are a function pointer
if not r \
and is_function_pointer_stack(nameStack):
r = True
return r
def detect_lineno(s):
"""Detect the line number for a given token string"""
try:
rtn = s.lineno()
if rtn != -1:
return rtn
except: pass
global curLine
return curLine
def filter_out_attribute_keyword(stack):
"""Strips __attribute__ and its parenthetical expression from the stack"""
if "__attribute__" not in stack:
return stack
try:
debug.debug("Stripping __attribute__ from %s"% stack)
attr_index = stack.index("__attribute__")
attr_end = attr_index + 1 #Assuming not followed by parenthetical expression which wont happen
#Find final paren
if stack[attr_index + 1] == '(':
paren_count = 1
for i in xrange(attr_index + 2, len(stack)):
elm = stack[i]
if elm == '(':
paren_count += 1
elif elm == ')':
paren_count -= 1
if paren_count == 0:
attr_end = i + 1
break
new_stack = stack[0:attr_index] + stack[attr_end:]
debug.debug("stripped stack is %s"% new_stack)
return new_stack
except:
return stack
supportedAccessSpecifier = [
'public',
'protected',
'private'
]
##
## @brief Join the class name element : ['class', 'Bar', ':', ':', 'Foo'] -> ['class', 'Bar::Foo']
## @param table Input table to convert. ex: [':', '\t', 'class', 'Bar', ':', ':', 'Foo']
## @return The new table. ex: ['class', 'Bar::Foo']
##
def create_compleate_class_name(table):
compleateLine = ""
compleateLine = compleateLine.join(table);
if "::" not in compleateLine:
return table
# we need to convert it :
out = []
for name in table:
if len(out) == 0:
out.append(name)
elif name == ":" \
and out[-1].endswith(":"):
out[-1] += name
elif out[-1].endswith("::"):
out[-2] += out[-1] + name
del out[-1]
else:
out.append(name)
return out
class parse_file():
def __init__(self, fileName):
self.m_classes = []
self.m_elementParseStack = []
debug.info("Parse File tod document : '" + fileName + "'")
self.headerFileName = fileName
self.anon_union_counter = [-1, 0]
# load all the file data :
headerFileStr = lutinTools.FileReadData(fileName)
# Make sure supportedAccessSpecifier are sane
for i in range(0, len(supportedAccessSpecifier)):
if " " not in supportedAccessSpecifier[i]: continue
supportedAccessSpecifier[i] = re.sub("[ ]+", " ", supportedAccessSpecifier[i]).strip()
# Strip out template declarations
# TODO : What is the real need ???
headerFileStr = re.sub("template[\t ]*<[^>]*>", "", headerFileStr)
# remove all needed \r unneeded ==> this simplify next resExp ...
headerFileStr = re.sub("\r", "\r\n", headerFileStr)
headerFileStr = re.sub("\r\n\n", "\r\n", headerFileStr)
headerFileStr = re.sub("\r", "", headerFileStr)
# TODO : Can generate some error ...
headerFileStr = re.sub("\#if 0(.*?)(\#endif|\#else)", "", headerFileStr, flags=re.DOTALL)
debug.debug(headerFileStr)
# Change multi line #defines and expressions to single lines maintaining line nubmers
matches = re.findall(r'(?m)^(?:.*\\\n)+.*$', headerFileStr)
is_define = re.compile(r'[ \t\v]*#[Dd][Ee][Ff][Ii][Nn][Ee]')
for m in matches:
#Keep the newlines so that linecount doesnt break
num_newlines = len(filter(lambda a: a=="\n", m))
if is_define.match(m):
new_m = m.replace("\n", "<**multiLine**>\\n")
else:
# Just expression taking up multiple lines, make it take 1 line for easier parsing
new_m = m.replace("\\\n", " ")
if (num_newlines > 0):
new_m += "\n"*(num_newlines)
headerFileStr = headerFileStr.replace(m, new_m)
#Filter out Extern "C" statements. These are order dependent
headerFileStr = re.sub(r'extern( |\t)+"[Cc]"( |\t)*{', "{", headerFileStr)
###### debug.info(headerFileStr)
self.stack = [] # token stack to find the namespace and the element name ...
self.nameStack = [] #
self.braceDepth = 0
lex.lex()
lex.input(headerFileStr)
global curLine
global curChar
curLine = 0
curChar = 0
while True:
tok = lex.token()
if not tok:
break
tok.value = TagStr(tok.value, lineno=tok.lineno)
debug.debug("TOK: " + str(tok))
self.stack.append( tok.value )
curLine = tok.lineno
curChar = tok.lexpos
if (tok.type in ('PRECOMP_MACRO', 'PRECOMP_MACRO_CONT')):
debug.debug("PRECOMP: " + str(tok))
self.stack = []
self.nameStack = []
# Do nothing for macro ==> many time not needed ...
continue
if (tok.type == 'OPEN_BRACE'):
# When we open a brace, this is the time to parse the stack ...
# Clean the stack : (remove \t\r\n , and concatenate the 'xx', ':', ':', 'yy' in 'xx::yy',
self.nameStack = create_compleate_class_name(self.nameStack)
if len(self.nameStack) <= 0:
#open brace with no name ...
debug.warning("[" + str(self.braceDepth) + "] find an empty stack ...")
elif 'namespace' in self.nameStack:
debug.info("[" + str(self.braceDepth) + "] find a namespace : " + str(self.nameStack));
elif 'class' in self.nameStack:
debug.info("[" + str(self.braceDepth) + "] find a class : " + str(self.nameStack));
elif 'enum' in self.nameStack:
debug.info("[" + str(self.braceDepth) + "] find a enum : " + str(self.nameStack));
elif 'struct' in self.nameStack:
debug.info("[" + str(self.braceDepth) + "] find a struct : " + str(self.nameStack));
elif 'typedef' in self.nameStack:
debug.info("[" + str(self.braceDepth) + "] find a typedef : " + str(self.nameStack));
elif 'union' in self.nameStack:
debug.info("[" + str(self.braceDepth) + "] find a union : " + str(self.nameStack));
else:
debug.warning("[" + str(self.braceDepth) + "] find an unknow stack : " + str(self.nameStack))
self.stack = []
self.nameStack = []
self.braceDepth += 1
elif tok.type == 'CLOSE_BRACE':
self.braceDepth -= 1
debug.info("[" + str(self.braceDepth) + "] close brace");
if len(self.m_elementParseStack) != 0 \
and self.m_elementParseStack[len(self.m_elementParseStack)-1]['level'] == self.braceDepth :
self.m_elementParseStack.pop()
if tok.type == 'OPEN_PAREN':
self.nameStack.append(tok.value)
elif tok.type == 'CLOSE_PAREN':
self.nameStack.append(tok.value)
elif tok.type == 'OPEN_SQUARE_BRACKET':
self.nameStack.append(tok.value)
elif tok.type == 'CLOSE_SQUARE_BRACKET':
self.nameStack.append(tok.value)
elif tok.type == 'TAB':
pass
elif tok.type == 'EQUALS':
self.nameStack.append(tok.value)
elif tok.type == 'COMMA':
self.nameStack.append(tok.value)
elif tok.type == 'BACKSLASH':
self.nameStack.append(tok.value)
elif tok.type == 'PIPE':
self.nameStack.append(tok.value)
elif tok.type == 'PERCENT':
self.nameStack.append(tok.value)
elif tok.type == 'CARET':
self.nameStack.append(tok.value)
elif tok.type == 'EXCLAMATION':
self.nameStack.append(tok.value)
elif tok.type == 'SQUOTE':
pass
elif tok.type == 'NUMBER':
self.nameStack.append(tok.value)
elif tok.type == 'MINUS':
self.nameStack.append(tok.value)
elif tok.type == 'PLUS':
self.nameStack.append(tok.value)
elif tok.type == 'STRING_LITERAL':
self.nameStack.append(tok.value)
elif tok.type == 'NAME' \
or tok.type == 'AMPERSTAND' \
or tok.type == 'ASTERISK' \
or tok.type == 'CHAR_LITERAL':
self.nameStack.append(tok.value)
elif tok.type == 'COLON':
if self.nameStack[0] in ['private', 'protected', 'public']:
debug.info("[" + str(self.braceDepth) + "] change visibility : " + self.nameStack[0]);
self.nameStack = []
self.stack = []
else :
self.nameStack.append(tok.value)
elif tok.type == 'SEMI_COLON':
if len(self.nameStack) != 0:
debug.info("[" + str(self.braceDepth) + "] semicolumn : " + str(self.nameStack));
self.stack = []
self.nameStack = []

View File

@ -1,598 +0,0 @@
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.9.1: http://docutils.sourceforge.net/" />
<title></title>
<style type="text/css">
/*
:Author: David Goodger (goodger@python.org)
:Id: $Id: html4css1.css 7434 2012-05-11 21:06:27Z milde $
:Copyright: This stylesheet has been placed in the public domain.
Default cascading style sheet for the HTML output of Docutils.
See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
customize this style sheet.
*/
/* used to remove borders from tables and images */
.borderless, table.borderless td, table.borderless th {
border: 0 }
table.borderless td, table.borderless th {
/* Override padding for "table.docutils td" with "! important".
The right padding separates the table cells. */
padding: 0 0.5em 0 0 ! important }
.first {
/* Override more specific margin styles with "! important". */
margin-top: 0 ! important }
.last, .with-subtitle {
margin-bottom: 0 ! important }
.hidden {
display: none }
a.toc-backref {
text-decoration: none ;
color: black }
blockquote.epigraph {
margin: 2em 5em ; }
dl.docutils dd {
margin-bottom: 0.5em }
object[type="image/svg+xml"], object[type="application/x-shockwave-flash"] {
overflow: hidden;
}
/* Uncomment (and remove this text!) to get bold-faced definition list terms
dl.docutils dt {
font-weight: bold }
*/
div.abstract {
margin: 2em 5em }
div.abstract p.topic-title {
font-weight: bold ;
text-align: center }
div.admonition, div.attention, div.caution, div.danger, div.error,
div.hint, div.important, div.note, div.tip, div.warning {
margin: 2em ;
border: medium outset ;
padding: 1em }
div.admonition p.admonition-title, div.hint p.admonition-title,
div.important p.admonition-title, div.note p.admonition-title,
div.tip p.admonition-title {
font-weight: bold ;
font-family: sans-serif }
div.attention p.admonition-title, div.caution p.admonition-title,
div.danger p.admonition-title, div.error p.admonition-title,
div.warning p.admonition-title {
color: red ;
font-weight: bold ;
font-family: sans-serif }
/* Uncomment (and remove this text!) to get reduced vertical space in
compound paragraphs.
div.compound .compound-first, div.compound .compound-middle {
margin-bottom: 0.5em }
div.compound .compound-last, div.compound .compound-middle {
margin-top: 0.5em }
*/
div.dedication {
margin: 2em 5em ;
text-align: center ;
font-style: italic }
div.dedication p.topic-title {
font-weight: bold ;
font-style: normal }
div.figure {
margin-left: 2em ;
margin-right: 2em }
div.footer, div.header {
clear: both;
font-size: smaller }
div.line-block {
display: block ;
margin-top: 1em ;
margin-bottom: 1em }
div.line-block div.line-block {
margin-top: 0 ;
margin-bottom: 0 ;
margin-left: 1.5em }
div.sidebar {
margin: 0 0 0.5em 1em ;
border: medium outset ;
padding: 1em ;
background-color: #ffffee ;
width: 40% ;
float: right ;
clear: right }
div.sidebar p.rubric {
font-family: sans-serif ;
font-size: medium }
div.system-messages {
margin: 5em }
div.system-messages h1 {
color: red }
div.system-message {
border: medium outset ;
padding: 1em }
div.system-message p.system-message-title {
color: red ;
font-weight: bold }
div.topic {
margin: 2em }
h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
margin-top: 0.4em }
h1.title {
text-align: center }
h2.subtitle {
text-align: center }
hr.docutils {
width: 75% }
img.align-left, .figure.align-left, object.align-left {
clear: left ;
float: left ;
margin-right: 1em }
img.align-right, .figure.align-right, object.align-right {
clear: right ;
float: right ;
margin-left: 1em }
img.align-center, .figure.align-center, object.align-center {
display: block;
margin-left: auto;
margin-right: auto;
}
.align-left {
text-align: left }
.align-center {
clear: both ;
text-align: center }
.align-right {
text-align: right }
/* reset inner alignment in figures */
div.align-right {
text-align: inherit }
/* div.align-center * { */
/* text-align: left } */
ol.simple, ul.simple {
margin-bottom: 1em }
ol.arabic {
list-style: decimal }
ol.loweralpha {
list-style: lower-alpha }
ol.upperalpha {
list-style: upper-alpha }
ol.lowerroman {
list-style: lower-roman }
ol.upperroman {
list-style: upper-roman }
p.attribution {
text-align: right ;
margin-left: 50% }
p.caption {
font-style: italic }
p.credits {
font-style: italic ;
font-size: smaller }
p.label {
white-space: nowrap }
p.rubric {
font-weight: bold ;
font-size: larger ;
color: maroon ;
text-align: center }
p.sidebar-title {
font-family: sans-serif ;
font-weight: bold ;
font-size: larger }
p.sidebar-subtitle {
font-family: sans-serif ;
font-weight: bold }
p.topic-title {
font-weight: bold }
pre.address {
margin-bottom: 0 ;
margin-top: 0 ;
font: inherit }
pre.literal-block, pre.doctest-block, pre.math, pre.code {
margin-left: 2em ;
margin-right: 2em }
pre.code .ln { /* line numbers */
color: grey;
}
.code {
background-color: #eeeeee
}
span.classifier {
font-family: sans-serif ;
font-style: oblique }
span.classifier-delimiter {
font-family: sans-serif ;
font-weight: bold }
span.interpreted {
font-family: sans-serif }
span.option {
white-space: nowrap }
span.pre {
white-space: pre }
span.problematic {
color: red }
span.section-subtitle {
/* font-size relative to parent (h1..h6 element) */
font-size: 80% }
table.citation {
border-left: solid 1px gray;
margin-left: 1px }
table.docinfo {
margin: 2em 4em }
table.docutils {
margin-top: 0.5em ;
margin-bottom: 0.5em }
table.footnote {
border-left: solid 1px black;
margin-left: 1px }
table.docutils td, table.docutils th,
table.docinfo td, table.docinfo th {
padding-left: 0.5em ;
padding-right: 0.5em ;
vertical-align: top }
table.docutils th.field-name, table.docinfo th.docinfo-name {
font-weight: bold ;
text-align: left ;
white-space: nowrap ;
padding-left: 0 }
h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
font-size: 100% }
ul.auto-toc {
list-style-type: none }
/*customization*/
pre.literal-block{
color: #6A6A6A;
}
</style>
</head>
<body>
<div class="document">
<div class="section" id="python-package-cppheaderparser">
<h1>Python package &quot;CppHeaderParser&quot;</h1>
<p><strong>Purpose:</strong> Parse C++ header files and generate a data structure representing the class</p>
<p><strong>Author:</strong> Jashua Cloutier</p>
<p><strong>Licence:</strong> BSD</p>
<p><strong>External modules required:</strong> PLY</p>
<p><strong>Quick start</strong>:</p>
<pre class="literal-block" width="1200px" style="max-width: 1200px">
#include &lt;vector&gt;
#include &lt;string&gt;
#define DEF_1 1
#define OS_NAME &quot;Linux&quot;
using namespace std;
class SampleClass
{
public:
SampleClass();
/*!
* Method 1
*/
string meth1();
///
/// Method 2 description
///
/// &#64;param v1 Variable 1
///
int meth2(int v1);
/**
* Method 3 description
*
* \param v1 Variable 1
* \param v2 Variable 2
*/
void meth3(const string &amp; v1, vector&lt;string&gt; &amp; v2);
/**********************************
* Method 4 description
*
* &#64;return Return value
*********************************/
unsigned int meth4();
private:
void * meth5(){return NULL};
/// prop1 description
string prop1;
//! prop5 description
int prop5;
};
namespace Alpha
{
class AlphaClass
{
public:
AlphaClass();
void alphaMethod();
string alphaString;
};
namespace Omega
{
class OmegaClass
{
public:
OmegaClass();
string omegaString;
};
};
}
int sampleFreeFunction(int i)
{
return i + 1;
}
int anotherFreeFunction(void);
}
</pre>
<p><strong>Python code</strong>:</p>
<pre class="literal-block" width="1200px" style="max-width: 1200px">
#!/usr/bin/python
import sys
sys.path = [&quot;../&quot;] + sys.path
import CppHeaderParser
try:
cppHeader = CppHeaderParser.CppHeader(&quot;SampleClass.h&quot;)
except CppHeaderParser.CppParseError, e:
print e
sys.exit(1)
print &quot;CppHeaderParser view of %s&quot;%cppHeader
sampleClass = cppHeader.classes[&quot;SampleClass&quot;]
print &quot;Number of public methods %d&quot;%(len(sampleClass[&quot;methods&quot;][&quot;public&quot;]))
print &quot;Number of private properties %d&quot;%(len(sampleClass[&quot;properties&quot;][&quot;private&quot;]))
meth3 = [m for m in sampleClass[&quot;methods&quot;][&quot;public&quot;] if m[&quot;name&quot;] == &quot;meth3&quot;][0] #get meth3
meth3ParamTypes = [t[&quot;type&quot;] for t in meth3[&quot;parameters&quot;]] #get meth3s parameters
print &quot;Parameter Types for public method meth3 %s&quot;%(meth3ParamTypes)
print &quot;\nReturn type for meth1:&quot;
print cppHeader.classes[&quot;SampleClass&quot;][&quot;methods&quot;][&quot;public&quot;][1][&quot;rtnType&quot;]
print &quot;\nDoxygen for meth2:&quot;
print cppHeader.classes[&quot;SampleClass&quot;][&quot;methods&quot;][&quot;public&quot;][2][&quot;doxygen&quot;]
print &quot;\nParameters for meth3:&quot;
print cppHeader.classes[&quot;SampleClass&quot;][&quot;methods&quot;][&quot;public&quot;][3][&quot;parameters&quot;]
print &quot;\nDoxygen for meth4:&quot;
print cppHeader.classes[&quot;SampleClass&quot;][&quot;methods&quot;][&quot;public&quot;][4][&quot;doxygen&quot;]
print &quot;\nReturn type for meth5:&quot;
print cppHeader.classes[&quot;SampleClass&quot;][&quot;methods&quot;][&quot;private&quot;][0][&quot;rtnType&quot;]
print &quot;\nDoxygen type for prop1:&quot;
print cppHeader.classes[&quot;SampleClass&quot;][&quot;properties&quot;][&quot;private&quot;][0][&quot;doxygen&quot;]
print &quot;\nType for prop5:&quot;
print cppHeader.classes[&quot;SampleClass&quot;][&quot;properties&quot;][&quot;private&quot;][1][&quot;type&quot;]
print &quot;\nNamespace for AlphaClass is:&quot;
print cppHeader.classes[&quot;AlphaClass&quot;][&quot;namespace&quot;]
print &quot;\nReturn type for alphaMethod is:&quot;
print cppHeader.classes[&quot;AlphaClass&quot;][&quot;methods&quot;][&quot;public&quot;][0][&quot;rtnType&quot;]
print &quot;\nNamespace for OmegaClass is:&quot;
print cppHeader.classes[&quot;OmegaClass&quot;][&quot;namespace&quot;]
print &quot;\nType for omegaString is:&quot;
print cppHeader.classes[&quot;AlphaClass&quot;][&quot;properties&quot;][&quot;public&quot;][0][&quot;type&quot;]
print &quot;\nFree functions are:&quot;
for func in cppHeader.functions:
print &quot; %s&quot;%func[&quot;name&quot;]
print &quot;\n#includes are:&quot;
for incl in cppHeader.includes:
print &quot; %s&quot;%incl
print &quot;\n#defines are:&quot;
for define in cppHeader.defines:
print &quot; %s&quot;%define
</pre>
<p><strong>Output</strong>:</p>
<pre class="literal-block" width="1200px" style="max-width: 1200px">
CppHeaderParser view of class SampleClass
{
public
// Methods
{'line_number': 11, 'static': False, 'rtnType': 'void', 'const': False, 'parameters': [], 'namespace': '', 'virtual': False, 'destructor': False, 'returns': '', 'template': False, 'friend': False, 'returns_class': False, 'extern': False, 'path': 'SampleClass', 'returns_pointer': 0, 'class': None, 'name': 'SampleClass', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': True, 'constructor': True, 'debug': 'SampleClass ( ) ;', 'inline': False}
{'line_number': 15, 'static': False, 'rtnType': 'string', 'returns_unknown': True, 'const': False, 'parameters': [], 'namespace': '', 'virtual': False, 'destructor': False, 'returns': 'string', 'template': False, 'friend': False, 'returns_class': False, 'inline': False, 'extern': False, 'path': 'SampleClass', 'class': None, 'doxygen': '/*!\n* Method 1\n*/', 'name': 'meth1', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': False, 'constructor': False, 'debug': 'string meth1 ( ) ;', 'returns_pointer': 0}
{'line_number': 22, 'static': False, 'rtnType': 'int', 'const': False, 'parameters': [{'line_number': 22, 'constant': 0, 'name': 'v1', 'reference': 0, 'type': 'int', 'static': 0, 'pointer': 0, 'desc': 'Variable 1'}], 'namespace': '', 'virtual': False, 'destructor': False, 'returns': 'int', 'template': False, 'friend': False, 'returns_class': False, 'inline': False, 'extern': False, 'path': 'SampleClass', 'class': None, 'doxygen': '///\n/// Method 2 description\n///\n/// &#64;param v1 Variable 1\n///', 'name': 'meth2', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': True, 'constructor': False, 'debug': 'int meth2 ( int v1 ) ;', 'returns_pointer': 0}
{'line_number': 30, 'static': False, 'rtnType': 'void', 'const': False, 'parameters': [{'line_number': 30, 'constant': 1, 'name': 'v1', 'reference': 1, 'type': 'const string &amp;', 'static': 0, 'pointer': 0, 'desc': 'Variable 1'}, {'line_number': 30, 'constant': 0, 'name': 'v2', 'reference': 1, 'type': 'vector&lt;string&gt; &amp;', 'static': 0, 'pointer': 0, 'desc': 'Variable 2'}], 'namespace': '', 'virtual': False, 'destructor': False, 'returns': 'void', 'template': False, 'friend': False, 'unresolved_parameters': True, 'returns_class': False, 'inline': False, 'extern': False, 'path': 'SampleClass', 'class': None, 'doxygen': '/**\n* Method 3 description\n*\n* \\param v1 Variable 1\n* \\param v2 Variable 2\n*/', 'name': 'meth3', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': True, 'constructor': False, 'debug': 'void meth3 ( const string &amp; v1 , vector &lt;string&gt; &amp; v2 ) ;', 'returns_pointer': 0}
{'line_number': 37, 'static': False, 'rtnType': 'unsigned int', 'const': False, 'parameters': [], 'namespace': '', 'virtual': False, 'destructor': False, 'returns': 'unsigned int', 'template': False, 'friend': False, 'returns_class': False, 'inline': False, 'extern': False, 'path': 'SampleClass', 'class': None, 'doxygen': '/**********************************\n* Method 4 description\n*\n* &#64;return Return value\n*********************************/', 'name': 'meth4', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': True, 'constructor': False, 'debug': 'unsigned int meth4 ( ) ;', 'returns_pointer': 0}
protected
private
// Properties
{'line_number': 42, 'constant': 0, 'name': 'prop1', 'reference': 0, 'type': 'string', 'static': 0, 'pointer': 0}
{'line_number': 44, 'constant': 0, 'name': 'prop5', 'reference': 0, 'type': 'int', 'static': 0, 'pointer': 0}
// Methods
{'line_number': 39, 'static': False, 'rtnType': 'void *', 'const': False, 'parameters': [], 'namespace': '', 'virtual': False, 'destructor': False, 'returns': 'void', 'template': False, 'friend': False, 'returns_class': False, 'extern': False, 'path': 'SampleClass', 'returns_pointer': 1, 'class': None, 'name': 'meth5', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': True, 'constructor': False, 'debug': 'void * meth5 ( ) {', 'inline': False}
}
class Alpha::AlphaClass
{
public
// Properties
{'line_number': 55, 'constant': 0, 'name': 'alphaString', 'reference': 0, 'type': 'string', 'static': 0, 'pointer': 0}
// Methods
{'line_number': 51, 'static': False, 'rtnType': 'void', 'const': False, 'parameters': [], 'namespace': 'Alpha::', 'virtual': False, 'destructor': False, 'returns': '', 'template': False, 'friend': False, 'returns_class': False, 'extern': False, 'path': 'Alpha::AlphaClass', 'returns_pointer': 0, 'class': None, 'name': 'AlphaClass', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': True, 'constructor': True, 'debug': 'AlphaClass ( ) ;', 'inline': False}
{'line_number': 53, 'static': False, 'rtnType': 'void', 'const': False, 'parameters': [], 'namespace': 'Alpha::', 'virtual': False, 'destructor': False, 'returns': 'void', 'template': False, 'friend': False, 'returns_class': False, 'extern': False, 'path': 'Alpha::AlphaClass', 'returns_pointer': 0, 'class': None, 'name': 'alphaMethod', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': True, 'constructor': False, 'debug': 'void alphaMethod ( ) ;', 'inline': False}
protected
private
}
class Alpha::Omega::OmegaClass
{
public
// Properties
{'line_number': 65, 'constant': 0, 'name': 'omegaString', 'reference': 0, 'type': 'string', 'static': 0, 'pointer': 0}
// Methods
{'line_number': 63, 'static': False, 'rtnType': 'void', 'const': False, 'parameters': [], 'namespace': 'Alpha::Omega::', 'virtual': False, 'destructor': False, 'returns': '', 'template': False, 'friend': False, 'returns_class': False, 'extern': False, 'path': 'Alpha::Omega::OmegaClass', 'returns_pointer': 0, 'class': None, 'name': 'OmegaClass', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': True, 'constructor': True, 'debug': 'OmegaClass ( ) ;', 'inline': False}
protected
private
}
// functions
{'line_number': 70, 'static': False, 'rtnType': 'int', 'const': False, 'parameters': [{'line_number': 70, 'constant': 0, 'name': 'i', 'reference': 0, 'type': 'int', 'static': 0, 'pointer': 0}], 'namespace': '', 'virtual': False, 'destructor': False, 'returns': 'int', 'template': False, 'friend': False, 'returns_class': False, 'extern': False, 'returns_pointer': 0, 'class': None, 'name': 'sampleFreeFunction', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': True, 'constructor': False, 'debug': 'int sampleFreeFunction ( int i ) {', 'inline': False}
{'line_number': 75, 'static': False, 'rtnType': 'int', 'const': False, 'parameters': [{'line_number': 75, 'constant': 0, 'name': '', 'reference': 0, 'type': 'void', 'static': 0, 'pointer': 0}], 'namespace': '', 'virtual': False, 'destructor': False, 'returns': 'int', 'template': False, 'friend': False, 'returns_class': False, 'extern': False, 'returns_pointer': 0, 'class': None, 'name': 'anotherFreeFunction', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': True, 'constructor': False, 'debug': 'int anotherFreeFunction ( void ) ;', 'inline': False}
Number of public methods 5
Number of private properties 2
Parameter Types for public method meth3 ['const string &amp;', 'vector&lt;string&gt; &amp;']
Return type for meth1:
string
Doxygen for meth2:
///
/// Method 2 description
///
/// &#64;param v1 Variable 1
///
Parameters for meth3:
[{'line_number': 30, 'constant': 1, 'name': 'v1', 'reference': 1, 'type': 'const string &amp;', 'static': 0, 'pointer': 0, 'desc': 'Variable 1'}, {'line_number': 30, 'constant': 0, 'name': 'v2', 'reference': 1, 'type': 'vector&lt;string&gt; &amp;', 'static': 0, 'pointer': 0, 'desc': 'Variable 2'}]
Doxygen for meth4:
/**********************************
* Method 4 description
*
* &#64;return Return value
*********************************/
Return type for meth5:
void *
Doxygen type for prop1:
/// prop1 description
Type for prop5:
int
Namespace for AlphaClass is:
Alpha
Return type for alphaMethod is:
void
Namespace for OmegaClass is:
Alpha::Omega
Type for omegaString is:
string
Free functions are:
sampleFreeFunction
anotherFreeFunction
#includes are:
&lt;vector&gt;
&lt;string&gt;
#defines are:
DEF_1 1
OS_NAME &quot;Linux&quot;
</pre>
</div>
<div class="section" id="contributors">
<h1>Contributors</h1>
<ul class="simple">
<li>Chris Love</li>
<li>HartsAntler</li>
</ul>
</div>
</div>
</body>
</html>

View File

@ -1,266 +0,0 @@
Python package "CppHeaderParser"
--------------------------------
**Purpose:** Parse C++ header files and generate a data structure representing the class
**Author:** Jashua Cloutier
**Licence:** BSD
**External modules required:** PLY
**Quick start**::
#include <vector>
#include <string>
#define DEF_1 1
#define OS_NAME "Linux"
using namespace std;
class SampleClass
{
public:
SampleClass();
/*!
* Method 1
*/
string meth1();
///
/// Method 2 description
///
/// @param v1 Variable 1
///
int meth2(int v1);
/**
* Method 3 description
*
* \param v1 Variable 1
* \param v2 Variable 2
*/
void meth3(const string & v1, vector<string> & v2);
/**********************************
* Method 4 description
*
* @return Return value
*********************************/
unsigned int meth4();
private:
void * meth5(){return NULL};
/// prop1 description
string prop1;
//! prop5 description
int prop5;
};
namespace Alpha
{
class AlphaClass
{
public:
AlphaClass();
void alphaMethod();
string alphaString;
};
namespace Omega
{
class OmegaClass
{
public:
OmegaClass();
string omegaString;
};
};
}
int sampleFreeFunction(int i)
{
return i + 1;
}
int anotherFreeFunction(void);
}
**Python code**::
#!/usr/bin/python
import sys
sys.path = ["../"] + sys.path
import CppHeaderParser
try:
cppHeader = CppHeaderParser.CppHeader("SampleClass.h")
except CppHeaderParser.CppParseError, e:
print e
sys.exit(1)
print "CppHeaderParser view of %s"%cppHeader
sampleClass = cppHeader.classes["SampleClass"]
print "Number of public methods %d"%(len(sampleClass["methods"]["public"]))
print "Number of private properties %d"%(len(sampleClass["properties"]["private"]))
meth3 = [m for m in sampleClass["methods"]["public"] if m["name"] == "meth3"][0] #get meth3
meth3ParamTypes = [t["type"] for t in meth3["parameters"]] #get meth3s parameters
print "Parameter Types for public method meth3 %s"%(meth3ParamTypes)
print "\nReturn type for meth1:"
print cppHeader.classes["SampleClass"]["methods"]["public"][1]["rtnType"]
print "\nDoxygen for meth2:"
print cppHeader.classes["SampleClass"]["methods"]["public"][2]["doxygen"]
print "\nParameters for meth3:"
print cppHeader.classes["SampleClass"]["methods"]["public"][3]["parameters"]
print "\nDoxygen for meth4:"
print cppHeader.classes["SampleClass"]["methods"]["public"][4]["doxygen"]
print "\nReturn type for meth5:"
print cppHeader.classes["SampleClass"]["methods"]["private"][0]["rtnType"]
print "\nDoxygen type for prop1:"
print cppHeader.classes["SampleClass"]["properties"]["private"][0]["doxygen"]
print "\nType for prop5:"
print cppHeader.classes["SampleClass"]["properties"]["private"][1]["type"]
print "\nNamespace for AlphaClass is:"
print cppHeader.classes["AlphaClass"]["namespace"]
print "\nReturn type for alphaMethod is:"
print cppHeader.classes["AlphaClass"]["methods"]["public"][0]["rtnType"]
print "\nNamespace for OmegaClass is:"
print cppHeader.classes["OmegaClass"]["namespace"]
print "\nType for omegaString is:"
print cppHeader.classes["AlphaClass"]["properties"]["public"][0]["type"]
print "\nFree functions are:"
for func in cppHeader.functions:
print " %s"%func["name"]
print "\n#includes are:"
for incl in cppHeader.includes:
print " %s"%incl
print "\n#defines are:"
for define in cppHeader.defines:
print " %s"%define
**Output**::
CppHeaderParser view of class SampleClass
{
public
// Methods
{'line_number': 11, 'static': False, 'rtnType': 'void', 'const': False, 'parameters': [], 'namespace': '', 'virtual': False, 'destructor': False, 'returns': '', 'template': False, 'friend': False, 'returns_class': False, 'extern': False, 'path': 'SampleClass', 'returns_pointer': 0, 'class': None, 'name': 'SampleClass', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': True, 'constructor': True, 'debug': 'SampleClass ( ) ;', 'inline': False}
{'line_number': 15, 'static': False, 'rtnType': 'string', 'returns_unknown': True, 'const': False, 'parameters': [], 'namespace': '', 'virtual': False, 'destructor': False, 'returns': 'string', 'template': False, 'friend': False, 'returns_class': False, 'inline': False, 'extern': False, 'path': 'SampleClass', 'class': None, 'doxygen': '/*!\n* Method 1\n*/', 'name': 'meth1', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': False, 'constructor': False, 'debug': 'string meth1 ( ) ;', 'returns_pointer': 0}
{'line_number': 22, 'static': False, 'rtnType': 'int', 'const': False, 'parameters': [{'line_number': 22, 'constant': 0, 'name': 'v1', 'reference': 0, 'type': 'int', 'static': 0, 'pointer': 0, 'desc': 'Variable 1'}], 'namespace': '', 'virtual': False, 'destructor': False, 'returns': 'int', 'template': False, 'friend': False, 'returns_class': False, 'inline': False, 'extern': False, 'path': 'SampleClass', 'class': None, 'doxygen': '///\n/// Method 2 description\n///\n/// @param v1 Variable 1\n///', 'name': 'meth2', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': True, 'constructor': False, 'debug': 'int meth2 ( int v1 ) ;', 'returns_pointer': 0}
{'line_number': 30, 'static': False, 'rtnType': 'void', 'const': False, 'parameters': [{'line_number': 30, 'constant': 1, 'name': 'v1', 'reference': 1, 'type': 'const string &', 'static': 0, 'pointer': 0, 'desc': 'Variable 1'}, {'line_number': 30, 'constant': 0, 'name': 'v2', 'reference': 1, 'type': 'vector<string> &', 'static': 0, 'pointer': 0, 'desc': 'Variable 2'}], 'namespace': '', 'virtual': False, 'destructor': False, 'returns': 'void', 'template': False, 'friend': False, 'unresolved_parameters': True, 'returns_class': False, 'inline': False, 'extern': False, 'path': 'SampleClass', 'class': None, 'doxygen': '/**\n* Method 3 description\n*\n* \\param v1 Variable 1\n* \\param v2 Variable 2\n*/', 'name': 'meth3', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': True, 'constructor': False, 'debug': 'void meth3 ( const string & v1 , vector <string> & v2 ) ;', 'returns_pointer': 0}
{'line_number': 37, 'static': False, 'rtnType': 'unsigned int', 'const': False, 'parameters': [], 'namespace': '', 'virtual': False, 'destructor': False, 'returns': 'unsigned int', 'template': False, 'friend': False, 'returns_class': False, 'inline': False, 'extern': False, 'path': 'SampleClass', 'class': None, 'doxygen': '/**********************************\n* Method 4 description\n*\n* @return Return value\n*********************************/', 'name': 'meth4', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': True, 'constructor': False, 'debug': 'unsigned int meth4 ( ) ;', 'returns_pointer': 0}
protected
private
// Properties
{'line_number': 42, 'constant': 0, 'name': 'prop1', 'reference': 0, 'type': 'string', 'static': 0, 'pointer': 0}
{'line_number': 44, 'constant': 0, 'name': 'prop5', 'reference': 0, 'type': 'int', 'static': 0, 'pointer': 0}
// Methods
{'line_number': 39, 'static': False, 'rtnType': 'void *', 'const': False, 'parameters': [], 'namespace': '', 'virtual': False, 'destructor': False, 'returns': 'void', 'template': False, 'friend': False, 'returns_class': False, 'extern': False, 'path': 'SampleClass', 'returns_pointer': 1, 'class': None, 'name': 'meth5', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': True, 'constructor': False, 'debug': 'void * meth5 ( ) {', 'inline': False}
}
class Alpha::AlphaClass
{
public
// Properties
{'line_number': 55, 'constant': 0, 'name': 'alphaString', 'reference': 0, 'type': 'string', 'static': 0, 'pointer': 0}
// Methods
{'line_number': 51, 'static': False, 'rtnType': 'void', 'const': False, 'parameters': [], 'namespace': 'Alpha::', 'virtual': False, 'destructor': False, 'returns': '', 'template': False, 'friend': False, 'returns_class': False, 'extern': False, 'path': 'Alpha::AlphaClass', 'returns_pointer': 0, 'class': None, 'name': 'AlphaClass', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': True, 'constructor': True, 'debug': 'AlphaClass ( ) ;', 'inline': False}
{'line_number': 53, 'static': False, 'rtnType': 'void', 'const': False, 'parameters': [], 'namespace': 'Alpha::', 'virtual': False, 'destructor': False, 'returns': 'void', 'template': False, 'friend': False, 'returns_class': False, 'extern': False, 'path': 'Alpha::AlphaClass', 'returns_pointer': 0, 'class': None, 'name': 'alphaMethod', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': True, 'constructor': False, 'debug': 'void alphaMethod ( ) ;', 'inline': False}
protected
private
}
class Alpha::Omega::OmegaClass
{
public
// Properties
{'line_number': 65, 'constant': 0, 'name': 'omegaString', 'reference': 0, 'type': 'string', 'static': 0, 'pointer': 0}
// Methods
{'line_number': 63, 'static': False, 'rtnType': 'void', 'const': False, 'parameters': [], 'namespace': 'Alpha::Omega::', 'virtual': False, 'destructor': False, 'returns': '', 'template': False, 'friend': False, 'returns_class': False, 'extern': False, 'path': 'Alpha::Omega::OmegaClass', 'returns_pointer': 0, 'class': None, 'name': 'OmegaClass', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': True, 'constructor': True, 'debug': 'OmegaClass ( ) ;', 'inline': False}
protected
private
}
// functions
{'line_number': 70, 'static': False, 'rtnType': 'int', 'const': False, 'parameters': [{'line_number': 70, 'constant': 0, 'name': 'i', 'reference': 0, 'type': 'int', 'static': 0, 'pointer': 0}], 'namespace': '', 'virtual': False, 'destructor': False, 'returns': 'int', 'template': False, 'friend': False, 'returns_class': False, 'extern': False, 'returns_pointer': 0, 'class': None, 'name': 'sampleFreeFunction', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': True, 'constructor': False, 'debug': 'int sampleFreeFunction ( int i ) {', 'inline': False}
{'line_number': 75, 'static': False, 'rtnType': 'int', 'const': False, 'parameters': [{'line_number': 75, 'constant': 0, 'name': '', 'reference': 0, 'type': 'void', 'static': 0, 'pointer': 0}], 'namespace': '', 'virtual': False, 'destructor': False, 'returns': 'int', 'template': False, 'friend': False, 'returns_class': False, 'extern': False, 'returns_pointer': 0, 'class': None, 'name': 'anotherFreeFunction', 'pure_virtual': False, 'explicit': False, 'returns_fundamental': True, 'constructor': False, 'debug': 'int anotherFreeFunction ( void ) ;', 'inline': False}
Number of public methods 5
Number of private properties 2
Parameter Types for public method meth3 ['const string &', 'vector<string> &']
Return type for meth1:
string
Doxygen for meth2:
///
/// Method 2 description
///
/// @param v1 Variable 1
///
Parameters for meth3:
[{'line_number': 30, 'constant': 1, 'name': 'v1', 'reference': 1, 'type': 'const string &', 'static': 0, 'pointer': 0, 'desc': 'Variable 1'}, {'line_number': 30, 'constant': 0, 'name': 'v2', 'reference': 1, 'type': 'vector<string> &', 'static': 0, 'pointer': 0, 'desc': 'Variable 2'}]
Doxygen for meth4:
/**********************************
* Method 4 description
*
* @return Return value
*********************************/
Return type for meth5:
void *
Doxygen type for prop1:
/// prop1 description
Type for prop5:
int
Namespace for AlphaClass is:
Alpha
Return type for alphaMethod is:
void
Namespace for OmegaClass is:
Alpha::Omega
Type for omegaString is:
string
Free functions are:
sampleFreeFunction
anotherFreeFunction
#includes are:
<vector>
<string>
#defines are:
DEF_1 1
OS_NAME "Linux"
Contributors
------------
* Chris Love
* HartsAntler

0
cppParser/Resolver.py Normal file
View File

22
cppParser/Struct.py Normal file
View File

@ -0,0 +1,22 @@
#!/usr/bin/python
try :
# normal install module
import ply.lex as lex
except ImportError :
# local module
import lex
import os
import sys
import re
import inspect
class CppStruct(dict):
Structs = []
def __init__(self, nameStack):
if len(nameStack) >= 2: self['type'] = nameStack[1]
else: self['type'] = None
self['fields'] = []
self.Structs.append( self )
global curLine
self["line_number"] = curLine

28
cppParser/Type.py Normal file
View File

@ -0,0 +1,28 @@
#!/usr/bin/python
try :
# normal install module
import ply.lex as lex
except ImportError :
# local module
import lex
import os
import sys
import re
import inspect
class Type():
def __init__(self):
self.name = ""
self.const = False # the const xxxxx
self.constVar = False # the char* const VarName
self.static = False
self.inline = False
self.reference = False
class TypeVoid(Type):
def __init__(self):
Type.__init__()
self.name = "void"

10
cppParser/Union.py Normal file
View File

@ -0,0 +1,10 @@
#!/usr/bin/python
try :
# normal install module
import ply.lex as lex
except ImportError :
# local module
import lex
import os
import sys
import re

23
cppParser/Variable.py Normal file
View File

@ -0,0 +1,23 @@
#!/usr/bin/python
try :
# normal install module
import ply.lex as lex
except ImportError :
# local module
import lex
import os
import sys
import re
class Variable():
def __init__(self):
self.name = ""
self.type = Type.TypeVoid()
self.const = False # the const xxxxx
self.constVar = False # the char* const VarName
self.static = False
self.external = False
def to_str(self) :
return ""

View File

@ -1,43 +0,0 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys, glob
from distutils.core import setup
DESCRIPTION = (
'Parse C++ header files and generate a data structure '
'representing the class'
)
CLASSIFIERS = [
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 3',
'Programming Language :: C++',
'License :: OSI Approved :: BSD License',
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'Topic :: Software Development',
'Topic :: Software Development :: Code Generators',
'Topic :: Software Development :: Compilers',
'Topic :: Software Development :: Disassemblers'
]
setup(
name = 'CppHeaderParser',
version = '2.4',
author = 'Jashua Cloutier',
author_email = 'jashuac@bellsouth.net',
url = 'http://senexcanis.com/open-source/cppheaderparser/',
description = DESCRIPTION,
long_description = open('README.txt').read(),
license = 'BSD',
platforms = 'Platform Independent',
packages = ['CppHeaderParser'],
keywords = 'c++ header parser ply',
classifiers = CLASSIFIERS,
requires = ['ply'],
package_data = { 'CppHeaderParser': ['README', 'README.html', 'doc/*.*', 'examples/*.*'], },
)

View File

@ -5,9 +5,11 @@ import lutinTools
# TODO : Add try of generic input ... # TODO : Add try of generic input ...
sys.path.append(lutinTools.GetCurrentPath(__file__) + "/ply/ply/") sys.path.append(lutinTools.GetCurrentPath(__file__) + "/ply/ply/")
sys.path.append(lutinTools.GetCurrentPath(__file__) + "/cppParser/CppHeaderParser/") sys.path.append(lutinTools.GetCurrentPath(__file__) + "/cppParser/CppHeaderParser/")
sys.path.append(lutinTools.GetCurrentPath(__file__) + "/cppParser/")
sys.path.append(lutinTools.GetCurrentPath(__file__) + "/codeBB/") sys.path.append(lutinTools.GetCurrentPath(__file__) + "/codeBB/")
sys.path.append(lutinTools.GetCurrentPath(__file__) + "/codeHL/") sys.path.append(lutinTools.GetCurrentPath(__file__) + "/codeHL/")
import CppHeaderParser import CppHeaderParser
import Parse
import lutinDocHtml import lutinDocHtml
import lutinDocMd import lutinDocMd
import os import os
@ -131,6 +133,9 @@ class doc:
## ##
def add_file(self, filename): def add_file(self, filename):
debug.debug("adding file in documantation : '" + filename + "'"); debug.debug("adding file in documantation : '" + filename + "'");
plop = Parse.parse_file('/home/edupin/dev/perso/Widget.h')
debug.error("parse done");
try: try:
metaData = CppHeaderParser.CppHeader(filename) metaData = CppHeaderParser.CppHeader(filename)
except CppHeaderParser.CppParseError, e: except CppHeaderParser.CppParseError, e: