Hi,

I don't know very well what you want to do, but if you want to parse c++, take a look at "GCC-XML python" (http://www.gccxml.org) and the python binding (http://pygccxml.sourceforge.net/). These tools translate c++ code to XML. Then, you can parse xml with your favorite tools and find the namespaces for example...

Your code don't seem very "pythonic", and it seems there is a bug... For example what's append if a comment contains a '}' inside a namesapce? (or a string contains a '{' ?

I think you should better use most appropriate tools to do this kind of jobs...

Cyril


On 5/28/05, Michael <[EMAIL PROTECTED]> wrote:
Hi,
I'm fairly new at Python, and have the following code that works but isn't
very concise, is there a better way of writing it?? It seems much more
lengthy than python code i have read..... :-)
(takes a C++ block and extracts the namespaces from it)



def ExtractNamespaces(data):
print("Extracting Namespaces")
p = re.compile( 'namespace (?P<name>[\w]*)[\n\t ]*{')

subNamespaces = []

newNS = p.search(data)
while( newNS ):
  print "\t" + newNS.group("name")

  OPCount = 1
  Offset = newNS.end()
  while(OPCount > 0):
   if( data[Offset] == "}" ):
    OPCount = OPCount -1;
   elif( data[Offset] == "{" ):
    OPCount = OPCount + 1;
   Offset = Offset+1;

  #Extract Data:
  newNSData = data[newNS.end():Offset-1]
  data = "" + data[Offset:]
  newNamespace = [newNS.group("name"), newNSData];
  subNamespaces.append(newNamespace)

  #Perform NewSearch
  newNS = p.search(data)
return [subNamespaces,data]






--
http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to