-
Notifications
You must be signed in to change notification settings - Fork 1
/
merlin-fixs.py
142 lines (117 loc) · 2.21 KB
/
merlin-fixs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import sublime
import sublime_plugin
TABS = (10, 15, 28)
STROPS = set((b'ASC', b'DCI', b'FLS', b'INV', b'REV' b'STR', b'STRL'))
XDIGITS = set("0123456789ABCDEFabcdef")
DQ = '"'
SQ = "'"
WS = " \t"
QUOTES = "\"'"
def tab_to(line, ts):
pos = TABS[ts-1]
line.append(0x20)
while len(line) < pos: line.append(0x20)
def fixs(s):
q = 0
st = 0
opc = bytearray()
rv = bytearray()
for c in s:
if c == "\t": c = " "
cc = ord(c)
if st == 0:
if c == " ":
st = 2
continue
if c == "*":
st = 7
elif c == ";":
st = 7
tab_to(rv, 3)
else:
st += 1
rv.append(cc)
continue
if st == 1:
# label
if c == " ":
st += 1
continue
rv.append(cc)
if st == 2:
# label ws
if c == " ": continue
if c == "*" and len(rv) == 0:
st = 7
elif c == ";":
st = 7
tab_to(rv, 3)
else:
tab_to(rv, 1)
st += 1
opc.append(cc & ~0x20)
rv.append(cc)
continue
if st == 3:
# opcode
if c == " ":
st += 1;
continue
rv.append(cc)
opc.append(cc & ~0x20)
continue
if st == 4:
# opcode ws
if c == " ": continue
if c == ";":
st = 7
tab_to(rv, 3)
else:
st += 1
if bytes(opc) in STROPS and c not in XDIGITS: q = c
if c in QUOTES: q = c
tab_to(rv, 2)
rv.append(cc)
continue
if st == 5:
# operand
if q:
rv.append(cc)
if q == cc: q = 0
continue
if c == " ":
st += 1
continue
rv.append(cc)
if c in QUOTES: q = cc
continue
if st == 6:
# operand ws
if c == " ": continue
# insert ; ????
st += 1
tab_to(rv, 3)
rv.append(cc)
continue
if st == 7:
# comment
rv.append(cc)
#
return rv.decode('ascii')
class MerlinFixs(sublime_plugin.TextCommand):
def is_enabled(self):
scope = self.view.scope_name(0)
lang = scope.split(' ')[0]
return lang == 'source.asm.65816.merlin' or lang == "source.linker.merlin"
def run(self, edit):
view = self.view
all = sublime.Region(0, view.size())
# disable space indentation.
view.settings().set('translate_tabs_to_spaces', False)
data = []
for r in view.lines(all):
text = view.substr(r).rstrip()
data.append(fixs(text))
data.append('')
text = '\n'.join(data)
view.replace(edit, all, text)