def BuildTags(self, buff, lexer):
        """
        @param buff: code buffer
        @param lexer: xml lexer
        @return: taglib.DocStruct instance for the given buff
        """
        rtags = taglib.DocStruct()
        rtags.SetElementDescription(self.TAG_ID, '/')
        line_count = 0
        current_line = []
        code_lines = []

        # Parse the file into tokens and values
        for ttype, value in lex(buff.read(), lexer):
            if '\n' in value:
                if len(current_line) > 0:
                    code_lines.append((line_count, current_line))
                current_line = []
                line_count += value.count('\n')
                continue
            if ttype == Token.Name.Tag and len(value) > 1:
                current_line.append((ttype, value))
        docroot = self.Parse(code_lines)
        if docroot != None:
            rtags.AddElement(self.TAG_ID, docroot)
        return rtags
Beispiel #2
0
    def format(self, tokensource, outfile):
        """Format the input text
        @note: overrides Formatter.format

        """
        self.rtags = taglib.DocStruct()
        self.rtags.SetElementDescription('package', "Packages")
        self.rtags.SetElementPriority('package', 3)
        self.rtags.SetElementDescription('subdec', "Subroutine Declarations")
        self.rtags.SetElementPriority('subdec', 2)
        self.rtags.SetElementDescription('subroutine', "Subroutines")
        self.rtags.SetElementPriority('subroutine', 1)

        line_count = 0
        current_line = []
        code_lines = []

        # Parse the file into tokens and values
        for ttype, value in tokensource:
            if '\n' in value:
                code_lines.append((line_count, current_line))
                current_line = []
                line_count += value.count('\n')
                continue
            current_line.append((ttype, value))

        self.parseTags(code_lines)
Beispiel #3
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
Beispiel #4
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
Beispiel #5
0
def GenerateTags(buff):
    """Create a DocStruct object that represents a Configuration File
    @param buff: a file like buffer object (StringIO)

    """
    rtags = taglib.DocStruct()
    rtags.SetElementDescription('section', "Sections")
    section = None

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

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

        # Check for a section
        if line.startswith(u"["):
            secend = line.find(u"]")
            if secend != -1:
                section = taglib.Section(line[1:secend], lnum)
                rtags.AddElement('section', section)
            continue # Go to next line

        # Look for keys
        if u"=" in line and section is not None:
            key = taglib.Variable(line.split(u"=")[0], lnum)
            section.AddElement('key', key)

    return rtags
Beispiel #6
0
def GenerateTags(buff):
    """Create a DocStruct object that represents a Shell Script
    @param buff: a file like buffer object (StringIO)

    """
    rtags = taglib.DocStruct()
    rtags.SetElementDescription('function', "Function Definitions")

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

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

        # Check Regular Function Defs
        if parselib.IsToken(line, 0, u'function'):
            parts = line.split()
            plen = len(parts)
            if plen >= 2 and parselib.IsGoodName(parts[1]):
                if plen == 2 or parts[2] == u"{":
                    rtags.AddFunction(taglib.Function(parts[1], lnum))
            continue

        # Check fname () function defs
        if u"(" in line:
            parts = line.split()
            plen = len(parts)
            if plen >= 2 and parselib.IsGoodName(parts[0]):
                if u''.join(parts[1:]).startswith("()"):
                    rtags.AddFunction(taglib.Function(parts[0], lnum))
            else:
                continue

    return rtags
Beispiel #7
0
def GenerateTags(buff):
    """Create a DocStruct object that represents a Scheme document
    @param buff: a file like buffer object (StringIO)

    """
    rtags = taglib.DocStruct()
    rtags.SetElementDescription('function', "Function Definitions")

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

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

        # Check Function Definitions
        if line.startswith('(') and u'define' in line:
            llen = len(line)
            idx = 1
            idx += (len(line[idx:]) - len(line[idx:].lstrip()))
            if llen > idx and line[idx:].startswith(u'define') and \
               (llen > (idx + 6)) and line[idx+6].isspace():
                idx = parselib.SkipWhitespace(line, idx + 6)
                if llen > idx and line[idx] == u'(':
                    # function with parameters
                    idx = parselib.SkipWhitespace(line, idx + 1)
                name = GetIdentifierName(line[idx:])
                if name is not None:
                    rtags.AddFunction(taglib.Function(name, lnum))

    return rtags
