Debian Codegraph @

js/src/frontend/align_stack_comment.py

raw
1 #!/usr/bin/python -B
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/.
5
6
7 """ Usage: align_stack_comment.py FILE
8
9 This script aligns the stack transition comment in BytecodeEmitter and
10 its helper classes.
11
12 The stack transition comment looks like the following:
13 // [stack] VAL1 VAL2 VAL3
14 """
15
16 import re
17 import sys
18
19 # The column index of '[' of '[stack]'
20 ALIGNMENT_COLUMN = 20
21
22 # The maximum column for comment
23 MAX_CHARS_PER_LINE = 80
24
25 stack_comment_pat = re.compile(r"^( *//) *(\[stack\].*)$")
26
27
28 def align_stack_comment(path):
29 lines = []
30 changed = False
31
32 with open(path) as f:
33 max_head_len = 0
34 max_comment_len = 0
35
36 line_num = 0
37
38 for line in f:
39 line_num += 1
40 # Python includes \n in lines.
41 line = line.rstrip("\n")
42
43 m = stack_comment_pat.search(line)
44 if m:
45 head = m.group(1) + " "
46 head_len = len(head)
47 comment = m.group(2)
48 comment_len = len(comment)
49
50 if head_len > ALIGNMENT_COLUMN:
51 print(
52 f"Warning: line {line_num} overflows from alignment column {ALIGNMENT_COLUMN}: {head_len}",
53 file=sys.stderr,
54 )
55
56 line_len = max(head_len, ALIGNMENT_COLUMN) + comment_len
57 if line_len > MAX_CHARS_PER_LINE:
58 print(
59 f"Warning: line {line_num} overflows from {MAX_CHARS_PER_LINE} chars: {line_len}",
60 file=sys.stderr,
61 )
62
63 max_head_len = max(max_head_len, head_len)
64 max_comment_len = max(max_comment_len, comment_len)
65
66 spaces = max(ALIGNMENT_COLUMN - head_len, 0)
67 formatted = head + " " * spaces + comment
68
69 if formatted != line:
70 changed = True
71
72 lines.append(formatted)
73 else:
74 lines.append(line)
75
76 print(
77 f"Info: Minimum column number for [stack]: {max_head_len}",
78 file=sys.stderr,
79 )
80 print(
81 f"Info: Alignment column number for [stack]: {ALIGNMENT_COLUMN}",
82 file=sys.stderr,
83 )
84 print(
85 f"Info: Max length of stack transition comments: {max_comment_len}",
86 file=sys.stderr,
87 )
88
89 if changed:
90 with open(path, "w") as f:
91 for line in lines:
92 print(line, file=f)
93 else:
94 print("No change.")
95
96
97 if __name__ == "__main__":
98 if len(sys.argv) < 2:
99 print("Usage: align_stack_comment.py FILE", file=sys.stderr)
100 sys.exit(1)
101
102 for path in sys.argv[1:]:
103 print(path)
104 align_stack_comment(path)