Skip to content

Commit 9ab033a

Browse files
committed
グローバル修飾リストの自動ソートスクリプトを追加
1 parent 9127cd7 commit 9ab033a

File tree

3 files changed

+246
-97
lines changed

3 files changed

+246
-97
lines changed
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# グローバル修飾リストと、プライマリオーバーロード特殊化リストをソートするスクリプト
2+
# トップディレクトリで以下を実行する:
3+
# python3 .github/workflows/script/sort_global_qualify.py
4+
#
5+
# GLOBAL_QUALIFY_LIST.txtのソートルール:
6+
# 1. コメントグループと修飾グループに分ける
7+
# 2. コメントグループはそのままの順序で、修飾グループはヘッダの辞書順にソートする
8+
# 3. 修飾グループ内では、以下のルールでソートする:
9+
# 3.1. 名前空間が深い順にソートする (::の数が多い順)
10+
# 3.2. 長い名前順にソートする ("abc"と"abcdef"では後者を優先)
11+
# 3.3. 大文字・小文字を区別する
12+
#
13+
# PRIMARY_OVERLOAD_SPECIALIZATION.txtのソートルール:
14+
# 1. 空行でグループを分ける
15+
# 1.1. 先頭行が`# header`で始まらないグループはコメントグループとみなし、位置を維持する
16+
# 1.2. そうではないグループは、修飾グループとみなす
17+
# 2. 修飾グループ内では、GLOBAL_QUALIFY_LIST.txtと同様のルールでソートする
18+
19+
from enum import Enum
20+
import functools
21+
import os
22+
23+
class Group(Enum):
24+
NONE = 0
25+
COMMENT = 1
26+
QUALIFY = 2
27+
28+
def flatten(arr: list[list[str]]) -> list[str]:
29+
return sum(arr, [])
30+
31+
def load_global_qualify_list() -> list[list[str]]:
32+
ls: list[list[str]] = []
33+
with open('GLOBAL_QUALIFY_LIST.txt', 'r', encoding='utf-8') as f:
34+
file = f.read()
35+
group_type = Group.NONE
36+
group: list[str] = []
37+
for line in file.split("\n"):
38+
if not line:
39+
continue
40+
41+
if line.startswith("#"):
42+
if group_type != Group.COMMENT:
43+
group_type = Group.COMMENT
44+
if len(group) > 0:
45+
ls.append(group)
46+
group = []
47+
group.append(line)
48+
else:
49+
if group_type != Group.QUALIFY:
50+
group_type = Group.QUALIFY
51+
if len(group) > 0:
52+
ls.append(group)
53+
group = []
54+
elif not line.startswith(" "):
55+
if len(group) > 0:
56+
ls.append(group)
57+
group = []
58+
group.append(line)
59+
60+
if len(group) > 0:
61+
ls.append(group)
62+
return ls
63+
64+
def compare(a: str, b: str) -> int:
65+
aa = a.startswith(" ")
66+
bb = b.startswith(" ")
67+
if aa != bb:
68+
return aa - bb
69+
70+
x = a.split("[")[0]
71+
y = b.split("[")[0]
72+
x_ns = x.split("::")
73+
y_ns = y.split("::")
74+
if len(x_ns) != len(y_ns):
75+
return len(y_ns) - len(x_ns)
76+
77+
for x_part, y_part in zip(x_ns, y_ns):
78+
if x_part == y_part:
79+
continue
80+
81+
min_len = min(len(x_part), len(y_part))
82+
if x_part[:min_len] == y_part[:min_len]:
83+
return len(y_part) - len(x_part)
84+
85+
if x_part < y_part:
86+
return -1
87+
else:
88+
return 1
89+
90+
return 0
91+
92+
def sort_global_qualify_list():
93+
ls = load_global_qualify_list()
94+
95+
outer_sorted = sorted(ls, key=lambda x: (not x[0].startswith("#"), x[0]))
96+
inner_sorted = []
97+
for group in outer_sorted:
98+
if group[0].startswith("#"):
99+
inner_sorted.append(group)
100+
else:
101+
inner_sorted.append(sorted(group, key=functools.cmp_to_key(compare)))
102+
return flatten(inner_sorted)
103+
104+
def execute_sort_global_qualify_list():
105+
sorted_ls = sort_global_qualify_list()
106+
with open('GLOBAL_QUALIFY_LIST.txt', 'w', encoding='utf-8') as f:
107+
f.write("\n".join(sorted_ls))
108+
109+
def load_primary_list() -> list[list[str]]:
110+
ls: list[list[str]] = []
111+
with open('PRIMARY_OVERLOAD_SPECIALIZATION.txt', 'r', encoding='utf-8') as f:
112+
file = f.read()
113+
group_type = Group.NONE
114+
group: list[str] = []
115+
for line in file.split("\n"):
116+
if not line:
117+
if len(group) > 0:
118+
ls.append(group)
119+
group = []
120+
continue
121+
122+
group.append(line)
123+
124+
if len(group) > 0:
125+
ls.append(group)
126+
return ls
127+
128+
def sort_primary_list():
129+
ls = load_primary_list()
130+
131+
outer_sorted = sorted(ls, key=lambda x: (x[0].startswith("# header"), x[0]))
132+
inner_sorted = []
133+
for group in outer_sorted:
134+
if group[0].startswith("#"):
135+
inner_sorted.append(group)
136+
else:
137+
inner_sorted.append(sorted(group, key=get_inner_sort_key))
138+
inner_sorted.append([""])
139+
return flatten(inner_sorted)
140+
141+
def execute_sort_primary_list():
142+
sorted_ls = sort_primary_list()
143+
with open('PRIMARY_OVERLOAD_SPECIALIZATION.txt', 'w', encoding='utf-8') as f:
144+
f.write("\n".join(sorted_ls))
145+
146+
if __name__ == '__main__':
147+
execute_sort_global_qualify_list()
148+
execute_sort_primary_list()

0 commit comments

Comments
 (0)