Beispiel #8
0
def GenerateTags(buff):
    """Create a DocStruct object that represents a Lua Script
    @param buff: a file like buffer object (StringIO)
    @todo: generate tags for lua tables?

    """
    rtags = taglib.DocStruct()
    rtags.SetElementDescription('function', "Function Definitions")

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

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

        # Check Regular Function Defs
        if u'function' in line:
            name = None
            if u'=' in line and line.index(u'=') < line.index(u'function'):
                name = line[:line.index(u'=')].strip()
            elif u'(' in line:
                idx = line.find(u'function')
                idx2 = line.find(u'(')
                if idx < idx2:
                    name = line[idx + 9:idx2].strip()
            else:
                continue

            if name is not None:
                rtags.AddFunction(taglib.Function(name, lnum))

    return rtags
Beispiel #9
0
    def format(self, tokensource, outfile):
        """Format the tokens from the document"""
        self.rtags = taglib.DocStruct()
        self.rtags.SetElementDescription('function', "Function Definitions")
        self.rtags.SetElementDescription('class', "Class Definitions")
        self.rtags.SetElementDescription('module', "Modules")
        self.SetSortOrder(self.rtags)

        line_count = 0
        current_line = []
        code_lines = []

        # Parse the file into lines of tokens and values
        for ttype, value in tokensource:
            if '\n' in value:
                if len(current_line):
                    code_lines.append((line_count, current_line))
                current_line = []
                line_count += value.count('\n')
                continue

            # Skip comments, strings, and space tokens
            if ttype in (Token.Comment.Single, Token.Literal.String.Double,
                         Token.Literal.String.Heredoc,
                         Token.Literal.String.Other):
                continue
            elif value.isspace():
                continue
            else:
                current_line.append((ttype, value))

        self.FindElements(code_lines)
Beispiel #10
0
def GenerateTags(buff):
    """Create a DocStruct object that represents a batch Script
    @param buff: a file like buffer object (StringIO)
    @todo: generate tags for batch tables?

    """
    rtags = taglib.DocStruct()
    rtags.SetElementDescription('label', "Labels")
    rtags.SetElementDescription('section', "Labels")

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

        # Skip comment and empty lines
        if (line.startswith(u"rem") and llen > 3 and line[3].isspace()) or not line:
            continue

        # Check for labels
        if line.startswith(u":"):
            name = parselib.GetFirstIdentifier(line[1:])
            if name is not None:
                rtags.AddElement('label', taglib.Section(name, lnum))

    return rtags
Beispiel #11
0
def GenerateTags(buff):
    """Create a DocStruct object that represents a XText file
    @param buff: a file like buffer object (StringIO)

    """
    rtags = taglib.DocStruct()
    rtags.SetElementDescription('parserrule', "Parser Rule Definitions")
    rtags.SetElementDescription('terminal', "Terminal Definitions")
    rtags.SetElementPriority('terminal', 2)
    rtags.SetElementDescription('enum', "Enum Definitions")
    rtags.SetElementPriority('enum', 1)
    rtags.SetElementDescription('literal', "Literal")

    scope_metatype = None
    # scope_elements list for preventing duplicates
    scope_elements = []
    scope = None
    feature = None
    line = 0

    for index, token, txt in lexer.get_tokens_unprocessed(buff.read()):
        #        print index, token, txt
        if token is Token.EndOfLine:
            line += 1
            continue
        elif token is Keyword and txt in ['enum', 'terminal']:
            scope = txt
            continue

        if token is Name.CrossRef:
            if feature and not '->' in feature.GetName():
                feature.SetName("%s->%s" % (feature.GetName(), txt))

        if token is Name.AbstractRule:
            if scope is None:
                scope_metatype = ParserRule(txt, line)
                rtags.AddElement('parserrule', scope_metatype)
            elif scope == 'enum':
                scope_metatype = Enum(txt, line)
                rtags.AddElement('enum', scope_metatype)
            elif scope == 'terminal':
                scope_metatype = Terminal(txt, line)
                rtags.AddElement('terminal', scope_metatype)

            scope = None
            feature = None
            del scope_elements[:]

        elif token is Name.Feature:
            if scope is None or scope == 'terminal':
                if not txt in scope_elements:
                    feature = Feature(txt, line)
                    scope_metatype.AddElement('feature', feature)
                    scope_elements.append(txt)
            elif scope == 'enum':
                scope_metatype.AddElement('literal', Literal(txt, line))

    return rtags
