def getCFV(self, code_lines): container_list = [] vset = set() for num, line in code_lines: try: # FUNCTION # if parselib.HasToken(line, Token.Keyword, "def"): self.insertFunction(line, num, container_list) # CLASS # elif parselib.HasToken(line, Token.Keyword, "class"): self.insertClass(line, num, container_list) # Global Variable Only # elif parselib.HasToken(line, Token.Operator, "="): vname = parselib.GetTokenValue(line, Token.Name) flevel = self._getLevel(line) if flevel == 0: if not vname in vset: self.rtags.AddVariable(taglib.Variable(vname, num)) vset.add(vname) except parselib.TokenNotFound: pass return self.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 insertClass(self, line, num, container_list): cname = parselib.GetTokenValue(line, Token.Name.Class) clevel = self._getLevel(line) ctag = None # Top Level Class if clevel == 0: ctag = taglib.Class(cname, num) self.rtags.AddClass(ctag) # Somewhere else else: for l, c, ob in reversed(container_list): if l < clevel: ctag = taglib.Class(cname, num, c) ob.AddElement('class', ctag) break if ctag: container_list.append((clevel, cname, ctag))
def getCFV(self, code_lines): container_list = [] vset = set() for num, line in code_lines: try: if len(line): ltxt = u"".join([l[1].strip() for l in line]) if len(ltxt) and not ltxt.startswith(u"#"): if parselib.BeginsWithAnyOf( line, Token.Keyword, (u"def", u"class", u"if", u"else")): self.pushPopScope(self._getLevel(line)) if len(self._levels) == 1: container_list = [(0, u"", self.rtags)] # FUNCTION # if parselib.HasToken(line, Token.Keyword, "def"): self.insertFunction(line, num, container_list) # CLASS # elif parselib.HasToken(line, Token.Keyword, "class"): self.insertClass(line, num, container_list) # Global Variable Only # elif parselib.HasToken(line, Token.Operator, "="): vname = parselib.GetTokenValue(line, Token.Name) flevel = self._getLevel(line) if flevel == 0: if not vname in vset: self.rtags.AddVariable(taglib.Variable(vname, num)) vset.add(vname) except parselib.TokenNotFound: pass return self.rtags
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 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