parameters: (self, hname, wmode=True)
return_statement: return decls
docstring: The main method. Parses the input file.
          Returns the list of declarations (that can be print using print_decls)
docstring_summary: The main method. Parses the input file.
          Returns the list of declarations (that can be print using print_decls)
docstring_tokens: The main method . Parses the input file . Returns the list of declarations ( that can be print using print_decls )
function: def parse(self, hname, wmode=True):
          """
          The main method. Parses the input file.
          Returns the list of declarations (that can be print using print_decls)
          """
          self.hname = hname
          decls = []
          f = io.open(hname, 'rt', encoding='utf-8')
          linelist = list(f.readlines())
          f.close()
  
          # states:
          SCAN = 0 # outside of a comment or preprocessor directive
          COMMENT = 1 # inside a multi-line comment
          DIRECTIVE = 2 # inside a multi-line preprocessor directive
          DOCSTRING = 3 # inside a multi-line docstring
          DIRECTIVE_IF_0 = 4 # inside a '#if 0' directive
  
          state = SCAN
  
          self.block_stack = [["file", hname, True, True, None]]
          block_head = ""
          docstring = ""
          self.lineno = 0
          self.wrap_mode = wmode
  
          depth_if_0 = 0
  
          for l0 in linelist:
              self.lineno += 1
              #print(state, self.lineno, l0)
  
              l = l0.strip()
  
              # G-API specific aliases
              l = self.batch_replace(l, [
                      ("GAPI_EXPORTS", "CV_EXPORTS"),
                      ("GAPI_EXPORTS_W", "CV_EXPORTS_W"),
                      ("GAPI_EXPORTS_W_SIMPLE","CV_EXPORTS_W_SIMPLE"),
                      ("GAPI_WRAP", "CV_WRAP"),
                      ("GAPI_PROP", "CV_PROP"),
                      ("GAPI_PROP_RW", "CV_PROP_RW"),
                      ('defined(GAPI_STANDALONE)', '0'),
                  ])
  
              if state == SCAN and l.startswith("#"):
                  state = DIRECTIVE
                  # fall through to the if state == DIRECTIVE check
  
              if state == DIRECTIVE:
                  if l.endswith("\\"):
                      continue
                  state = SCAN
                  l = re.sub(r'//(.+)?', '', l).strip()  # drop // comment
                  if l in [
                      '#if 0',
                      '#if defined(__OPENCV_BUILD)', '#ifdef __OPENCV_BUILD',
                      '#if !defined(OPENCV_BINDING_PARSER)', '#ifndef OPENCV_BINDING_PARSER',
                  ]:
                      state = DIRECTIVE_IF_0
                      depth_if_0 = 1
                  continue
  
              if state == DIRECTIVE_IF_0:
                  if l.startswith('#'):
                      l = l[1:].strip()
                      if l.startswith("if"):
                          depth_if_0 += 1
                          continue
                      if l.startswith("endif"):
                          depth_if_0 -= 1
                          if depth_if_0 == 0:
                              state = SCAN
                  else:
                      # print('---- {:30s}:{:5d}: {}'.format(hname[-30:], self.lineno, l))
                      pass
                  continue
  
              if state == COMMENT:
                  pos = l.find("*/")
                  if pos < 0:
                      continue
                  l = l[pos+2:]
                  state = SCAN
  
              if state == DOCSTRING:
                  pos = l.find("*/")
                  if pos < 0:
                      docstring += l0
                      continue
                  docstring += l[:pos] + "\n"
                  l = l[pos+2:]
                  state = SCAN
  
              if l.startswith('CV__') or l.startswith('__CV_'): # just ignore these lines
                  #print('IGNORE: ' + l)
                  state = SCAN
                  continue
  
              if state != SCAN:
                  print("Error at %d: invalid state = %d" % (self.lineno, state))
                  sys.exit(-1)
  
              while 1:
                  # NB: Avoid parsing '{' for case:
                  # foo(Obj&& = {});
                  if re.search(r'=\s*\{\s*\}', l):
                      token, pos = ';', len(l)
                  else:
                      token, pos = self.find_next_token(l, [";", "\"", "{", "}", "//", "/*"])
  
                  if not token:
                      block_head += " " + l
                      block_head = block_head.strip()
                      if len(block_head) > 0 and block_head[-1] == ')' and block_head.startswith('CV_ENUM_FLAGS('):
                          l = ''
                          token = ';'
                      else:
                          break
  
                  if token == "//":
                      block_head += " " + l[:pos]
                      l = ''
                      continue
  
                  if token == "/*":
                      block_head += " " + l[:pos]
                      end_pos = l.find("*/", pos+2)
                      if len(l) > pos + 2 and l[pos+2] == "*":
                          # '/**', it's a docstring
                          if end_pos < 0:
                              state = DOCSTRING
                              docstring = l[pos+3:] + "\n"
                              break
                          else:
                              docstring = l[pos+3:end_pos]
  
                      elif end_pos < 0:
                          state = COMMENT
                          break
                      l = l[end_pos+2:]
                      continue
  
                  if token == "\"":
                      pos2 = pos + 1
                      while 1:
                          t2, pos2 = self.find_next_token(l, ["\\", "\""], pos2)
                          if t2 == "":
                              print("Error at %d: no terminating '\"'" % (self.lineno,))
                              sys.exit(-1)
                          if t2 == "\"":
                              break
                          pos2 += 2
  
                      block_head += " " + l[:pos2+1]
                      l = l[pos2+1:]
                      continue
  
                  stmt = (block_head + " " + l[:pos]).strip()
                  stmt = " ".join(stmt.split()) # normalize the statement
                  #print(stmt)
                  stack_top = self.block_stack[-1]
  
                  if stmt.startswith("@"):
                      # Objective C ?
                      break
  
                  decl = None
                  if stack_top[self.PROCESS_FLAG]:
                      # even if stack_top[PUBLIC_SECTION] is False, we still try to process the statement,
                      # since it can start with "public:"
                      docstring = docstring.strip()
                      stmt_type, name, parse_flag, decl = self.parse_stmt(stmt, token, docstring=docstring)
                      if decl:
                          if stmt_type.startswith("enum"):
                              decls.append([stmt_type + " " + self.get_dotted_name(name), "", [], decl, None, ""])
                          else:
                              decls.append(decl)
  
                              if self._generate_gpumat_decls and ("cv.cuda" in decl[0] or decl[0] in [
                                  "cv.imshow", # https://github.com/opencv/opencv/issues/18553
                              ]):
                                  # If function takes as one of arguments Mat or vector<Mat> - we want to create the
                                  # same declaration working with GpuMat
                                  args = decl[3]
                                  has_mat = len(list(filter(lambda x: x[0] in {"Mat", "vector_Mat"}, args))) > 0
                                  if has_mat:
                                      _, _, _, gpumat_decl = self.parse_stmt(stmt, token, mat="cuda::GpuMat", docstring=docstring)
                                      if gpumat_decl != decl:
                                          decls.append(gpumat_decl)
  
                              if self._generate_umat_decls:
                                  # If function takes as one of arguments Mat or vector<Mat> - we want to create the
                                  # same declaration working with UMat (this is important for T-Api access)
                                  args = decl[3]
                                  has_mat = len(list(filter(lambda x: x[0] in {"Mat", "vector_Mat"}, args))) > 0
                                  if has_mat:
                                      _, _, _, umat_decl = self.parse_stmt(stmt, token, mat="UMat", docstring=docstring)
                                      if umat_decl != decl:
                                          decls.append(umat_decl)
  
                          docstring = ""
                      if stmt_type == "namespace":
                          chunks = [block[1] for block in self.block_stack if block[0] == 'namespace'] + [name]
                          self.namespaces.add('.'.join(chunks))
                  else:
                      stmt_type, name, parse_flag = "block", "", False
  
                  if token == "{":
                      if stmt_type == "class":
                          public_section = False
                      else:
                          public_section = True
                      self.block_stack.append([stmt_type, name, parse_flag, public_section, decl])
  
                  if token == "}":
                      if not self.block_stack:
                          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
  
                  block_head = ""
                  l = l[pos+1:]
  
          return decls
