aboutsummaryrefslogtreecommitdiff
path: root/coqpp/coqpp_parser.ml
diff options
context:
space:
mode:
authorThéo Zimmermann2019-07-20 12:50:17 +0200
committerThéo Zimmermann2019-07-20 12:50:17 +0200
commitcd6fc50854285f02bf151e94bdfb819988531fd2 (patch)
tree798fcfd6a0bd529a3d8ae8a25e1c1e62b728be26 /coqpp/coqpp_parser.ml
parentc80dfb6bd8ff8625ced2cae8b6789707f904a118 (diff)
parentcf868740c3d18ee9ce9a6b38dd617784625a3cae (diff)
Merge PR #9884: doc_grammar, a utility to extract Coq's grammar from .mlg files and insert it into .rst files
Ack-by: Zimmi48 Ack-by: gares Ack-by: ppedrot
Diffstat (limited to 'coqpp/coqpp_parser.ml')
-rw-r--r--coqpp/coqpp_parser.ml44
1 files changed, 44 insertions, 0 deletions
diff --git a/coqpp/coqpp_parser.ml b/coqpp/coqpp_parser.ml
new file mode 100644
index 0000000000..1fa47f554c
--- /dev/null
+++ b/coqpp/coqpp_parser.ml
@@ -0,0 +1,44 @@
+(************************************************************************)
+(* * The Coq Proof Assistant / The Coq Development Team *)
+(* v * INRIA, CNRS and contributors - Copyright 1999-2019 *)
+(* <O___,, * (see CREDITS file for the list of authors) *)
+(* \VV/ **************************************************************)
+(* // * This file is distributed under the terms of the *)
+(* * GNU Lesser General Public License Version 2.1 *)
+(* * (see LICENSE file for the text of the license) *)
+(************************************************************************)
+
+open Lexing
+open Coqpp_ast
+
+let pr_loc loc =
+ let file = loc.loc_start.pos_fname in
+ let line = loc.loc_start.pos_lnum in
+ let bpos = loc.loc_start.pos_cnum - loc.loc_start.pos_bol in
+ let epos = loc.loc_end.pos_cnum - loc.loc_start.pos_bol in
+ Printf.sprintf "File \"%s\", line %d, characters %d-%d:" file line bpos epos
+
+
+let fatal msg =
+ let () = Format.eprintf "Error: %s@\n%!" msg in
+ exit 1
+
+let parse_file f =
+ let chan = open_in f in
+ let lexbuf = Lexing.from_channel chan in
+ let () = lexbuf.lex_curr_p <- { lexbuf.lex_curr_p with pos_fname = f } in
+ let ans =
+ try Coqpp_parse.file Coqpp_lex.token lexbuf
+ with
+ | Coqpp_lex.Lex_error (loc, msg) ->
+ let () = close_in chan in
+ let () = Printf.eprintf "%s\n%!" (pr_loc loc) in
+ fatal msg
+ | Parsing.Parse_error ->
+ let () = close_in chan in
+ let loc = Coqpp_lex.loc lexbuf in
+ let () = Printf.eprintf "%s\n%!" (pr_loc loc) in
+ fatal "syntax error"
+ in
+ let () = close_in chan in
+ ans