Jorge Canizales | 1bef95d | 2015-10-26 13:29:09 -0700 | [diff] [blame] | 1 | #!/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 Canizales | fef5bee | 2015-10-19 12:24:28 -0700 | [diff] [blame] | 32 | |
| 33 | import re |
| 34 | import sys |
| 35 | |
Jorge Canizales | fef5bee | 2015-10-19 12:24:28 -0700 | [diff] [blame] | 36 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 37 | if len(sys.argv) < 2: |
| 38 | print("Please provide at least one source file name as argument.") |
Jorge Canizales | 55e9742 | 2015-10-26 15:14:47 -0700 | [diff] [blame] | 39 | sys.exit() |
Jorge Canizales | 88c3284 | 2015-10-26 10:18:59 -0700 | [diff] [blame] | 40 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 41 | for file_name in sys.argv[1:]: |
| 42 | |
| 43 | print("Modifying format of {file} comments in place...".format( |
Jorge Canizales | 1bef95d | 2015-10-26 13:29:09 -0700 | [diff] [blame] | 44 | file=file_name, |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 45 | )) |
Jorge Canizales | 88c3284 | 2015-10-26 10:18:59 -0700 | [diff] [blame] | 46 | |
| 47 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 48 | # Input |
Jorge Canizales | 88c3284 | 2015-10-26 10:18:59 -0700 | [diff] [blame] | 49 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 50 | with open(file_name, "r") as input_file: |
| 51 | lines = input_file.readlines() |
Jorge Canizales | fef5bee | 2015-10-19 12:24:28 -0700 | [diff] [blame] | 52 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 53 | def peek(): |
| 54 | return lines[0] |
Jorge Canizales | fef5bee | 2015-10-19 12:24:28 -0700 | [diff] [blame] | 55 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 56 | def read_line(): |
| 57 | return lines.pop(0) |
Jorge Canizales | fef5bee | 2015-10-19 12:24:28 -0700 | [diff] [blame] | 58 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 59 | def more_input_available(): |
| 60 | return lines |
Jorge Canizales | fef5bee | 2015-10-19 12:24:28 -0700 | [diff] [blame] | 61 | |
| 62 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 63 | # Output |
Jorge Canizales | 88c3284 | 2015-10-26 10:18:59 -0700 | [diff] [blame] | 64 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 65 | output_lines = [] |
Jorge Canizales | 88c3284 | 2015-10-26 10:18:59 -0700 | [diff] [blame] | 66 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 67 | def write(line): |
| 68 | output_lines.append(line) |
Jorge Canizales | 88c3284 | 2015-10-26 10:18:59 -0700 | [diff] [blame] | 69 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 70 | def flush_output(): |
Jorge Canizales | 1bef95d | 2015-10-26 13:29:09 -0700 | [diff] [blame] | 71 | with open(file_name, "w") as output_file: |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 72 | for line in output_lines: |
Jorge Canizales | 1bef95d | 2015-10-26 13:29:09 -0700 | [diff] [blame] | 73 | output_file.write(line) |
Jorge Canizales | 88c3284 | 2015-10-26 10:18:59 -0700 | [diff] [blame] | 74 | |
| 75 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 76 | # Pattern matching |
Jorge Canizales | 88c3284 | 2015-10-26 10:18:59 -0700 | [diff] [blame] | 77 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 78 | comment_regex = r'^(\s*)//\s(.*)$' |
Jorge Canizales | fef5bee | 2015-10-19 12:24:28 -0700 | [diff] [blame] | 79 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 80 | def is_comment(line): |
| 81 | return re.search(comment_regex, line) |
Jorge Canizales | fef5bee | 2015-10-19 12:24:28 -0700 | [diff] [blame] | 82 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 83 | def isnt_comment(line): |
| 84 | return not is_comment(line) |
Jorge Canizales | fef5bee | 2015-10-19 12:24:28 -0700 | [diff] [blame] | 85 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 86 | def next_line(predicate): |
Jorge Canizales | 1bef95d | 2015-10-26 13:29:09 -0700 | [diff] [blame] | 87 | return more_input_available() and predicate(peek()) |
Jorge Canizales | fef5bee | 2015-10-19 12:24:28 -0700 | [diff] [blame] | 88 | |
| 89 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 90 | # Transformation |
Jorge Canizales | fef5bee | 2015-10-19 12:24:28 -0700 | [diff] [blame] | 91 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 92 | def indentation_of(line): |
| 93 | match = re.search(comment_regex, line) |
| 94 | return match.group(1) |
Jorge Canizales | 88c3284 | 2015-10-26 10:18:59 -0700 | [diff] [blame] | 95 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 96 | def content(line): |
| 97 | match = re.search(comment_regex, line) |
| 98 | return match.group(2) |
Jorge Canizales | 88c3284 | 2015-10-26 10:18:59 -0700 | [diff] [blame] | 99 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 100 | def format_as_block(comment_block): |
| 101 | if len(comment_block) == 0: |
| 102 | return [] |
Jorge Canizales | 88c3284 | 2015-10-26 10:18:59 -0700 | [diff] [blame] | 103 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 104 | indent = indentation_of(comment_block[0]) |
Jorge Canizales | 88c3284 | 2015-10-26 10:18:59 -0700 | [diff] [blame] | 105 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 106 | if len(comment_block) == 1: |
| 107 | return [indent + "/** " + content(comment_block[0]) + " */\n"] |
Jorge Canizales | 88c3284 | 2015-10-26 10:18:59 -0700 | [diff] [blame] | 108 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 109 | block = ["/**"] + [" * " + content(line) for line in comment_block] + [" */"] |
| 110 | return [indent + line.rstrip() + "\n" for line in block] |
Jorge Canizales | fef5bee | 2015-10-19 12:24:28 -0700 | [diff] [blame] | 111 | |
| 112 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 113 | # Main algorithm |
Jorge Canizales | 88c3284 | 2015-10-26 10:18:59 -0700 | [diff] [blame] | 114 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 115 | while more_input_available(): |
| 116 | while next_line(isnt_comment): |
| 117 | write(read_line()) |
Jorge Canizales | fef5bee | 2015-10-19 12:24:28 -0700 | [diff] [blame] | 118 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 119 | 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 Canizales | fef5bee | 2015-10-19 12:24:28 -0700 | [diff] [blame] | 124 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 125 | for line in format_as_block(comment_block): |
| 126 | write(line) |
Jorge Canizales | 88c3284 | 2015-10-26 10:18:59 -0700 | [diff] [blame] | 127 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 128 | flush_output() |