Friday, April 27, 2007

More on reg exps. Parsing a C or C++ file with regexps in Python

Ok, so while i'm on the regexp wave. It's plain dumb but works fine. I know i could have used ctags or smth else, but for the sake of it. Also I googled for an example like this one but found nothing. It's always a good idea to have smth like this i case you need to generate prototypes for a long C file or nomatter what.


1 #!/usr/bin/env python
2
3
import os, re, sys
4
5 class Cexploder():
6 Body = re.compile("{[^{}]*}", re.DOTALL|re.MULTILINE)
7 Func = re.compile("(\w+\s+)*\w+\s*\(.*?\)", re.DOTALL| re.MULTILINE)
8 MultiLineComment = re.compile("/\*.*?\*/", re.DOTALL|re.MULTILINE)
9 OneLineComment = re.compile("//.*?$", re.DOTALL|re.MULTILINE)
10 CppDirectives = re.compile("^\s*#.*?$", re.DOTALL|re.MULTILINE)
11 #^\s*#(.*?(\\\n)*)+\n
12
13
def __init__(self, File=''):
14 f = open(File, 'r')
15 self.data = f.read()
16 f.close()
17
18 def GetPrototypes(self):
19 FuncList = []
20 pdata = self.data
21 pdata = self.OneLineComment.sub('', pdata)
22 pdata = self.MultiLineComment.sub('', pdata)
23 pdata = self.CppDirectives.sub('', pdata)
24
25 while self.Body.search(pdata) != None:
26 pdata = self.Body.sub('', pdata)
27
28 fiter = self.Func.finditer(pdata)
29 for i in fiter:
30 FuncList.append( pdata[i.start() : i.end()] )
31
32 return FuncList
33
34 c = Cexploder(sys.argv[1])
35 f = c.GetPrototypes()
36 for i in f:
37 print i

No comments: