Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new feature: Keep line number #311

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bin/moonc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ local help = [[Usage: %s [options] files/directories...
-l Perform lint on the file instead of compiling
-b Dump parse and compile time (doesn't write output)
-v Print version

-n Keep line numbers so that every output line maps a input line
-- Read from standard in, print to standard out
(Must be first and only argument)
]]
Expand Down Expand Up @@ -248,6 +248,7 @@ else
benchmark = opts.b,
show_posmap = opts.X,
show_parse_tree = opts.T,
keep_line_number = opts.n
})

if not success then
Expand Down
33 changes: 33 additions & 0 deletions moonscript/cmd/moonc.moon
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ gettime = do
else
nil, "LuaSocket needed for benchmark"


import pos_to_line from require "moonscript.util"

reverse_line_number = (code, line_table, line_num) ->
for i = line_num,0,-1
if line_table[i]
return pos_to_line code, line_table[i]
"unknown"

-- compiles file to lua, returns lua code
-- returns nil, error on error
-- returns true if some option handled the output instead
Expand Down Expand Up @@ -107,6 +116,30 @@ compile_file_text = (text, opts={}) ->
}, "\n"
return true

if opts.keep_line_number
line_map = {} -- lua line to moon line
for lua_line_number,moon_pos in pairs(posmap_or_err)
line_map[lua_line_number] = reverse_line_number text, posmap_or_err, lua_line_number
aligned_code = ""
lua_line_number = 1
current_moon_line_number = 1
for line in string.gmatch(code..'\n', "(.-)\n")
if moon_line_number = line_map[lua_line_number]
to_next_line = false
while current_moon_line_number < moon_line_number
to_next_line = true
aligned_code ..= '\n'
current_moon_line_number += 1
unless to_next_line
aligned_code ..= ' ' -- add a space
else
aligned_code ..= ' '
-- BUG cannot tell whether it is part of multi-line string
-- there should be \n only if it is a multi-line string
aligned_code ..= line
lua_line_number += 1
code = aligned_code .. '\n'

code

write_file = (fname, code) ->
Expand Down