2 # This Source Code Form is subject to the terms of the Mozilla Public
3 # License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 # You can obtain one at http://mozilla.org/MPL/2.0/.
7 """ Usage: align_stack_comment.py FILE
9 This script aligns the stack transition comment in BytecodeEmitter and
12 The stack transition comment looks like the following:
13 // [stack] VAL1 VAL2 VAL3
19 # The column index of '[' of '[stack]'
22 # The maximum column for comment
23 MAX_CHARS_PER_LINE
= 80
25 stack_comment_pat
= re
.compile("^( *//) *(\[stack\].*)$")
28 def align_stack_comment(path
):
40 # Python includes \n in lines.
41 line
= line
.rstrip("\n")
43 m
= stack_comment_pat
.search(line
)
45 head
= m
.group(1) + " "
48 comment_len
= len(comment
)
50 if head_len
> ALIGNMENT_COLUMN
:
52 "Warning: line {} overflows from alignment column {}: {}".format(
53 line_num
, ALIGNMENT_COLUMN
, head_len
58 line_len
= max(head_len
, ALIGNMENT_COLUMN
) + comment_len
59 if line_len
> MAX_CHARS_PER_LINE
:
61 "Warning: line {} overflows from {} chars: {}".format(
62 line_num
, MAX_CHARS_PER_LINE
, line_len
67 max_head_len
= max(max_head_len
, head_len
)
68 max_comment_len
= max(max_comment_len
, comment_len
)
70 spaces
= max(ALIGNMENT_COLUMN
- head_len
, 0)
71 formatted
= head
+ " " * spaces
+ comment
76 lines
.append(formatted
)
81 "Info: Minimum column number for [stack]: {}".format(max_head_len
),
85 "Info: Alignment column number for [stack]: {}".format(ALIGNMENT_COLUMN
),
89 "Info: Max length of stack transition comments: {}".format(max_comment_len
),
94 with
open(path
, "w") as f
:
101 if __name__
== "__main__":
102 if len(sys
.argv
) < 2:
103 print("Usage: align_stack_comment.py FILE", file=sys
.stderr
)
106 for path
in sys
.argv
[1:]:
108 align_stack_comment(path
)