Updated generator and parser to support Python 3.

This commit is contained in:
Gabe Schwartz
2013-06-12 16:02:30 -04:00
parent 6faf00b8e5
commit 931ebab822
2 changed files with 71 additions and 59 deletions

View File

@@ -1,5 +1,6 @@
#!/usr/bin/env python
from __future__ import print_function
import os, sys, re, string
# the list only for debugging. The real list, used in the real OpenCV build, is specified in CMakeLists.txt
@@ -43,13 +44,13 @@ class CppHeaderParser(object):
def get_macro_arg(self, arg_str, npos):
npos2 = npos3 = arg_str.find("(", npos)
if npos2 < 0:
print "Error: no arguments for the macro at %d" % (self.lineno,)
print("Error: no arguments for the macro at %d" % (self.lineno,))
sys.exit(-1)
balance = 1
while 1:
t, npos3 = self.find_next_token(arg_str, ['(', ')'], npos3+1)
if npos3 < 0:
print "Error: no matching ')' in the macro call at %d" % (self.lineno,)
print("Error: no matching ')' in the macro call at %d" % (self.lineno,))
sys.exit(-1)
if t == '(':
balance += 1
@@ -143,13 +144,13 @@ class CppHeaderParser(object):
angle_stack.append(0)
elif w == "," or w == '>':
if not angle_stack:
print "Error at %d: argument contains ',' or '>' not within template arguments" % (self.lineno,)
print("Error at %d: argument contains ',' or '>' not within template arguments" % (self.lineno,))
sys.exit(-1)
if w == ",":
arg_type += "_and_"
elif w == ">":
if angle_stack[0] == 0:
print "Error at %s:%d: template has no arguments" % (self.hname, self.lineno)
print("Error at %s:%d: template has no arguments" % (self.hname, self.lineno))
sys.exit(-1)
if angle_stack[0] > 1:
arg_type += "_end_"
@@ -173,7 +174,7 @@ class CppHeaderParser(object):
p1 = arg_name.find("[")
p2 = arg_name.find("]",p1+1)
if p2 < 0:
print "Error at %d: no closing ]" % (self.lineno,)
print("Error at %d: no closing ]" % (self.lineno,))
sys.exit(-1)
counter_str = arg_name[p1+1:p2].strip()
if counter_str == "":
@@ -358,7 +359,7 @@ class CppHeaderParser(object):
if bool(re.match(r".*\)\s*const(\s*=\s*0)?", decl_str)):
decl[2].append("/C")
if "virtual" in decl_str:
print decl_str
print(decl_str)
return decl
def parse_func_decl(self, decl_str):
@@ -412,12 +413,12 @@ class CppHeaderParser(object):
if decl_str.startswith("CVAPI"):
rtype_end = decl_str.find(")", args_begin+1)
if rtype_end < 0:
print "Error at %d. no terminating ) in CVAPI() macro: %s" % (self.lineno, decl_str)
print("Error at %d. no terminating ) in CVAPI() macro: %s" % (self.lineno, decl_str))
sys.exit(-1)
decl_str = decl_str[args_begin+1:rtype_end] + " " + decl_str[rtype_end+1:]
args_begin = decl_str.find("(")
if args_begin < 0:
print "Error at %d: no args in '%s'" % (self.lineno, decl_str)
print("Error at %d: no args in '%s'" % (self.lineno, decl_str))
sys.exit(-1)
decl_start = decl_str[:args_begin].strip()
@@ -425,7 +426,7 @@ class CppHeaderParser(object):
if decl_start.endswith("operator"):
args_begin = decl_str.find("(", args_begin+1)
if args_begin < 0:
print "Error at %d: no args in '%s'" % (self.lineno, decl_str)
print("Error at %d: no args in '%s'" % (self.lineno, decl_str))
sys.exit(-1)
decl_start = decl_str[:args_begin].strip()
# TODO: normalize all type of operators
@@ -455,7 +456,7 @@ class CppHeaderParser(object):
return [] # exotic - dynamic 2d array
else:
#print rettype, funcname, modlist, argno
print "Error at %s:%d the function/method name is missing: '%s'" % (self.hname, self.lineno, decl_start)
print("Error at %s:%d the function/method name is missing: '%s'" % (self.hname, self.lineno, decl_start))
sys.exit(-1)
if self.wrap_mode and (("::" in funcname) or funcname.startswith("~")):
@@ -486,9 +487,9 @@ class CppHeaderParser(object):
npos += 1
t, npos = self.find_next_token(decl_str, ["(", ")", ",", "<", ">"], npos)
if not t:
print "Error: no closing ')' at %d" % (self.lineno,)
print decl_str
print decl_str[arg_start:]
print("Error: no closing ')' at %d" % (self.lineno,))
print(decl_str)
print(decl_str[arg_start:])
sys.exit(-1)
if t == "<":
angle_balance += 1
@@ -583,7 +584,7 @@ class CppHeaderParser(object):
if block_type in ["file", "enum"]:
continue
if block_type not in ["struct", "class", "namespace"]:
print "Error at %d: there are non-valid entries in the current block stack " % (self.lineno, self.block_stack)
print("Error at %d: there are non-valid entries in the current block stack " % (self.lineno, self.block_stack))
sys.exit(-1)
if block_name:
n += block_name + "."
@@ -605,7 +606,7 @@ class CppHeaderParser(object):
stmt_type = "block"
if context == "block":
print "Error at %d: should not call parse_stmt inside blocks" % (self.lineno,)
print("Error at %d: should not call parse_stmt inside blocks" % (self.lineno,))
sys.exit(-1)
if context == "class" or context == "struct":
@@ -632,7 +633,7 @@ class CppHeaderParser(object):
try:
classname, bases, modlist = self.parse_class_decl(stmt[len("typedef "):])
except:
print "Error at %s:%d" % (self.hname, self.lineno)
print("Error at %s:%d" % (self.hname, self.lineno))
exit(1)
if classname.startswith("_Ipl"):
classname = classname[1:]
@@ -647,7 +648,7 @@ class CppHeaderParser(object):
try:
classname, bases, modlist = self.parse_class_decl(stmt)
except:
print "Error at %s:%d" % (self.hname, self.lineno)
print("Error at %s:%d" % (self.hname, self.lineno))
exit(1)
decl = []
if ("CV_EXPORTS_W" in stmt) or ("CV_EXPORTS_AS" in stmt) or (not self.wrap_mode):# and ("CV_EXPORTS" in stmt)):
@@ -767,7 +768,7 @@ class CppHeaderParser(object):
state = SCAN
if state != SCAN:
print "Error at %d: invlid state = %d" % (self.lineno, state)
print("Error at %d: invlid state = %d" % (self.lineno, state))
sys.exit(-1)
while 1:
@@ -795,7 +796,7 @@ class CppHeaderParser(object):
while 1:
t2, pos2 = self.find_next_token(l, ["\\", "\""], pos2)
if t2 == "":
print "Error at %d: no terminating '\"'" % (self.lineno,)
print("Error at %d: no terminating '\"'" % (self.lineno,))
sys.exit(-1)
if t2 == "\"":
break
@@ -836,7 +837,7 @@ class CppHeaderParser(object):
if token == "}":
if not self.block_stack:
print "Error at %d: the block stack is empty" % (self.lineno,)
print("Error at %d: the block stack is empty" % (self.lineno,))
self.block_stack[-1:] = []
if pos+1 < len(l) and l[pos+1] == ';':
pos += 1
@@ -851,13 +852,13 @@ class CppHeaderParser(object):
Prints the list of declarations, retrieived by the parse() method
"""
for d in decls:
print d[0], d[1], ";".join(d[2])
print(d[0], d[1], ";".join(d[2]))
for a in d[3]:
print " ", a[0], a[1], a[2],
print(" ", a[0], a[1], a[2], end="")
if a[3]:
print "; ".join(a[3])
print("; ".join(a[3]))
else:
print
print()
if __name__ == '__main__':
parser = CppHeaderParser()
@@ -867,4 +868,4 @@ if __name__ == '__main__':
#for hname in sys.argv[1:]:
#decls += parser.parse(hname, wmode=False)
parser.print_decls(decls)
print len(decls)
print(len(decls))