blob: 9aa0e0c9f5a196ff66f0269a907528d2a2244d43 [file] [log] [blame]
Jorge Canizales1bef95d2015-10-26 13:29:09 -07001#!/usr/bin/env python2.7
2# Copyright 2015, Google Inc.
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
8#
9# * Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# * Redistributions in binary form must reproduce the above
12# copyright notice, this list of conditions and the following disclaimer
13# in the documentation and/or other materials provided with the
14# distribution.
15# * Neither the name of Google Inc. nor the names of its
16# contributors may be used to endorse or promote products derived from
17# this software without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31"""Change comments style of source files from // to /** */"""
Jorge Canizalesfef5bee2015-10-19 12:24:28 -070032
33import re
34import sys
35
Jorge Canizalesfef5bee2015-10-19 12:24:28 -070036
Jorge Canizalesbbb77742015-10-26 10:44:38 -070037if len(sys.argv) < 2:
38 print("Please provide at least one source file name as argument.")
Jorge Canizales55e97422015-10-26 15:14:47 -070039 sys.exit()
Jorge Canizales88c32842015-10-26 10:18:59 -070040
Jorge Canizalesbbb77742015-10-26 10:44:38 -070041for file_name in sys.argv[1:]:
42
43 print("Modifying format of {file} comments in place...".format(
Jorge Canizales1bef95d2015-10-26 13:29:09 -070044 file=file_name,
Jorge Canizalesbbb77742015-10-26 10:44:38 -070045 ))
Jorge Canizales88c32842015-10-26 10:18:59 -070046
47
Jorge Canizalesbbb77742015-10-26 10:44:38 -070048 # Input
Jorge Canizales88c32842015-10-26 10:18:59 -070049
Jorge Canizalesbbb77742015-10-26 10:44:38 -070050 with open(file_name, "r") as input_file:
51 lines = input_file.readlines()
Jorge Canizalesfef5bee2015-10-19 12:24:28 -070052
Jorge Canizalesbbb77742015-10-26 10:44:38 -070053 def peek():
54 return lines[0]
Jorge Canizalesfef5bee2015-10-19 12:24:28 -070055
Jorge Canizalesbbb77742015-10-26 10:44:38 -070056 def read_line():
57 return lines.pop(0)
Jorge Canizalesfef5bee2015-10-19 12:24:28 -070058
Jorge Canizalesbbb77742015-10-26 10:44:38 -070059 def more_input_available():
60 return lines
Jorge Canizalesfef5bee2015-10-19 12:24:28 -070061
62
Jorge Canizalesbbb77742015-10-26 10:44:38 -070063 # Output
Jorge Canizales88c32842015-10-26 10:18:59 -070064
Jorge Canizalesbbb77742015-10-26 10:44:38 -070065 output_lines = []
Jorge Canizales88c32842015-10-26 10:18:59 -070066
Jorge Canizalesbbb77742015-10-26 10:44:38 -070067 def write(line):
68 output_lines.append(line)
Jorge Canizales88c32842015-10-26 10:18:59 -070069
Jorge Canizalesbbb77742015-10-26 10:44:38 -070070 def flush_output():
Jorge Canizales1bef95d2015-10-26 13:29:09 -070071 with open(file_name, "w") as output_file:
Jorge Canizalesbbb77742015-10-26 10:44:38 -070072 for line in output_lines:
Jorge Canizales1bef95d2015-10-26 13:29:09 -070073 output_file.write(line)
Jorge Canizales88c32842015-10-26 10:18:59 -070074
75
Jorge Canizalesbbb77742015-10-26 10:44:38 -070076 # Pattern matching
Jorge Canizales88c32842015-10-26 10:18:59 -070077
Jorge Canizalesbbb77742015-10-26 10:44:38 -070078 comment_regex = r'^(\s*)//\s(.*)$'
Jorge Canizalesfef5bee2015-10-19 12:24:28 -070079
Jorge Canizalesbbb77742015-10-26 10:44:38 -070080 def is_comment(line):
81 return re.search(comment_regex, line)
Jorge Canizalesfef5bee2015-10-19 12:24:28 -070082
Jorge Canizalesbbb77742015-10-26 10:44:38 -070083 def isnt_comment(line):
84 return not is_comment(line)
Jorge Canizalesfef5bee2015-10-19 12:24:28 -070085
Jorge Canizalesbbb77742015-10-26 10:44:38 -070086 def next_line(predicate):
Jorge Canizales1bef95d2015-10-26 13:29:09 -070087 return more_input_available() and predicate(peek())
Jorge Canizalesfef5bee2015-10-19 12:24:28 -070088
89
Jorge Canizalesbbb77742015-10-26 10:44:38 -070090 # Transformation
Jorge Canizalesfef5bee2015-10-19 12:24:28 -070091
Jorge Canizalesbbb77742015-10-26 10:44:38 -070092 def indentation_of(line):
93 match = re.search(comment_regex, line)
94 return match.group(1)
Jorge Canizales88c32842015-10-26 10:18:59 -070095
Jorge Canizalesbbb77742015-10-26 10:44:38 -070096 def content(line):
97 match = re.search(comment_regex, line)
98 return match.group(2)
Jorge Canizales88c32842015-10-26 10:18:59 -070099
Jorge Canizalesbbb77742015-10-26 10:44:38 -0700100 def format_as_block(comment_block):
101 if len(comment_block) == 0:
102 return []
Jorge Canizales88c32842015-10-26 10:18:59 -0700103
Jorge Canizalesbbb77742015-10-26 10:44:38 -0700104 indent = indentation_of(comment_block[0])
Jorge Canizales88c32842015-10-26 10:18:59 -0700105
Jorge Canizalesbbb77742015-10-26 10:44:38 -0700106 if len(comment_block) == 1:
107 return [indent + "/** " + content(comment_block[0]) + " */\n"]
Jorge Canizales88c32842015-10-26 10:18:59 -0700108
Jorge Canizalesbbb77742015-10-26 10:44:38 -0700109 block = ["/**"] + [" * " + content(line) for line in comment_block] + [" */"]
110 return [indent + line.rstrip() + "\n" for line in block]
Jorge Canizalesfef5bee2015-10-19 12:24:28 -0700111
112
Jorge Canizalesbbb77742015-10-26 10:44:38 -0700113 # Main algorithm
Jorge Canizales88c32842015-10-26 10:18:59 -0700114
Jorge Canizalesbbb77742015-10-26 10:44:38 -0700115 while more_input_available():
116 while next_line(isnt_comment):
117 write(read_line())
Jorge Canizalesfef5bee2015-10-19 12:24:28 -0700118
Jorge Canizalesbbb77742015-10-26 10:44:38 -0700119 comment_block = []
120 # Get all lines in the same comment block. We could restrict the indentation
121 # to be the same as the first line of the block, but it's probably ok.
122 while (next_line(is_comment)):
123 comment_block.append(read_line())
Jorge Canizalesfef5bee2015-10-19 12:24:28 -0700124
Jorge Canizalesbbb77742015-10-26 10:44:38 -0700125 for line in format_as_block(comment_block):
126 write(line)
Jorge Canizales88c32842015-10-26 10:18:59 -0700127
Jorge Canizalesbbb77742015-10-26 10:44:38 -0700128 flush_output()