function_tokens: def parse ( self , hname , wmode = True ) : self . hname = hname decls = [ ] f = io . open ( hname , 'rt' , encoding = 'utf-8' ) linelist = list ( f . readlines ( ) ) f . close ( ) # states: SCAN = 0 # outside of a comment or preprocessor directive COMMENT = 1 # inside a multi-line comment DIRECTIVE = 2 # inside a multi-line preprocessor directive DOCSTRING = 3 # inside a multi-line docstring DIRECTIVE_IF_0 = 4 # inside a '#if 0' directive state = SCAN self . block_stack = [ [ "file" , hname , True , True , None ] ] block_head = "" docstring = "" self . lineno = 0 self . wrap_mode = wmode depth_if_0 = 0 for l0 in linelist : self . lineno += 1 #print(state, self.lineno, l0) l = l0 . strip ( ) # G-API specific aliases l = self . batch_replace ( l , [ ( "GAPI_EXPORTS" , "CV_EXPORTS" ) , ( "GAPI_EXPORTS_W" , "CV_EXPORTS_W" ) , ( "GAPI_EXPORTS_W_SIMPLE" , "CV_EXPORTS_W_SIMPLE" ) , ( "GAPI_WRAP" , "CV_WRAP" ) , ( "GAPI_PROP" , "CV_PROP" ) , ( "GAPI_PROP_RW" , "CV_PROP_RW" ) , ( 'defined(GAPI_STANDALONE)' , '0' ) , ] ) if state == SCAN and l . startswith ( "#" ) : state = DIRECTIVE # fall through to the if state == DIRECTIVE check if state == DIRECTIVE : if l . endswith ( "\\" ) : continue state = SCAN l = re . sub ( r'//(.+)?' , '' , l ) . strip ( ) # drop // comment if l in [ '#if 0' , '#if defined(__OPENCV_BUILD)' , '#ifdef __OPENCV_BUILD' , '#if !defined(OPENCV_BINDING_PARSER)' , '#ifndef OPENCV_BINDING_PARSER' , ] : state = DIRECTIVE_IF_0 depth_if_0 = 1 continue if state == DIRECTIVE_IF_0 : if l . startswith ( '#' ) : l = l [ 1 : ] . strip ( ) if l . startswith ( "if" ) : depth_if_0 += 1 continue if l . startswith ( "endif" ) : depth_if_0 -= 1 if depth_if_0 == 0 : state = SCAN else : # print('---- {:30s}:{:5d}: {}'.format(hname[-30:], self.lineno, l)) pass continue if state == COMMENT : pos = l . find ( "*/" ) if pos < 0 : continue l = l [ pos + 2 : ] state = SCAN if state == DOCSTRING : pos = l . find ( "*/" ) if pos < 0 : docstring += l0 continue docstring += l [ : pos ] + "\n" l = l [ pos + 2 : ] state = SCAN if l . startswith ( 'CV__' ) or l . startswith ( '__CV_' ) : # just ignore these lines #print('IGNORE: ' + l) state = SCAN continue if state != SCAN : print ( "Error at %d: invalid state = %d" % ( self . lineno , state ) ) sys . exit ( - 1 ) while 1 : # NB: Avoid parsing '{' for case: # foo(Obj&& = {}); if re . search ( r'=\s*\{\s*\}' , l ) : token , pos = ';' , len ( l ) else : token , pos = self . find_next_token ( l , [ ";" , "\"" , "{" , "}" , "//" , "/*" ] ) if not token : block_head += " " + l block_head = block_head . strip ( ) if len ( block_head ) > 0 and block_head [ - 1 ] == ')' and block_head . startswith ( 'CV_ENUM_FLAGS(' ) : l = '' token = ';' else : break if token == "//" : block_head += " " + l [ : pos ] l = '' continue if token == "/*" : block_head += " " + l [ : pos ] end_pos = l . find ( "*/" , pos + 2 ) if len ( l ) > pos + 2 and l [ pos + 2 ] == "*" : # '/**', it's a docstring if end_pos < 0 : state = DOCSTRING docstring = l [ pos + 3 : ] + "\n" break else : docstring = l [ pos + 3 : end_pos ] elif end_pos < 0 : state = COMMENT break l = l [ end_pos + 2 : ] continue if token == "\"" : pos2 = pos + 1 while 1 : t2 , pos2 = self . find_next_token ( l , [ "\\" , "\"" ] , pos2 ) if t2 == "" : print ( "Error at %d: no terminating '\"'" % ( self . lineno , ) ) sys . exit ( - 1 ) if t2 == "\"" : break pos2 += 2 block_head += " " + l [ : pos2 + 1 ] l = l [ pos2 + 1 : ] continue stmt = ( block_head + " " + l [ : pos ] ) . strip ( ) stmt = " " . join ( stmt . split ( ) ) # normalize the statement #print(stmt) stack_top = self . block_stack [ - 1 ] if stmt . startswith ( "@" ) : # Objective C ? break decl = None if stack_top [ self . PROCESS_FLAG ] : # even if stack_top[PUBLIC_SECTION] is False, we still try to process the statement, # since it can start with "public:" docstring = docstring . strip ( ) stmt_type , name , parse_flag , decl = self . parse_stmt ( stmt , token , docstring = docstring ) if decl : if stmt_type . startswith ( "enum" ) : decls . append ( [ stmt_type + " " + self . get_dotted_name ( name ) , "" , [ ] , decl , None , "" ] ) else : decls . append ( decl ) if self . _generate_gpumat_decls and ( "cv.cuda" in decl [ 0 ] or decl [ 0 ] in [ "cv.imshow" , # https://github.com/opencv/opencv/issues/18553 ] ) : # If function takes as one of arguments Mat or vector<Mat> - we want to create the # same declaration working with GpuMat args = decl [ 3 ] has_mat = len ( list ( filter ( lambda x : x [ 0 ] in { "Mat" , "vector_Mat" } , args ) ) ) > 0 if has_mat : _ , _ , _ , gpumat_decl = self . parse_stmt ( stmt , token , mat = "cuda::GpuMat" , docstring = docstring ) if gpumat_decl != decl : decls . append ( gpumat_decl ) if self . _generate_umat_decls : # If function takes as one of arguments Mat or vector<Mat> - we want to create the # same declaration working with UMat (this is important for T-Api access) args = decl [ 3 ] has_mat = len ( list ( filter ( lambda x : x [ 0 ] in { "Mat" , "vector_Mat" } , args ) ) ) > 0 if has_mat : _ , _ , _ , umat_decl = self . parse_stmt ( stmt , token , mat = "UMat" , docstring = docstring ) if umat_decl != decl : decls . append ( umat_decl ) docstring = "" if stmt_type == "namespace" : chunks = [ block [ 1 ] for block in self . block_stack if block [ 0 ] == 'namespace' ] + [ name ] self . namespaces . add ( '.' . join ( chunks ) ) else : stmt_type , name , parse_flag = "block" , "" , False if token == "{" : if stmt_type == "class" : public_section = False else : public_section = True self . block_stack . append ( [ stmt_type , name , parse_flag , public_section , decl ] ) if token == "}" : if not self . block_stack : 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 block_head = "" l = l [ pos + 1 : ] return decls