Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 2a4cd8e

Browse files
committed
Make CLI flags more rigorous
See the screenshot attached to the PR for more details. And @IwanKaramazow's upcoming explanations of `forPrinter`'s role in this
1 parent 756b07a commit 2a4cd8e

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ build-native: lib/refmt.exe lib/rescript.exe depend
5151

5252
bootstrap: build-native
5353
ocaml unix.cma ./scripts/bspack.ml -bs-main Res_cli -I src -o ./lib/rescript.ml
54-
./lib/rescript.exe -parse ml -print ns ./lib/rescript.ml > ./lib/rescript2.ml
54+
./lib/rescript.exe -parse ml -print res ./lib/rescript.ml > ./lib/rescript2.ml
5555
$(OCAMLOPT) -w a -pp "./lib/rescript.exe -print binary" -O2 -o ./lib/rescript2.exe -I +compiler-libs ocamlcommon.cmxa -I lib ./lib/rescript2.ml
5656
mv ./lib/rescript2.exe ./lib/rescript.exe
5757

src/res_cli.ml

+27-25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(*
22
This CLI isn't used apart for this repo's testing purposes. The syntax
3-
itself is used by BS programmatically through various other apis.
3+
itself is used by ReScript's compiler programmatically through various other apis.
44
*)
55

66
(*
@@ -21,8 +21,8 @@
2121
But when used by this cli file, that coloring logic doesn't render properly
2222
because we're compiling against vanilla OCaml 4.06 instead of ReScript's
2323
OCaml fork. For example, the vanilla compiler doesn't support the `dim`
24-
color (grey). So we emulate the right coloring logic by copy pasting how BS'
25-
OCaml does it.
24+
color (grey). So we emulate the right coloring logic by copy pasting how our
25+
forked OCaml compiler does it.
2626
*)
2727
module Color = struct
2828
(* use ANSI color codes, see https://en.wikipedia.org/wiki/ANSI_escape_code *)
@@ -164,7 +164,6 @@ module ResClflags: sig
164164
val origin: string ref
165165
val files: string list ref
166166
val interface: bool ref
167-
val report: string ref
168167
val ppx: string ref
169168

170169
val parse: unit -> unit
@@ -175,27 +174,26 @@ end = struct
175174
let files = ref []
176175
let addFilename filename = files := filename::(!files)
177176

178-
let print = ref ""
179-
let origin = ref ""
177+
let print = ref "res"
178+
let origin = ref "res"
180179
let interface = ref false
181-
let report = ref "pretty"
182180
let ppx = ref ""
183181

184-
let usage = "Usage:\n rescript <options> <file>\n\n" ^
182+
let usage = "\n**This command line is for the repo developer's testing purpose only. DO NOT use it in production**!\n\n" ^
183+
"Usage:\n rescript <options> <file>\n\n" ^
185184
"Examples:\n" ^
186185
" rescript myFile.res\n" ^
187-
" rescript -parse ml -print ns myFile.ml\n" ^
188-
" rescript -parse ns -print binary -interface myFile.resi\n\n" ^
186+
" rescript -parse ml -print res myFile.ml\n" ^
187+
" rescript -parse res -print binary -interface myFile.resi\n\n" ^
189188
"Options are:"
190189

191190
let spec = [
192191
("-recover", Arg.Unit (fun () -> recover := true), "Emit partial ast");
193-
("-parse", Arg.String (fun txt -> origin := txt), "Parse reasonBinary, ml or ns. Default: ns");
194-
("-print", Arg.String (fun txt -> print := txt), "Print either binary or ns. Default: ns");
192+
("-parse", Arg.String (fun txt -> origin := txt), "Parse reasonBinary, ml or res. Default: res");
193+
("-print", Arg.String (fun txt -> print := txt), "Print either binary, ml, ast, sexp or res. Default: res");
195194
("-width", Arg.Int (fun w -> width := w), "Specify the line length for the printer (formatter)");
196195
("-interface", Arg.Unit (fun () -> interface := true), "Parse as interface");
197196
("-ppx", Arg.String (fun txt -> ppx := txt), "Apply a specific built-in ppx before parsing, none or jsx. Default: none");
198-
(* ("-report", Arg.String (fun txt -> report := txt), "Stylize errors and messages using color and context. Accepts `Pretty` and `Plain`. Default `Plain`") *)
199197
]
200198

201199
let parse () = Arg.parse spec addFilename usage
@@ -204,29 +202,35 @@ end
204202
module CliArgProcessor = struct
205203
type backend = Parser: ('diagnostics) Res_driver.parsingEngine -> backend [@@unboxed]
206204

207-
let processFile ~isInterface ~width ~recover ~origin ~target ~report:_ ~ppx filename =
205+
let processFile ~isInterface ~width ~recover ~origin ~target ~ppx filename =
208206
try
209207
let len = String.length filename in
210208
let processInterface =
211209
isInterface || len > 0 && (String.get [@doesNotRaise]) filename (len - 1) = 'i'
212210
in
213211
let parsingEngine =
214-
match origin with
215-
| "reasonBinary" -> Parser Res_driver_reason_binary.parsingEngine
216-
| "ml" | "ocaml" -> Parser Res_driver_ml_parser.parsingEngine
217-
| _ -> Parser Res_driver.parsingEngine
212+
match origin with
213+
| "reasonBinary" -> Parser Res_driver_reason_binary.parsingEngine
214+
| "ml" -> Parser Res_driver_ml_parser.parsingEngine
215+
| "res" -> Parser Res_driver.parsingEngine
216+
| origin ->
217+
print_endline ("-parse needs to be either reasonBinary, ml or res. You provided " ^ origin);
218+
exit 1
218219
in
219220
let printEngine =
220221
match target with
221-
| "ml" | "ocaml" -> Res_driver_ml_parser.printEngine
222+
| "binary" -> Res_driver_binary.printEngine
223+
| "ml" -> Res_driver_ml_parser.printEngine
222224
| "ast" -> Res_ast_debugger.printEngine
223225
| "sexp" -> Res_ast_debugger.sexpPrintEngine
224-
| "binary" -> Res_driver_binary.printEngine
225-
| _ -> Res_driver.printEngine
226+
| "res" -> Res_driver.printEngine
227+
| target ->
228+
print_endline ("-print needs to be either binary, ml, ast, sexp or res. You provided " ^ target);
229+
exit 1
226230
in
227231

228232
let forPrinter = match target with
229-
| "res" | "rescript" | "sexp" -> true
233+
| "res" | "sexp" -> true
230234
| _ -> false
231235
in
232236

@@ -239,7 +243,7 @@ module CliArgProcessor = struct
239243
backend.stringOfDiagnostics
240244
~source:parseResult.source
241245
~filename:parseResult.filename
242-
parseResult.diagnostics;
246+
parseResult.diagnostics;
243247
if recover then
244248
printEngine.printInterface
245249
~width ~filename ~comments:parseResult.comments parseResult.parsetree
@@ -292,7 +296,6 @@ let [@raises Invalid_argument, exit] () =
292296
~recover:!ResClflags.recover
293297
~target:!ResClflags.print
294298
~origin:!ResClflags.origin
295-
~report:!ResClflags.report
296299
~ppx:!ResClflags.ppx
297300
""
298301
| files ->
@@ -303,7 +306,6 @@ let [@raises Invalid_argument, exit] () =
303306
~recover:!ResClflags.recover
304307
~target:!ResClflags.print
305308
~origin:!ResClflags.origin
306-
~report:!ResClflags.report
307309
~ppx:!ResClflags.ppx
308310
filename
309311
) files

0 commit comments

Comments
 (0)