rufus-lua-moon 0.2.0 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 64a42dafd47150d25613181b8f9898f7456e89ce
4
- data.tar.gz: 22e9a59e022d27df880130803b6fa7a2aad8be10
3
+ metadata.gz: e343762d37d17e9fc502fa6221ed3e376cb1a1a7
4
+ data.tar.gz: 3d1b775db07fa27e42fbb475ba48bd06421b8822
5
5
  SHA512:
6
- metadata.gz: 602ddba5e7e2abfca7c15ec506332e19ab5ddc7f81a3dc454488ced77b97e6559d9d9381cabbac08dde5cf6a86be575e68155ce7b3a7580a7187e714a0b66b32
7
- data.tar.gz: ff8982b11792e45e2d21007527d0b0f84a8f1e58fa2e848caf05e9561255450822bc3868de0e36999b8d5e4662dc19b2b24862a0840a88ab35e8ffc86b044c78
6
+ metadata.gz: 13b413ebc32760c0d5bc13ea70450957554491bb46d34fea38aea574ab2602bd6f27aedfce728c68a8fcad10410b36dd2a854163e8e793ebb06754e15771cafe
7
+ data.tar.gz: 9534f6e6fea762307abf43fb83029e7740444e013a295f8e9829f486db66cfc8f4e9f22e0415e815274fb690dcfd9a274a136a7aeecb124a32eb3ad76b9b17b5
@@ -134,3 +134,4 @@ fold = (items, fn)->
134
134
  else
135
135
  items[1]
136
136
 
137
+ nil
@@ -18,12 +18,142 @@ do
18
18
  end
19
19
  local concat, insert = table.concat, table.insert
20
20
  local pos_to_line, get_closest_line, trim = util.pos_to_line, util.get_closest_line, util.trim
