4cb18af5a0cfb81492deeb562c4e5474e19270f3
[goals.git] / src / parser.mly
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 %{
21 open Printf
22 %}
23
24 (* Tokens. *)
25 %token <Ast.substs> CODE
26 %token COLON
27 %token COMMA
28 %token <string> ID
29 %token EQUALS
30 %token EOF
31 %token GOAL
32 %token LEFT_ARRAY
33 %token LEFT_PAREN
34 %token LET
35 %token RIGHT_ARRAY
36 %token RIGHT_PAREN
37 %token <Ast.substs> STRING
38
39 %type <Ast.expr> expr
40 %type <Ast.stmt> stmt
41
42 (* Start non-terminal. *)
43 %start <Ast.file> file
44 %%
45
46 file:
47     | stmts EOF  { $1 }
48     ;
49
50 stmts:
51     | list(stmt) { $1 }
52     ;
53 stmt:
54     | option(goal_stmt) patterns COLON barelist option(CODE)
55     { let name, params =
56         match $1 with
57         | None ->
58            let pos = $startpos in
59            sprintf "_goal@%d" pos.pos_lnum, []
60         | Some x -> x in
61       Ast.Goal (name, params, $2, $4, $5)
62     }
63     | goal_stmt CODE
64     {
65       let name, params = $1 in
66       Ast.Goal (name, params, [], [], Some $2)
67     }
68     | LET ID EQUALS expr { Ast.Let ($2, $4) }
69     ;
70
71 goal_stmt:
72     | GOAL ID option(param_decl) EQUALS
73     { $2, match $3 with None -> [] | Some ps -> ps }
74     ;
75 param_decl:
76     | LEFT_PAREN separated_list(COMMA, ID) RIGHT_PAREN { $2 }
77     ;
78
79 patterns:
80     | separated_list(COMMA, pattern) { $1 }
81     ;
82 pattern:
83     | STRING     { Ast.PTactic ("file", [$1]) }
84     | ID pattern_params { Ast.PTactic ($1, $2) }
85     | ID         { Ast.PVarSubst $1 }
86     ;
87 pattern_params:
88     | LEFT_PAREN separated_list(COMMA, pattern_param) RIGHT_PAREN { $2 }
89     ;
90 pattern_param:
91     | STRING     { $1 }
92     ;
93
94 expr:
95     | ID params  { Ast.ECall ($1, $2) }
96     | ID         { Ast.EVar $1 (* This might be replaced with ECall later. *) }
97     | STRING     { Ast.EString $1 }
98     | LEFT_ARRAY barelist RIGHT_ARRAY { Ast.EList $2 }
99     ;
100 barelist:
101     | separated_list(COMMA, expr) { $1 }
102     ;
103 params:
104     | LEFT_PAREN separated_list(COMMA, expr) RIGHT_PAREN { $2 }
105     ;