Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for oneof #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions proto2cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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.
Expand All @@ -138,7 +141,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.
Expand All @@ -147,15 +150,31 @@ 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("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
Expand Down