Parsing has never been this much fun
[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 *)
20open Ast
21
22%%
23%header (functor DomtoolLrValsFn(structure Token : TOKEN))
24
25%term
26 EOF
27 | SYMBOL of string | CSYMBOL of string
28 | STRING of string
29 | INT of int
30 | ARROW | DARROW | COLON | CARET | BANG | AND
31 | LPAREN | RPAREN | LBRACK | RBRACK | LBRACE | RBRACE
32 | EQ | COMMA | BSLASH | SEMI | LET | IN | END
33 | ROOT
34
35%nonterm
36 file of exp
37 | exp of exp
38 | apps of exp
39 | term of exp
40 | elist of exp list
41 | elistNe of exp list
42 | clist of exp list
43 | typ of typ
44 | ctxt of context
45 | recd of record
46 | recdNe of record
47
48%verbose (* print summary of errors *)
49%pos int (* positions *)
50%start file
51%pure
52%eop EOF
53%noshift EOF
54
55%name Domtool
56
57%right SEMI
58%nonassoc COLON
59%nonassoc IN
60%right ARROW DARROW
61%right COMMA
62%nonassoc EQ
63%right AND
64%nonassoc CARET BANG
65
66%%
67
68file : exp (exp)
69
70exp : apps (apps)
71 | BSLASH SYMBOL COLON LPAREN typ RPAREN ARROW exp (ELam (SYMBOL, typ, exp), (BSLASHleft, expright))
72 | CSYMBOL EQ exp (ESet (CSYMBOL, exp), (CSYMBOLleft, expright))
73 | exp SEMI exp (let
74 val ls = case #1 exp2 of
75 ESeq ls => exp :: ls
76 | _ => [exp1, exp2]
77 in
78 (ESeq ls, (exp1left, exp2right))
79 end)
80
81apps : term (term)
82 | apps term (EApp (apps, term), (appsleft, termright))
83
84term : LPAREN exp RPAREN (exp)
85 | INT (EInt INT, (INTleft, INTright))
86 | STRING (EString STRING, (STRINGleft, STRINGright))
87 | LBRACK elist RBRACK (EList elist, (LBRACKleft, RBRACKright))
88 | LET exp IN exp END (let
89 val ls = case (#1 exp1, #1 exp2) of
90 (ESeq ls1, ESeq ls2) => ls1 @ ls2
91 | (ESeq ls, _) => ls @ [exp2]
92 | (_, ESeq ls) => exp1 :: ls
93 | _ => [exp1, exp2]
94 in
95 (ELocal (ESeq ls, (exp1left, exp2right)),
96 (exp1left, exp2right))
97 end)
98 | SYMBOL (EVar SYMBOL, (SYMBOLleft, SYMBOLright))
99 | CSYMBOL (EEnv CSYMBOL, (CSYMBOLleft, CSYMBOLright))
100
101elist : ([])
102 | elistNe (elistNe)
103
104elistNe: exp ([exp])
105 | exp COMMA elistNe (exp :: elistNe)
106
107typ : SYMBOL (TBase SYMBOL, (SYMBOLleft, SYMBOLright))
108 | LBRACK typ RBRACK (TList typ, (LBRACKleft, RBRACKright))
109 | typ ARROW typ (TArrow (typ1, typ2), (typleft, typright))
110 | LBRACK ctxt RBRACK recd DARROW recd (TAction (ctxt, recd1, recd2), (ctxtleft, recd2right))
111 | LBRACK ctxt RBRACK recd (TAction (ctxt, recd, StringMap.empty),
112 (ctxtleft, recdright))
113 | LBRACK ctxt RBRACK (TAction (ctxt, StringMap.empty, StringMap.empty),
114 (ctxtleft, ctxtright))
115 | LPAREN typ RPAREN (typ)
116
117recd : LBRACE RBRACE (StringMap.empty)
118 | LBRACE recdNe RBRACE (recdNe)
119
120recdNe : CSYMBOL COLON typ (StringMap.insert (StringMap.empty, CSYMBOL, typ))
121 | CSYMBOL COLON typ COMMA recdNe (StringMap.insert (recdNe, CSYMBOL, typ))
122
123ctxt : ROOT (CRoot, (ROOTleft, ROOTright))
124 | CSYMBOL (CConst CSYMBOL, (CSYMBOLleft, CSYMBOLright))
125 | CARET ctxt (CPrefix ctxt, (CARETleft, ctxtright))
126 | BANG ctxt (CNot ctxt, (BANGleft, ctxtright))
127 | ctxt AND ctxt (CAnd (ctxt1, ctxt2), (ctxt1left, ctxt2right))
128 | LPAREN ctxt RPAREN (ctxt)