Skip to content

Commit

Permalink
Merge pull request #861 from nhuurre/bugfix/file-not-found
Browse files Browse the repository at this point in the history
Error for compiling non-existent file
  • Loading branch information
rok-cesnovar authored Mar 23, 2021
2 parents c738080 + bb5cd90 commit a7f4b74
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 11 deletions.
23 changes: 14 additions & 9 deletions src/frontend/Parse.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion src/middle/Errors.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "") _ =
Expand Down Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion src/middle/Errors.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions test/integration/cli-args/dune
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
2 changes: 2 additions & 0 deletions test/integration/cli-args/notfound.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$ ../../../../install/default/bin/stanc notfound.stan
Cannot not open file notfound.stan

0 comments on commit a7f4b74

Please sign in to comment.