blob: 4de8a336889cc53f2f7b8b504afaf2e5822e520b [file] [log] [blame]
Jorge Canizales1bef95d2015-10-26 13:29:09 -07001#!/usr/bin/env python2.7
Jan Tattermusch7897ae92017-06-07 22:57:36 +02002# Copyright 2015 gRPC authors.
Jorge Canizales1bef95d2015-10-26 13:29:09 -07003#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02004# 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
Jorge Canizales1bef95d2015-10-26 13:29:09 -07007#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02008# http://www.apache.org/licenses/LICENSE-2.0
Jorge Canizales1bef95d2015-10-26 13:29:09 -07009#
Jan Tattermusch7897ae92017-06-07 22:57:36 +020010# 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.
Jorge Canizales1bef95d2015-10-26 13:29:09 -070015
16"""Change comments style of source files from // to /** */"""
Jorge Canizalesfef5bee2015-10-19 12:24:28 -070017
18import re
19import sys
20
Jorge Canizalesfef5bee2015-10-19 12:24:28 -070021
Jorge Canizalesbbb77742015-10-26 10:44:38 -070022if len(sys.argv) < 2:
23 print("Please provide at least one source file name as argument.")
Jorge Canizales55e97422015-10-26 15:14:47 -070024 sys.exit()
Jorge Canizales88c32842015-10-26 10:18:59 -070025
Jorge Canizalesbbb77742015-10-26 10:44:38 -070026for file_name in sys.argv[1:]:
27
28 print("Modifying format of {file} comments in place...".format(
Jorge Canizales1bef95d2015-10-26 13:29:09 -070029 file=file_name,
Jorge Canizalesbbb77742015-10-26 10:44:38 -070030 ))
Jorge Canizales88c32842015-10-26 10:18:59 -070031
32
Jorge Canizalesbbb77742015-10-26 10:44:38 -070033 # Input
Jorge Canizales88c32842015-10-26 10:18:59 -070034
Jorge Canizalesbbb77742015-10-26 10:44:38 -070035 with open(file_name, "r") as input_file:
36 lines = input_file.readlines()
Jorge Canizalesfef5bee2015-10-19 12:24:28 -070037
Jorge Canizalesbbb77742015-10-26 10:44:38 -070038 def peek():
39 return lines[0]
Jorge Canizalesfef5bee2015-10-19 12:24:28 -070040
Jorge Canizalesbbb77742015-10-26 10:44:38 -070041 def read_line():
42 return lines.pop(0)
Jorge Canizalesfef5bee2015-10-19 12:24:28 -070043
Jorge Canizalesbbb77742015-10-26 10:44:38 -070044 def more_input_available():
45 return lines
Jorge Canizalesfef5bee2015-10-19 12:24:28 -070046
47
Jorge Canizalesbbb77742015-10-26 10:44:38 -070048 # Output
Jorge Canizales88c32842015-10-26 10:18:59 -070049
Jorge Canizalesbbb77742015-10-26 10:44:38 -070050 output_lines = []
Jorge Canizales88c32842015-10-26 10:18:59 -070051
Jorge Canizalesbbb77742015-10-26 10:44:38 -070052 def write(line):
53 output_lines.append(line)
Jorge Canizales88c32842015-10-26 10:18:59 -070054
Jorge Canizalesbbb77742015-10-26 10:44:38 -070055 def flush_output():
Jorge Canizales1bef95d2015-10-26 13:29:09 -070056 with open(file_name, "w") as output_file:
Jorge Canizalesbbb77742015-10-26 10:44:38 -070057 for line in output_lines:
Jorge Canizales1bef95d2015-10-26 13:29:09 -070058 output_file.write(line)
Jorge Canizales88c32842015-10-26 10:18:59 -070059
60
Jorge Canizalesbbb77742015-10-26 10:44:38 -070061 # Pattern matching
Jorge Canizales88c32842015-10-26 10:18:59 -070062
Jorge Canizalesbbb77742015-10-26 10:44:38 -070063 comment_regex = r'^(\s*)//\s(.*)$'
Jorge Canizalesfef5bee2015-10-19 12:24:28 -070064
Jorge Canizalesbbb77742015-10-26 10:44:38 -070065 def is_comment(line):
66 return re.search(comment_regex, line)
Jorge Canizalesfef5bee2015-10-19 12:24:28 -070067
Jorge Canizalesbbb77742015-10-26 10:44:38 -070068 def isnt_comment(line):
69 return not is_comment(line)
Jorge Canizalesfef5bee2015-10-19 12:24:28 -070070
Jorge Canizalesbbb77742015-10-26 10:44:38 -070071 def next_line(predicate):
Jorge Canizales1bef95d2015-10-26 13:29:09 -070072 return more_input_available() and predicate(peek())
Jorge Canizalesfef5bee2015-10-19 12:24:28 -070073
74
Jorge Canizalesbbb77742015-10-26 10:44:38 -070075 # Transformation
Jorge Canizalesfef5bee2015-10-19 12:24:28 -070076
Jorge Canizalesbbb77742015-10-26 10:44:38 -070077 def indentation_of(line):
78 match = re.search(comment_regex, line)
79 return match.group(1)
Jorge Canizales88c32842015-10-26 10:18:59 -070080
Jorge Canizalesbbb77742015-10-26 10:44:38 -070081 def content(line):
82 match = re.search(comment_regex, line)
83 return match.group(2)
Jorge Canizales88c32842015-10-26 10:18:59 -070084
Jorge Canizalesbbb77742015-10-26 10:44:38 -070085 def format_as_block(comment_block):
86 if len(comment_block) == 0:
87 return []
Jorge Canizales88c32842015-10-26 10:18:59 -070088
Jorge Canizalesbbb77742015-10-26 10:44:38 -070089 indent = indentation_of(comment_block[0])
Jorge Canizales88c32842015-10-26 10:18:59 -070090
Jorge Canizalesbbb77742015-10-26 10:44:38 -070091 if len(comment_block) == 1:
92 return [indent + "/** " + content(comment_block[0]) + " */\n"]
Jorge Canizales88c32842015-10-26 10:18:59 -070093
Jorge Canizalesbbb77742015-10-26 10:44:38 -070094 block = ["/**"] + [" * " + content(line) for line in comment_block] + [" */"]
95 return [indent + line.rstrip() + "\n" for line in block]
Jorge Canizalesfef5bee2015-10-19 12:24:28 -070096
97
Jorge Canizalesbbb77742015-10-26 10:44:38 -070098 # Main algorithm
Jorge Canizales88c32842015-10-26 10:18:59 -070099
Jorge Canizalesbbb77742015-10-26 10:44:38 -0700100 while more_input_available():
101 while next_line(isnt_comment):
102 write(read_line())
Jorge Canizalesfef5bee2015-10-19 12:24:28 -0700103
Jorge Canizalesbbb77742015-10-26 10:44:38 -0700104 comment_block = []
105 # Get all lines in the same comment block. We could restrict the indentation
106 # to be the same as the first line of the block, but it's probably ok.
107 while (next_line(is_comment)):
108 comment_block.append(read_line())
Jorge Canizalesfef5bee2015-10-19 12:24:28 -0700109
Jorge Canizalesbbb77742015-10-26 10:44:38 -0700110 for line in format_as_block(comment_block):
111 write(line)
Jorge Canizales88c32842015-10-26 10:18:59 -0700112
Jorge Canizalesbbb77742015-10-26 10:44:38 -0700113 flush_output()