Skip to content

Commit f30a079

Browse files
committed
Fixed braces in comments affecting processing in bracecomment.py
1 parent c6656ec commit f30a079

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

wpiformat/test/test_bracecomment.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,36 @@ def test_bracecomment():
135135
"} // namespace" + os.linesep)
136136
test.add_latest_input_as_output(True)
137137

138+
# Handle braces in comments properly
139+
test.add_input("./Path.h",
140+
"#ifndef ALLWPILIB_WPI_PATH_H_" + os.linesep + \
141+
"#define ALLWPILIB_WPI_PATH_H_" + os.linesep + \
142+
os.linesep + \
143+
"namespace wpi {" + os.linesep + \
144+
"namespace sys {" + os.linesep + \
145+
"namespace path {" + os.linesep + \
146+
os.linesep + \
147+
"/// @{" + os.linesep + \
148+
os.linesep + \
149+
"} // end namespace path" + os.linesep + \
150+
"} // namespace sys" + os.linesep + \
151+
"} // namespace wpi" + os.linesep + \
152+
os.linesep + \
153+
"#endif // ALLWPILIB_WPI_PATH_H_" + os.linesep)
154+
test.add_output(
155+
"#ifndef ALLWPILIB_WPI_PATH_H_" + os.linesep + \
156+
"#define ALLWPILIB_WPI_PATH_H_" + os.linesep + \
157+
os.linesep + \
158+
"namespace wpi {" + os.linesep + \
159+
"namespace sys {" + os.linesep + \
160+
"namespace path {" + os.linesep + \
161+
os.linesep + \
162+
"/// @{" + os.linesep + \
163+
os.linesep + \
164+
"} // namespace path" + os.linesep + \
165+
"} // namespace sys" + os.linesep + \
166+
"} // namespace wpi" + os.linesep + \
167+
os.linesep + \
168+
"#endif // ALLWPILIB_WPI_PATH_H_" + os.linesep, True, True)
169+
138170
test.run(OutputType.FILE)

wpiformat/wpiformat/bracecomment.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,55 @@ def run_pipeline(self, config_file, name, lines):
1717
output = ""
1818

1919
brace_prefix = "(?P<prefix>(extern|namespace)\s+[\w\"]*)"
20-
brace_postfix = "\s*/(/|\*)[^\r\n]*"
20+
brace_postfix = "[ \t]*/(/|\*)[^\r\n]*"
2121

2222
brace_regex = regex.compile(
23+
r"/\*|\*/|//|\\\\|\\\"|\"|\\'|'|" + linesep + "|" + \
2324
"(" + brace_prefix + "\s*)?{|" # "{" with optional prefix
24-
"\}(" + brace_postfix + ")?") # "}" with optional comment postfix
25+
"}(" + brace_postfix + ")?") # "}" with optional comment postfix
2526

2627
name_stack = []
2728
brace_count = 0
2829
extract_location = 0
30+
in_multicomment = False
31+
in_singlecomment = False
32+
in_string = False
33+
in_char = False
2934
for match in brace_regex.finditer(lines):
3035
token = match.group()
3136

32-
if match.group("prefix"):
37+
if token == "/*":
38+
if not in_singlecomment and not in_string and not in_char:
39+
in_multicomment = True
40+
elif token == "*/":
41+
if not in_singlecomment and not in_string and not in_char:
42+
in_multicomment = False
43+
elif token == "//":
44+
if not in_multicomment and not in_string and not in_char:
45+
in_singlecomment = True
46+
elif in_singlecomment and linesep in token:
47+
# Ignore token if it's in a singleline comment. Only check it
48+
# for newlines to end the comment.
49+
in_singlecomment = False
50+
elif in_multicomment or in_singlecomment:
51+
# Tokens processed after this branch are ignored if they are in
52+
# comments
53+
continue
54+
elif token == "\\\"":
55+
continue
56+
elif token == "\"":
57+
if not in_char:
58+
in_string = not in_string
59+
elif token == "\\'":
60+
continue
61+
elif token == "'":
62+
if not in_string:
63+
in_char = not in_char
64+
elif in_string or in_char:
65+
# Tokens processed after this branch are ignored if they are in
66+
# double or single quotes
67+
continue
68+
elif match.group("prefix"):
3369
brace_count += 1
3470
name_stack.append((brace_count, match.group("prefix").rstrip()))
3571
elif "{" in token:

0 commit comments

Comments
 (0)