Centralize error message construction
authorSiraaj Khandkar <siraaj@khandkar.net>
Wed, 6 Jun 2018 01:14:22 +0000 (21:14 -0400)
committerSiraaj Khandkar <siraaj@khandkar.net>
Wed, 6 Jun 2018 01:14:22 +0000 (21:14 -0400)
compiler/src/exe/tigerc.ml
compiler/src/lib/tiger/tiger.ml
compiler/src/lib/tiger/tiger_error.ml [new file with mode: 0644]
compiler/src/lib/tiger/tiger_error.mli [new file with mode: 0644]
compiler/src/lib/tiger/tiger_parser.mly
compiler/src/lib/tiger/tiger_position.ml
compiler/src/lib/tiger/tiger_position.mli

index 708ff17..42fcf00 100644 (file)
@@ -1,15 +1,17 @@
+let lexbuf_set_filename lb filename : unit =
+  let Lexing.({lex_start_p; lex_curr_p}) = lb in
+  lb.Lexing.lex_start_p <- {lex_start_p with Lexing.pos_fname = filename};
+  lb.Lexing.lex_curr_p  <- {lex_curr_p  with Lexing.pos_fname = filename}
+
 let () =
   let path_to_program_file = Sys.argv.(1) in
   let ic = open_in path_to_program_file in
   let lexbuf = Lexing.from_channel ic in
+  lexbuf_set_filename lexbuf path_to_program_file;
   (match Tiger.Parser.program Tiger.Lexer.token lexbuf with
-  | exception Parsing.Parse_error ->
-      let
-        Lexing.({lex_curr_p = {pos_lnum; pos_bol; pos_cnum; _}; _}) = lexbuf
-      in
-      Printf.printf
-        "Syntax error in file %S, around line: %d, column: %d\n"
-        path_to_program_file pos_lnum (pos_cnum - pos_bol)
+  | exception Tiger.Error.T msg ->
+      Printf.eprintf "%s\n" msg;
+      exit 1;
   | absyn ->
       print_endline (Tiger.Absyn.to_string absyn)
   );
index 08eac82..2b415b0 100644 (file)
@@ -1,4 +1,5 @@
 module Absyn  = Tiger_absyn
+module Error  = Tiger_error
 module Lexer  = Tiger_lexer
 module Parser = Tiger_parser
 module Parser_token = Tiger_parser_token
diff --git a/compiler/src/lib/tiger/tiger_error.ml b/compiler/src/lib/tiger/tiger_error.ml
new file mode 100644 (file)
index 0000000..eb2c177
--- /dev/null
@@ -0,0 +1,7 @@
+module Pos = Tiger_position
+
+exception T of string
+
+let exn ~pos ~msg =
+  let msg = Printf.sprintf "Error: %s. In %s." msg (Pos.to_string pos) in
+  raise (T msg)
diff --git a/compiler/src/lib/tiger/tiger_error.mli b/compiler/src/lib/tiger/tiger_error.mli
new file mode 100644 (file)
index 0000000..25072b4
--- /dev/null
@@ -0,0 +1,3 @@
+exception T of string
+
+val exn : pos:Tiger_position.t -> msg:string -> 'a
index 1bb9388..78d56a0 100644 (file)
 
 %%
 
-program: exp EOF { $1 };
+program:
+  | exp EOF { $1 }
+  | error {Tiger_error.exn ~pos:(pos ()) ~msg:"invalid syntax"}
+  ;
 
 exp:
   | NIL
index 01c230c..217de6b 100644 (file)
@@ -19,3 +19,15 @@ let of_lexing_positions
     ; end_char   = ecnum - ebol
     ; end_line   = eline
     }
+
+let to_string
+  { file
+  ; start_char
+  ; start_line
+  ; end_char
+  ; end_line
+  }
+=
+  Printf.sprintf
+    "file: %S, between (line/char) %d/%d and %d/%d"
+    file start_line start_char end_line end_char
index dab4067..4f9d230 100644 (file)
@@ -10,3 +10,5 @@ val of_lexing_positions
   :  pos_start:Lexing.position
   -> pos_end:Lexing.position
   -> t
+
+val to_string : t -> string
This page took 0.030198 seconds and 4 git commands to generate.