~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/perltags.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: perltags.py                                                           #
 
3
# Purpose: Generate Tags for Perl Scripts                                     #
 
4
# Author: Cody Precord <cprecord@editra.org>                                  #
 
5
# Copyright: (c) 2008 Cody Precord <staff@editra.org>                         #
 
6
# License: wxWindows License                                                  #
 
7
###############################################################################
 
8
 
 
9
"""
 
10
FILE: perltags.py
 
11
AUTHOR: Cody Precord
 
12
LANGUAGE: Python
 
13
SUMMARY:
 
14
  Generate a DocStruct object that captures the structure of a Perl Script.
 
15
Currently supports parsing of subroutines and their declarations, as well as
 
16
package declarations.
 
17
 
 
18
Subroutine Declarations can be in a number of forms
 
19
sub foo;
 
20
sub foo :attr;
 
21
sub foo (bar);
 
22
sub foo (bar) :attr;
 
23
 
 
24
"""
 
25
 
 
26
__author__ = "Cody Precord <cprecord@editra.org>"
 
27
__svnid__ = "$Id: perltags.py 54087 2008-06-11 01:17:58Z CJP $"
 
28
__revision__ = "$Revision: 54087 $"
 
29
 
 
30
#--------------------------------------------------------------------------#
 
31
# Dependancies
 
32
import taglib
 
33
import parselib
 
34
 
 
35
from pygments import highlight
 
36
from pygments.token import Token
 
37
from pygments.lexers import get_lexer_by_name
 
38
from pygments.formatter import Formatter
 
39
 
 
40
#--------------------------------------------------------------------------#
 
41
 
 
42
class PerlFormatter(Formatter):
 
43
    
 
44
    def format(self, tokensource, outfile):
 
45
        """Format the input text
 
46
        @note: overrides Formatter.format
 
47
 
 
48
        """
 
49
        self.rtags = taglib.DocStruct()
 
50
        self.rtags.SetElementDescription('package', "Packages")
 
51
        self.rtags.SetElementPriority('package', 3)
 
52
        self.rtags.SetElementDescription('subdec', "Subroutine Declarations")
 
53
        self.rtags.SetElementPriority('subdec', 2)
 
54
        self.rtags.SetElementDescription('subroutine', "Subroutines")
 
55
        self.rtags.SetElementPriority('subroutine', 1)
 
56
 
 
57
        line_count = 0
 
58
        current_line = []
 
59
        code_lines = []
 
60
 
 
61
        # Parse the file into tokens and values
 
62
        for ttype, value in tokensource:
 
63
            if '\n' in value:
 
64
                code_lines.append((line_count, current_line))
 
65
                current_line = []
 
66
                line_count += value.count('\n')
 
67
                continue
 
68
            current_line.append((ttype, value))
 
69
 
 
70
        self.parseTags(code_lines)
 
71
 
 
72
    def parseTags(self, code_lines):
 
73
        """Parse all the tokens found in the lines of code"""
 
74
        container_list = []
 
75
        vset = set()
 
76
 
 
77
        for num, line in code_lines:
 
78
            try:
 
79
                # Subroutine
 
80
                if parselib.HasToken(line, Token.Keyword, "sub"):
 
81
                    fname = parselib.GetTokenValue(line, Token.Name.Function)
 
82
                    self.rtags.AddElement('subroutine',
 
83
                                          taglib.Function(fname, num, "subroutine"))
 
84
 
 
85
                # Packages
 
86
                if parselib.HasToken(line, Token.Name.Builtin, "package"):
 
87
                    name = None
 
88
                    next = True
 
89
                    for token, value in line:
 
90
                        if not next:
 
91
                            name += value
 
92
                            break
 
93
 
 
94
                        if token == Token.Name.Namespace:
 
95
                            name = value
 
96
                            next = False
 
97
 
 
98
                    if name is not None:
 
99
                        self.rtags.AddElement('package', taglib.Package(name, num))
 
100
 
 
101
            except parselib.TokenNotFound:
 
102
                pass
 
103
 
 
104
    def getTags(self):
 
105
        return self.rtags
 
106
 
 
107
#--------------------------------------------------------------------------#
 
108
 
 
109
def GenerateTags(buff):
 
110
    """Create a DocStruct object that represents a Perl Script
 
111
    @param buff: a file like buffer object (StringIO)
 
112
 
 
113
    """
 
114
    formatter = PerlFormatter()
 
115
    highlight(buff.read(), get_lexer_by_name("perl"), formatter)
 
116
    return formatter.getTags()
 
117
 
 
118
#-----------------------------------------------------------------------------#
 
119
 
 
120
# Test
 
121
if __name__ == '__main__':
 
122
    import sys
 
123
    import StringIO
 
124
    fhandle = open(sys.argv[1])
 
125
    txt = fhandle.read()
 
126
    fhandle.close()
 
127
    tags = GenerateTags(StringIO.StringIO(txt))
 
128
    print "\n\nElements:"
 
129
    for element in tags.GetElements():
 
130
        print "\n%s:" % element.keys()[0]
 
131
        for val in element.values()[0]:
 
132
            print "%s [%d]" % (val.GetName(), val.GetLine())
 
133
    print "END"