1
###############################################################################
3
# Purpose: Generate Tags for Vala documents #
4
# Author: Cody Precord <cprecord@editra.org> #
5
# Copyright: (c) 2009 Cody Precord <staff@editra.org> #
6
# License: wxWindows License #
7
###############################################################################
14
Generate a DocStruct object that captures the structure of a Vala document.
15
Currently it supports parsing for Classes, Class Methods, and Function
20
__author__ = "Cody Precord <cprecord@editra.org>"
21
__svnid__ = "$Id: valatags.py 58799 2009-02-09 03:33:15Z CJP $"
22
__revision__ = "$Revision: 58799 $"
24
#--------------------------------------------------------------------------#
32
#--------------------------------------------------------------------------#
35
RE_METH = re.compile(r"([A-Za-z0-9_]+\s+)+([A-Za-z0-9_:]+)\s*\([^)]*\){0,1}\s*")
36
KEYWORDS = "if else for while catch do"
38
#--------------------------------------------------------------------------#
40
def GenerateTags(buff):
41
"""Create a DocStruct object that represents a vala dacument
42
@param buff: a file like buffer object (StringIO)
45
rtags = taglib.DocStruct()
47
# Setup document structure
48
rtags.SetElementDescription('function', "Function Definitions")
50
inclass = False # Inside a class defintion
51
incomment = False # Inside a comment
52
infundef = False # Inside a function definition
55
openb = 0 # Keep track of open brackets
57
for lnum, line in enumerate(buff):
61
while idx < len(line):
63
idx = parselib.SkipWhitespace(line, idx)
66
if line[idx:].startswith(u'/*'):
69
elif line[idx:].startswith(u'//') or line[idx:].startswith(u'#'):
70
break # go to next line
71
elif line[idx:].startswith(u'*/'):
83
elif line[idx] == u'{':
86
# Class name must be followed by a {
87
if not inclass and lastclass is not None:
89
rtags.AddClass(lastclass)
90
elif lastfun is not None:
96
elif line[idx] == u'}':
99
if inclass and openb == 0:
102
elif infundef and inclass and openb == 1:
104
elif infundef and openb == 0:
110
elif not infundef and parselib.IsToken(line, idx, u'class'):
112
idx = parselib.SkipWhitespace(line, idx + 5)
113
name = parselib.GetFirstIdentifier(line[idx:])
115
idx += len(name) # Move past the class name
116
lastclass = taglib.Class(name, lnum)
119
match = RE_METH.match(line[idx:])
120
if match is not None:
121
# Check that not a method call
123
if sline.endswith(u';'):
127
# Most likely a definition so store it in the DocStruct
128
name = match.group(2)
130
# Secondary check for other end cases regex will find
136
if inclass and lastclass is not None:
137
lastclass.AddMethod(taglib.Method(name, lnum, lastclass.GetName()))
139
rtags.AddFunction(taglib.Function(name, lnum))
146
#-----------------------------------------------------------------------------#
148
if __name__ == '__main__':
151
fhandle = open(sys.argv[1])
154
tags = GenerateTags(StringIO.StringIO(txt))
155
print "\n\nElements:"
156
for element in tags.GetElements():
157
print "\n%s:" % element.keys()[0]
158
for val in element.values()[0]:
159
print "%s [%d]" % (val.GetName(), val.GetLine())