Skip to content

Commit

Permalink
one single error for all parse errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Niols authored and benozol committed Oct 17, 2018
1 parent da597b4 commit 6506be7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
25 changes: 22 additions & 3 deletions src/colis.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@ module FromShell = FromShell

(* CoLiS *)

exception ParseError of string
exception ConversionError of string

type colis = AST.program

let colis_from_lexbuf ?(filename="-") lexbuf =
lexbuf.Lexing.lex_curr_p <-
{ lexbuf.Lexing.lex_curr_p
with Lexing.pos_fname = filename };
ColisParser.program ColisLexer.token lexbuf
try
ColisParser.program ColisLexer.token lexbuf
with
| ColisLexer.LexerError s ->
raise (ParseError s)
| ColisParser.Error ->
raise (ParseError "")

let colis_from_channel ?(filename="-") channel =
let lexbuf = Lexing.from_channel channel in
Expand Down Expand Up @@ -53,9 +62,19 @@ let colis_to_file filename colis =

type shell = Morsmall.AST.program

let shell_from_file = Morsmall.parse_file
let shell_from_file file =
try
Morsmall.parse_file file
with
Morsmall.SyntaxError _pos ->
raise (ParseError "")

let shell_to_colis = FromShell.program__to__program
let shell_to_colis shell =
try
FromShell.program__to__program shell
with
FromShell.Unsupported feat ->
raise (ConversionError ("unsupported feature: " ^ feat))

(* Interpret *)

Expand Down
17 changes: 9 additions & 8 deletions src/colis.mli
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,28 @@ type shell = Morsmall.AST.program

(** {2 Parsing} *)

exception ParseError of string (* FIXME: and position *)

val colis_from_channel : ?filename:string -> in_channel -> colis
(** Reads Colis syntax from a channel and returns the corresponding AST.
@raises {!ColisLexer.LexerError}
@raises {!ColisParser.Error} *)
@raises {!ParseError} *)

val colis_from_file : string -> colis
(** Reads Colis syntax from a file and returns the corresponding AST.
@raises {!ColisLexer.LexerError}
@raises {!ColisParser.Error} *)
@raises {!ParseError} *)

val colis_from_string : string -> colis
(** Reads Colis syntax from a string and returns the corresponding AST.
@raises {!ColisLexer.LexerError}
@raises {!ColisParser.Error} *)
@raises {!ParseError} *)

val shell_from_file : string -> shell
(** Reads a Shell file and returns the corresponding AST. This is a
wrapper around Morsmall.parse_file.
@raises Morsmall.SyntaxError *)
@raises {!ParseError} *)

(** {2 Printing} *)

Expand All @@ -53,10 +52,12 @@ val pp_print_colis : Format.formatter -> colis -> unit

(** {2 Converting} *)

exception ConversionError of string

val shell_to_colis : shell -> colis
(** Converts a Shell program to a Colis program.
@raises {!FromShell.Unsupported} *)
@raises {!ConversionError} *)

(** {2 Interpreting} *)

Expand Down
14 changes: 4 additions & 10 deletions src/colis_cmd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,11 @@ let main () =
| Shell -> Colis.(shell_from_file ||> shell_to_colis)
)
with
| Colis.ColisLexer.LexerError s ->
eprintf "Lexing error: %s@." s;
| Colis.ParseError msg ->
eprintf "Parse error: %s@." msg;
exit 2
| Colis.ColisParser.Error ->
eprintf "Parsing error@.";
exit 2
| Morsmall.SyntaxError _pos ->
eprintf "Syntax error@.";
exit 2
| Colis.FromShell.Unsupported feat ->
eprintf "Unsupported feature: %s@." feat;
| Colis.ConversionError msg ->
eprintf "Conversion error: %s@." msg;
exit 3
in
match get_action () with
Expand Down

0 comments on commit 6506be7

Please sign in to comment.