21
- local Line
22
- Line = (function()
21
+ local mtype = util.moon.type
22
+ local Line, Lines
23
+ do
23
24
  local _parent_0 = nil
24
25
  local _base_0 = {
26
+ mark_pos = function(self, pos, line)
27
+ if line == nil then
28
+ line = #self
29
+ end
30
+ if not (self.posmap[line]) then
31
+ self.posmap[line] = pos
32
+ end
33
+ end,
34
+ add = function(self, item)
35
+ local _exp_0 = mtype(item)
36
+ if Line == _exp_0 then
37
+ item:render(self)
38
+ elseif Block == _exp_0 then
39
+ item:render(self)
40
+ else
41
+ self[#self + 1] = item
42
+ end
43
+ return self
44
+ end,
45
+ flatten_posmap = function(self, line_no, out)
46
+ if line_no == nil then
47
+ line_no = 0
48
+ end
49
+ if out == nil then
50
+ out = { }
51
+ end
52
+ local posmap = self.posmap
53
+ for i, l in ipairs(self) do
54
+ local _exp_0 = type(l)
55
+ if "table" == _exp_0 then
56
+ local _
57
+ _, line_no = l:flatten_posmap(line_no, out)
58
+ elseif "string" == _exp_0 then
59
+ line_no = line_no + 1
60
+ out[line_no] = posmap[i]
61
+ end
62
+ end
63
+ return out, line_no
64
+ end,
65
+ flatten = function(self, indent, buffer)
66
+ if indent == nil then
67
+ indent = nil
68
+ end
69
+ if buffer == nil then
70
+ buffer = { }
71
+ end
72
+ for i = 1, #self do
73
+ local l = self[i]
74
+ local _exp_0 = type(l)
75
+ if "string" == _exp_0 then
76
+ if indent then
77
+ insert(buffer, indent)
78
+ end
79
+ insert(buffer, l)
80
+ if "string" == type(self[i + 1]) then
81
+ local lc = l:sub(-1)
82
+ if (lc == ")" or lc == "]") and self[i + 1]:sub(1, 1) == "(" then
83
+ insert(buffer, ";")
84
+ end
85
+ end
86
+ insert(buffer, "\n")
87
+ local last = l
88
+ elseif "table" == _exp_0 then
89
+ l:flatten(indent and indent .. indent_char or indent_char, buffer)
90
+ end
91
+ end
92
+ return buffer
93
+ end,
94
+ __tostring = function(self)
95
+ local strip
96
+ strip = function(t)
97
+ if "table" == type(t) then
98
+ return (function()
99
+ local _accum_0 = { }
100
+ local _len_0 = 0
101
+ local _list_0 = t
102
+ for _index_0 = 1, #_list_0 do
103
+ local v = _list_0[_index_0]
104
+ _len_0 = _len_0 + 1
105
+ _accum_0[_len_0] = strip(v)
106
+ end
107
+ return _accum_0
108
+ end)()
109
+ else
110
+ return t
111
+ end
112
+ end
113
+ return "Lines<" .. tostring(util.dump(strip(self)):sub(1, -2)) .. ">"
114
+ end
115
+ }
116
+ _base_0.__index = _base_0
117
+ if _parent_0 then
118
+ setmetatable(_base_0, _parent_0.__base)
119
+ end
120
+ local _class_0 = setmetatable({
121
+ __init = function(self)
122
+ self.posmap = { }
123
+ end,
124
+ __base = _base_0,
125
+ __name = "Lines",
126
+ __parent = _parent_0
127
+ }, {
128
+ __index = function(cls, name)
129
+ local val = rawget(_base_0, name)
130
+ if val == nil and _parent_0 then
131
+ return _parent_0[name]
132
+ else
133
+ return val
134
+ end
135
+ end,
136
+ __call = function(cls, ...)
137
+ local _self_0 = setmetatable({}, _base_0)
138
+ cls.__init(_self_0, ...)
139
+ return _self_0
140
+ end
141
+ })
142
+ _base_0.__class = _class_0
143
+ if _parent_0 and _parent_0.__inherited then
144
+ _parent_0.__inherited(_parent_0, _class_0)
145
+ end
146
+ Lines = _class_0
147
+ end
148
+ do
149
+ local _parent_0 = nil
150
+ local _base_0 = {
151
+ pos = nil,
25
152
  _append_single = function(self, item)
26
- if util.moon.type(item) == Line then
153
+ if Line == mtype(item) then
154
+ if not (self.pos) then
155
+ self.pos = item.pos
156
+ end
27
157
  local _list_0 = item
28
158
  for _index_0 = 1, #_list_0 do
29
159
  value = _list_0[_index_0]
@@ -41,6 +171,7 @@ Line = (function()
41
171
  insert(self, delim)
42
172
  end
43
173
  end
174
+ return nil
44
175
  end,
45
176
  append = function(self, ...)
46
177
  local _list_0 = {
@@ -52,20 +183,40 @@ Line = (function()
52
183
  end
53
184
  return nil
54
185
  end,
55
- render = function(self)
56
- local buff = { }
57
- for i = 1, #self do
58
- local c = self[i]
59
- insert(buff, (function()
60
- if util.moon.type(c) == Block then
61
- c:bubble()
62
- return c:render()
63
- else
64
- return c
186
+ render = function(self, buffer)
187
+ local current = { }
188
+ local add_current
189
+ add_current = function()
190
+ buffer:add(concat(current))
191
+ return buffer:mark_pos(self.pos)
192
+ end
193
+ local _list_0 = self
194
+ for _index_0 = 1, #_list_0 do
195
+ local chunk = _list_0[_index_0]
196
+ local _exp_0 = mtype(chunk)
197
+ if Block == _exp_0 then
198
+ local _list_1 = chunk:render(Lines())
199
+ for _index_1 = 1, #_list_1 do
200
+ local block_chunk = _list_1[_index_1]
201
+ if "string" == type(block_chunk) then
202
+ insert(current, block_chunk)
203
+ else
204
+ add_current()
205
+ buffer:add(block_chunk)
206
+ current = { }
207
+ end
65
208
  end
66
- end)())
209
+ else
210
+ insert(current, chunk)
211
+ end
212
+ end
213
+ if #current > 0 then
214
+ add_current()
67
215
  end
68
- return concat(buff)
216
+ return buffer
217
+ end,
218
+ __tostring = function(self)
219
+ return "Line<" .. tostring(util.dump(self):sub(1, -2)) .. ">"
69
220
  end
70
221
  }
71
222
  _base_0.__index = _base_0
@@ -97,9 +248,12 @@ Line = (function()
97
248
  end
98
249
  })
99
250
  _base_0.__class = _class_0
100
- return _class_0
101
- end)()
102
- Block = (function()
251
+ if _parent_0 and _parent_0.__inherited then
252
+ _parent_0.__inherited(_parent_0, _class_0)
253
+ end
254
+ Line = _class_0
255
+ end
256
+ do
103
257
  local _parent_0 = nil
104
258
  local _base_0 = {
105
259
  header = "do",
@@ -107,17 +261,13 @@ Block = (function()
107
261
  export_all = false,
108
262
  export_proper = false,
109
263
  __tostring = function(self)
110
- return "Block<> <- " .. tostring(self.parent)
111
- end,
112
- bubble = function(self, other)
113
- if other == nil then
114
- other = self.parent
264
+ local h
265
+ if "string" == type(self.header) then
266
+ h = self.header
267
+ else
268
+ h = unpack(self.header:render({ }))
115
269
  end
116
- local has_varargs = self.has_varargs and not self:has_name("...")
117
- other.has_varargs = other.has_varargs or has_varargs
118
- end,
119
- line_table = function(self)
120
- return self._posmap
270
+ return "Block<" .. tostring(h) .. "> <- " .. tostring(self.parent)
121
271
  end,
122
272
  set = function(self, name, value)
123
273
  self._state[name] = value
@@ -125,6 +275,20 @@ Block = (function()
125
275
  get = function(self, name)
126
276
  return self._state[name]
127
277
  end,
278
+ listen = function(self, name, fn)
279
+ self._listeners[name] = fn
280
+ end,
281
+ unlisten = function(self, name)
282
+ self._listeners[name] = nil
283
+ end,
284
+ send = function(self, name, ...)
285
+ do
286
+ local fn = self._listeners[name]
287
+ if fn then
288
+ return fn(self, ...)
289
+ end
290
+ end
291
+ end,
128
292
  declare = function(self, names)
129
293
  local undeclared = (function()
130
294
  local _accum_0 = { }
@@ -134,7 +298,7 @@ Block = (function()
134
298
  local name = _list_0[_index_0]
135
299
  local is_local = false
136
300
  local real_name
137
- local _exp_0 = util.moon.type(name)
301
+ local _exp_0 = mtype(name)
138
302
  if LocalName == _exp_0 then
139
303
  is_local = true
140
304
  real_name = name:get_name(self)
@@ -164,11 +328,15 @@ Block = (function()
164
328
  whitelist_names = function(self, names)
165
329
  self._name_whitelist = Set(names)
166
330
  end,
167
- put_name = function(self, name)
168
- if util.moon.type(name) == NameProxy then
331
+ put_name = function(self, name, ...)
332
+ value = ...
333
+ if select("#", ...) == 0 then
334
+ value = true
335
+ end
336
+ if NameProxy == mtype(name) then
169
337
  name = name:get_name(self)
170
338
  end
171
- self._names[name] = true
339
+ self._names[name] = value
172
340
  end,
173
341
  has_name = function(self, name, skip_exports)
174
342
  if not skip_exports then
@@ -219,99 +387,26 @@ Block = (function()
219
387
  })
220
388
  return name
221
389
  end,
222
- mark_pos = function(self, node)
223
- if node[-1] then
224
- self.last_pos = node[-1]
225
- if not self._posmap[self.current_line] then
226
- self._posmap[self.current_line] = self.last_pos
227
- end
228
- end
390
+ add = function(self, item)
391
+ self._lines:add(item)
392
+ return item
229
393
  end,
230
- add_line_text = function(self, text)
231
- return insert(self._lines, text)
232
- end,
233
- append_line_table = function(self, sub_table, offset)
234
- offset = offset + self.current_line
235
- for line, source in pairs(sub_table) do
236
- local line = line + offset
237
- if not self._posmap[line] then
238
- self._posmap[line] = source
239
- end
240
- end
241
- end,
242
- add_line_tables = function(self, line)
243
- local _list_0 = line
244
- for _index_0 = 1, #_list_0 do
245
- local chunk = _list_0[_index_0]
246
- if util.moon.type(chunk) == Block then
247
- local current = chunk
248
- while current do
249
- if util.moon.type(current.header) == Line then
250
- self:add_line_tables(current.header)
251
- end
252
- self:append_line_table(current:line_table(), 0)
253
- self.current_line = self.current_line + current.current_line
254
- current = current.next
255
- end
256
- end
257
- end
258
- end,
259
- add = function(self, line)
260
- local t = util.moon.type(line)
261
- if t == "string" then
262
- self:add_line_text(line)
263
- elseif t == Block then
264
- self:add(self:line(line))
265
- elseif t == Line then
266
- self:add_line_tables(line)
267
- self:add_line_text(line:render())
268
- self.current_line = self.current_line + 1
394
+ render = function(self, buffer)
395
+ buffer:add(self.header)
396
+ buffer:mark_pos(self.pos)
397
+ if self.next then
398
+ buffer:add(self._lines)
399
+ self.next:render(buffer)
269
400
  else
270
- error("Adding unknown item")
271
- end
272
- return nil
273
- end,
274
- _insert_breaks = function(self)
275
- for i = 1, #self._lines - 1 do
276
- local left, right = self._lines[i], self._lines[i + 1]
277
- if left:sub(-1) == ")" and right:sub(1, 1) == "(" then
278
- self._lines[i] = self._lines[i] .. ";"
279
- end
280
- end
281
- end,
282
- render = function(self)
283
- local flatten
284
- flatten = function(line)
285
- if type(line) == "string" then
286
- return line
401
+ if #self._lines == 0 and "string" == type(buffer[#buffer]) then
402
+ buffer[#buffer] = buffer[#buffer] .. (" " .. (unpack(Lines():add(self.footer))))
287
403
  else
288
- return line:render()
404
+ buffer:add(self._lines)
405
+ buffer:add(self.footer)
406
+ buffer:mark_pos(self.pos)
289
407
  end
290
408
  end
291
- local header = flatten(self.header)
292
- if #self._lines == 0 then
293
- local footer = flatten(self.footer)
294
- return concat({
295
- header,
296
- footer
297
- }, " ")
298
- end
299
- local indent = indent_char:rep(self.indent)
300
- if not self.delim then
301
- self:_insert_breaks()
302
- end
303
- local body = indent .. concat(self._lines, (self.delim or "") .. "\n" .. indent)
304
- return concat({
305
- header,
306
- body,
307
- indent_char:rep(self.indent - 1) .. (function()
308
- if self.next then
309
- return self.next:render()
310
- else
311
- return flatten(self.footer)
312
- end
313
- end)()
314
- }, "\n")
409
+ return buffer
315
410
  end,
316
411
  block = function(self, header, footer)
317
412
  return Block(self, header, footer)
@@ -334,19 +429,29 @@ Block = (function()
334
429
  return self:value(node)
335
430
  end,
336
431
  value = function(self, node, ...)
337
- node = self.root.transform.value(node)
432
+ node = self.transform.value(node)
338
433
  local action
339
434
  if type(node) ~= "table" then
340
435
  action = "raw_value"
341
436
  else
342
- self:mark_pos(node)
343
437
  action = node[1]
344
438
  end
345
439
  local fn = value_compile[action]
346
440
  if not fn then
347
441
  error("Failed to compile value: " .. dump.value(node))
348
442
  end
349
- return fn(self, node, ...)
443
+ local out = fn(self, node, ...)
444
+ if type(node) == "table" and node[-1] then
445
+ if type(out) == "string" then
446
+ do
447
+ local _with_0 = Line()
448
+ _with_0:append(out)
449
+ out = _with_0
450
+ end
451
+ end
452
+ out.pos = node[-1]
453
+ end
454
+ return out
350
455
  end,
351
456
  values = function(self, values, delim)
352
457
  delim = delim or ', '
@@ -370,28 +475,33 @@ Block = (function()
370
475
  if not node then
371
476
  return
372
477
  end
373
- node = self.root.transform.statement(node)
374
- local fn = line_compile[ntype(node)]
375
- if not fn then
376
- if has_value(node) then
377
- self:stm({
378
- "assign",
379
- {
380
- "_"
381
- },
382
- {
383
- node
384
- }
385
- })
478
+ node = self.transform.statement(node)
479
+ local result
480
+ do
481
+ local fn = line_compile[ntype(node)]
482
+ if fn then
483
+ result = fn(self, node, ...)
386
484
  else
387
- self:add(self:value(node))
485
+ if has_value(node) then
486
+ result = self:stm({
487
+ "assign",
488
+ {
489
+ "_"
490
+ },
491
+ {
492
+ node
493
+ }
494
+ })
495
+ else
496
+ result = self:value(node)
497
+ end
388
498
  end
389
- else
390
- self:mark_pos(node)
391
- local out = fn(self, node, ...)
392
- if out then
393
- self:add(out)
499
+ end
500
+ if result then
501
+ if type(node) == "table" and type(result) == "table" and node[-1] then
502
+ result.pos = node[-1]
394
503
  end
504
+ self:add(result)
395
505
  end
396
506
  return nil
397
507
  end,
@@ -405,6 +515,14 @@ Block = (function()
405
515
  self:stm(stm)
406
516
  end
407
517
  return nil
518
+ end,
519
+ splice = function(self, fn)
520
+ local lines = {
521
+ "lines",
522
+ self._lines
523
+ }
524
+ self._lines = Lines()
525
+ return self:stms(fn(lines))
408
526
  end
409
527
  }
410
528
  _base_0.__index = _base_0
@@ -414,17 +532,26 @@ Block = (function()
414
532
  local _class_0 = setmetatable({
415
533
  __init = function(self, parent, header, footer)
416
534
  self.parent, self.header, self.footer = parent, header, footer
417
- self.current_line = 1
418
- self._lines = { }
419
- self._posmap = { }
535
+ self._lines = Lines()
420
536
  self._names = { }
421
537
  self._state = { }
538
+ self._listeners = { }
539
+ do
540
+ local _with_0 = transform
541
+ self.transform = {
542
+ value = _with_0.Value:bind(self),
543
+ statement = _with_0.Statement:bind(self)
544
+ }
545
+ end
422
546
  if self.parent then
423
547
  self.root = self.parent.root
424
548
  self.indent = self.parent.indent + 1
425
- return setmetatable(self._state, {
549
+ setmetatable(self._state, {
426
550
  __index = self.parent._state
427
551
  })
552
+ return setmetatable(self._listeners, {
553
+ __index = self.parent._listeners
554
+ })
428
555
  else
429
556
  self.indent = 0
430
557
  end
@@ -448,18 +575,33 @@ Block = (function()
448
575
  end
449
576
  })
450
577
  _base_0.__class = _class_0
451
- return _class_0
452
- end)()
453
- local RootBlock
454
- RootBlock = (function()
578
+ if _parent_0 and _parent_0.__inherited then
579
+ _parent_0.__inherited(_parent_0, _class_0)
580
+ end
581
+ Block = _class_0
582
+ end
583
+ do
455
584
  local _parent_0 = Block
456
585
  local _base_0 = {
457
586
  __tostring = function(self)
458
587
  return "RootBlock<>"
459
588
  end,
589
+ root_stms = function(self, stms)
590
+ if not (self.options.implicitly_return_root == false) then
591
+ stms = transform.Statement.transformers.root_stms(self, stms)
592
+ end
593
+ local _list_0 = stms
594
+ for _index_0 = 1, #_list_0 do
595
+ local s = _list_0[_index_0]
596
+ self:stm(s)
597
+ end
598
+ end,
460
599
  render = function(self)
461
- self:_insert_breaks()
462
- return concat(self._lines, "\n")
600
+ local buffer = self._lines:flatten()
601
+ if buffer[#buffer] == "\n" then
602
+ buffer[#buffer] = nil
603
+ end
604
+ return table.concat(buffer)
463
605
  end
464
606
  }
465
607
  _base_0.__index = _base_0
@@ -467,13 +609,10 @@ RootBlock = (function()
467
609
  setmetatable(_base_0, _parent_0.__base)
468
610
  end
469
611
  local _class_0 = setmetatable({
470
- __init = function(self, ...)
612
+ __init = function(self, options)
613
+ self.options = options
471
614
  self.root = self
472
- self.transform = {
473
- value = transform.Value:instance(self),
474
- statement = transform.Statement:instance(self)
475
- }
476
- return _parent_0.__init(self, ...)
615
+ return _parent_0.__init(self)
477
616
  end,
478
617
  __base = _base_0,
479
618
  __name = "RootBlock",
@@ -494,8 +633,11 @@ RootBlock = (function()
494
633
  end
495
634
  })
496
635
  _base_0.__class = _class_0
497
- return _class_0
498
- end)()
636
+ if _parent_0 and _parent_0.__inherited then
637
+ _parent_0.__inherited(_parent_0, _class_0)
638
+ end
639
+ RootBlock = _class_0
640
+ end
499
641
  format_error = function(msg, pos, file_str)
500
642
  local line = pos_to_line(file_str, pos)
501
643
  local line_str
@@ -515,35 +657,35 @@ value = function(value)
515
657
  end
516
658
  return out
517
659
  end
518
- tree = function(tree)
519
- local scope = RootBlock()
660
+ tree = function(tree, options)
661
+ if options == nil then
662
+ options = { }
663
+ end
664
+ assert(tree, "missing tree")
665
+ local scope = (options.scope or RootBlock)(options)
520
666
  local runner = coroutine.create(function()
521
- local _list_0 = tree
522
- for _index_0 = 1, #_list_0 do
523
- local line = _list_0[_index_0]
524
- scope:stm(line)
525
- end
526
- return scope:render()
667
+ return scope:root_stms(tree)
527
668
  end)
528
- local success, result = coroutine.resume(runner)
669
+ local success, err = coroutine.resume(runner)
529
670
  if not success then
530
671
  local error_msg
531
- if type(result) == "table" then
532
- local error_type = result[1]
672
+ if type(err) == "table" then
673
+ local error_type = err[1]
533
674
  if error_type == "user-error" then
534
- error_msg = result[2]
675
+ error_msg = err[2]
535
676
  else
536
677
  error_msg = error("Unknown error thrown", util.dump(error_msg))
537
678
  end
538
679
  else
539
680
  error_msg = concat({
540
- result,
681
+ err,
541
682
  debug.traceback(runner)
542
683
  }, "\n")
543
684
  end
544
685
  return nil, error_msg, scope.last_pos
545
686
  else
546
- local tbl = scope:line_table()
547
- return result, tbl
687
+ local lua_code = scope:render()
688
+ local posmap = scope._lines:flatten_posmap()
689
+ return lua_code, posmap
548
690
  end
549
691
  end