Make the script support multiple input files

pull/3961/head
Jorge Canizales 9 years ago
parent 88c3284349
commit bbb7774b83
  1. 122
      src/objective-c/change-comments.py
  2. 3
      src/objective-c/format-all-comments.sh

@ -4,97 +4,99 @@ import re
import sys import sys
if len(sys.argv) != 2: if len(sys.argv) < 2:
print("Please provide a source file name as only argument.") print("Please provide at least one source file name as argument.")
quit() quit()
print("Modifying format of {file} comments in place...".format( for file_name in sys.argv[1:]:
file = sys.argv[1],
))
print("Modifying format of {file} comments in place...".format(
file = file_name,
))
# Input
lines = [] # Input
with open(sys.argv[1], "r") as input_file: lines = []
lines = input_file.readlines()
def peek(): with open(file_name, "r") as input_file:
return lines[0] lines = input_file.readlines()
def read_line(): def peek():
return lines.pop(0) return lines[0]
def more_input_available(): def read_line():
return lines return lines.pop(0)
def more_input_available():
return lines
# Output
output_lines = [] # Output
def write(line): output_lines = []
output_lines.append(line)
def flush_output(): def write(line):
with open(sys.argv[1], "w") as otuput_file: output_lines.append(line)
for line in output_lines:
otuput_file.write(line)
def flush_output():
with open(file_name, "w") as otuput_file:
for line in output_lines:
otuput_file.write(line)
# Pattern matching
comment_regex = r'^(\s*)//\s(.*)$' # Pattern matching
def is_comment(line): comment_regex = r'^(\s*)//\s(.*)$'
return re.search(comment_regex, line)
def isnt_comment(line): def is_comment(line):
return not is_comment(line) return re.search(comment_regex, line)
def next_line(predicate): def isnt_comment(line):
if not more_input_available(): return not is_comment(line)
return False
return predicate(peek())
def next_line(predicate):
if not more_input_available():
return False
return predicate(peek())
# Transformation
def indentation_of(line): # Transformation
match = re.search(comment_regex, line)
return match.group(1)
def content(line): def indentation_of(line):
match = re.search(comment_regex, line) match = re.search(comment_regex, line)
return match.group(2) return match.group(1)
def format_as_block(comment_block): def content(line):
if len(comment_block) == 0: match = re.search(comment_regex, line)
return [] return match.group(2)
indent = indentation_of(comment_block[0]) def format_as_block(comment_block):
if len(comment_block) == 0:
return []
if len(comment_block) == 1: indent = indentation_of(comment_block[0])
return [indent + "/** " + content(comment_block[0]) + " */\n"]
block = ["/**"] + [" * " + content(line) for line in comment_block] + [" */"] if len(comment_block) == 1:
return [indent + line + "\n" for line in block] return [indent + "/** " + content(comment_block[0]) + " */\n"]
block = ["/**"] + [" * " + content(line) for line in comment_block] + [" */"]
return [indent + line.rstrip() + "\n" for line in block]
# Main algorithm
while more_input_available(): # Main algorithm
while next_line(isnt_comment):
write(read_line())
comment_block = [] while more_input_available():
# Get all lines in the same comment block. We could restrict the indentation while next_line(isnt_comment):
# to be the same as the first line of the block, but it's probably ok. write(read_line())
while (next_line(is_comment)):
comment_block.append(read_line())
for line in format_as_block(comment_block): comment_block = []
write(line) # Get all lines in the same comment block. We could restrict the indentation
# to be the same as the first line of the block, but it's probably ok.
while (next_line(is_comment)):
comment_block.append(read_line())
flush_output() for line in format_as_block(comment_block):
write(line)
flush_output()

@ -0,0 +1,3 @@
#!/usr/bin/bash
find . -type f -name "*.h" ! -path "*/Pods/*" ! -path "./generated_libraries/*" ! -path "./examples/*" ! -path "./tests/*" | xargs ./change-comments.py
Loading…
Cancel
Save