Repository navigation
Expand file tree
/
Copy pathtransform.lua
More file actions
270 lines (218 loc) · 6.95 KB
/
Copy pathtransform.lua
File metadata and controls
270 lines (218 loc) · 6.95 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
local keywords = {}
for word in ([[
and break do else elseif end false for function goto if in local
nil not or repeat return then true until while
]]):gmatch('%S+') do
keywords[word] = true
end
local function tokenize(text)
local tokens = {}
local i, length = 1, #text
local function longFinish(start)
local equals = text:match('^%[(=*)%[', start)
if not equals then return end
local _, finish = text:find(
']' .. equals .. ']',
start + #equals + 2,
true
)
return finish or length
end
while i <= length do
local c = text:sub(i, i)
local start = i
if c:match('%s') then
i = i + 1
elseif text:sub(i, i + 1) == '--' then
local finish = longFinish(i + 2)
i = finish and finish + 1
or (text:find('[\r\n]', i + 2) or length + 1)
else
local kind
local finish = c == '[' and longFinish(i)
if finish then
kind = 'string'
i = finish + 1
elseif c == '"' or c == "'" or c == '`' then
kind = 'string'
i = i + 1
while i <= length do
local ch = text:sub(i, i)
if ch == '\\' then
i = i + 2
elseif ch == c then
i = i + 1
break
else
i = i + 1
end
end
i = math.min(i, length + 1)
else
local name = text:match('^[%a_][%w_]*', i)
if name then
kind = keywords[name] and 'keyword' or 'name'
i = i + #name
else
kind = 'symbol'
local dots = text:match('^%.%.%.?', i)
i = i + (dots and #dots or 1)
end
end
tokens[#tokens + 1] = {
value = text:sub(start, i - 1),
kind = kind,
start = start,
finish = i - 1,
}
end
end
return tokens
end
local continuation = {}
for value in ('( [ { : . , + - * / % ^ & | ~ < > = .. and or'):gmatch('%S+') do
continuation[value] = true
end
local closing = {
['}'] = '{',
[']'] = '[',
[')'] = '(',
}
local groupClosing = {
['('] = ')',
['['] = ']',
['{'] = '}',
}
local function skipGroup(tokens, i)
local stack = { groupClosing[tokens[i].value] }
i = i + 1
while tokens[i] do
local value = tokens[i].value
if groupClosing[value] then
stack[#stack + 1] = groupClosing[value]
elseif value == ')' or value == ']' or value == '}' then
if value ~= stack[#stack] then return end
stack[#stack] = nil
if #stack == 0 then return i + 1 end
end
i = i + 1
end
end
local function readArguments(tokens, i)
local token = tokens[i]
if not token then return end
if token.kind == 'string' then
return i + 1
end
if token.value == '(' or token.value == '{' then
return skipGroup(tokens, i)
end
end
local function readSource(tokens, i)
local token = tokens[i]
if not token then return end
if token.kind == 'name' then
i = i + 1
elseif token.value == '(' then
i = skipGroup(tokens, i)
else
return
end
while i and tokens[i] do
local value = tokens[i].value
if value == '.' or value == ':' then
if not tokens[i + 1] or tokens[i + 1].kind ~= 'name' then
return
end
i = i + 2
if value == ':' then
i = readArguments(tokens, i)
end
elseif value == '[' then
i = skipGroup(tokens, i)
elseif value == '(' or value == '{' or tokens[i].kind == 'string' then
i = readArguments(tokens, i)
else
break
end
end
return i
end
local function transformSyntax(text, diffs)
local tokens = tokenize(text)
local stack = {}
local function edit(start, finish, replacement)
diffs[#diffs + 1] = {
start = start,
finish = finish,
text = replacement,
}
end
for i, token in ipairs(tokens) do
local value = token.value
if value == 'local' then
local names = {}
local j = i + 1
while tokens[j] and tokens[j].kind == 'name' do
names[#names + 1] = tokens[j].value
j = j + 1
if not tokens[j] or tokens[j].value ~= ',' then break end
j = j + 1
end
if #names > 0
and tokens[j] and tokens[j].value == 'in'
and tokens[j + 1]
then
local inToken = tokens[j]
local first = tokens[j + 1]
j = readSource(tokens, j + 1)
local last = j and tokens[j - 1]
local nextToken = j and tokens[j]
if last and (
not nextToken
or (
not continuation[nextToken.value]
and nextToken.kind ~= 'string'
)
) then
local source = text:sub(first.start, last.finish)
local fields = { ').' .. names[1] }
for n = 2, #names do
fields[#fields + 1] =
', (' .. source .. ').' .. names[n]
end
edit(inToken.start, inToken.finish, '=')
edit(first.start, first.start - 1, '(')
edit(last.finish + 1, last.finish, table.concat(fields))
end
end
elseif value == '.' and stack[#stack] == '{' then
local previous = tokens[i - 1]
local name = tokens[i + 1]
local following = tokens[i + 2]
if previous
and (
previous.value == '{'
or previous.value == ','
or previous.value == ';'
)
and name and name.kind == 'name'
and following
and (
following.value == ','
or following.value == ';'
or following.value == '}'
)
then
edit(token.start, token.finish, '')
edit(name.finish + 1, name.finish, ' = true')
end
end
if value == '{' or value == '[' or value == '(' then
stack[#stack + 1] = value
elseif closing[value] and stack[#stack] == closing[value] then
stack[#stack] = nil
end
end
end
return transformSyntax