-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathf-string.lua
More file actions
322 lines (264 loc) · 7.79 KB
/
f-string.lua
File metadata and controls
322 lines (264 loc) · 7.79 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
--- f-string.lua - an implementation of Python-style f-strings in Lua
-- @author [Ezhik](https://ezhik.jp)
-- @module f-string
-- @license MPL-2.0
local function getEnvWithLocals(level)
if level == nil then level = 2 end
return setmetatable({}, {
__index = function(_, k)
local v = nil
-- first, check locals
for i = 1, math.maxinteger do
local lk, lv = debug.getlocal(level, i)
if lk == nil then
break
end
if lk == k then
v = lv
end
end
if v ~= nil then
return v
end
-- upvalues
-- BUG: this only works if you access the upvalue outside of the f-string
local f = debug.getinfo(level, "f").func
for i = 1, math.maxinteger do
local uk, uv = debug.getupvalue(f, i)
if uk == nil then
break
end
if uk == k then
return uv
end
end
-- globals
return _G[k]
end,
__newindex = function(_, k, v)
-- locals
local lIdx = nil
for i = 1, math.maxinteger do
local lk, lv = debug.getlocal(level, i)
if lk == nil then
break
end
if lk == k then
lIdx = i
end
end
if lIdx ~= nil then
debug.setlocal(level, lIdx, v)
return
end
-- upvalues
-- BUG: this only works if you access the upvalue outside of the f-string
local uIdx = nil
local f = debug.getinfo(level, "f").func
for i = 1, math.maxinteger do
local uk, uv = debug.getupvalue(f, i)
if uk == nil then
break
end
if uk == k then
uIdx = i
break
end
end
if uIdx ~= nil then
debug.setupvalue(f, uIdx, v)
return
end
-- globals
_G[k] = v
end,
})
end
local function f(input)
local env = getEnvWithLocals(5)
local function at(i)
if i > #input then
return nil
end
return input:sub(i, i)
end
local idx = 1
local function err(msg)
error(string.format("f-string: %s at position %s", msg, idx))
end
local function next(i)
return at(idx + (i or 0))
end
local function takeNext(n)
if n == nil then
n = 1
end
local s = input:sub(idx, idx + n - 1)
idx = idx + n
return s
end
local function is(c)
return input:sub(idx, idx + #c - 1) == c
end
local function expect(c)
if not is(c) then
err(string.format("Expected '%s'", c))
end
end
local function take(c)
expect(c)
return takeNext(#c)
end
local function parseQuotedString()
local parts = {}
local function add(str) table.insert(parts, str) end
if not is('"') and not is("'") then
err("Expected '\"' or '''")
end
local quote = takeNext(1)
add(quote)
while next() do
if is(quote) then
add(takeNext(1))
return table.concat(parts)
elseif is("\\") then
add(takeNext(2))
else
add(takeNext())
end
end
err("Expected '" .. quote .. "'")
end
local function parseMultilineString()
local parts = {}
local function add(str) table.insert(parts, str) end
add(take("["))
local close = "]"
while next() == "=" do
add(takeNext(1))
close = close .. "="
end
close = close .. "]"
add(take("["))
while next() do
if is(close) then
add(takeNext(#close))
return table.concat(parts)
else
add(takeNext())
end
end
err("Expected '" .. close .. "'")
end
local function parseSingleLineComment()
local parts = {}
local function add(str) table.insert(parts, str) end
add(take("--"))
while next() do
if is("\n") then
add(takeNext(1))
return table.concat(parts)
else
add(takeNext())
end
end
return table.concat(parts)
end
local function parseMultiLineComment()
local parts = {}
local function add(str) table.insert(parts, str) end
add(take("--[["))
while next() do
if is("]]") then
add(takeNext(2))
return table.concat(parts)
else
add(takeNext())
end
end
err("Expected ']]'")
end
local function parseBraces(topLevel)
local parts = {}
local function add(str) table.insert(parts, str) end
add(take("{"))
local spec
while next() do
-- braces
if is("}") then
add(takeNext(1))
local result = table.concat(parts)
local eqMatch
if topLevel then
eqMatch = result:match("%s*=%s*}$")
if eqMatch then
eqMatch = eqMatch:sub(1, #eqMatch - 1)
result = result:sub(1, #result - #eqMatch - 1) .. "}"
end
end
return result, eqMatch, spec
elseif is("{") then
add(parseBraces())
-- comments
elseif is("--[[") then
add(parseMultiLineComment())
elseif is("--") then
add(parseSingleLineComment())
-- strings
elseif is('"') or is("'") then
add(parseQuotedString())
elseif is("[[") or is("[=") then
add(parseMultilineString())
-- etc
elseif topLevel and is(":%") then
takeNext(1)
spec = ""
while not is("}") do
spec = spec .. takeNext(1)
end
else
add(takeNext())
end
end
error("Expected '}'")
end
local function parseExpr()
local expr, eqMatch, spec = parseBraces(true)
expr = expr:sub(2, #expr - 1)
local result = load("return " .. expr, expr, "t", env)()
spec = spec or "%s"
result = string.format(spec, result)
if eqMatch then
return string.format("%s%s%s", expr, eqMatch, result)
else
return result
end
end
local function parseString()
local parts = {}
local function add(str) table.insert(parts, str) end
while next() do
-- curly left
if is"{{" then
takeNext(2)
add("{")
elseif is("{}") then
error("valid expression required before '}'")
elseif is("{") then
add(parseExpr())
-- curly right
elseif is("}}") then
takeNext(2)
add("}")
elseif is("}") then
error("single '}' is not allowed")
-- the rest is just text
else
add(takeNext(1))
end
end
return table.concat(parts)
end
return parseString()
end
return f