Beispiel #12
0
def GenerateTags(buff):
    """Create a DocStruct object that represents a Perl Script
    @param buff: a file like buffer object (StringIO)

    """
    rtags = taglib.DocStruct()
    rtags.SetElementDescription('package', "Packages")
    rtags.SetElementPriority('package', 3)
    rtags.SetElementDescription('subdec', "Subroutine Declarations")
    rtags.SetElementPriority('subdec', 2)
    rtags.SetElementDescription('subroutine', "Subroutines")
    rtags.SetElementPriority('subroutine', 1)
    inpod = False

    for lnum, line in enumerate(buff):
        # Check for POD docs and skip as necessary
        if line.startswith(u"=") and len(line) > 2:
            if line.startswith(u"=cut"):
                inpod = False
            elif line[1].isalpha():
                inpod = True
            continue

        if inpod:
            continue

        # Not in POD so try to parse for elements
        line = line.strip()
        llen = len(line)

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

        # Check for subroutines
        if llen > 3 and line.startswith('sub') and line[3].isspace():
            sub = ExtractSubroutine(line)
            if sub is not None:
                if sub[0]:
                    rtags.AddElement('subdec',
                                     taglib.Function(sub[1], lnum, "subdec"))
                else:
                    rtags.AddElement(
                        'subroutine',
                        taglib.Function(sub[1], lnum, "subroutine"))
        elif llen > 7 and line.startswith('package') and line[7].isspace():
            # Look for a package declaration
            parts = line.split()
            if line.endswith(u";") and len(parts) <= 3:
                name = parts[1].rstrip(u";")
                rtags.AddElement('package', Package(name, lnum))
        else:
            pass

    return rtags
Beispiel #13
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
Beispiel #14
0
def GenerateTags(buff):
    """Create a DocStruct object that represents an Inno Setup Script
    @param buff: a file like buffer object (StringIO)
    @todo: perhaps group functions, procedures within the Scope of a Section
           object.

    """
    rtags = taglib.DocStruct()
    rtags.SetElementDescription('section', "Sections")
    rtags.SetElementDescription('variable', "Defines")
    rtags.SetElementDescription('function', "Function Definitions")
    rtags.SetElementDescription('procedure', "Procedure Definitions")
    rtags.SetElementPriority('section', 3)
    rtags.SetElementPriority('function', 2)
    rtags.SetElementPriority('procedure', 1)

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

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

        # Check for a section
        if line.startswith(u"["):
            secend = line.find(u"]")
            if secend != -1:
                section = taglib.Section(line[1:secend], lnum)
                rtags.AddElement('section', section)
        elif line.startswith(u"function") and \
              len(line) > 8 and line[8].isspace():
            name = parselib.GetFirstIdentifier(line[8:].strip())
            if name is not None:
                rtags.AddFunction(taglib.Function(name, lnum))
        elif line.startswith(u"procedure") and \
              len(line) > 9 and line[9].isspace():
            name = parselib.GetFirstIdentifier(line[9:].strip())
            if name is not None:
                rtags.AddElement('procedure',
                                 taglib.Function(name, lnum, 'procedure'))
        elif line.startswith(u"#define") and \
              len(line) > 7 and line[7].isspace():
            name = parselib.GetFirstIdentifier(line[7:].strip())
            if name is not None:
                rtags.AddVariable(taglib.Variable(name, lnum))
        else:
            pass

    return rtags
