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
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
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
def parseTags(self, code_lines): """Parse all the tokens found in the lines of code""" container_list = [] vset = set() for num, line in code_lines: try: # Subroutine if parselib.HasToken(line, Token.Keyword, "sub"): fname = parselib.GetTokenValue(line, Token.Name.Function) self.rtags.AddElement( 'subroutine', taglib.Function(fname, num, "subroutine")) # Packages if parselib.HasToken(line, Token.Name.Builtin, "package"): name = None next = True for token, value in line: if not next: name += value break if token == Token.Name.Namespace: name = value next = False if name is not None: self.rtags.AddElement('package', taglib.Package(name, num)) except parselib.TokenNotFound: pass
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
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
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
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
def action_AddFunc(self, token, value, num, parms): """ Found function, add to rtag, and pop name """ varid, to_token, to_value, next_state = parms if token == to_token and value == to_value: self.state = next_state fname = self.ids[varid].pop() self.rtags.AddElement('function', taglib.Function(fname, num)) return True return False
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
def action_memfunc(self, token, value, num, parms): """ Found mem func, add to rtag, and pop name """ varid, to_token, to_value, next_state = parms if token == to_token and value == to_value: self.state = next_state fname = self.ids[varid].pop() self.currentclasstag.AddMethod( taglib.Function(fname, num, self.currentclasstag.GetName())) return True return False
def CheckForFunction(line, lnum): """Check for a function definition @param line: line to check @param lnum: line num @return: None or Function object """ match = RE_FUNC.match(line) if match is not None: fname = match.groups()[-1] fobj = taglib.Function(fname, lnum) return fobj else: return None
def insertFunction(self, line, num, container_list): fname = parselib.GetTokenValue(line, Token.Name.Function) flevel = self._getLevel(line) ftag = None # top level funcion if flevel == 0: ftag = taglib.Function(fname, num) self.rtags.AddFunction(ftag) # Somewhere else else: for l, c, ob in reversed(container_list): if l < flevel: if isinstance(ob, taglib.Class): ob.AddMethod(taglib.Method(fname, num, c)) else: ob.AddElement('function', taglib.Function(fname, num, c)) break if ftag: container_list.append((flevel, fname, ftag))
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 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
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
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
def GenerateTags(buff): """Create a DocStruct object that represents a haXe Script @param buff: a file like buffer object (StringIO) """ rtags = taglib.DocStruct() # Setup document structure rtags.SetElementDescription('class', "Class Definitions") 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'//'): 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 not infundef and parselib.IsToken( line, idx, u'var'): # Look for class variables idx = parselib.SkipWhitespace(line, idx + 3) name = parselib.GetFirstIdentifier(line[idx:]) if name is not None and lastclass is not None: lastclass.AddVariable( taglib.Variable(name, lnum, lastclass.GetName())) idx += len(name) else: idx += 1 return rtags
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
def GenerateTags(buff): """Create a DocStruct object that represents a Python file @param buff: a file like buffer object (StringIO) """ rtags = taglib.DocStruct() rtags.SetElementDescription('function', "Function Definitions") rtags.SetElementDescription('class', "Class Definitions") # Variables for managing the state of the parse parents = list() indent = 0 fn_indent = 0 parens = 0 # Paren nesting count indocstring = False ind_string = False # Double quote string ins_string = False # Single quote string infunction = False lastclass = None def NotInString(): """Return whether the current state of the parse is in a string or not. """ return not indocstring and not ind_string and not ins_string # Do the parse of the text for lnum, line in enumerate(buff): indent = 0 idx = 0 llen = len(line) while idx < llen: # Check for docstrings if not (ind_string or ins_string) and llen >= idx + 3 and line[idx:idx + 3] in [ '"""', "'''" ]: indocstring = not indocstring idx += 3 continue # If end of line or start of comment start next line if idx == llen or (line[idx] == u"#" and NotInString()): break # Check indent sensitive tokens if not indocstring and not line[idx].isspace(): if infunction and indent < fn_indent: infunction = False if lastclass is not None: if indent <= lastclass.get('indent', 0): parents = PopScopes(parents, indent) if len(parents): lastclass = parents[-1] else: lastclass = None # Check for if in a string or not if line[idx] == u"'" and not ind_string and \ idx > 0 and line[idx-1] != "\\": # Single string ins_string = not ins_string idx += 1 elif line[idx] == u'"' and not ins_string and \ idx > 0 and line[idx-1] != "\\": # Double String ind_string = not ind_string idx += 1 else: pass # Parse and look for elements to add to the DocStruct if not NotInString(): # Token is in a string so ignore and move on idx = idx + 1 elif line[idx].isspace(): # Get indent width for current scope if idx == 0: indent = (len(line) - len(line.lstrip())) idx += indent else: # Non indent space idx += 1 elif parselib.IsToken(line, idx, u'class'): idx += 5 cname = parselib.GetFirstIdentifier(line[idx:]) if cname is not None: if lastclass is None: rtags.AddClass(taglib.Class(cname, lnum)) # TODO: check for classes defined within classes lastclass = dict(name=cname, indent=indent) parents.append(dict(lastclass)) break # Go to next line elif parselib.IsToken(line, idx, u'def'): # Function/Method Definition idx += 3 fname = parselib.GetFirstIdentifier(line[idx:]) if line[idx].isspace() and fname is not None: infunction = True fn_indent = indent + 1 if not line[0].isspace() or lastclass is None or \ not len(lastclass.get("name", "")): rtags.AddFunction(taglib.Function(fname, lnum)) else: lclass = rtags.GetLastClass() if lclass is not None: lclass.AddMethod( taglib.Method(fname, lnum, lclass.GetName())) else: # Something must have failed with the parse so # ignore this element. pass break elif not infunction and line[idx] in u"()": # Track paren nesting to help with variable parsing if line[idx] == u"(": parens += 1 else: parens -= 1 idx += 1 elif not infunction and line[idx] == u"=" and not parens: # Check for Global and Class variables idx += 1 if line[idx] != u"=": # ignore == statements var = line[:idx - 1].strip().split() if len(var) == 1 and parselib.IsGoodName(var[0]): lclass = rtags.GetLastClass() # Check if we are still inside a class def or not if lastclass is not None and lclass is not None: vobj = taglib.Variable(var[0], lnum, lclass.GetName()) lclass.AddVariable(vobj) else: # Global Scope variable rtags.AddVariable(taglib.Variable(var[0], lnum)) else: # Nothing so skip ahead idx += 1 # Return the document structure object return rtags
def GenerateTags(buff): """Create a DocStruct object that represents a Verilog document @param buff: a file like buffer object (StringIO) @todo: add support for parsing module definitions / class variables """ rtags = taglib.DocStruct() rtags.SetElementDescription('class', "Class Definitions") rtags.SetElementDescription('task', "Task Definitions") rtags.SetElementDescription('function', "Function Definitions") rtags.SetElementPriority('class', 3) rtags.SetElementPriority('task', 2) rtags.SetElementPriority('function', 1) # Variables to track parse state inclass = False # Inside a class defintion incomment = False # Inside a comment intask = False # Inside a task definition infunction = False # Inside a function definition # Parse the text for lnum, line in enumerate(buff): line = line.strip() llen = len(line) idx = 0 while idx < len(line): # Skip any leading Whitespace idx = parselib.SkipWhitespace(line, idx) # Check for coments 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 parselib.IsToken(line, idx, u'class'): idx = parselib.SkipWhitespace(line, idx + 5) cname = parselib.GetFirstIdentifier(line[idx:]) if cname is not None: inclass = True rtags.AddClass(taglib.Class(cname, lnum)) break # go to next line elif inclass and parselib.IsToken(line, idx, u'endclass'): inclass = False break # go to next line elif parselib.IsToken(line, idx, u'task'): idx += 4 tname = parselib.GetTokenParenLeft(line[idx:]) if tname is not None: intask = True if inclass: lclass = rtags.GetLastClass() task = taglib.Function(tname, lnum, 'task', lclass.GetName()) lclass.AddElement('task', task) else: task = taglib.Function(tname, lnum, 'task') rtags.AddElement('task', task) break # goto next line elif parselib.IsToken(line, idx, u'function'): idx += 8 fname = parselib.GetTokenParenLeft(line[idx:]) if fname is not None: infunction = True if inclass: lclass = rtags.GetLastClass() lclass.AddMethod(taglib.Method(fname, lnum)) else: rtags.AddFunction(taglib.Function(fname, lnum)) break elif intask and parselib.IsToken(line, idx, u'endtask'): intask = False break # go to next line elif infunction and parselib.IsToken(line, idx, 'endfunction'): infunction = False break else: idx += 1 return rtags
def FindElements(self, lines): """Find the code elements in the list of formatted lines @param lines: list of code items """ # Parse state varibles containers = list() endcount = 0 # Parse each line for elements for lnum, line in lines: try: for token, value in line: if token == Token.Keyword: if value == "end": endcount = max(endcount - 1, 0) ccount = len(containers) dcount = endcount - ccount if ccount == 1: if not endcount: containers.pop() elif ccount > 1 and dcount == ccount or dcount < 0: containers.pop() elif value in "begin case do for if unless until while": # These items require an end clause endcount = endcount + 1 elif value == "def": nspace, fname = GetRubyFunction(line) if endcount and len(containers): meth = taglib.Method(fname, lnum) if nspace is not None: self.InsertMethod(nspace, meth) else: containers[-1].AddElement('method', meth) else: self.rtags.AddFunction( taglib.Function(fname, lnum)) endcount = endcount + 1 elif value == "class": cname = parselib.GetTokenValue( line, Token.Name.Class) cobj = taglib.Class(cname, lnum) self.SetSortOrder(cobj) if endcount and len(containers): containers[-1].AddElement('class', cobj) else: self.rtags.AddClass(cobj) containers.append(cobj) endcount = endcount + 1 elif value == "module": mname = parselib.GetTokenValue( line, Token.Name.Namespace) mobj = taglib.Module(mname, lnum) self.SetSortOrder(mobj) if endcount and len(containers): containers[-1].AddElement('module', mobj) else: self.rtags.AddElement('module', mobj) containers.append(mobj) endcount = endcount + 1 elif value == "raise": break continue else: continue except parselib.TokenNotFound, msg: pass
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
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
def GenerateTags(buff): """Create a DocStruct object that represents a SQL document @param buff: a file like buffer object (StringIO) """ rtags = taglib.DocStruct() # Setup document structure rtags.SetElementDescription('function', "Function Definitions") rtags.SetElementDescription('procedure', "Procedure Definitions") rtags.SetElementDescription('package', "Packages") rtags.SetElementPriority('package', 3) rtags.SetElementPriority('function', 2) rtags.SetElementPriority('procedure', 1) # State Variables inpackage = False # Inside a package incomment = False # Inside a comment infunpro = False # Inside a function or proceedure definition lastname = None # Name of last found element lastpkg = None # Last found package object lastpkgname = None # Name of last package 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 elif (inpackage or infunpro) and \ parselib.IsToken(line, idx, u'end', True): idx = parselib.SkipWhitespace(line, idx + 3) name = parselib.GetFirstIdentifier(line[idx:]) if inpackage and name == lastpkgname: inpackage = False lastpkgname = None elif infunpro and name == lastname: infunpro = False lastname = None elif not infunpro and parselib.IsToken(line, idx, u'package', True): # Skip whitespace idx = parselib.SkipWhitespace(line, idx + 7) name = parselib.GetFirstIdentifier(line[idx:]) if name is not None and name.lower() == u'body': idx = parselib.SkipWhitespace(line, idx + 3) name = parselib.GetFirstIdentifier(line[idx:]) if name is not None: inpackage = True lastpkgname = name lastpkg = taglib.Package(name, lnum) rtags.AddElement('package', lastpkg) elif parselib.IsToken(line, idx, u'function', True): # Skip whitespace idx = parselib.SkipWhitespace(line, idx + 8) name = parselib.GetFirstIdentifier(line[idx:]) if name is not None: infunpro = True lastname = name if lastpkg is not None: lastpkg.AddElement('function', taglib.Function(name, lnum)) else: rtags.AddFunction(taglib.Function(name, lnum)) elif parselib.IsToken(line, idx, u'procedure', True): # Skip whitespace idx = parselib.SkipWhitespace(line, idx + 9) name = parselib.GetFirstIdentifier(line[idx:]) if name is not None: infunpro = True lastname = name if lastpkg is not None: lastpkg.AddElement('procedure', taglib.Procedure(name, lnum)) else: rtags.AddElement('procedure', taglib.Procedure(name, lnum)) else: idx += 1 return rtags
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