This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathc-comments-to-cpp.py
executable file
·175 lines (158 loc) · 6.95 KB
/
c-comments-to-cpp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python3
# -*- mode: Python; tab-width: 4; indent-tabs-mode: nil; -*-
"""
Copyright (C) 2017-2020 Marcus Geelnard
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
"""
import argparse
import sys
def convert(in_file, out_file, keep_empty_start_end, drop_empty_lines):
inside_c_comment = False
comment_style = "//"
comment_indent = 0
for line in in_file:
# Start by dropping trailing whitespace from the input.
line = line.rstrip()
out_line = ""
# Cater for empty lines in the middle of comments
# which we want to be empty comment lines to make
# it a consistent block.
if (not drop_empty_lines) and len(line) == 0 and inside_c_comment:
out_line += comment_style
inside_string = False
inside_indentation = True
start_or_end_line = False
k = 0
while k < len(line):
if inside_string:
assert k > 0
if line[k] == '"' and line[k - 1] != "\\":
inside_string = False
out_line += line[k]
k += 1
else:
if inside_c_comment:
if inside_indentation:
if (k >= comment_indent) or not (line[k] in [" ", "\t"]):
inside_indentation = False
out_line += comment_style
# Consume up to len(comment_style) chars from the line.
for m in range(k, min(k + len(comment_style), len(line))):
if line[m] in [" ", "\t"]:
k += 1
elif line[m] == "*" and (
(m + 1) == len(line) or line[m + 1] != "/"
):
k += 1
else:
break
if k < len(line) and not (line[k] in [" ", "\t", "*"]):
out_line += " "
if k < len(line):
if (
line[k] == "*"
and (k + 1) < len(line)
and line[k + 1] == "/"
):
inside_c_comment = False
start_or_end_line = True
if k > 0 and line[k - 1] == "*":
# Replace '*/' with '**' in case this is a '...*****/'-style line.
out_line += "**"
out_line += "\n"
k += 2
else:
out_line += line[k]
k += 1
else:
if line[k] == "/" and (k + 1) < len(line) and line[k + 1] == "*":
# Start of C style comment.
comment_indent = k
inside_c_comment = True
start_or_end_line = True
comment_style = "//"
k += 2
if (
(k + 1) < len(line)
and line[k] == "*"
and line[k + 1] == "/"
):
# Ridiculous case of a completely empty, single line C-style comment
inside_c_comment = False
start_or_end_line = True
break
if k < len(line) and (line[k] == "*" or line[k] == "!"):
if (k + 1) >= len(line) or line[k + 1] != "*":
# Start of Doxygen comment.
comment_style = "///"
k += 1
if k < len(line) and line[k] == "<":
# Start of Doxygen after-member comment.
comment_style = "///<"
k += 1
out_line += comment_style
inside_indentation = False
elif line[k] == "/" and (k + 1) < len(line) and line[k + 1] == "/":
# Start of C++ style comment.
out_line += line[k:]
k = len(line)
else:
if line[k] == '"':
inside_string = True
out_line += line[k]
k += 1
# Strip trailing whitespace (including newline chars).
out_line = out_line.rstrip()
# Print output if options and circumstances permit.
empty_comment_line = len(out_line.lstrip()) == len(comment_style)
if (
(not empty_comment_line)
or (start_or_end_line and keep_empty_start_end)
or (not start_or_end_line and not drop_empty_lines)
):
out_file.write(f"{out_line}\n")
def main():
# Handle the program arguments.
parser = argparse.ArgumentParser(
description="Convert C-style comments to C++-style."
)
parser.add_argument(
"--keep-empty-start-end",
action="store_true",
help="preserve empty start/end comment lines",
)
parser.add_argument(
"--drop-empty-lines",
action="store_true",
help="drop empty lines in comment blocks",
)
parser.add_argument("infile", nargs="?", help="input file (default: stdin)")
parser.add_argument("outfile", nargs="?", help="output file (default: stdout)")
args = parser.parse_args()
# Select input and output files.
in_file = sys.stdin
out_file = sys.stdout
if args.infile:
in_file = open(args.infile, "r", encoding="utf8")
if args.outfile:
out_file = open(args.outfile, "w", encoding="utf8")
convert(
in_file=in_file,
out_file=out_file,
keep_empty_start_end=args.keep_empty_start_end,
drop_empty_lines=args.drop_empty_lines,
)
if __name__ == "__main__":
main()