~brian-sidebotham/wxwidgets-cmake/wxpython-2.9.4

« back to all changes in this revision

Viewing changes to wxPython/wx/tools/Editra/plugins/codebrowser/codebrowser/gentag/valatags.py

  • Committer: Brian Sidebotham
  • Date: 2013-08-03 14:30:08 UTC
  • Revision ID: brian.sidebotham@gmail.com-20130803143008-c7806tkych1tp6fc
Initial import into Bazaar

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
###############################################################################
 
2
# Name: valatags.py                                                           #
 
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
###############################################################################
 
8
 
 
9
"""
 
10
FILE: valatags.py
 
11
AUTHOR: Cody Precord
 
12
LANGUAGE: Python
 
13
SUMMARY:
 
14
  Generate a DocStruct object that captures the structure of a Vala document.
 
15
Currently it supports parsing for Classes, Class Methods, and Function
 
16
Definitions.
 
17
 
 
18
"""
 
19
 
 
20
__author__ = "Cody Precord <cprecord@editra.org>"
 
21
__svnid__ = "$Id: valatags.py 58799 2009-02-09 03:33:15Z CJP $"
 
22
__revision__ = "$Revision: 58799 $"
 
23
 
 
24
#--------------------------------------------------------------------------#
 
25
# Imports
 
26
import re
 
27
 
 
28
# Local Imports
 
29
import taglib
 
30
import parselib
 
31
 
 
32
#--------------------------------------------------------------------------#
 
33
# Globals
 
34
 
 
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"
 
37
 
 
38
#--------------------------------------------------------------------------#
 
39
 
 
40
def GenerateTags(buff):
 
41
    """Create a DocStruct object that represents a vala dacument
 
42
    @param buff: a file like buffer object (StringIO)
 
43
 
 
44
    """
 
45
    rtags = taglib.DocStruct()
 
46
 
 
47
    # Setup document structure
 
48
    rtags.SetElementDescription('function', "Function Definitions")
 
49
 
 
50
    inclass = False      # Inside a class defintion
 
51
    incomment = False    # Inside a comment
 
52
    infundef = False     # Inside a function definition
 
53
    lastclass = None
 
54
    lastfun = None
 
55
    openb = 0            # Keep track of open brackets
 
56
 
 
57
    for lnum, line in enumerate(buff):
 
58
        line = line.strip()
 
59
        llen = len(line)
 
60
        idx = 0
 
61
        while idx < len(line):
 
62
            # Skip Whitespace
 
63
            idx = parselib.SkipWhitespace(line, idx)
 
64
 
 
65
            # Check for coments
 
66
            if line[idx:].startswith(u'/*'):
 
67
                idx += 2
 
68
                incomment = True
 
69
            elif line[idx:].startswith(u'//') or line[idx:].startswith(u'#'):
 
70
                break # go to next line
 
71
            elif line[idx:].startswith(u'*/'):
 
72
                idx += 2
 
73
                incomment = False
 
74
 
 
75
            # At end of line
 
76
            if idx >= llen:
 
77
                break
 
78
 
 
79
            # Look for tags
 
80
            if incomment:
 
81
                idx += 1
 
82
                continue
 
83
            elif line[idx] == u'{':
 
84
                idx += 1
 
85
                openb += 1
 
86
                # Class name must be followed by a {
 
87
                if not inclass and lastclass is not None:
 
88
                    inclass = True
 
89
                    rtags.AddClass(lastclass)
 
90
                elif lastfun is not None:
 
91
                    infundef = True
 
92
                    lastfun = None
 
93
                else:
 
94
                    pass
 
95
                continue
 
96
            elif line[idx] == u'}':
 
97
                idx += 1
 
98
                openb -= 1
 
99
                if inclass and openb == 0:
 
100
                    inclass = False
 
101
                    lastclass = None
 
102
                elif infundef and inclass and openb == 1:
 
103
                    infundef = False
 
104
                elif infundef and openb == 0:
 
105
                    infundef = False
 
106
                    lastfun = None
 
107
                else:
 
108
                    pass
 
109
                continue
 
110
            elif not infundef and parselib.IsToken(line, idx, u'class'):
 
111
                # Skip whitespace
 
112
                idx = parselib.SkipWhitespace(line, idx + 5)
 
113
                name = parselib.GetFirstIdentifier(line[idx:])
 
114
                if name is not None:
 
115
                    idx += len(name) # Move past the class name
 
116
                    lastclass = taglib.Class(name, lnum)
 
117
                continue
 
118
 
 
119
            match = RE_METH.match(line[idx:])
 
120
            if match is not None:
 
121
                # Check that not a method call
 
122
                sline = line.strip()
 
123
                if sline.endswith(u';'):
 
124
                    idx += match.end(2)
 
125
                    continue
 
126
 
 
127
                # Most likely a definition so store it in the DocStruct
 
128
                name = match.group(2)
 
129
 
 
130
                # Secondary check for other end cases regex will find
 
131
                if name in KEYWORDS:
 
132
                    idx += match.end(2)
 
133
                    continue
 
134
 
 
135
                lastfun = name
 
136
                if inclass and lastclass is not None:
 
137
                    lastclass.AddMethod(taglib.Method(name, lnum, lastclass.GetName()))
 
138
                else:
 
139
                    rtags.AddFunction(taglib.Function(name, lnum))
 
140
                idx += match.end(2)
 
141
            else:
 
142
                idx += 1
 
143
 
 
144
    return rtags
 
145
 
 
146
#-----------------------------------------------------------------------------#
 
147
# Test
 
148
if __name__ == '__main__':
 
149
    import sys
 
150
    import StringIO
 
151
    fhandle = open(sys.argv[1])
 
152
    txt = fhandle.read()
 
153
    fhandle.close()
 
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())
 
160
    print "END"