|
|
|
@ -4,72 +4,74 @@ import re |
|
|
|
|
import sys |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(sys.argv) != 2: |
|
|
|
|
print("Please provide a source file name as only argument.") |
|
|
|
|
if len(sys.argv) < 2: |
|
|
|
|
print("Please provide at least one source file name as argument.") |
|
|
|
|
quit() |
|
|
|
|
|
|
|
|
|
print("Modifying format of {file} comments in place...".format( |
|
|
|
|
file = sys.argv[1], |
|
|
|
|
)) |
|
|
|
|
for file_name in 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 = [] |
|
|
|
|
|
|
|
|
|
with open(file_name, "r") as input_file: |
|
|
|
|
lines = input_file.readlines() |
|
|
|
|
|
|
|
|
|
def peek(): |
|
|
|
|
def peek(): |
|
|
|
|
return lines[0] |
|
|
|
|
|
|
|
|
|
def read_line(): |
|
|
|
|
def read_line(): |
|
|
|
|
return lines.pop(0) |
|
|
|
|
|
|
|
|
|
def more_input_available(): |
|
|
|
|
def more_input_available(): |
|
|
|
|
return lines |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Output |
|
|
|
|
# Output |
|
|
|
|
|
|
|
|
|
output_lines = [] |
|
|
|
|
output_lines = [] |
|
|
|
|
|
|
|
|
|
def write(line): |
|
|
|
|
def write(line): |
|
|
|
|
output_lines.append(line) |
|
|
|
|
|
|
|
|
|
def flush_output(): |
|
|
|
|
with open(sys.argv[1], "w") as otuput_file: |
|
|
|
|
def flush_output(): |
|
|
|
|
with open(file_name, "w") as otuput_file: |
|
|
|
|
for line in output_lines: |
|
|
|
|
otuput_file.write(line) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Pattern matching |
|
|
|
|
# Pattern matching |
|
|
|
|
|
|
|
|
|
comment_regex = r'^(\s*)//\s(.*)$' |
|
|
|
|
comment_regex = r'^(\s*)//\s(.*)$' |
|
|
|
|
|
|
|
|
|
def is_comment(line): |
|
|
|
|
def is_comment(line): |
|
|
|
|
return re.search(comment_regex, line) |
|
|
|
|
|
|
|
|
|
def isnt_comment(line): |
|
|
|
|
def isnt_comment(line): |
|
|
|
|
return not is_comment(line) |
|
|
|
|
|
|
|
|
|
def next_line(predicate): |
|
|
|
|
def next_line(predicate): |
|
|
|
|
if not more_input_available(): |
|
|
|
|
return False |
|
|
|
|
return predicate(peek()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Transformation |
|
|
|
|
# Transformation |
|
|
|
|
|
|
|
|
|
def indentation_of(line): |
|
|
|
|
def indentation_of(line): |
|
|
|
|
match = re.search(comment_regex, line) |
|
|
|
|
return match.group(1) |
|
|
|
|
|
|
|
|
|
def content(line): |
|
|
|
|
def content(line): |
|
|
|
|
match = re.search(comment_regex, line) |
|
|
|
|
return match.group(2) |
|
|
|
|
|
|
|
|
|
def format_as_block(comment_block): |
|
|
|
|
def format_as_block(comment_block): |
|
|
|
|
if len(comment_block) == 0: |
|
|
|
|
return [] |
|
|
|
|
|
|
|
|
@ -79,12 +81,12 @@ def format_as_block(comment_block): |
|
|
|
|
return [indent + "/** " + content(comment_block[0]) + " */\n"] |
|
|
|
|
|
|
|
|
|
block = ["/**"] + [" * " + content(line) for line in comment_block] + [" */"] |
|
|
|
|
return [indent + line + "\n" for line in block] |
|
|
|
|
return [indent + line.rstrip() + "\n" for line in block] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Main algorithm |
|
|
|
|
# Main algorithm |
|
|
|
|
|
|
|
|
|
while more_input_available(): |
|
|
|
|
while more_input_available(): |
|
|
|
|
while next_line(isnt_comment): |
|
|
|
|
write(read_line()) |
|
|
|
|
|
|
|
|
@ -97,4 +99,4 @@ while more_input_available(): |
|
|
|
|
for line in format_as_block(comment_block): |
|
|
|
|
write(line) |
|
|
|
|
|
|
|
|
|
flush_output() |
|
|
|
|
flush_output() |
|
|
|
|