1 #!/usr/bin/env python2.7
2 # Copyright 2015 gRPC authors.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 """Change comments style of source files from // to /** */"""
21 print("Please provide at least one source file name as argument.")
24 for file_name in sys.argv[1:]:
26 print("Modifying format of {file} comments in place...".format(
31 with open(file_name, "r") as input_file:
32 lines = input_file.readlines()
40 def more_input_available():
48 output_lines.append(line)
51 with open(file_name, "w") as output_file:
52 for line in output_lines:
53 output_file.write(line)
57 comment_regex = r'^(\s*)//\s(.*)$'
60 return re.search(comment_regex, line)
62 def isnt_comment(line):
63 return not is_comment(line)
65 def next_line(predicate):
66 return more_input_available() and predicate(peek())
70 def indentation_of(line):
71 match = re.search(comment_regex, line)
75 match = re.search(comment_regex, line)
78 def format_as_block(comment_block):
79 if len(comment_block) == 0:
82 indent = indentation_of(comment_block[0])
84 if len(comment_block) == 1:
85 return [indent + "/** " + content(comment_block[0]) + " */\n"]
87 block = ["/**"] + [" * " + content(line) for line in comment_block
89 return [indent + line.rstrip() + "\n" for line in block]
93 while more_input_available():
94 while next_line(isnt_comment):
98 # Get all lines in the same comment block. We could restrict the indentation
99 # to be the same as the first line of the block, but it's probably ok.
100 while (next_line(is_comment)):
101 comment_block.append(read_line())
103 for line in format_as_block(comment_block):