Пример #1
0
def GenerateTags(buff):
    """Create a DocStruct object that represents the structure of a C source
    file.
    @param buff: a file like buffer object (StringIO)

    """
    rtags = taglib.DocStruct()
    rtags.SetElementDescription('macro', "Macros")
    rtags.SetElementPriority('macro', 3)
    rtags.SetElementDescription('function', "Function Definitions")
    rtags.SetElementPriority('function', 1)

    kwords = ("if else for while switch case")
    txt = buff.read()

    # Get function defintions
    pat = re.compile(
        r"([A-Za-z0-9_]+[ \t\r\n]+)+([A-Za-z0-9_]+)[ \t\r\n]*\([^)]+\)[ \t\r\n]*\{"
    )
    for match in re.finditer(pat, txt):
        fname = match.group(2)
        if fname and fname not in kwords:
            line = txt.count('\n', 0, match.start(2))
            rtags.AddFunction(taglib.Function(fname, line))

    # Find all Macro defintions
    pat = re.compile(r"#define[ \t]+([A-Za-z0-9_]+)")
    for match in re.finditer(pat, txt):
        line = txt.count('\n', 0, match.start(1))
        rtags.AddElement('macro', taglib.Macro(match.group(1), line))

    return rtags
Пример #2
0
def GenerateTags(buff):
    """Create a DocStruct object that represents a NSIS Script
    @param buff: a file like buffer object (StringIO)
    @todo: generate tags for lua tables?

    """
    rtags = taglib.DocStruct()

    # Set Descriptions of Document Element Types
    rtags.SetElementDescription('variable', "Defines")
    rtags.SetElementDescription('section', "Section Definitions")
    rtags.SetElementDescription('macro', "Macro Definitions")
    rtags.SetElementDescription('function', "Function Definitions")
    rtags.SetElementPriority('variable', 4)
    rtags.SetElementPriority('section', 3)
    rtags.SetElementPriority('function', 2)
    rtags.SetElementPriority('macro', 1)

    # Parse the lines for code objects
    for lnum, line in enumerate(buff):
        line = line.strip()
        llen = len(line)

        # Skip comment and empty lines
        if line.startswith(u"#") or line.startswith(u";") or not line:
            continue

        # Look for functions and sections
        if parselib.IsToken(line, 0, u'Function'):
            parts = line.split()
            if len(parts) > 1:
                rtags.AddFunction(taglib.Function(parts[1], lnum))
        elif parselib.IsToken(line, 0, u'Section'):
            parts = line.split()
            if len(parts) > 1 and parts[1][0] not in ['"', "'", "`"]:
                rtags.AddElement('section', taglib.Section(parts[1], lnum))
            else:
                for idx, part in enumerate(parts[1:]):
                    if parts[idx][-1] in ['"', "'", "`"]:
                        rtags.AddElement('section', taglib.Section(part, lnum))
                        break
        elif parselib.IsToken(line, 0, u'!macro'):
            parts = line.split()
            if len(parts) > 1:
                rtags.AddElement('macro', taglib.Macro(parts[1], lnum))
        elif parselib.IsToken(line, 0, u'!define'):
            parts = line.split()
            if len(parts) > 1 and parts[1][0].isalpha():
                rtags.AddVariable(taglib.Variable(parts[1], lnum))
        else:
            continue

    return rtags
Пример #3
0
def GenerateTags(buff):
    """Create a DocStruct object that represents the structure of a C source
    file.
    @param buff: a file like buffer object (StringIO)

    """
    rtags = taglib.DocStruct()
    rtags.SetElementDescription('macro', "Macros")
    rtags.SetElementPriority('macro', 3)
    rtags.SetElementDescription('class', "Class Definitions")
    rtags.SetElementPriority('class', 2)
    rtags.SetElementDescription('function', "Function Definitions")
    rtags.SetElementPriority('function', 1)

    kwords = ("if else for while switch case catch")
    txt = buff.read()

    # Get class/method/function definitions
    for match in RE_METH.finditer(txt):
        fname = match.group(2)
        if fname and fname not in kwords:
            line = txt.count('\n', 0, match.start(2))
            if u"::" in fname:
                scopes = fname.split("::")
                cname = scopes[0].lstrip('*')
                cname = scopes[0].lstrip('&')
                cobj = rtags.GetElement('class', cname)
                if cobj == None:
                    cobj = taglib.Class(cname, line)
                    rtags.AddClass(cobj)
                cobj.AddMethod(taglib.Method(u'::'.join(scopes[1:]), line))
            else:
                fname = fname.replace("*", "")
                fname = fname.replace("&", "")
                rtags.AddFunction(taglib.Function(fname, line))

    # Find all Macro definitions
    for match in RE_DEF.finditer(txt):
        line = txt.count('\n', 0, match.start(1))
        rtags.AddElement('macro', taglib.Macro(match.group(1), line))

    return rtags
