summaryrefslogtreecommitdiff
path: root/frontend/file_parser.ml
blob: fa7ff7d25e6170975db035b9303b06b2f2fc2b74 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
(*
  Cours "Sémantique et Application à la Vérification de programmes"
  
  Antoine Miné 2014
  Ecole normale supérieure, Paris, France / CNRS / INRIA
*)

(*
  Opens and parses a file given as argument.
*)

open Abstract_syntax_tree
open Abstract_syntax_printer
open Lexing

(* parsing, with nice error messages *)

let parse_file (filename:string) : prog =
  let f = open_in filename in
  let lex = from_channel f in
  try
    lex.lex_curr_p <- { lex.lex_curr_p with pos_fname = filename; };
    Parser.file Lexer.token lex
  with
  | Parser.Error ->
      Printf.eprintf "Parse error (invalid syntax) near %s\n" 
        (string_of_position lex.lex_start_p);
      failwith "Parse error"
  | Failure "lexing: empty token" ->
      Printf.eprintf "Parse error (invalid token) near %s\n" 
        (string_of_position lex.lex_start_p);
      failwith "Parse error"