1 (* Copyright (C) 2005, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
28 exception Parse_error of Token.flocation * string
30 module NotationLexer =
32 type te = string * string
33 let lexer = CicNotationLexer.notation_lexer
35 module NotationGrammar = Grammar.GMake (NotationLexer)
37 let level1_pattern = NotationGrammar.Entry.create "level1_pattern"
38 let level2_pattern = NotationGrammar.Entry.create "level2_pattern"
39 let level3_interpretation = NotationGrammar.Entry.create "level3_interpretation"
41 let return_term loc term = ()
43 (*let fail floc msg =*)
44 (* let (x, y) = CicAst.loc_of_floc floc in*)
45 (* failwith (sprintf "Error at characters %d - %d: %s" x y msg)*)
49 Pervasives.int_of_string s
51 failwith (sprintf "Lexer failure: string_of_int \"%s\" failed" s)
53 GEXTEND NotationGrammar
54 GLOBAL: level1_pattern level2_pattern level3_interpretation;
55 (* {{{ Grammar for concrete syntax patterns, notation level 1 *)
56 level1_pattern: [ [ p = l1_pattern -> () ] ];
57 l1_pattern: [ [ p = LIST1 l1_simple_pattern -> () ] ];
63 sep: [ [ SYMBOL "\\SEP"; sep = literal -> () ] ];
64 row_sep: [ [ SYMBOL "\\ROWSEP"; sep = literal -> () ] ];
65 field_sep: [ [ SYMBOL "\\FIELDSEP"; sep = literal -> () ] ];
67 [ SYMBOL "\\HBOX"; p = l1_simple_pattern -> ()
68 | SYMBOL "\\VBOX"; p = l1_simple_pattern -> ()
69 | SYMBOL "\\BREAK" -> ()
73 [ SYMBOL "\\LIST0"; p = l1_simple_pattern; sep = OPT sep -> ()
74 | SYMBOL "\\LIST1"; p = l1_simple_pattern; sep = OPT sep -> ()
75 | SYMBOL "\\OPT"; p = l1_simple_pattern -> ()
78 l1_pattern_variable: [
80 | SYMBOL "\\NUM"; id = IDENT -> ()
81 | SYMBOL "\\IDENT"; id = IDENT -> ()
86 [ p1 = SELF; SYMBOL "\\SUB"; p2 = SELF -> ()
87 | p1 = SELF; SYMBOL "\\SUP"; p2 = SELF -> ()
88 | p1 = SELF; SYMBOL "\\BELOW"; p2 = SELF -> ()
89 | p1 = SELF; SYMBOL "\\ABOVE"; p2 = SELF -> ()
90 | SYMBOL "["; p1 = l1_pattern; SYMBOL "\\OVER"; p2 = l1_pattern;
93 | SYMBOL "["; p1 = l1_pattern; SYMBOL "\\ATOP"; p2 = l1_pattern;
96 | SYMBOL "\\ARRAY"; p = SELF; fsep = OPT field_sep; rsep = OPT row_sep ->
98 | SYMBOL "\\FRAC"; p1 = SELF; p2 = SELF -> ()
99 | SYMBOL "\\SQRT"; p = SELF -> ()
100 | SYMBOL "\\ROOT"; arg = l1_pattern; SYMBOL "\\OF"; index = SELF -> ()
103 [ m = l1_magic_pattern -> ()
104 | v = l1_pattern_variable -> ()
105 | b = l1_box_pattern -> ()
107 | SYMBOL "["; p = l1_pattern; SYMBOL "]" -> ()
111 (* {{{ Grammar for ast patterns, notation level 2 *)
112 level2_pattern: [ [ p = l2_pattern -> () ] ];
114 [ SYMBOL "\\PROP" -> ()
115 | SYMBOL "\\SET" -> ()
116 | SYMBOL "\\TYPE" -> ()
120 [ (* TODO explicit substitution *)
124 [ (* TODO meta substitution *)
127 possibly_typed_name: [
128 [ SYMBOL "("; i = IDENT; SYMBOL ":"; typ = l2_pattern; SYMBOL ")" -> ()
134 | SYMBOL "("; head = IDENT; vars = LIST1 possibly_typed_name; SYMBOL ")" ->
139 [ SYMBOL <:unicode<Pi>> (* Π *) -> ()
140 | SYMBOL <:unicode<exists>> (* ∃ *) -> ()
141 | SYMBOL <:unicode<forall>> (* ∀ *) -> ()
142 | SYMBOL <:unicode<lambda>> (* λ *) -> ()
146 [ vars = LIST1 IDENT SEP SYMBOL ",";
147 typ = OPT [ SYMBOL ":"; p = l2_pattern -> () ] -> ()
149 SYMBOL "("; vars = LIST1 IDENT SEP SYMBOL ",";
150 typ = OPT [ SYMBOL ":"; p = l2_pattern -> () ]; SYMBOL ")" -> ()
157 | IDENT "corec" -> ()
162 name = IDENT; args = bound_names;
163 index_name = OPT [ IDENT "on"; idx = IDENT -> () ];
164 ty = OPT [ SYMBOL ":" ; p = l2_pattern -> () ];
165 SYMBOL <:unicode<def>> (* ≝ *); body = l2_pattern ->
171 l2_pattern_variable: [
172 [ SYMBOL "\\NUM"; id = IDENT -> ()
173 | SYMBOL "\\IDENT"; id = IDENT -> ()
174 | SYMBOL "\\FRESH"; id = IDENT -> ()
178 [ SYMBOL "\\FOLD"; n = OPT NUMBER; [ IDENT "left" | IDENT "right" ];
179 LIST1 IDENT; SYMBOL "."; p1 = l2_pattern;
180 OPT [ SYMBOL "\\LAMBDA"; LIST1 IDENT ]; p2 = l2_pattern ->
182 | SYMBOL "\\DEFAULT"; id = IDENT; p1 = l2_pattern; p2 = l2_pattern -> ()
187 [ IDENT "let"; var = possibly_typed_name; SYMBOL <:unicode<def>> (* ≝ *);
188 p1 = l2_pattern; "in"; p2 = l2_pattern ->
190 | IDENT "let"; k = induction_kind; defs = let_defs; IDENT "in";
195 [ b = binder; bound_names; SYMBOL "."; body = l2_pattern -> () ]
199 [ p1 = l2_pattern; p2 = l2_pattern -> () ]
202 | i = IDENT; s = explicit_subst -> ()
206 | m = META; s = meta_subst -> ()
210 | outtyp = OPT [ SYMBOL "["; typ = l2_pattern; SYMBOL "]" ];
211 IDENT "match"; t = l2_pattern;
212 indty_ident = OPT [ SYMBOL ":"; id = IDENT ];
213 IDENT "with"; SYMBOL "[";
215 lhs = match_pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *);
221 | SYMBOL "("; p1 = l2_pattern; SYMBOL ":"; p2 = l2_pattern; SYMBOL ")" ->
223 | SYMBOL "("; p = l2_pattern; SYMBOL ")" -> ()
224 | v = l2_pattern_variable -> ()
225 | m = l2_magic_pattern -> ()
229 (* {{{ Grammar for interpretation, notation level 3 *)
230 level3_interpretation: [ [ i = interpretation -> () ] ];
233 | SYMBOL <:unicode<eta>> (* η *); SYMBOL "."; a = SELF -> ()
234 | SYMBOL <:unicode<eta>> (* η *); i = IDENT; SYMBOL "."; a = SELF -> ()
240 | SYMBOL "("; terms = LIST1 SELF; SYMBOL ")" -> ()
244 [ IDENT "interpretation"; s = SYMBOL; args = LIST1 argument; IDENT "as";
252 let exc_located_wrapper f =
256 | Stdpp.Exc_located (floc, Stream.Error msg) ->
257 raise (Parse_error (floc, msg))
258 | Stdpp.Exc_located (floc, exn) ->
259 raise (Parse_error (floc, (Printexc.to_string exn)))
261 let parse_syntax_pattern stream =
264 (NotationGrammar.Entry.parse level1_pattern
265 (NotationGrammar.parsable stream)))
267 let parse_ast_pattern stream =
270 (NotationGrammar.Entry.parse level2_pattern
271 (NotationGrammar.parsable stream)))
273 let parse_interpretation stream =
276 (NotationGrammar.Entry.parse level3_interpretation
277 (NotationGrammar.parsable stream)))
279 (* vim:set encoding=utf8 foldmethod=marker: *)