Beispiel #15
0
def GenerateTags(buff):
    """Create a DocStruct object that represents a Fortran
    @param buff: a file like buffer object (StringIO)

    """
    rtags = taglib.DocStruct()
    rtags.SetElementDescription('pubfunct', "Public Functions")
    rtags.SetElementPriority('pubfunct', 4)
    rtags.SetElementDescription('function', "Functions")
    rtags.SetElementPriority('function', 3)
    rtags.SetElementDescription('pubsub', "Public Subroutines")
    rtags.SetElementPriority('pubsub', 2)
    rtags.SetElementDescription('subroutine', "Subroutines")
    rtags.SetElementPriority('subroutine', 1)

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

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

        # Temporary variables
        llen = len(line)
        tline = line.lower()

        # Check Subroutine, and Function Defs
        if tline.startswith(u'sub') and llen > 3 and line[3].isspace():
            name = parselib.GetFirstIdentifier(line[3:].strip())
            rtags.AddElement('sub', taglib.Function(name, lnum, 'subroutine'))
        elif tline.startswith(
                u'public sub') and llen > 10 and line[10].isspace():
            name = parselib.GetFirstIdentifier(line[10:].strip())
            rtags.AddElement('pubsub', taglib.Function(name, lnum, 'pubsub'))
        elif tline.startswith(u'function') and llen > 8 and line[8].isspace():
            name = parselib.GetFirstIdentifier(line[8:].strip())
            rtags.AddFunction(taglib.Function(name, lnum))
        elif tline.startswith(
                u'public function') and llen > 15 and line[15].isspace():
            name = parselib.GetFirstIdentifier(line[15:].strip())
            rtags.AddElement('pubfunct',
                             taglib.Function(name, lnum, 'pubfunct'))
        else:
            pass
    return rtags
Beispiel #16
0
    def format(self, tokensource, outfile):
        self.rtags = taglib.DocStruct()
        self.rtags.SetElementDescription('function', "Function Definitions")
        self.rtags.SetElementDescription('class', "Class Definitions")

        line_count = 0
        current_line = []
        code_lines = []

        # Parse the file into tokens and values
        for ttype, value in tokensource:
            if '\n' in value:
                code_lines.append((line_count, current_line))
                current_line = []
                line_count += value.count('\n')
                continue
            current_line.append((ttype, value))

        self.getCFV(code_lines)
Beispiel #17
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
def GenerateTags(buff):
    """Create a DocStruct object that represents the list of files in a patch
    @param buff: a file like buffer object (StringIO)
    """
    rtags = taglib.DocStruct()
    rtags.SetElementDescription('variable', "Files")

    # Parse the buffer
    for lnum, line in enumerate(buff):

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

        match = RE_FILE.match(line)
        if match:
            cname = match.groups()[-1]
            cobj = taglib.Variable(cname, lnum)
            rtags.AddVariable(cobj)
            continue
    return rtags
Beispiel #19
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 and parts[1].isalnum():
                rtags.AddElement('procedure', taglib.Procedure(parts[1], lnum))

    return rtags
