示例#1
0
def GenerateTags(buff):
    """Create a DocStruct object that represents a Tcl Script
    @param buff: a file like buffer object (StringIO)

    """
    rtags = taglib.DocStruct()
    rtags.SetElementDescription('procedure', "Procedure Definitions")

    for lnum, line in enumerate(buff):
        line = line.strip()

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

        # Check for Procedure defs
        if parselib.IsToken(line, 0, u'proc'):
            parts = line.split()
            if len(parts) > 1:
                name = parts[1]
                if u"::" in name:
                    spaces = name.split("::")
                    space_l = rtags.GetElement('namespace', spaces[0])
                    if space_l == None:
                        space_l = taglib.Namespace(spaces[0], lnum)
                        rtags.AddElement('namespace', space_l)
                    space_l.AddElement('procedure', taglib.Procedure(spaces[-1], lnum))
                else:
                    rtags.AddElement('procedure', taglib.Procedure(parts[1], lnum))

    return rtags
示例#2
0
def GenerateTags(buff):
    """Create a DocStruct object that represents a Tcl Script
    @param buff: a file like buffer object (StringIO)

    """
    rtags = taglib.DocStruct()
    rtags.SetElementDescription('procedure', "Procedure Definitions")
    rtags.SetElementDescription('class', "SNIT")

    # Scope tracking for SNIT blocks
    insnit = False
    cursnit = None
    openparens = 0

    for lnum, line in enumerate(buff):
        line = line.strip()

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

        if insnit and cursnit is not None:
            if parselib.IsToken(line, 0, u'method') or \
               parselib.IsToken(line, 0, u'typemethod'):
                parts = line.split()
                if len(parts) > 1:
                    name = parts[1]
                    cursnit.AddMethod(taglib.Method(name, lnum))
            elif parselib.IsToken(line, 0, u'typevariable') or \
                 parselib.IsToken(line, 0, u'variable'):
                parts = line.split()
                if len(parts) > 1:
                    name = parts[1]
                    cursnit.AddVariable(taglib.Variable(name, lnum))
            elif parselib.IsToken(u'constructor', 0, line) or \
                 parselib.IsToken(u'destructor', 0, line):
                 name = parselib.GetFirstIdentifier(line)
                 cursnit.AddMethod(taglib.Method(name, lnum))
            elif parselib.IsToken(line, 0, u"package"):
                 pkg = GetPackage(line, lnum)
                 if pkg:
                     cursnit.AddElement('package', pkg)

            # Update Scope
            openparens += GetParenCount(line)
            if openparens == 0:
                insnit = False
                cursnit = None
            continue

        # Check for Procedure defs
        if parselib.IsToken(line, 0, u'proc'):
            parts = line.split()
            if len(parts) > 1:
                name = parts[1]
                if u"::" in name:
                    spaces = name.split("::")
                    space_l = rtags.GetElement('namespace', spaces[0])
                    if space_l == None:
                        space_l = taglib.Namespace(spaces[0], lnum)
                        rtags.AddElement('namespace', space_l)
                    space_l.AddElement('procedure', taglib.Procedure(spaces[-1], lnum))
                else:
                    rtags.AddElement('procedure', taglib.Procedure(parts[1], lnum))
        elif line.startswith(u'snit::'):
            parts = line.split()
            if len(parts) > 1:
                insnit = True
                openparens = GetParenCount(line)
                name = parts[1]
                cursnit = taglib.Class(name, lnum)
                rtags.AddClass(cursnit)
        elif parselib.IsToken(line, 0, u"package"):
             pkg = GetPackage(line, lnum)
             if pkg:
                 rtags.AddElement('package', pkg)

    return rtags