Пример #4
0
def GenerateTags(buff):
    """Create a DocStruct object that represents the structure of a Java source
    file.
    @param buff: a file like buffer object (StringIO)
    """
    rtags = taglib.DocStruct()
    rtags.SetElementDescription('class', "<no package>")
    rtags.SetElementDescription('variable', "Imports")

    # State Variables
    inComment    = False

    currentLevel = 0
    methodSignature = None
    methodLnum   = 0
    methodClass  = None

    lastClass = []
    imports = None

    # Parse the buffer
    # Simple line based parser, likely not to be accurate in all cases
    for lnum, line in enumerate(buff):

        lastLevel = currentLevel

        lineCodeOnly = line[:]
        lineCodeOnly = RE_BACKSLASHEDQUOTE_INLINE.sub("'",lineCodeOnly)
        lineCodeOnly = RE_STRING_INLINE.sub('',lineCodeOnly)
        lineCodeOnly = RE_CHARACTER_INLINE.sub('',lineCodeOnly)
        #print "[[[",lineCodeOnly,"]]]"
        
        # remove trailing comments
        cut = line.find('//')
        if cut>-1:
            line = line[:cut]

        line = RE_COMMENT_INLINE.sub('',line)

        if inComment:
            cut = line.find('*/')
            if cut>-1:
                line = line[cut+2:]
                inComment = False
            else:
                continue

        # remove starting comments
        cut = line.find('/*')
        if cut>-1:
            line = line[:cut]
            inComment = True

        line = line.strip()
        if len(line)==0:
            continue

        diff = lineCodeOnly.count('{') - lineCodeOnly.count('}')
        currentLevel += diff

        print "<<<",line,">>>", lnum, currentLevel, diff, len(lastClass), inComment
        if diff < 0:
            while len(lastClass) > currentLevel:
                #print "POP", len(lastClass), currentLevel, lastClass[-1]
                lastClass.pop()

        # handle multi-line method definition
        if methodSignature:
            cl = line.find(')')
            if cl > -1:
                if cl==0:
                    methodSignature += ')'
                else:
                    methodSignature += ' ' + line[:cl]
                methodClass.AddMethod(taglib.Method(methodSignature, methodLnum))
                #print "METH == ", methodSignature
                methodSignature = None
                continue
            else:
                methodSignature += ' ' + line
                #print "METH ++ ", methodSignature
                continue

        if currentLevel == 0:
            match = RE_PACKAGE.match(line)
            if match:
                groups = match.groups()
                #print "PACKAGE", groups
                rtags.SetElementDescription('class', groups[-1])
                continue

            match = RE_IMPORT.match(line)
            if match:
                groups = match.groups()
                #print "IMPORT", groups
                cobj = taglib.Variable(groups[-1], lnum)
                rtags.AddVariable(cobj)
                continue

        match = RE_CLASS.match(line)
        if match:
            cname = match.groups()[-1]
            if len(lastClass) > 0:
                cname = '$ '+cname
            cobj = taglib.Class(cname, lnum)
            rtags.AddClass(cobj)
            lastClass.append(cobj)
            #print "CLASS", cname
            continue

        if len(lastClass) == lastLevel:
            match = RE_METH.match(line)
            if match:
                groups = match.groups()
                prefix = ''
                methodSignature = groups[-2]
                warning = None
                if groups[3] == None:
                    contructor_for = lastClass[-1].GetName()
                    if contructor_for[0] == '$':
                      contructor_for = contructor_for[2:]
                    if groups[-2] == contructor_for:
                        prefix = '>'
                    else:
                        warning = 'tag_red'
                        methodSignature += ' - ???'
                else:
                    methodSignature += ' - ' +  groups[3]
                methodSignature += ' ('
                if groups[1] and (groups[1].find('static') > -1):
                    prefix += '_'
                if groups[2] and (groups[2].find('abstract') > -1):
                    prefix += '@'
                if len(prefix) > 0:
                    methodSignature = prefix + ' ' + methodSignature
                if groups[-1]:
                    methodSignature += groups[-1]
                if line.find(')') > -1:
                    methodSignature += ')'
                    cobj = taglib.Method(methodSignature, lnum)
                    if warning:
                        cobj.type = warning
                    lastClass[-1].AddMethod(cobj)
                    #print "METH", groups, methodSignature, lastClass[-1]
                    methodSignature = None
                else:
                    methodLnum = lnum
                    methodClass = lastClass[-1]
                continue

            match = RE_CONST.match(line)
            if match:
                groups = match.groups()
                #print "CONST", groups, lastClass[-1]
                cname = groups[-1] + ' -- ' +  groups[-2]
                cobj = taglib.Macro(cname, lnum)
                lastClass[-1].AddVariable(cobj)
                continue

            match = RE_VAR.match(line)
            if match:
                groups = match.groups()
                #print "VAR", groups, lastClass[-1]
                cname = groups[-1] + ' - ' +  groups[-2]
                #print groups[-2]
                if groups[-2][:6]=='throws':
                    continue
                if groups[1] and (groups[1].find('static') > -1):
                    cname = '_ ' + cname
                cobj = taglib.Variable(cname, lnum)
                lastClass[-1].AddVariable(cobj)
                continue

    return rtags