dist: Add extra files to tarball.
[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 Lexing
21 open Printf
22
23 open Utils
24 open Lexer
25
26 let string_position () lexbuf =
27   let pos = lexbuf.lex_curr_p in
28   sprintf "%s:%d:%d" pos.pos_fname pos.pos_lnum (pos.pos_cnum - pos.pos_bol)
29
30 let parse_file env lexbuf =
31   try
32     let env' = Parser.file Lexer.read lexbuf in
33     Ast.Env.merge env env'
34   with
35   | SyntaxError msg ->
36      failwithf "%a: %s" string_position lexbuf msg
37   | Parser.Error ->
38      failwithf "%a: parse error" string_position lexbuf
39
40 let parse_expr lexbuf =
41   try Parser.expr_only Lexer.read lexbuf
42   with
43   | SyntaxError msg ->
44      failwithf "%a: %s" string_position lexbuf msg
45   | Parser.Error ->
46      failwithf "%a: parse error" string_position lexbuf
47
48 (* This is used to parse the Goalfile. *)
49 let parse_goalfile env filename =
50   Cmdline.debug "parse file: %s" filename;
51   let fp = open_in filename in
52   let lexbuf = Lexing.from_channel ~with_positions:true fp in
53   let pos = lexbuf.lex_curr_p in
54   lexbuf.lex_curr_p <- { pos with pos_fname = filename };
55   let env' = parse_file env lexbuf in
56   close_in fp;
57   env'
58
59 (* This is used to parse expressions on the command line and
60  * the output from functions.
61  *)
62 let parse_expr source str =
63   Cmdline.debug "parse expression: %S" str;
64   let lexbuf = Lexing.from_string ~with_positions:true str in
65   let pos = lexbuf.lex_curr_p in
66   lexbuf.lex_curr_p <- { pos with pos_fname = source };
67   parse_expr lexbuf