Skip to content

Commit 4fd5159

Browse files
committed
Add internal bsc option ignore-parse-errors
For debugging purposes, don't stop the compiler after parse errors are encountered. This lets you take a look at the parse tree produced after parse errors, or format in the presence of errors.
1 parent e0c869a commit 4fd5159

File tree

7 files changed

+37
-23
lines changed

7 files changed

+37
-23
lines changed

jscomp/bsc/rescript_compiler_main.ml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ let process_file sourcefile ?(kind ) ppf =
7272
let sourcefile = set_abs_input_name sourcefile in
7373
setup_compiler_printer `rescript;
7474
Js_implementation.implementation
75-
~parser:Res_driver.parse_implementation
75+
~parser:(Res_driver.parse_implementation ~ignoreParseErrors:!Clflags.ignore_parse_errors)
7676
ppf sourcefile
7777
| Resi ->
7878
let sourcefile = set_abs_input_name sourcefile in
7979
setup_compiler_printer `rescript;
8080
Js_implementation.interface
81-
~parser:Res_driver.parse_interface
81+
~parser:(Res_driver.parse_interface ~ignoreParseErrors:!Clflags.ignore_parse_errors)
8282
ppf sourcefile
8383
| Intf_ast
8484
->
@@ -153,7 +153,7 @@ let format_file input =
153153
| Ml | Mli -> `ml
154154
| Res | Resi -> `res
155155
| _ -> Bsc_args.bad_arg ("don't know what to do with " ^ input) in
156-
let formatted = Res_multi_printer.print syntax ~input in
156+
let formatted = Res_multi_printer.print ~ignoreParseErrors:!Clflags.ignore_parse_errors syntax ~input in
157157
match !Clflags.output_name with
158158
| None ->
159159
output_string stdout formatted
@@ -392,6 +392,9 @@ let buckle_script_flags : (string * Bsc_args.spec * string) array =
392392
"-only-parse", set Clflags.only_parse,
393393
"*internal* stop after parsing";
394394

395+
"-ignore-parse-errors", set Clflags.ignore_parse_errors,
396+
"*internal* continue after parse errors";
397+
395398
"-where", unit_call print_standard_library,
396399
"*internal* Print location of standard library and exit";
397400

jscomp/ml/clflags.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ let dump_parsetree = ref false (* -dparsetree *)
2525
and dump_typedtree = ref false (* -dtypedtree *)
2626
and dump_rawlambda = ref false (* -drawlambda *)
2727
and dump_lambda = ref false (* -dlambda *)
28-
and only_parse = ref false (* -only-parse *)
28+
and only_parse = ref false (* -only-parse *)
29+
and ignore_parse_errors = ref false (* -ignore-parse-errors *)
2930

3031
let dont_write_files = ref false (* set to true under ocamldoc *)
3132

jscomp/ml/clflags.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ val dont_write_files : bool ref
2525
val keep_docs : bool ref
2626
val keep_locs : bool ref
2727
val only_parse : bool ref
28+
val ignore_parse_errors: bool ref
2829

2930

3031
val parse_color_setting : string -> Misc.Color.setting option

res_syntax/src/res_driver.ml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,24 +131,31 @@ let printEngine =
131131
print_string (Res_printer.printInterface ~width signature ~comments));
132132
}
133133

134-
let parse_implementation sourcefile =
134+
let parse_implementation ?(ignoreParseErrors = false) sourcefile =
135135
Location.input_name := sourcefile;
136136
let parseResult =
137137
parsingEngine.parseImplementation ~forPrinter:false ~filename:sourcefile
138138
in
139139
if parseResult.invalid then (
140140
Res_diagnostics.printReport parseResult.diagnostics parseResult.source;
141-
exit 1);
141+
if not ignoreParseErrors then exit 1);
142142
parseResult.parsetree
143143
[@@raises exit]
144144

