@@ -6,38 +6,41 @@ local lfs = require "lfs"
6
6
local parser = argparse ()
7
7
8
8
parser :flag (" -l --lint" , " Perform a lint on the file instead of compiling" )
9
+
9
10
parser :flag (" -v --version" , " Print version" )
10
11
parser :flag (" -w --watch" , " Watch file/directory for updates" )
11
12
parser :option (" --transform" , " Transform syntax tree with module" )
13
+
12
14
parser :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)" )
19
21
)
22
+
20
23
parser :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)" )
22
25
23
26
local read_stdin = arg [1 ] == " --" -- luacheck: ignore 113
24
27
25
28
if not read_stdin then
26
- parser :argument (" file/directory" ):args (" +" )
29
+ parser :argument (" file/directory" ):args (" +" )
27
30
end
28
31
29
32
local opts = parser :parse ()
30
33
31
34
if 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 ()
35
38
end
36
39
37
40
function 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
41
44
end
42
45
43
46
local moonc = require (" moonscript.cmd.moonc" )
@@ -47,186 +50,186 @@ local compile_and_write = moonc.compile_and_write
47
50
local path_to_target = moonc .path_to_target
48
51
49
52
local 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
66
69
end
67
70
68
71
local 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
81
84
end
82
85
83
86
-- creates tuples of input and target
84
87
local 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
102
105
end
103
106
104
107
if 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"
107
110
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 )
110
113
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 )
113
116
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
117
120
118
- print (code )
119
- os.exit ()
121
+ print (code )
122
+ os.exit ()
120
123
end
121
124
122
125
local inputs = opts [" file/directory" ]
123
126
124
127
local files = {}
125
128
for _ , input in ipairs (inputs ) do
126
- get_files (input , files )
129
+ get_files (input , files )
127
130
end
128
131
129
132
files = remove_dups (files , function (f )
130
- return f [2 ]
133
+ return f [2 ]
131
134
end )
132
135
133
136
-- returns an iterator that returns files that have been updated
134
137
local function create_watcher (files )
135
- local watchers = require (" moonscript.cmd.watchers" )
138
+ local watchers = require (" moonscript.cmd.watchers" )
136
139
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
140
143
141
- return watchers .SleepWatcher (files ):each_update ()
144
+ return watchers .SleepWatcher (files ):each_update ()
142
145
end
143
146
144
147
if 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 " )
192
195
elseif 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
209
212
else
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
230
233
end
231
234
232
235
0 commit comments