示例#3
0
def GenerateTags(buff):
    """Create a DocStruct object that represents a Ferite document
    @param buff: a file like buffer object (StringIO)

    """
    rtags = taglib.DocStruct()

    # Setup document structure
    rtags.SetElementDescription('namespace', "Namespaces")
    rtags.SetElementDescription('class', "Class Definitions")
    rtags.SetElementDescription('protocol', "Protocols")
    rtags.SetElementDescription('function', "Function Definitions")
    rtags.SetElementPriority('namespace', 4)
    rtags.SetElementPriority('class', 3)
    rtags.SetElementPriority('protocol', 2)
    rtags.SetElementPriority('function', 1)

    # Variables for tracking parse state
    incomment = False  # Inside a comment
    innamespace = False  # Inside a namespace
    inclass = False  # Inside a class defintion
    inprotocol = False  # Inside a protocol
    infundef = False  # Inside a function definition
    lastnspace = None  # Last Namespace
    lastclass = None  # Last Class
    lastprotocol = None  # Last Protocol
    lastfun = None  # last Function
    openb = 0  # Keep track of open brackets for scope resolution

    def InSubScope():
        return innamespace or inclass or inprotocol or infundef

    # Parse the contents of the buffer
    for lnum, line in enumerate(buff):
        line = line.strip()
        llen = len(line)
        idx = 0
        while idx < len(line):
            # Skip Whitespace
            idx = parselib.SkipWhitespace(line, idx)

            # Check for comments
            if line[idx:].startswith(u'/*'):
                idx += 2
                incomment = True
            elif line[idx:].startswith(u'//'):
                break  # go to next line
            elif line[idx:].startswith(u'*/'):
                idx += 2
                incomment = False

            # At end of line
            if idx >= llen:
                break

            # Look for tags
            if incomment:
                idx += 1
            elif line[idx] == u'{':
                idx += 1
                openb += 1
                # Namespace/Class/Protocol names must be followed by a {
                if not InSubScope() and lastnspace is not None:
                    innamespace = True
                    rtags.AddElement('namespace', lastnspace)
                elif not inclass and lastclass is not None:
                    inclass = True
                    if lastnspace is not None:
                        # Class is in a namespace
                        lastnspace.AddElement('class', lastclass)
                    else:
                        # Class is at the global scope
                        rtags.AddClass(lastclass)
                elif not InSubScope() and lastprotocol is not None:
                    inprotocol = True
                    rtags.AddElement('protocol', lastprotocol)
                elif lastfun is not None:
                    infundef = True
                    lastfun = None
                else:
                    pass
            elif line[idx] == u'}':
                idx += 1
                openb -= 1
                # Check if the scope needs to change
                if innamespace and openb == 0:
                    innamespace = False
                    lastnspace = None
                elif innamespace and inclass and openb == 1:
                    inclass = False
                    lastclass = None
                elif (innamespace and inclass and infundef and openb == 2) or \
                     (innamespace and infundef and openb == 1):
                    infundef = False
                    lastfun = None
                elif inclass and openb == 0:
                    inclass = False
                    lastclass = None
                elif inclass and infundef and openb == 1:
                    infundef = False
                    lastfun = None
                elif inprotocol and openb == 0:
                    inprotocol = False
                    lastprotocol = None
                elif inprotocol and infundef and openb == 1:
                    infundef = False
                    lastfun = None
                elif infundef and openb == 0:
                    infundef = False
                    lastfun = None
                else:
                    pass
            elif not infundef and parselib.IsToken(line, idx, u'class'):
                # Skip whitespace
                idx = parselib.SkipWhitespace(line, idx + 5)
                name = parselib.GetFirstIdentifier(line[idx:])
                if name is not None:
                    idx += len(name)  # Move past the class name
                    lastclass = taglib.Class(name, lnum)
            elif not infundef and not inclass and \
                 parselib.IsToken(line, idx, 'namespace'):
                idx = parselib.SkipWhitespace(line, idx + 9)
                name = GetElementName(line[idx:])
                if name is not None:
                    idx += len(name)
                    lastnspace = taglib.Namespace(name, lnum)
            elif parselib.IsToken(line, idx, u'protocol'):
                idx = parselib.SkipWhitespace(line, idx + 8)
                name = parselib.GetFirstIdentifier(line[idx:])
                if name is not None:
                    idx += len(name)
                    lastprotocol = Protocol(name, lnum)
            elif parselib.IsToken(line, idx, u'function'):
                # Skip whitespace
                idx = parselib.SkipWhitespace(line, idx + 8)
                name = parselib.GetFirstIdentifier(line[idx:])
                if name is not None:
                    lastfun = name
                    # Skip whitespace
                    idx = parselib.SkipWhitespace(line, idx + len(name))

                    if line[idx] != u'(':
                        continue

                    tfun = taglib.Function(name, lnum)
                    if innamespace and not inclass and lastnspace:
                        lastnspace.AddElement('function', tfun)
                    elif inclass and lastclass is not None:
                        lastclass.AddMethod(
                            taglib.Method(name, lnum, lastclass.GetName()))
                    elif inprotocol and lastprotocol is not None:
                        lastprotocol.AddElement('function', tfun)
                    else:
                        rtags.AddFunction(tfun)
            else:
                idx += 1

    return rtags