From 672674186eea4ccfa0a0a33f79412fd0ca62b45f Mon Sep 17 00:00:00 2001 From: Yann Roth Date: Wed, 31 Aug 2022 13:53:46 +0200 Subject: [PATCH 1/2] match expression on complete words only --- proto2cpp.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/proto2cpp.py b/proto2cpp.py index 96506aa..1bf9c83 100644 --- a/proto2cpp.py +++ b/proto2cpp.py @@ -138,7 +138,7 @@ def parseFile(self, inputFile): # Search for "enum" and if one is found before comment, # start changing all semicolons (";") to commas (","). - matchEnum = re.search("enum", line) + matchEnum = re.search(r'\benum\b', line) if matchEnum is not None and (matchComment is None or matchEnum.start() < matchComment.start()): isEnum = True # Search again for semicolon if we have detected an enum, and replace semicolon with comma. @@ -155,7 +155,7 @@ def parseFile(self, inputFile): # be a proper C(++) struct and Doxygen will handle it correctly. line = line[:matchClosingBrace.start()] + "};" + line[matchClosingBrace.end():] # Search for 'message' and replace it with 'struct' unless 'message' is behind a comment. - matchMsg = re.search("message", line) + matchMsg = re.search(r'\bmessage\b', line) if matchMsg is not None and (matchComment is None or matchMsg.start() < matchComment.start()): output = "struct" + line[:matchMsg.start()] + line[matchMsg.end():] theOutput += output From 0deed5da06b3567534bfeac370c33c00db971093 Mon Sep 17 00:00:00 2001 From: Yann Roth Date: Wed, 31 Aug 2022 15:03:37 +0200 Subject: [PATCH 2/2] Add support for oneof as named union --- proto2cpp.py | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/proto2cpp.py b/proto2cpp.py index 1bf9c83..a02f6cc 100644 --- a/proto2cpp.py +++ b/proto2cpp.py @@ -93,6 +93,8 @@ def handleFile(self, fileName): pass except IOError as e: self.logError('the file ' + filename + ' could not be opened for reading') + except ValueError as e: + self.logError('Parsing of ' + filename + ' failed :' + str(e)) elif not fnmatch.fnmatch(filename, os.path.basename(inspect.getfile(inspect.currentframe()))): self.log('\nXXXXXXXXXX\nXX ' + filename + '\nXXXXXXXXXX\n\n') @@ -120,6 +122,7 @@ def handleFile(self, fileName): def parseFile(self, inputFile): # Go through the input file line by line. isEnum = False + isOneof = False # This variable is here as a workaround for not getting extra line breaks (each line # ends with a line separator and print() method will add another one). # We will be adding lines into this var and then print the var out at the end. @@ -147,13 +150,29 @@ def parseFile(self, inputFile): line = line[:matchSemicolon.start()] + "," + line[matchSemicolon.end():] # Search for a closing brace. matchClosingBrace = re.search("}", line[:matchComment.start()] if matchComment else line) - if isEnum is True and matchClosingBrace is not None: - line = line[:matchClosingBrace.start()] + "};" + line[matchClosingBrace.end():] - isEnum = False - elif isEnum is False and matchClosingBrace is not None: - # Message (to be struct) ends => add semicolon so that it'll - # be a proper C(++) struct and Doxygen will handle it correctly. - line = line[:matchClosingBrace.start()] + "};" + line[matchClosingBrace.end():] + if matchClosingBrace is not None: + if isEnum is True: + line = line[:matchClosingBrace.start()] + "};" + line[matchClosingBrace.end():] + isEnum = False + elif isOneof is True: + line = line[:matchClosingBrace.end()] + " " + matchOneofName.group(0) + ";" + line[matchClosingBrace.end():] + isOneof = False + else: + # Message (to be struct) ends => add semicolon so that it'll + # be a proper C(++) struct and Doxygen will handle it correctly. + line = line[:matchClosingBrace.start()] + "};" + line[matchClosingBrace.end():] + if isEnum is True and isOneof is True: + raise ValueError("Found enum in oneof or vis-versa") + + # Search for "oneof" and if one is found before comment, + # Replace them with named union to get proper linking by doxygen. + matchOneof = re.search(r'\boneof\b', line) + if matchOneof is not None and (matchComment is None or matchOneof.start() < matchComment.start()): + isOneof = True + # Save the name of the enum + matchOneofName = re.search(r'(?<=\boneof\s)(\w+)', line) + line = line[:matchOneof.start()] + "union" + line[matchOneof.end():] + # Search for 'message' and replace it with 'struct' unless 'message' is behind a comment. matchMsg = re.search(r'\bmessage\b', line) if matchMsg is not None and (matchComment is None or matchMsg.start() < matchComment.start()):