145-
let parse_interface sourcefile =
145+
let parse_interface ?(ignoreParseErrors = false) sourcefile =
146146
Location.input_name := sourcefile;
147147
let parseResult =
148148
parsingEngine.parseInterface ~forPrinter:false ~filename:sourcefile
149149
in
150150
if parseResult.invalid then (
151151
Res_diagnostics.printReport parseResult.diagnostics parseResult.source;
152-
exit 1);
152+
if not ignoreParseErrors then exit 1);
153153
parseResult.parsetree
154154
[@@raises exit]
155+
156+
(* suppress unused optional arg *)
157+
let _ =
158+
fun s ->
159+
( parse_implementation ~ignoreParseErrors:false s,
160+
parse_interface ~ignoreParseErrors:false s )
161+
[@@raises exit]

res_syntax/src/res_driver.mli

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ val parsingEngine : Res_diagnostics.t list parsingEngine
5353
val printEngine : printEngine
5454

5555
(* ReScript implementation parsing compatible with ocaml pparse driver. Used by the compiler. *)
56-
val parse_implementation : string -> Parsetree.structure
56+
val parse_implementation :
57+
?ignoreParseErrors:bool -> string -> Parsetree.structure
5758
[@@live] [@@raises Location.Error]
5859

5960
(* ReScript interface parsing compatible with ocaml pparse driver. Used by the compiler *)
60-
val parse_interface : string -> Parsetree.signature
61+
val parse_interface : ?ignoreParseErrors:bool -> string -> Parsetree.signature
6162
[@@live] [@@raises Location.Error]

res_syntax/src/res_multi_printer.ml

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
let defaultPrintWidth = 100
22

33
(* print res files to res syntax *)
4-
let printRes ~isInterface ~filename =
5-
if isInterface then
4+
let printRes ~ignoreParseErrors ~isInterface ~filename =
5+
if isInterface then (
66
let parseResult =
77
Res_driver.parsingEngine.parseInterface ~forPrinter:true ~filename
88
in
99
if parseResult.invalid then (
1010
Res_diagnostics.printReport parseResult.diagnostics parseResult.source;
11-
exit 1)
12-
else
13-
Res_printer.printInterface ~width:defaultPrintWidth
14-
~comments:parseResult.comments parseResult.parsetree
11+
if not ignoreParseErrors then exit 1);
12+
Res_printer.printInterface ~width:defaultPrintWidth
13+
~comments:parseResult.comments parseResult.parsetree)
1514
else
1615
let parseResult =
1716
Res_driver.parsingEngine.parseImplementation ~forPrinter:true ~filename
1817
in
1918
if parseResult.invalid then (
2019
Res_diagnostics.printReport parseResult.diagnostics parseResult.source;
21-
exit 1)
22-
else
23-
Res_printer.printImplementation ~width:defaultPrintWidth
24-
~comments:parseResult.comments parseResult.parsetree
20+
if not ignoreParseErrors then exit 1);
21+
Res_printer.printImplementation ~width:defaultPrintWidth
22+
~comments:parseResult.comments parseResult.parsetree
2523
[@@raises exit]
2624

2725
(* print ocaml files to res syntax *)
@@ -42,12 +40,15 @@ let printMl ~isInterface ~filename =
4240
~comments:parseResult.comments parseResult.parsetree
4341

4442
(* print the given file named input to from "language" to res, general interface exposed by the compiler *)
45-
let print language ~input =
43+
let print ?(ignoreParseErrors = false) language ~input =
4644
let isInterface =
4745
let len = String.length input in
4846
len > 0 && String.unsafe_get input (len - 1) = 'i'
4947
in
5048
match language with
51-
| `res -> printRes ~isInterface ~filename:input
49+
| `res -> printRes ~ignoreParseErrors ~isInterface ~filename:input
5250
| `ml -> printMl ~isInterface ~filename:input
5351
[@@raises exit]
52+
53+
(* suppress unused optional arg *)
54+
let _ = fun s -> print ~ignoreParseErrors:false s [@@raises exit]

res_syntax/src/res_multi_printer.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
(* Interface to print source code from different languages to res.
22
* Takes a filename called "input" and returns the corresponding formatted res syntax *)
3-
val print : [`ml | `res] -> input:string -> string
3+
val print : ?ignoreParseErrors:bool -> [`ml | `res] -> input:string -> string

0 commit comments

Comments
 (0)