2 * Copyright (C) 2019 Richard W.M. Jones
3 * Copyright (C) 2019 Red Hat Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 let print_position fp lexbuf =
26 let pos = lexbuf.lex_curr_p in
28 pos.pos_fname pos.pos_lnum (pos.pos_cnum - pos.pos_bol)
30 let parse_file env lexbuf =
32 let env' = Parser.file Lexer.read lexbuf in
33 Ast.Env.merge env env'
36 eprintf "%a: %s\n" print_position lexbuf msg;
39 eprintf "%a: parse error\n" print_position lexbuf;
42 let parse_expr lexbuf =
43 try Parser.expr Lexer.read lexbuf
46 eprintf "%a: %s\n" print_position lexbuf msg;
49 eprintf "%a: parse error\n" print_position lexbuf;
52 (* This is used to parse the Goalfile. *)
53 let parse_goalfile env filename =
54 Cmdline.debug "parse file: %s" filename;
55 let fp = open_in filename in
56 let lexbuf = Lexing.from_channel ~with_positions:true fp in
57 let pos = lexbuf.lex_curr_p in
58 lexbuf.lex_curr_p <- { pos with pos_fname = filename };
59 let env' = parse_file env lexbuf in
63 (* This is used to parse expressions on the command line and
64 * the output from functions.
66 let parse_expr source str =
67 Cmdline.debug "parse expression: %S" str;
68 let lexbuf = Lexing.from_string ~with_positions:true str in
69 let pos = lexbuf.lex_curr_p in
70 lexbuf.lex_curr_p <- { pos with pos_fname = source };