|
17 | 17 | ntype = _table_0.ntype |
18 | 18 | end |
19 | 19 | local concat, insert = table.concat, table.insert |
20 | | -local pos_to_line, get_closest_line, trim = util.pos_to_line, util.get_closest_line, util.trim |
| 20 | +local pos_to_line, get_line, get_closest_line, trim = util.pos_to_line, util.get_line, util.get_closest_line, util.trim |
21 | 21 | local mtype = util.moon.type |
22 | 22 | local Line |
23 | 23 | Line = (function() |
@@ -123,7 +123,7 @@ Line = (function() |
123 | 123 | return _class_0 |
124 | 124 | end)() |
125 | 125 | Block = (function() |
126 | | - local add_to_buffer |
| 126 | + local add_to_buffer, block_iterator |
127 | 127 | local _parent_0 = nil |
128 | 128 | local _base_0 = { |
129 | 129 | header = "do", |
@@ -260,54 +260,36 @@ Block = (function() |
260 | 260 | }) |
261 | 261 | return name |
262 | 262 | end, |
263 | | - mark_pos = function(self, node) |
264 | | - if node[-1] then |
265 | | - self.last_pos = node[-1] |
266 | | - if not self._posmap[self.current_line] then |
267 | | - self._posmap[self.current_line] = self.last_pos |
| 263 | + mark_pos = function(self, line_no, node) |
| 264 | + do |
| 265 | + local pos = node[-1] |
| 266 | + if pos then |
| 267 | + self.last_pos = pos |
| 268 | + if not (self._posmap[line_no]) then |
| 269 | + self._posmap[line_no] = pos |
| 270 | + end |
268 | 271 | end |
269 | 272 | end |
270 | 273 | end, |
| 274 | + append_posmap = function(self, map) |
| 275 | + print("appending pos", self) |
| 276 | + self._posmap[#self._posmap + 1] = map |
| 277 | + end, |
271 | 278 | add_raw = function(self, item) |
272 | 279 | return insert(self._lines, item) |
273 | 280 | end, |
274 | | - append_line_table = function(self, sub_table, offset) |
275 | | - offset = offset + self.current_line |
276 | | - for line, source in pairs(sub_table) do |
277 | | - line = line + offset |
278 | | - if not self._posmap[line] then |
279 | | - self._posmap[line] = source |
280 | | - end |
281 | | - end |
282 | | - end, |
283 | | - add_line_tables = function(self, line) |
284 | | - local _list_0 = line |
285 | | - for _index_0 = 1, #_list_0 do |
286 | | - local chunk = _list_0[_index_0] |
287 | | - if util.moon.type(chunk) == Block then |
288 | | - local current = chunk |
289 | | - while current do |
290 | | - if util.moon.type(current.header) == Line then |
291 | | - self:add_line_tables(current.header) |
292 | | - end |
293 | | - self:append_line_table(current:line_table(), 0) |
294 | | - self.current_line = self.current_line + current.current_line |
295 | | - current = current.next |
296 | | - end |
297 | | - end |
298 | | - end |
299 | | - end, |
300 | 281 | add = function(self, line) |
301 | 282 | local _exp_0 = util.moon.type(line) |
302 | 283 | if "string" == _exp_0 then |
303 | | - return insert(self._lines, line) |
| 284 | + insert(self._lines, line) |
304 | 285 | elseif Block == _exp_0 then |
305 | | - return line:render(self._lines) |
| 286 | + line:render(self._lines) |
306 | 287 | elseif Line == _exp_0 then |
307 | | - return line:render(self._lines) |
| 288 | + line:render(self._lines) |
308 | 289 | else |
309 | | - return error("Adding unknown item") |
| 290 | + error("Adding unknown item") |
310 | 291 | end |
| 292 | + return line |
311 | 293 | end, |
312 | 294 | render = function(self, buffer) |
313 | 295 | add_to_buffer(buffer, self.header) |
@@ -361,7 +343,6 @@ Block = (function() |
361 | 343 | if type(node) ~= "table" then |
362 | 344 | action = "raw_value" |
363 | 345 | else |
364 | | - self:mark_pos(node) |
365 | 346 | action = node[1] |
366 | 347 | end |
367 | 348 | local fn = value_compile[action] |
@@ -393,26 +374,50 @@ Block = (function() |
393 | 374 | return |
394 | 375 | end |
395 | 376 | node = self.transform.statement(node) |
396 | | - local fn = line_compile[ntype(node)] |
397 | | - if not fn then |
398 | | - if has_value(node) then |
399 | | - self:stm({ |
400 | | - "assign", |
401 | | - { |
402 | | - "_" |
403 | | - }, |
404 | | - { |
405 | | - node |
406 | | - } |
407 | | - }) |
| 377 | + local before = #self._lines |
| 378 | + local added |
| 379 | + do |
| 380 | + local fn = line_compile[ntype(node)] |
| 381 | + if fn then |
| 382 | + local out = fn(self, node, ...) |
| 383 | + if out then |
| 384 | + added = self:add(out) |
| 385 | + end |
408 | 386 | else |
409 | | - self:add(self:value(node)) |
| 387 | + if has_value(node) then |
| 388 | + added = self:stm({ |
| 389 | + "assign", |
| 390 | + { |
| 391 | + "_" |
| 392 | + }, |
| 393 | + { |
| 394 | + node |
| 395 | + } |
| 396 | + }) |
| 397 | + else |
| 398 | + added = self:add(self:value(node)) |
| 399 | + end |
410 | 400 | end |
411 | | - else |
412 | | - self:mark_pos(node) |
413 | | - local out = fn(self, node, ...) |
414 | | - if out then |
415 | | - self:add(out) |
| 401 | + end |
| 402 | + if added then |
| 403 | + print("added " .. tostring(#self._lines - before) .. " lines") |
| 404 | + local list |
| 405 | + if Line == mtype(added) then |
| 406 | + list = added |
| 407 | + else |
| 408 | + list = { |
| 409 | + added |
| 410 | + } |
| 411 | + end |
| 412 | + local next_block = block_iterator(list) |
| 413 | + for l = before + 1, #self._lines do |
| 414 | + if "table" == type(self._lines[l]) then |
| 415 | + local block = next_block() |
| 416 | + block._posmap.num_lines = #block._lines |
| 417 | + self._posmap[l] = block._posmap |
| 418 | + else |
| 419 | + self:mark_pos(l, node) |
| 420 | + end |
416 | 421 | end |
417 | 422 | end |
418 | 423 | return nil |
@@ -444,7 +449,6 @@ Block = (function() |
444 | 449 | local _class_0 = setmetatable({ |
445 | 450 | __init = function(self, parent, header, footer) |
446 | 451 | self.parent, self.header, self.footer = parent, header, footer |
447 | | - self.current_line = 1 |
448 | 452 | self._lines = { } |
449 | 453 | self._posmap = { } |
450 | 454 | self._names = { } |
@@ -499,6 +503,17 @@ Block = (function() |
499 | 503 | end |
500 | 504 | return buffer |
501 | 505 | end |
| 506 | + block_iterator = function(list) |
| 507 | + return coroutine.wrap(function() |
| 508 | + local _list_0 = list |
| 509 | + for _index_0 = 1, #_list_0 do |
| 510 | + local item = _list_0[_index_0] |
| 511 | + if Block == mtype(item) then |
| 512 | + coroutine.yield(item) |
| 513 | + end |
| 514 | + end |
| 515 | + end) |
| 516 | + end |
502 | 517 | if _parent_0 and _parent_0.__inherited then |
503 | 518 | _parent_0.__inherited(_parent_0, _class_0) |
504 | 519 | end |
@@ -534,6 +549,76 @@ flatten_lines = function(lines, indent, buffer) |
534 | 549 | end |
535 | 550 | return buffer |
536 | 551 | end |
| 552 | +local flatten_posmap |
| 553 | +flatten_posmap = function(posmap, dl, out) |
| 554 | + if dl == nil then |
| 555 | + dl = 0 |
| 556 | + end |
| 557 | + if out == nil then |
| 558 | + out = { } |
| 559 | + end |
| 560 | + for k, v in pairs(posmap) do |
| 561 | + local _continue_0 = false |
| 562 | + repeat |
| 563 | + if "string" == type(k) then |
| 564 | + _continue_0 = true |
| 565 | + break |
| 566 | + end |
| 567 | + if "table" == type(v) then |
| 568 | + flatten_posmap(v, k - 1 + dl, out) |
| 569 | + dl = dl + (v.num_lines - 1) |
| 570 | + else |
| 571 | + out[k + dl] = v |
| 572 | + end |
| 573 | + _continue_0 = true |
| 574 | + until true |
| 575 | + if not _continue_0 then |
| 576 | + break |
| 577 | + end |
| 578 | + end |
| 579 | + return out |
| 580 | +end |
| 581 | +local debug_posmap |
| 582 | +debug_posmap = function(posmap, fname, lua_code) |
| 583 | + if fname == nil then |
| 584 | + fname = error("pass in input file") |
| 585 | + end |
| 586 | + local moon_code = io.open(fname):read("*a") |
| 587 | + local tuples = (function() |
| 588 | + local _accum_0 = { } |
| 589 | + local _len_0 = 0 |
| 590 | + for k, v in pairs(posmap) do |
| 591 | + _len_0 = _len_0 + 1 |
| 592 | + _accum_0[_len_0] = { |
| 593 | + k, |
| 594 | + v |
| 595 | + } |
| 596 | + end |
| 597 | + return _accum_0 |
| 598 | + end)() |
| 599 | + table.sort(tuples, function(a, b) |
| 600 | + return a[1] < b[1] |
| 601 | + end) |
| 602 | + local lines = (function() |
| 603 | + local _accum_0 = { } |
| 604 | + local _len_0 = 0 |
| 605 | + local _list_0 = tuples |
| 606 | + for _index_0 = 1, #_list_0 do |
| 607 | + local pair = _list_0[_index_0] |
| 608 | + local lua_line, pos = unpack(pair) |
| 609 | + local moon_line = pos_to_line(moon_code, pos) |
| 610 | + local lua_text = get_line(lua_code, lua_line) |
| 611 | + local moon_text = get_closest_line(moon_code, moon_line) |
| 612 | + local _value_0 = tostring(pos) .. "\t " .. tostring(lua_line) .. ":[ " .. tostring(trim(lua_text)) .. " ] >> " .. tostring(moon_line) .. ":[ " .. tostring(trim(moon_text)) .. " ]" |
| 613 | + if _value_0 ~= nil then |
| 614 | + _len_0 = _len_0 + 1 |
| 615 | + _accum_0[_len_0] = _value_0 |
| 616 | + end |
| 617 | + end |
| 618 | + return _accum_0 |
| 619 | + end)() |
| 620 | + return concat(lines, "\n") .. "\n" |
| 621 | +end |
537 | 622 | RootBlock = (function() |
538 | 623 | local _parent_0 = Block |
539 | 624 | local _base_0 = { |
@@ -631,7 +716,11 @@ tree = function(tree, scope) |
631 | 716 | end |
632 | 717 | return nil, error_msg, scope.last_pos |
633 | 718 | else |
634 | | - local tbl = scope:line_table() |
635 | | - return result, tbl |
| 719 | + local raw_posmap = scope:line_table() |
| 720 | + local posmap = flatten_posmap(raw_posmap) |
| 721 | + print(util.dump(raw_posmap)) |
| 722 | + print(util.dump(posmap)) |
| 723 | + print(debug_posmap(posmap, "scrap.moon", result)) |
| 724 | + return result, posmap |
636 | 725 | end |
637 | 726 | end |
0 commit comments