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.
24 (* This is initialized with Lexer.read once the program
25 * starts. Doing this avoids a circular dependency caused
28 let lexer_read = ref None
30 let find_on_include_path filename =
31 if not (Filename.is_implicit filename) then filename
33 let rec loop = function
36 let path = inc // filename in
37 if Sys.file_exists path then path else loop incs
42 let do_include env loc filename optflag file =
43 let filename = Ast.substitute env loc filename in
44 let filename = find_on_include_path filename in
45 if optflag && not (Sys.file_exists filename) then env
47 let fp = open_in filename in
48 let lexbuf = Lexing.from_channel fp in
49 lexbuf.lex_curr_p <- { lexbuf.lex_curr_p with pos_fname = filename };
51 match !lexer_read with None -> assert false | Some r -> r in
52 let env' = file reader lexbuf in
54 Ast.Env.merge env env'
59 %token <Ast.substs> CODE
74 %token <Ast.substs> STRING
75 %token <string> TACTIC
78 (* Start nonterminals. *)
79 %start <Ast.expr Ast.Env.t> file
80 %start <Ast.expr> expr
88 | (* none *) { Ast.Env.empty }
89 | stmts INCLUDE STRING option(SEMICOLON)
90 { do_include $1 $loc $3 false file }
91 | stmts OPTINCLUDE STRING option(SEMICOLON)
92 { do_include $1 $loc $3 true file }
93 | stmts stmt option(SEMICOLON)
94 { let name, expr = $2 in Ast.Env.add name expr $1 }
98 | option(goal_stmt) patterns COLON barelist option(CODE)
102 sprintf "_goal@%d" $startpos.pos_lnum, []
104 name, Ast.EGoalDefn ($loc, (params, $2, $4, $5))
108 let name, params = $1 in
109 name, Ast.EGoalDefn ($loc, (params, [], [], Some $2))
111 | TACTIC_KEYWORD TACTIC params_decl EQUALS CODE
113 $2, Ast.ETacticDefn ($loc, ($3, $5))
115 | LET ID EQUALS expr { $2, $4 }
119 | GOAL ID option(params_decl) EQUALS
120 { $2, match $3 with None -> [] | Some ps -> ps }
123 | LEFT_PAREN separated_list(COMMA, param_decl) RIGHT_PAREN { $2 }
129 | separated_list(COMMA, pattern) { $1 }
132 | STRING { Ast.PTactic ($loc, "*file", [$1]) }
133 | ID pattern_params { Ast.PTactic ($loc, $1, $2) }
136 | LEFT_PAREN separated_list(COMMA, pattern_param) RIGHT_PAREN { $2 }
143 | ID params { Ast.ECallGoal ($loc, $1, $2) }
144 | ID { Ast.EVar ($loc, $1) }
145 | TACTIC params { Ast.ETacticCtor ($loc, $1, $2) }
146 | STRING { Ast.ESubsts ($loc, $1) }
147 | LEFT_ARRAY barelist RIGHT_ARRAY { Ast.EList ($loc, $2) }
150 | separated_list(COMMA, expr) { $1 }
153 | LEFT_PAREN separated_list(COMMA, expr) RIGHT_PAREN { $2 }