Add if..then..else
[hcoop/domtool2.git] / src / domtool.grm
... / ...
CommitLineData
1(* HCoop Domtool (http://hcoop.sourceforge.net/)
2 * Copyright (c) 2006, Adam Chlipala
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 *)
18
19(* Parser for Domtool configuration files *)
20
21open Ast
22
23%%
24%header (functor DomtoolLrValsFn(structure Token : TOKEN))
25
26%term
27 EOF
28 | SYMBOL of string | CSYMBOL of string
29 | STRING of string | DOC of string
30 | INT of int
31 | ARROW | DARROW | LARROW
32 | COLON | CARET | BANG | AND
33 | LPAREN | RPAREN | LBRACK | RBRACK | LBRACE | RBRACE
34 | EQ | COMMA | BSLASH | BSLASHBSLASH | SEMI | LET | IN | BEGIN | END
35 | IF | THEN | ELSE
36 | ROOT
37 | EXTERN | TYPE | VAL | WITH | WHERE | CONTEXT
38
39%nonterm
40 file of file
41 | decls of decl list
42 | decl of decl
43 | decl' of decl'
44 | docOpt of string option
45 | expOpt of exp option
46 | exp of exp
47 | apps of exp
48 | term of exp
49 | sets of exp list
50 | elist of exp list
51 | elistNe of exp list
52 | clist of exp list
53 | typ of typ
54 | ctxt of pred
55 | recd of record
56 | recdNe of record
57 | SEMIopt of unit
58
59%verbose (* print summary of errors *)
60%pos int (* positions *)
61%start file
62%pure
63%eop EOF
64%noshift EOF
65
66%name Domtool
67
68%right SEMI
69%nonassoc COLON
70%nonassoc IN
71%right ARROW DARROW
72%right COMMA
73%nonassoc EQ
74%right WITH
75%right WHERE
76%right AND
77%nonassoc CARET BANG
78
79%%
80
81file : docOpt decls expOpt (docOpt, decls, expOpt)
82
83decls : ([])
84 | decl decls (decl :: decls)
85
86decl : decl' SEMI docOpt (decl', docOpt, (decl'left, docOptright))
87
88decl' : EXTERN TYPE SYMBOL (DExternType SYMBOL)
89 | EXTERN VAL SYMBOL COLON typ (DExternVal (SYMBOL, typ))
90 | VAL SYMBOL EQ exp (DVal (SYMBOL, NONE, exp))
91 | VAL SYMBOL COLON typ EQ exp (DVal (SYMBOL, SOME typ, exp))
92 | CONTEXT CSYMBOL (DContext CSYMBOL)
93
94docOpt : (NONE)
95 | DOC (SOME DOC)
96
97expOpt : (NONE)
98 | exp (SOME (ELocal (exp, (ESkip, (expleft, expright))),
99 (expleft, expright)))
100
101
102exp : apps (apps)
103 | apps WHERE sets END (ELocal ((ESeq sets, (setsleft, setsright)), apps),
104 (appsleft, ENDright))
105 | apps WITH END (EWith (apps, (ESkip, (WITHleft, ENDright))),
106 (appsleft, ENDright))
107 | apps WITH exp END (EWith (apps, exp), (appsleft, ENDright))
108 | apps WHERE sets WITH END (ELocal ((ESeq sets, (setsleft, setsright)),
109 (EWith (apps, (ESkip, (WITHleft, ENDright))),
110 (appsleft, ENDright))),
111 (appsleft, ENDright))
112 | apps WHERE sets WITH exp END (ELocal ((ESeq sets, (setsleft, setsright)),
113 (EWith (apps, exp), (appsleft, ENDright))),
114 (appsleft, ENDright))
115
116 | BSLASH SYMBOL COLON LPAREN typ RPAREN ARROW exp (ELam (SYMBOL, SOME typ, exp),
117 (BSLASHleft, expright))
118 | BSLASH SYMBOL ARROW exp (ELam (SYMBOL, NONE, exp), (BSLASHleft, expright))
119 | BSLASHBSLASH SYMBOL COLON ctxt ARROW exp (EALam (SYMBOL, ctxt, exp),
120 (BSLASHBSLASHleft, expright))
121 | CSYMBOL EQ exp (ESet (CSYMBOL, exp), (CSYMBOLleft, expright))
122 | exp SEMI exp (let
123 val ls = case #1 exp2 of
124 ESeq ls => exp :: ls
125 | _ => [exp1, exp2]
126 in
127 (ESeq ls, (exp1left, exp2right))
128 end)
129 | exp SEMI (exp)
130 | SYMBOL LARROW CSYMBOL SEMI exp (EGet (SYMBOL, NONE, CSYMBOL, exp), (SYMBOLleft, expright))
131 | SYMBOL COLON typ LARROW CSYMBOL SEMI exp (EGet (SYMBOL, SOME typ, CSYMBOL, exp), (SYMBOLleft, expright))
132 | IF exp THEN exp ELSE exp (EIf (exp1, exp2, exp3), (IFleft, exp3right))
133
134apps : term (term)
135 | apps term (EApp (apps, term), (appsleft, termright))
136
137term : LPAREN exp RPAREN (exp)
138 | BEGIN exp END (exp)
139 | INT (EInt INT, (INTleft, INTright))
140 | STRING (EString STRING, (STRINGleft, STRINGright))
141 | LBRACK elist RBRACK (EList elist, (LBRACKleft, RBRACKright))
142 | LET exp IN exp END (ELocal (exp1, exp2), (LETleft, ENDright))
143 | SYMBOL (EVar SYMBOL, (SYMBOLleft, SYMBOLright))
144
145sets : CSYMBOL EQ apps SEMIopt ([(ESet (CSYMBOL, apps), (CSYMBOLleft, appsright))])
146 | CSYMBOL EQ apps SEMI sets ((ESet (CSYMBOL, apps), (CSYMBOLleft, appsright))
147 :: sets)
148
149SEMIopt: SEMI ()
150 | ()
151
152elist : ([])
153 | elistNe (elistNe)
154
155elistNe: exp ([exp])
156 | exp COMMA elistNe (exp :: elistNe)
157
158typ : SYMBOL (TBase SYMBOL, (SYMBOLleft, SYMBOLright))
159 | LBRACK typ RBRACK (TList typ, (LBRACKleft, RBRACKright))
160 | typ ARROW typ (TArrow (typ1, typ2), (typleft, typright))
161 | LBRACK ctxt RBRACK recd DARROW recd (TAction (ctxt, recd1, recd2), (LBRACKleft, recd2right))
162 | LBRACK ctxt RBRACK recd (TAction (ctxt, recd, StringMap.empty),
163 (LBRACKleft, recdright))
164 | LBRACK ctxt RBRACK (TAction (ctxt, StringMap.empty, StringMap.empty),
165 (LBRACKleft, ctxtright))
166 | LPAREN typ RPAREN (typ)
167 | ctxt DARROW typ (TNested (ctxt, typ), (ctxtleft, typright))
168
169recd : LBRACE RBRACE (StringMap.empty)
170 | LBRACE recdNe RBRACE (recdNe)
171
172recdNe : CSYMBOL COLON typ (StringMap.insert (StringMap.empty, CSYMBOL, typ))
173 | CSYMBOL COLON typ COMMA recdNe (StringMap.insert (recdNe, CSYMBOL, typ))
174
175ctxt : ROOT (CRoot, (ROOTleft, ROOTright))
176 | CSYMBOL (CConst CSYMBOL, (CSYMBOLleft, CSYMBOLright))
177 | CARET ctxt (CPrefix ctxt, (CARETleft, ctxtright))
178 | BANG ctxt (CNot ctxt, (BANGleft, ctxtright))
179 | ctxt AND ctxt (CAnd (ctxt1, ctxt2), (ctxt1left, ctxt2right))
180 | LPAREN ctxt RPAREN (ctxt)