Implement -include (optional include) command.
[goals.git] / src / parse.ml
1 (* Goalfile parser
2  * Copyright (C) 2019 Richard W.M. Jones
3  * Copyright (C) 2019 Red Hat Inc.
4  *
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.
9  *
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.
14  *
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.
18  *)
19
20 open Lexer
21 open Lexing
22
23 open Printf
24
25 let () =
26   Parser.lexer_read := Some Lexer.read
27
28 let print_position fp lexbuf =
29   let pos = lexbuf.lex_curr_p in
30   fprintf fp "%s:%d:%d"
31     pos.pos_fname pos.pos_lnum (pos.pos_cnum - pos.pos_bol)
32
33 let parse_file env lexbuf =
34   try
35     let env' = Parser.file Lexer.read lexbuf in
36     Ast.Env.merge env env'
37   with
38   | SyntaxError msg ->
39      eprintf "%a: %s\n" print_position lexbuf msg;
40      exit 1
41   | Parser.Error ->
42      eprintf "%a: parse error\n" print_position lexbuf;
43      exit 1
44
45 let parse_expr lexbuf =
46   try Parser.expr Lexer.read lexbuf
47   with
48   | SyntaxError msg ->
49      eprintf "%a: %s\n" print_position lexbuf msg;
50      exit 1
51   | Parser.Error ->
52      eprintf "%a: parse error\n" print_position lexbuf;
53      exit 1
54
55 (* This is used to parse the Goalfile. *)
56 let parse_goalfile env filename =
57   Cmdline.debug "parse file: %s" filename;
58   let fp = open_in filename in
59   let lexbuf = Lexing.from_channel fp in
60   lexbuf.lex_curr_p <- { lexbuf.lex_curr_p with pos_fname = filename };
61   let env' = parse_file env lexbuf in
62   close_in fp;
63   env'
64
65 (* This is used to parse dependency expressions on the command line. *)
66 let parse_cli_expr str =
67   Cmdline.debug "parsing from command line: %S" str;
68   let lexbuf = Lexing.from_string str in
69   lexbuf.lex_curr_p <- { lexbuf.lex_curr_p with pos_fname = "<command line>" };
70   parse_expr lexbuf