Beispiel #20
0
def GenerateTags(buff):
    """Create a DocStruct object that represents Ada source code
    @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 Regular Function Defs
        if parselib.IsToken(line, 0, u'procedure'):
            name = parselib.GetFirstIdentifier(line[9:].strip())
            if name is not None:
                rtags.AddElement('procedure',
                                 taglib.Function(name, lnum, 'procedure'))

    return rtags
Beispiel #21
0
def GenerateTags(buff):
    """Create a DocStruct object that represents a Lisp document
    @param buff: a file like buffer object (StringIO)

    """
    rtags = taglib.DocStruct()
    rtags.SetElementDescription('function', "Function Definitions")

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

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

        # Check Function Definitions
        if line.startswith('(') and u'defun' in line:
            dend = line.find(u'defun') + 5
            if dend < len(line) and line[dend].isspace():
                parts = line[dend:].split()
                if len(parts) > 1 and parts[1].startswith('('):
                    rtags.AddFunction(taglib.Function(parts[0], lnum))

    return rtags
Beispiel #22
0
def GenerateTags(buff):
    """Create a DocStruct object that represents a MatLab/Octave document
    @param buff: a file like buffer object (StringIO)

    """
    rtags = taglib.DocStruct()
    rtags.SetElementDescription('function', "Function Definitions")

    # Do the parse
    for lnum, line in enumerate(buff):
        line = line.strip()

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

        # Check Regular Function Defs
        if parselib.IsToken(line, 0, u'function'):
            name = parselib.GetFirstIdentifier(line[8:])
            if name is not None and len(name):
                rtags.AddFunction(taglib.Function(name, lnum))
            continue

    return rtags
Beispiel #23
0
    def format(self, tokensource, outfile):
        """ take code, and convert to token, value, num list 
        """

        NotUsed(outfile)

        self.rtags = taglib.DocStruct()
        self.rtags.SetElementDescription('function', "Function Definitions")
        self.rtags.SetElementDescription('class', "Class Definitions")

        line_count = 0
        current_line = []

        # Parse the file into tokens and values
        for ttype, value in tokensource:
            #print "LINE:", value
            if '\n' in value:
                line_count += value.count('\n')
                continue
            #if ( len(value.strip()) != 0 ):
            current_line.append((ttype, value, line_count))

        #print current_line
        self.getFunctions(current_line)
Beispiel #24
0
def GenerateTags(buff):
    """Create a DocStruct object that represents an Editra Style Sheet
    @param buff: a file like buffer object (StringIO)

    """
    rtags = taglib.DocStruct()
    rtags.SetElementDescription('styletag', "Style Tags")

    c_element = None  # Currently found document element
    incomment = False # Inside a comment
    indef = False     # Inside a style definition {}

    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 if valid item to add to document
            if c_element is not None and line[idx] == u'{':
                rtags.AddElement('styletag', c_element)
                c_element = None

            # Check for coments
            if line[idx] == u'/' and llen > idx and line[idx+1] == u'*':
                idx += 2
                incomment = True
            elif line[idx] == u'*' and llen > idx and line[idx+1] == 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
                indef = True
            elif indef and line[idx] == u'}':
                idx += 1
                indef = False
            elif not indef and line[idx].isalpha():
                # Found start of tag
                name = parselib.GetFirstIdentifier(line[idx:])
                if name is not None:
                    # Make a tag but don't add it to the DocStruct till we
                    # find if a { is the next non space character to follow
                    c_element = StyleTag(name, lnum)
                    idx += len(name)
                else:
                    # This should never happen but if it does there must
                    # be something wrong with the document or the parse has
                    # gone afowl.
                    idx += 1
            else:
                idx += 1

    return rtags
Beispiel #25
0
def GenerateTags(buff):
    """Create a DocStruct object that represents a vala dacument
    @param buff: a file like buffer object (StringIO)

    """
    rtags = taglib.DocStruct()

    # Setup document structure
    rtags.SetElementDescription('function', "Function Definitions")

    inclass = False  # Inside a class defintion
    incomment = False  # Inside a comment
    infundef = False  # Inside a function definition
    lastclass = None
    lastfun = None
    openb = 0  # Keep track of open brackets

    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 coments
            if line[idx:].startswith(u'/*'):
                idx += 2
                incomment = True
            elif line[idx:].startswith(u'//') or 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
                continue
            elif line[idx] == u'{':
                idx += 1
                openb += 1
                # Class name must be followed by a {
                if not inclass and lastclass is not None:
                    inclass = True
                    rtags.AddClass(lastclass)
                elif lastfun is not None:
                    infundef = True
                    lastfun = None
                else:
                    pass
                continue
            elif line[idx] == u'}':
                idx += 1
                openb -= 1
                if inclass and openb == 0:
                    inclass = False
                    lastclass = None
                elif infundef and inclass and openb == 1:
                    infundef = False
                elif infundef and openb == 0:
                    infundef = False
                    lastfun = None
                else:
                    pass
                continue
            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)
                continue

            match = RE_METH.match(line[idx:])
            if match is not None:
                # Check that not a method call
                sline = line.strip()
                if sline.endswith(u';'):
                    idx += match.end(2)
                    continue

                # Most likely a definition so store it in the DocStruct
                name = match.group(2)

                # Secondary check for other end cases regex will find
                if name in KEYWORDS:
                    idx += match.end(2)
                    continue

                lastfun = name
                if inclass and lastclass is not None:
                    lastclass.AddMethod(
                        taglib.Method(name, lnum, lastclass.GetName()))
                else:
                    rtags.AddFunction(taglib.Function(name, lnum))
                idx += match.end(2)
            else:
                idx += 1

    return rtags
Beispiel #26
0
def GenerateTags(buff):
    """Create a DocStruct object that represents a Php Script
    @param buff: a file like buffer object (StringIO)

    """
    rtags = taglib.DocStruct()

    # Setup document structure
    rtags.SetElementDescription('function', "Function Definitions")

    inphp = False  # Are we in a php section or not
    inclass = False  # Inside a class defintion
    incomment = False  # Inside a comment
    infundef = False  # Inside a style definition {}
    lastclass = None
    lastfun = None
    openb = 0  # Keep track of open brackets

    for lnum, line in enumerate(buff):
        line = line.strip()
        llen = len(line)
        idx = 0
        while idx < len(line):
            # Skip Whitespace
            idx += (len(line[idx:]) - len(line[idx:].lstrip()))

            # Check if in a <?php ?> block or not
            if line[idx:].startswith(u'<?php'):
                idx += 5
                inphp = True
            elif line[idx:].startswith(u'?>'):
                idx += 2
                inphp = False

            # Skip anything not in side of a php section
            if not inphp:
                idx += 1
                continue

            # Check for coments
            if line[idx:].startswith(u'/*'):
                idx += 2
                incomment = True
            elif line[idx:].startswith(u'//') or 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] in u'{':
                idx += 1
                openb += 1
                # Class name must be followed by a {
                if not inclass and lastclass is not None:
                    inclass = True
                    rtags.AddClass(lastclass)
                elif lastfun is not None:
                    infundef = True
                    lastfun = None
                else:
                    pass
            elif line[idx] == u'}':
                idx += 1
                openb -= 1
                if inclass and openb == 0:
                    inclass = False
                    lastclass = None
                elif infundef and inclass and openb == 1:
                    infundef = False
                elif infundef and openb == 0:
                    infundef = False
                    lastfun = None
                else:
                    pass
            elif not infundef and line[idx:].startswith(u'class') \
                 and llen > idx + 5 and line[idx+5].isspace():
                idx += 5

                # Skip whitespace
                idx += (len(line[idx:]) - len(line[idx:].lstrip()))

                name = parselib.GetFirstIdentifier(line[idx:])
                if name is not None:
                    idx += len(name)  # Move past the class name
                    lastclass = taglib.Class(name, lnum)
                else:
                    # Something must be wrong so skip ahead and keep going
                    idx += 5
            elif line[idx:].startswith(
                    u'function') and llen > idx + 8 and line[idx +
                                                             8].isspace():
                idx += 8

                # Skip whitespace
                idx += (len(line[idx:]) - len(line[idx:].lstrip()))

                name = parselib.GetFirstIdentifier(line[idx:])
                if name is not None:
                    lastfun = name
                    idx += len(name)

                    # Skip whitespace
                    idx += (len(line[idx:]) - len(line[idx:].lstrip()))

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

                    if inclass and lastclass is not None:
                        lastclass.AddMethod(
                            taglib.Method(name, lnum, lastclass.GetName()))
                    else:
                        rtags.AddFunction(taglib.Function(name, lnum))
            elif inclass and line[idx:].startswith(u'var') \
                 and llen > idx + 3 and line[idx+3].isspace():
                # Look for class variables
                idx += 3
                parts = line[idx:].split()
                if len(parts) and parts[0].startswith(u'$'):
                    name = parselib.GetFirstIdentifier(parts[0][1:])
                    if name is not None and lastclass is not None:
                        name = u'$' + name
                        lastclass.AddVariable(
                            taglib.Variable(name, lnum, lastclass.GetName()))
                        idx += len(name)
            elif line[idx] == u'=' and llen > idx + 1 and line[idx +
                                                               1] != u'=':
                break  # jump to next line when we find an assigment
            else:
                idx += 1

    return rtags
Beispiel #27
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
Beispiel #28
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
Beispiel #29
0
def GenerateTags(buff):
    """Create a DocStruct object that represents a Php Script
    @param buff: a file like buffer object (StringIO)

    """
    rtags = taglib.DocStruct()

    # Setup document structure
    rtags.SetElementDescription('function', "Function Definitions")

    inphp = False        # Are we in a php section or not
    inclass = False      # Inside a class defintion
    incomment = False    # Inside a comment
    infundef = False     # Inside a function definition
    lastclass = None
    lastfun = None
    instring = False
    openb = 0            # Keep track of open brackets

    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)

            # Walk through strings ignoring contents
            if instring or line[idx] in (u"'", u'"'):
                idx, instring = parselib.FindStringEnd(line[idx:], idx)
                # For multiline strings
                if instring:
                    continue

            # Check if in a <?php ?> block or not
            if line[idx:].startswith(u'<?'):
                idx += 2
                if line[idx:].startswith(u'php'):
                    idx += 5
                inphp = True
            elif line[idx:].startswith(u'?>'):
                idx += 2
                inphp = False

            # Skip anything not in side of a php section
            if not inphp:
                idx += 1
                continue

            # Check for coments
            if line[idx:].startswith(u'/*'):
                idx += 2
                incomment = True
            elif line[idx:].startswith(u'//') or 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
                # Class name must be followed by a {
                if not inclass and lastclass is not None:
                    inclass = True
                    rtags.AddClass(lastclass)
                elif lastfun is not None:
                    infundef = True
                    lastfun = None
                else:
                    pass
            elif line[idx] == u'}':
                idx += 1
                openb -= 1
                if inclass and openb == 0:
                    inclass = False
                    lastclass = None
                elif infundef and inclass and openb == 1:
                    infundef = False
                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 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

                    if inclass and lastclass is not None:
                        lastclass.AddMethod(taglib.Method(name, lnum, lastclass.GetName()))
                    else:
                        rtags.AddFunction(taglib.Function(name, lnum))
            elif inclass and parselib.IsToken(line, idx, u'var'):
                # Look for class variables
                idx += 3
                parts = line[idx:].split()
                if len(parts) and parts[0].startswith(u'$'):
                    name = parselib.GetFirstIdentifier(parts[0][1:])
                    if name is not None and lastclass is not None:
                        name = u'$' + name
                        lastclass.AddVariable(taglib.Variable(name, lnum, lastclass.GetName()))
                        idx += len(name)
            else:
                idx += 1

    return rtags
Beispiel #30
0
def GenerateTags(buff):
    """Create a DocStruct object that represents the structure of a D source
    file.
    @param buff: a file like buffer object (StringIO)

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

    # State Variables
    incomment = False
    inclass = False
    lastclass = None

    # Note: comments can begin with // /* /+

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

        line = line.strip()

        if incomment:
            continue

        # Check for a class definition
        match = RE_CLASS.match(line)
        if match is not None:
            cname = match.groups()[0]
            cobj = taglib.Class(cname, lnum)
            rtags.AddClass(cobj)
            lastclass = cobj  # save ref to the class obj
            continue

        if lastclass is not None:
            # Check for a class method
            match = RE_METH.match(line)
            if match is not None:
                groups = match.groups()
                lastclass.AddMethod(taglib.Method(groups[-1], lnum))
                continue

            fobj = CheckForFunction(line, lnum)
            if fobj is not None:
                fname = fobj.GetName()
                if fname == 'this':
                    lastclass.AddMethod(taglib.Method(fname, lnum))
#                else:
#                    print "OUT OF SCOPE", lnum
#                    lastclass = None # Must have left the classes scope
#                    rtags.AddFunction(fobj)
            continue

        fobj = CheckForFunction(line, lnum)
        if fobj is not None:
            rtags.AddFunction(fobj)

    return rtags