@@ -6,38 +6,41 @@ local lfs = require "lfs"
66local parser = argparse ()
77
88parser :flag (" -l --lint" ," Perform a lint on the file instead of compiling" )
9+
910parser :flag (" -v --version" ," Print version" )
1011parser :flag (" -w --watch" ," Watch file/directory for updates" )
1112parser :option (" --transform" ," Transform syntax tree with module" )
13+
1214parser :mutex (
13- parser :option (" -t --output-to" ," Specify where to place compiled files" ),
14- parser :option (" -o" ," Write output to file" ),
15- parser :flag (" -p" ," Write output to standard output" ),
16- parser :flag (" -T" ," Write parse tree instead of code (to stdout)" ),
17- parser :flag (" -b" ," Write parse and compile time instead of code(to stdout)" ),
18- parser :flag (" -X" ," Write line rewrite map instead of code (to stdout)" )
15+ parser :option (" -t --output-to" ," Specify where to place compiled files" ),
16+ parser :option (" -o" ," Write output to file" ),
17+ parser :flag (" -p" ," Write output to standard output" ),
18+ parser :flag (" -T" ," Write parse tree instead of code (to stdout)" ),
19+ parser :flag (" -b" ," Write parse and compile time instead of code(to stdout)" ),
20+ parser :flag (" -X" ," Write line rewrite map instead of code (to stdout)" )
1921)
22+
2023parser :flag (" -" ,
21- " Read from standard in, print to standard out (Must be only argument)" )
24+ " Read from standard in, print to standard out (Must be only argument)" )
2225
2326local read_stdin = arg [1 ]== " --" -- luacheck: ignore 113
2427
2528if not read_stdin then
26- parser :argument (" file/directory" ):args (" +" )
29+ parser :argument (" file/directory" ):args (" +" )
2730end
2831
2932local opts = parser :parse ()
3033
3134if opts .version then
32- local v = require " moonscript.version"
33- v .print_version ()
34- os.exit ()
35+ local v = require " moonscript.version"
36+ v .print_version ()
37+ os.exit ()
3538end
3639
3740function log_msg (...)
38- if not opts .p then
39- io.stderr :write (table.concat ({... }," " ).. " \n " )
40- end
41+ if not opts .p then
42+ io.stderr :write (table.concat ({... }," " ).. " \n " )
43+ end
4144end
4245
4346local moonc = require (" moonscript.cmd.moonc" )
@@ -47,186 +50,186 @@ local compile_and_write = moonc.compile_and_write
4750local path_to_target = moonc .path_to_target
4851
4952local function scan_directory (root ,collected )
50- root = normalize_dir (root )
51- collected = collected or {}
52-
53- for fname in lfs .dir (root )do
54- if not fname :match (" ^%." )then
55- local full_path = root .. fname
56-
57- if lfs .attributes (full_path ," mode" )== " directory" then
58- scan_directory (full_path ,collected )
59- elseif fname :match (" %.moon$" )then
60- table.insert (collected ,full_path )
61- end
62- end
63- end
64-
65- return collected
53+ root = normalize_dir (root )
54+ collected = collected or {}
55+
56+ for fname in lfs .dir (root )do
57+ if not fname :match (" ^%." )then
58+ local full_path = root .. fname
59+
60+ if lfs .attributes (full_path ," mode" )== " directory" then
61+ scan_directory (full_path ,collected )
62+ elseif fname :match (" %.moon$" )then
63+ table.insert (collected ,full_path )
64+ end
65+ end
66+ end
67+
68+ return collected
6669end
6770
6871local function remove_dups (tbl ,key_fn )
69- local hash = {}
70- local final = {}
71-
72- for _ ,v in ipairs (tbl )do
73- local dup_key = key_fn and key_fn (v )or v
74- if not hash [dup_key ]then
75- table.insert (final ,v )
76- hash [dup_key ]= true
77- end
78- end
79-
80- return final
72+ local hash = {}
73+ local final = {}
74+
75+ for _ ,v in ipairs (tbl )do
76+ local dup_key = key_fn and key_fn (v )or v
77+ if not hash [dup_key ]then
78+ table.insert (final ,v )
79+ hash [dup_key ]= true
80+ end
81+ end
82+
83+ return final
8184end
8285
8386-- creates tuples of input and target
8487local function get_files (fname ,files )
85- files = files or {}
86-
87- if lfs .attributes (fname ," mode" )== " directory" then
88- for _ ,sub_fname in ipairs (scan_directory (fname ))do
89- table.insert (files , {
90- sub_fname ,
91- path_to_target (sub_fname ,opts .output_to ,fname )
92- })
93- end
94- else
95- table.insert (files , {
96- fname ,
97- path_to_target (fname ,opts .output_to )
98- })
99- end
100-
101- return files
88+ files = files or {}
89+
90+ if lfs .attributes (fname ," mode" )== " directory" then
91+ for _ ,sub_fname in ipairs (scan_directory (fname ))do
92+ table.insert (files , {
93+ sub_fname ,
94+ path_to_target (sub_fname ,opts .output_to ,fname )
95+ })
96+ end
97+ else
98+ table.insert (files , {
99+ fname ,
100+ path_to_target (fname ,opts .output_to )
101+ })
102+ end
103+
104+ return files
102105end
103106
104107if read_stdin then
105- local parse = require " moonscript.parse"
106- local compile = require " moonscript.compile"
108+ local parse = require " moonscript.parse"
109+ local compile = require " moonscript.compile"
107110
108- local text = io.stdin :read (" *a" )
109- local tree ,err = parse .string (text )
111+ local text = io.stdin :read (" *a" )
112+ local tree ,err = parse .string (text )
110113
111- if not tree then error (err )end
112- local code ,err ,pos = compile .tree (tree )
114+ if not tree then error (err )end
115+ local code ,err ,pos = compile .tree (tree )
113116
114- if not code then
115- error (compile .format_error (err ,pos ,text ))
116- end
117+ if not code then
118+ error (compile .format_error (err ,pos ,text ))
119+ end
117120
118- print (code )
119- os.exit ()
121+ print (code )
122+ os.exit ()
120123end
121124
122125local inputs = opts [" file/directory" ]
123126
124127local files = {}
125128for _ ,input in ipairs (inputs )do
126- get_files (input ,files )
129+ get_files (input ,files )
127130end
128131
129132files = remove_dups (files ,function (f )
130- return f [2 ]
133+ return f [2 ]
131134end )
132135
133136-- returns an iterator that returns files that have been updated
134137local function create_watcher (files )
135- local watchers = require (" moonscript.cmd.watchers" )
138+ local watchers = require (" moonscript.cmd.watchers" )
136139
137- if watchers .InotifyWacher :available ()then
138- return watchers .InotifyWacher (files ):each_update ()
139- end
140+ if watchers .InotifyWacher :available ()then
141+ return watchers .InotifyWacher (files ):each_update ()
142+ end
140143
141- return watchers .SleepWatcher (files ):each_update ()
144+ return watchers .SleepWatcher (files ):each_update ()
142145end
143146
144147if opts .watch then
145- -- build function to check for lint or compile in watch
146- local handle_file
147- if opts .lint then
148- local lint = require " moonscript.cmd.lint"
149- handle_file = lint .lint_file
150- else
151- handle_file = compile_and_write
152- end
153-
154- local watcher = create_watcher (files )
155- -- catches interrupt error for ctl-c
156- local protected = function ()
157- local status ,file = true ,watcher ()
158- if status then
159- return file
160- elseif file ~= " interrupted!" then
161- error (file )
162- end
163- end
164-
165- for fname in protected do
166- local target = path_to_target (fname ,opts .t )
167-
168- if opts .o then
169- target = opts .o
170- end
171-
172- local success ,err = handle_file (fname ,target )
173- if opts .lint then
174- if success then
175- io.stderr :write (success .. " \n\n " )
176- elseif err then
177- io.stderr :write (fname .. " \n " .. err .. " \n\n " )
178- end
179- elseif not success then
180- io.stderr :write (table.concat ({
181- " " ,
182- " Error:" .. fname ,
183- err ,
184- " \n " ,
185- }," \n " ))
186- elseif success == " build" then
187- log_msg (" Built" ,fname ," ->" ,target )
188- end
189- end
190-
191- io.stderr :write (" \n Quitting...\n " )
148+ -- build function to check for lint or compile in watch
149+ local handle_file
150+ if opts .lint then
151+ local lint = require " moonscript.cmd.lint"
152+ handle_file = lint .lint_file
153+ else
154+ handle_file = compile_and_write
155+ end
156+
157+ local watcher = create_watcher (files )
158+ -- catches interrupt error for ctl-c
159+ local protected = function ()
160+ local status ,file = true ,watcher ()
161+ if status then
162+ return file
163+ elseif file ~= " interrupted!" then
164+ error (file )
165+ end
166+ end
167+
168+ for fname in protected do
169+ local target = path_to_target (fname ,opts .t )
170+
171+ if opts .o then
172+ target = opts .o
173+ end
174+
175+ local success ,err = handle_file (fname ,target )
176+ if opts .lint then
177+ if success then
178+ io.stderr :write (success .. " \n\n " )
179+ elseif err then
180+ io.stderr :write (fname .. " \n " .. err .. " \n\n " )
181+ end
182+ elseif not success then
183+ io.stderr :write (table.concat ({
184+ " " ,
185+ " Error:" .. fname ,
186+ err ,
187+ " \n " ,
188+ }," \n " ))
189+ elseif success == " build" then
190+ log_msg (" Built" ,fname ," ->" ,target )
191+ end
192+ end
193+
194+ io.stderr :write (" \n Quitting...\n " )
192195elseif opts .lint then
193- local has_linted_with_error ;
194- local lint = require " moonscript.cmd.lint"
195- for _ ,tuple in pairs (files )do
196- local fname = tuple [1 ]
197- local res ,err = lint .lint_file (fname )
198- if res then
199- has_linted_with_error = true
200- io.stderr :write (res .. " \n\n " )
201- elseif err then
202- has_linted_with_error = true
203- io.stderr :write (fname .. " \n " .. err .. " \n\n " )
204- end
205- end
206- if has_linted_with_error then
207- os.exit (1 )
208- end
196+ local has_linted_with_error ;
197+ local lint = require " moonscript.cmd.lint"
198+ for _ ,tuple in pairs (files )do
199+ local fname = tuple [1 ]
200+ local res ,err = lint .lint_file (fname )
201+ if res then
202+ has_linted_with_error = true
203+ io.stderr :write (res .. " \n\n " )
204+ elseif err then
205+ has_linted_with_error = true
206+ io.stderr :write (fname .. " \n " .. err .. " \n\n " )
207+ end
208+ end
209+ if has_linted_with_error then
210+ os.exit (1 )
211+ end
209212else
210- for _ ,tuple in ipairs (files )do
211- local fname ,target = util .unpack (tuple )
212- if opts .o then
213- target = opts .o
214- end
215-
216- local success ,err = compile_and_write (fname ,target , {
217- print = opts .p ,
218- fname = fname ,
219- benchmark = opts .b ,
220- show_posmap = opts .X ,
221- show_parse_tree = opts .T ,
222- transform_module = opts .transform
223- })
224-
225- if not success then
226- io.stderr :write (fname .. " \t " .. err .. " \n " )
227- os.exit (1 )
228- end
229- end
213+ for _ ,tuple in ipairs (files )do
214+ local fname ,target = util .unpack (tuple )
215+ if opts .o then
216+ target = opts .o
217+ end
218+
219+ local success ,err = compile_and_write (fname ,target , {
220+ print = opts .p ,
221+ fname = fname ,
222+ benchmark = opts .b ,
223+ show_posmap = opts .X ,
224+ show_parse_tree = opts .T ,
225+ transform_module = opts .transform
226+ })
227+
228+ if not success then
229+ io.stderr :write (fname .. " \t " .. err .. " \n " )
230+ os.exit (1 )
231+ end
232+ end
230233end
231234
232235