fastparser.py :  » Development » Bicycle-Repair-Man » bicyclerepair-0.9 » bike » parsing » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » Bicycle Repair Man 
Bicycle Repair Man » bicyclerepair 0.9 » bike » parsing » fastparser.py
#!/usr/bin/env python
from bike.parsing.fastparserast import *
from bike.parsing.parserutils import *
from parser import ParserError
#import exceptions

indentRE = re.compile("^\s*(\w+)")

# returns a tree of objects representing nested classes and functions
# in the source
def fastparser(src,modulename="",filename=""):
    try:
        return fastparser_impl(src,modulename,filename)
    except RuntimeError, ex:   # if recursive call exceeds maximum depth
        if str(ex) == "maximum recursion limit exceeded":
            raise ParserError,"maximum recursion depth exceeded when fast-parsing src "+filename
        else:
            raise

def fastparser_impl(src,modulename,filename):
    lines = src.splitlines(1)
    maskedSrc = maskPythonKeywordsInStringsAndComments(src)
    maskedLines = maskedSrc.splitlines(1)
    root = Module(filename,modulename,lines,maskedSrc)
    parentnode = root
    lineno = 0
    for line in maskedLines:
        lineno+=1
        #print "line",lineno,":",line
        m = indentRE.match(line)
        if m:
            indent = m.start(1)
            tokenstr = m.group(1)
            if tokenstr == "import" or tokenstr == "from":
                while indent <= parentnode.indent:   # root indent is -TABWIDTH
                    parentnode = parentnode.getParent()
                try:
                    parentnode.importlines.append(lineno)
                except AttributeError:
                    parentnode.importlines = [lineno]
            elif tokenstr == "class":
                m2 = classNameRE.match(line)
                if m2:
                    n = Class(m2.group(1), filename, root, lineno, indent, lines, maskedSrc)
                    root.flattenedNodes.append(n)

                    while indent <= parentnode.indent:
                        parentnode = parentnode.getParent()
                    parentnode.addChild(n)
                    parentnode = n

            elif tokenstr == "def":
                m2 = fnNameRE.match(line)
                if m2:
                    n = Function(m2.group(1), filename, root, lineno, indent, lines, maskedSrc)
                    root.flattenedNodes.append(n)

                    while indent <= parentnode.indent:
                        parentnode = parentnode.getParent()
                    parentnode.addChild(n)
                    parentnode = n

            elif indent <= parentnode.indent and \
                     tokenstr in ['if','for','while','try']:
                parentnode = parentnode.getParent()
                while indent <= parentnode.indent:
                    parentnode = parentnode.getParent()

    return root

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.