From bb5cd9074a70c5bc8713e90362dd84676f91d5d6 Mon Sep 17 00:00:00 2001 From: Niko Huurre Date: Tue, 23 Mar 2021 14:57:19 +0200 Subject: [PATCH] handle file not found error --- src/frontend/Parse.ml | 23 +++++++++++++-------- src/middle/Errors.ml | 6 +++++- src/middle/Errors.mli | 5 ++++- test/integration/cli-args/dune | 11 ++++++++++ test/integration/cli-args/notfound.expected | 2 ++ 5 files changed, 36 insertions(+), 11 deletions(-) create mode 100644 test/integration/cli-args/notfound.expected diff --git a/src/frontend/Parse.ml b/src/frontend/Parse.ml index c1bcc2fa9e..6a9c06f401 100644 --- a/src/frontend/Parse.ml +++ b/src/frontend/Parse.ml @@ -71,13 +71,18 @@ let parse_string parse_fun str = parse parse_fun lexbuf let parse_file parse_fun path = - let chan = In_channel.create path in - let lexbuf = - let open Lexing in - let lexbuf = from_channel chan in - lexbuf.lex_start_p - <- {pos_fname= path; pos_lnum= 1; pos_bol= 0; pos_cnum= 0} ; - lexbuf.lex_curr_p <- lexbuf.lex_start_p ; - lexbuf + let chan = + try Ok (In_channel.create path) with _ -> Error (Errors.FileNotFound path) in - parse parse_fun lexbuf + match chan with + | Error err -> (Error err, []) + | Ok chan -> + let lexbuf = + let open Lexing in + let lexbuf = from_channel chan in + lexbuf.lex_start_p + <- {pos_fname= path; pos_lnum= 1; pos_bol= 0; pos_cnum= 0} ; + lexbuf.lex_curr_p <- lexbuf.lex_start_p ; + lexbuf + in + parse parse_fun lexbuf diff --git a/src/middle/Errors.ml b/src/middle/Errors.ml index 9b26186c49..e90e04f17c 100644 --- a/src/middle/Errors.ml +++ b/src/middle/Errors.ml @@ -20,7 +20,10 @@ exception SemanticError of Semantic_error.t [msg]. *) exception FatalError of string -type t = Syntax_error of syntax_error | Semantic_error of Semantic_error.t +type t = + | FileNotFound of string + | Syntax_error of syntax_error + | Semantic_error of Semantic_error.t (* A fatal error reported by the toplevel *) let fatal_error ?(msg = "") _ = @@ -57,6 +60,7 @@ let pp_syntax_error ?printed_filename ppf = function pp_context_with_message (message, loc) let pp ?printed_filename ppf = function + | FileNotFound f -> Fmt.pf ppf "Cannot not open file %s@." f | Syntax_error e -> pp_syntax_error ?printed_filename ppf e | Semantic_error e -> pp_semantic_error ?printed_filename ppf e diff --git a/src/middle/Errors.mli b/src/middle/Errors.mli index d5437cfbab..8b3cc78d91 100644 --- a/src/middle/Errors.mli +++ b/src/middle/Errors.mli @@ -13,7 +13,10 @@ exception SyntaxError of syntax_error [msg], occurring at location [loc]. *) exception SemanticError of Semantic_error.t -type t = Syntax_error of syntax_error | Semantic_error of Semantic_error.t +type t = + | FileNotFound of string + | Syntax_error of syntax_error + | Semantic_error of Semantic_error.t val pp : ?printed_filename:string -> t Fmt.t val to_string : t -> string diff --git a/test/integration/cli-args/dune b/test/integration/cli-args/dune index 7b505903e8..c212415a31 100644 --- a/test/integration/cli-args/dune +++ b/test/integration/cli-args/dune @@ -30,3 +30,14 @@ (alias (name runtest) (action (diff info.expected info.output))) + +(rule + (targets notfound.output) + (deps (package stanc)) + (action + (with-stdout-to %{targets} + (run %{bin:run_bin_on_args} "%{bin:stanc}" notfound.stan)))) + +(alias + (name runtest) + (action (diff notfound.expected notfound.output))) \ No newline at end of file diff --git a/test/integration/cli-args/notfound.expected b/test/integration/cli-args/notfound.expected new file mode 100644 index 0000000000..9163338935 --- /dev/null +++ b/test/integration/cli-args/notfound.expected @@ -0,0 +1,2 @@ + $ ../../../../install/default/bin/stanc notfound.stan +Cannot not open file notfound.stan