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