@@ -31,10 +31,12 @@ def run_pipeline(self, config_file, name, lines):
31
31
#
32
32
# "def\\s+\w+" matches preprocessor directives "#ifdef" and "#ifndef" so
33
33
# their contents aren't used as a return type.
34
+ comment_str = "/\*|\*/|//|" + linesep + "|"
34
35
extern_str = "(?P<ext_decl>extern \" C(\+\+)?\" )\s+(?P<ext_brace>\{)?|"
35
36
braces_str = "\{|\}|;|def\s+\w+|\w+\s+\w+\s*(?P<paren>\(\))"
36
37
postfix_str = "(?=\s*(;|\{))"
37
- token_regex = regex .compile (extern_str + braces_str + postfix_str )
38
+ token_regex = regex .compile (
39
+ comment_str + extern_str + braces_str + postfix_str )
38
40
39
41
EXTRA_POP_OFFSET = 2
40
42
@@ -46,49 +48,64 @@ def run_pipeline(self, config_file, name, lines):
46
48
# is_c + pop offset == 3: C++ lang restore that needs extra brace pop
47
49
extern_brace_indices = [is_c ]
48
50
51
+ in_multicomment = False
52
+ in_comment = False
49
53
for match in token_regex .finditer (lines ):
50
54
token = match .group ()
51
55
52
- if token == "{" :
53
- extern_brace_indices .append (is_c )
54
- elif token == "}" :
55
- is_c = extern_brace_indices .pop ()
56
-
57
- # If the next stack frame is from an extern without braces, pop
58
- # it.
59
- if extern_brace_indices [- 1 ] >= EXTRA_POP_OFFSET :
60
- is_c = extern_brace_indices [- 1 ] - EXTRA_POP_OFFSET
61
- extern_brace_indices .pop ()
62
- elif token == ";" :
63
- # If the next stack frame is from an extern without braces, pop
64
- # it.
65
- if extern_brace_indices [- 1 ] >= EXTRA_POP_OFFSET :
66
- is_c = extern_brace_indices [- 1 ] - EXTRA_POP_OFFSET
67
- extern_brace_indices .pop ()
68
- elif token .startswith ("extern" ):
69
- # Back up language setting first
70
- if match .group ("ext_brace" ):
56
+ if token == "/*" :
57
+ in_multicomment = True
58
+ elif token == "*/" :
59
+ in_multicomment = False
60
+ in_comment = False
61
+ elif token == "//" :
62
+ print ("into comment" )
63
+ in_comment = True
64
+ elif token == linesep :
65
+ print ("out of comment" )
66
+ in_comment = False
67
+ elif not in_multicomment and not in_comment :
68
+ if token == "{" :
71
69
extern_brace_indices .append (is_c )
72
- else :
73
- # Handling an extern without braces changing the language
74
- # type is done by treating it as a pseudo-brace that gets
75
- # popped as well when the next "}" or ";" is encountered.
76
- # The "extra pop" offset is used as a flag on the top stack
77
- # value that is checked whenever a pop is performed.
78
- extern_brace_indices .append (is_c + EXTRA_POP_OFFSET )
79
-
80
- # Change language based on extern declaration
81
- if match .group ("ext_decl" ) == "extern \" C\" " :
82
- is_c = True
83
- else :
84
- is_c = False
85
- elif match .group (
86
- "paren" ) and "return " not in match .group () and is_c :
87
- # Replaces () with (void)
88
- output += lines [pos :match .span ("paren" )[0 ]] + "(void)"
89
- pos = match .span ("paren" )[0 ] + len ("()" )
90
-
91
- file_changed = True
70
+ elif token == "}" :
71
+ is_c = extern_brace_indices .pop ()
72
+
73
+ # If the next stack frame is from an extern without braces,
74
+ # pop it.
75
+ if extern_brace_indices [- 1 ] >= EXTRA_POP_OFFSET :
76
+ is_c = extern_brace_indices [- 1 ] - EXTRA_POP_OFFSET
77
+ extern_brace_indices .pop ()
78
+ elif token == ";" :
79
+ # If the next stack frame is from an extern without braces,
80
+ # pop it.
81
+ if extern_brace_indices [- 1 ] >= EXTRA_POP_OFFSET :
82
+ is_c = extern_brace_indices [- 1 ] - EXTRA_POP_OFFSET
83
+ extern_brace_indices .pop ()
84
+ elif token .startswith ("extern" ):
85
+ # Back up language setting first
86
+ if match .group ("ext_brace" ):
87
+ extern_brace_indices .append (is_c )
88
+ else :
89
+ # Handling an extern without braces changing the
90
+ # language type is done by treating it as a pseudo-brace
91
+ # that gets popped as well when the next "}" or ";" is
92
+ # encountered. The "extra pop" offset is used as a flag
93
+ # on the top stack value that is checked whenever a pop
94
+ # is performed.
95
+ extern_brace_indices .append (is_c + EXTRA_POP_OFFSET )
96
+
97
+ # Change language based on extern declaration
98
+ if match .group ("ext_decl" ) == "extern \" C\" " :
99
+ is_c = True
100
+ else :
101
+ is_c = False
102
+ elif match .group (
103
+ "paren" ) and "return " not in match .group () and is_c :
104
+ # Replaces () with (void)
105
+ output += lines [pos :match .span ("paren" )[0 ]] + "(void)"
106
+ pos = match .span ("paren" )[0 ] + len ("()" )
107
+
108
+ file_changed = True
92
109
93
110
# Write rest of file if it wasn't all processed
94
111
if pos < len (lines ):
0 commit comments