def isstatic(line): if line is None: return FALSE if len(line) < 10: return FALSE # min is "static int" words = wstring.split(line, " \t\r\n*;[](){}?:,", "*") i = 0 if string.lower(words[0]) == "extern": i = i + 1 return string.lower(words[i]) == "static"
def openstruct(line): if line is None: return FALSE line = removecomment(line) if len(line) == 0: return FALSE temp = string.lower(line) words = wstring.split(temp, " \t\r\n") if len(words) < 2: return FALSE # 2 tokens or 1 token and 1 ident first = string.lower(words[0]) second = string.lower(words[1]) if first in ["typedef"]: return FALSE if first in ["struct", "enum"]: return TRUE """ if first in ["extern"]: if second not in [ "struct"] : return FALSE # following code keep instances of struct inside classes # but I have decided to put them oustide classes along with decl. if ';' in temp: if not '{' in temp: return FALSE if not '}' in temp: return FALSE k = string.rfind(temp, '}') if i < k: return FALSE return TRUE """ return FALSE
def gettypeident(decl): l = wstring.split(decl, " *;", "*") t = [] n = "" for w in l: if w in types: t.append(w) else: n = w #print "gettypeident", decl, l,"type", t, "name=", n return t, n
def lastword(line): words = wstring.split(line, symstr) #print "lastword in", words l = len(words) if l < 1: return None last = words[l -1] #print "is ident?", last if not isident(last): return None return last
def typename(line): i = string.rfind(line, '}') if i == -1: i = 0; words = wstring.split(line[i:], " };*", "*") l = len(words) if l == 0: return None last = words[l - 1] if last in types: return None return last
def islocalvar(line): #print "isvardef", line if line is None: return FALSE line = removecomment(line) if len(line) < 6: return FALSE # Minimum is int a; if line[0] == "#": return FALSE # Define statement # I keep separators for further tests and * that is a keyword words = wstring.split(line, vardelimiters, '*,;[]()=') if len(words) < 3: return FALSE # extern declaration not concerned, * require a type before if words[0] in [ "extern", "*" ]: return FALSE # modifier concern either a var or a function if words[0] in modifiers : return TRUE if structinstance(words): return TRUE if linestruct(line): return TRUE if words[0] in predefined: words.remove(words[0]) if words[0] == '*': words.remove('*') else: # skipping all type keywords if words[0] not in types: return FALSE while words[0] in types: words.remove(words[0]) if len(words) == 0: return FALSE # Unknown statement, requires at last: ident and delimiter if len(words) < 2: return FALSE if not isident(words[0]): return FALSE second = words[1] if second == ';': return TRUE # End of declaration if second == ',': return TRUE # Multiple declarations if second == '[': return TRUE # Array if second == '=': return TRUE # Assignent if second == '(': return FALSE # Function if isident(second): print words[0], "is a type defined by the programmer" return TRUE if second in [ '*', 'huge', 'far', 'near']: try: if isident(words[2]): return TRUE except: pass print "What is \"" + second + "\" ->", line + '?' return FALSE
def typeonly(line): # Removing any embedded or trailing comment while commentfound(line): line = nocomment(line) if len(line) < 3: return FALSE # Minimum is: int words = wstring.split(line, " \t\r\n*;", "*;") # * is a word if len(words) < 1: return FALSE if words[0] == "extern": return FALSE for w in words: if w not in types: return FALSE return TRUE
def multivar(line): line = removecomment(line) line = wstring.strip(line) #print "in multivar", line l = wstring.split(line, ",;") #print "result=", len(l) if len(l) > 1: return TRUE if ',' in line: print "Error in multi-declaration, string", line sys.exit(0) return FALSE
def right(dec): i = string.find(dec, "(") j = string.find(dec, ")") right = dec[i+1:j] w = wstring.split(right, " ,\t*", '*') # I keep only the types in the list of words pw = [] for n in w: if n in reserved: pw.append(n) # word in list of types return pw
def getident(line): line = removecomment(line) l = wstring.split(line, symstr, '*') # * is both delimiter and keyword in C #print "getident", line, l for w in l: wi = string.lower(w) if wi == "typedef": continue if w in types: continue # Skipping type keywords if isident(w): return w # Identifier returned return None # Anything else is not of concern return None
def getvarnames(line): vlist = [] l = wstring.split(line, " *;,\t\r\n", '*') for w in l: i = string.find(w, '[') if i != -1: w = w[:i] w = wstring.strip(w) if w in types: continue if isident(w): vlist.append(w) return vlist
def structname(line): line = removecomment(line) words = wstring.split(line, " {;") if len(words) < 2: return None first = string.lower(words[0]) second = string.lower(words[1]) if first == "extern": if len(words) < 3: return None first = second second = string.lower(words[2]) if first == "struct": return second return None
def splitvardef(str): # split into strings separated by "," or ";" and remove trailing ";" # when curly braces openned, a flag is set and separators ignored # until curly braces closed str = removecomment(str) #print str words = [] newword = "" flag = FALSE str = wstring.strip(str) # Remove spaces for c in str: if (c in ",;") & (flag == FALSE): # char in delimiter list if newword != "": words.append(newword) # then word ended, add it newword = "" # clear it else: # else char in a word newword = newword + c # add char to word if c == '{': flag = TRUE if c == '}': flag = FALSE # End of string, either a word or a delimiter sequence remains here if newword != "": words.append(newword) # get types from the first string and remove it from the list first = words[0] words.remove(first) tlist = wstring.split(first, " \t*=", "*=") #print tlist flag = FALSE commontype = "" idfound = "" # get type keywords for v in tlist: if v == "=": break # right part of an assignment is ignored here if v in types: if flag == FALSE: commontype = commontype + v + ' ' continue print "Error, keyword \'" + v + "\' follows ident \'" + idfound + "\' in", str sys.exit(0) flag = TRUE idfound = v newlist = [] # list of separated vars newlist.append(addsemicolon(first)) # first string unchanged for n in words: if not isdeclaration(n): n = commontype + n newlist.append(n + ';') # add type to each other return newlist
def vartypelist(line): while commentfound(line): line = nocomment(line) words = wstring.split(line, " \t*;[", '*') t = [] # Adding all type keyword but "extern" # exiting when encountering the variable's name for w in words: if w in types: if w != "extern": t.append(w) else: break return t
def typedefstruct(line): if line is None: return FALSE line = removecomment(line) if len(line) == 0: return FALSE temp = string.lower(line) words = wstring.split(temp, " \t\r\n") if len(words) < 2: return FALSE # 2 tokens or 1 token and 1 ident first = string.lower(words[0]) second = string.lower(words[1]) if first not in ["typedef"]: return FALSE if second not in ["struct", "enum"]: return FALSE return TRUE
def getargs(line): i = string.find(line, '(') j = string.find(line, ')') if i == -1: return None, 0,0 if j <= i: return None, 0 ,0 line = line[i + 1: j] # Keeping arguments if line == None: return None, 0, 0 alist = wstring.split(line, ',') #print "getargs", arglist arglist = [] for w in alist: arglist.append(wstring.strip(w)) return arglist, i, j
def istypstruct(line): words = wstring.split(line, vardelimiters, "*") for w in words: if w in typstruct: #print "istypstruct - ", w, "in typedef struct" return TRUE if w in types: #print w, "in type" continue if isident(w): #print w, "in ident" return FALSE break return FALSE
def isfunction(line): global shortened if line is None: return FALSE # Is the line blank or too short? # The shortest seems to be a(), 3 characters long if len(line) < 3: return FALSE # Removing any embedded or trailing comment line = removecomment(line) if len(line) < 3: return FALSE # test again if line[0] == "#": return FALSE # Removing any block of statements i = string.find(line, "{") if i >= 0: line = line[:i] # test again if len(line) < 3: return FALSE # Is the last character a ; or a : ? if line[-1] == ";": return FALSE if line[-1] == ':': return FALSE # This code must be found if string.find(line, '(') == -1: return FALSE # Is the line splitted? Not handled here if line[-1] == "\\": return FALSE # Is the line starting a construct? w = wstring.split(line, " (") first = w[0] if first in constructs: return FALSE # Anything else starts a function definition # Is the return type missing? Add the default "int" type newreturn = "int " if first in reserved: newreturn = "" # Add nothing shortened = newreturn + line #print "function >", line return TRUE
def isdefinition(line): if line == None: return FALSE line = wstring.strip(line) le = len(line) if le < 3: return FALSE # minimum length: f() # I get the left part j = 0 for c in line: if c in [ '(', '[', ';', '=', ',' ]: break j = j + 1 # function name without type not a call (terminated by semicolon) soleident = ((c == '(') & (line[-1:] != ';')) #print "solident", line, soleident, line[-1:] le = min(le, j) # Analysing the left part of the line line = line[:le] if line == "": return FALSE words = wstring.split(line, " *", '*') if len(words) == 0: return FALSE if len(words) == 1: if isident(words[0]): return soleident # True is a identifier followed by ( #print "words >>>", words # Handling case of the *ident= assignment inside block if words[0] == '*': return FALSE tflag = FALSE iflag = FALSE for w in words: if w == "extern": return FALSE # External declarations are ignored if w in declarator: #print "type", w if iflag: return FALSE # Type following ident? tflag = TRUE continue # Continue to next word... if isident(w): #print "ident", w iflag = TRUE # Must be the last word continue return FALSE # Neither type nor identifier #print "ok", line return tflag # Found a type followed or not by an identifier then (=;,[
def isvardecl(line): if line is None: return FALSE if len(line) < 4: return FALSE # Minimum is "t a;" if t is a typedef line = wstring.strip(line) if line[0] == '#': return FALSE if isprototype(line): return FALSE # some delimiters are kept in the list of words words = wstring.split(line, vardelimiters , ";,()*[]") #print "(isvardecl word in type?)", words[0] if len(words) < 3: return FALSE if words[0] in modifiers: return TRUE if words[0] in types: return TRUE if words[0] in predefined: return TRUE return FALSE
def isarray(line): words = wstring.split(line, " \r\t\n*()[];{}=", "*[]();{}=") if len(words) == 0: return FALSE if words[0] not in types: return FALSE while len(words) > 0: if words[0] in types: words.remove(words[0]) continue break if len(words) == 0: return FALSE if not isident(words[0]): return FALSE words.remove(words[0]) if len(words) == 0: return FALSE if words[0] != '[': return FALSE return TRUE
def isdeclaration(line): if line is None: return FALSE if len(line) < 6: return FALSE # min is "int a;" words = wstring.split(line, " \t\r\n*;[](){}?:,", "*") if words[0] == "extern": words.remove(words[0]) first = words[0] if first == "*": return FALSE # no valid as first keyword if first not in types: return FALSE # no typed # first being a type keyword, then scanning the line for w in words: if w in types: continue if isident(w): return TRUE return FALSE return TRUE
def recordinstance(line): line = removecomment(line) if line is None: return FALSE if len(line) < 18: return FALSE # Minimum is "extern struct s a;" line = wstring.strip(line) if line[0] == '#': return FALSE words = wstring.split(line, symstr) if len(words) < 2: return FALSE first = string.lower(words[0]) second = string.lower(words[0]) if first == "enum": return TRUE if first != "extern" : return FALSE if second != "struct": return FALSE if ';' not in line: return FALSE if '{' in line: i = string.find(line, ';') j = string.find(line, '{') if j < i: return FALSE return TRUE
def isprototype(line): global prototype prototype = "" if line is None: return FALSE # Removing any embedded or trailing comment line = removecomment(line) # Is the line blank or too short? # The shortest seems to be a(), 3 characters long if len(line) < 3: return FALSE # Is the line a # statement? if line[0] == "#": return FALSE # Removing any block of statements i = string.find(line, "{") if i >= 0: line = line[:i] # Is the reduced string too short for a declaration? if len(line) < 3: return FALSE # Irrelevant if '=' in line: return FALSE # These codes must be found if '(' not in line : return FALSE if ')' not in line: return FALSE prototype = line # Is the line starting a struct? w = wstring.split(line, " (") if w[0] in ['typedef', 'struct', "union" ]: return FALSE # A function declaration or a prototype? line = wstring.strip(line) if line[-1] == ';': return TRUE return FALSE
def isvardef(line): #print "928 isvardef", line if line is None: return FALSE line = removecomment(line) if len(line) < 6: return FALSE # Minimum is int a; if line[0] == "#": return FALSE # Define statement if isfunction(line): #print "function >>>", line return FALSE #print "var >>>", line # I keep * that is a type keyword words = wstring.split(line, vardelimiters, '*;[') if len(words) < 3: return FALSE #print "words[0]", words[0] if words[0] in modifiers: return TRUE if words[0] in predefined: return TRUE if words[0] in types: return TRUE if structinstance(words): return TRUE return FALSE
def isextern(line): if line is None: return FALSE if len(line) < 6: return FALSE # min is "int a;" words = wstring.split(line, " \t\r\n*;[](){}?:,", "*") return string.lower(words[0]) == "extern";
def typeident(line): i = string.find(line, "(") words = wstring.split(line[:i], " \t*", "*") return words[len(words) - 1]
def left(line): i = string.find(line, "(") w = wstring.split(line[:i], " \t*", "*") return w