1
###############################################################################
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
###############################################################################
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
18
Subroutine Declarations can be in a number of forms
26
__author__ = "Cody Precord <cprecord@editra.org>"
27
__svnid__ = "$Id: perltags.py 54087 2008-06-11 01:17:58Z CJP $"
28
__revision__ = "$Revision: 54087 $"
30
#--------------------------------------------------------------------------#
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
40
#--------------------------------------------------------------------------#
42
class PerlFormatter(Formatter):
44
def format(self, tokensource, outfile):
45
"""Format the input text
46
@note: overrides Formatter.format
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)
61
# Parse the file into tokens and values
62
for ttype, value in tokensource:
64
code_lines.append((line_count, current_line))
66
line_count += value.count('\n')
68
current_line.append((ttype, value))
70
self.parseTags(code_lines)
72
def parseTags(self, code_lines):
73
"""Parse all the tokens found in the lines of code"""
77
for num, line in code_lines:
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"))
86
if parselib.HasToken(line, Token.Name.Builtin, "package"):
89
for token, value in line:
94
if token == Token.Name.Namespace:
99
self.rtags.AddElement('package', taglib.Package(name, num))
101
except parselib.TokenNotFound:
107
#--------------------------------------------------------------------------#
109
def GenerateTags(buff):
110
"""Create a DocStruct object that represents a Perl Script
111
@param buff: a file like buffer object (StringIO)
114
formatter = PerlFormatter()
115
highlight(buff.read(), get_lexer_by_name("perl"), formatter)
116
return formatter.getTags()
118
#-----------------------------------------------------------------------------#
121
if __name__ == '__main__':
124
fhandle = open(sys.argv[1])
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())