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
32 type te = string * string
33 let lexer = CicNotationLexer.syntax_pattern_lexer
35 module Level1Parser = Grammar.GMake (Level1Lexer)
39 type te = string * string
40 let lexer = CicNotationLexer.ast_pattern_lexer
42 module Level2Parser = Grammar.GMake (Level2Lexer)
46 type te = string * string
47 let lexer = CicNotationLexer.ast_pattern_lexer
49 module Level3Parser = Grammar.GMake (Level3Lexer)
51 let level1_pattern = Level1Parser.Entry.create "level1_pattern"
52 let level2_pattern = Level2Parser.Entry.create "level2_pattern"
53 let level3_interpretation = Level3Parser.Entry.create "level3_interpretation"
55 let return_term loc term = ()
57 (*let fail floc msg =*)
58 (* let (x, y) = CicAst.loc_of_floc floc in*)
59 (* failwith (sprintf "Error at characters %d - %d: %s" x y msg)*)
63 Pervasives.int_of_string s
65 failwith (sprintf "Lexer failure: string_of_int \"%s\" failed" s)
67 (* {{{ Grammar for concrete syntax patterns, notation level 1 *)
69 GLOBAL: level1_pattern;
70 level1_pattern: [ [ p = pattern -> () ] ];
71 pattern: [ [ p = LIST1 simple_pattern -> () ] ];
77 sep: [ [ SYMBOL "\\SEP"; sep = literal -> () ] ];
78 row_sep: [ [ SYMBOL "\\ROWSEP"; sep = literal -> () ] ];
79 field_sep: [ [ SYMBOL "\\FIELDSEP"; sep = literal -> () ] ];
81 [ SYMBOL "\\HBOX"; p = simple_pattern -> ()
82 | SYMBOL "\\VBOX"; p = simple_pattern -> ()
83 | SYMBOL "\\BREAK" -> ()
87 [ SYMBOL "\\LIST0"; p = simple_pattern; sep = OPT sep -> ()
88 | SYMBOL "\\LIST1"; p = simple_pattern; sep = OPT sep -> ()
89 | SYMBOL "\\OPT"; p = simple_pattern -> ()
94 | SYMBOL "\\NUM"; id = IDENT -> ()
95 | SYMBOL "\\IDENT"; id = IDENT -> ()
100 [ p1 = SELF; SYMBOL "\\SUB"; p2 = SELF -> ()
101 | p1 = SELF; SYMBOL "\\SUP"; p2 = SELF -> ()
102 | p1 = SELF; SYMBOL "\\BELOW"; p2 = SELF -> ()
103 | p1 = SELF; SYMBOL "\\ABOVE"; p2 = SELF -> ()
104 | SYMBOL "["; p1 = pattern; SYMBOL "\\OVER"; p2 = pattern; SYMBOL "]" ->
106 | SYMBOL "["; p1 = pattern; SYMBOL "\\ATOP"; p2 = pattern; SYMBOL "]" ->
108 | SYMBOL "\\ARRAY"; p = SELF; fsep = OPT field_sep; rsep = OPT row_sep ->
110 | SYMBOL "\\FRAC"; p1 = SELF; p2 = SELF -> ()
111 | SYMBOL "\\SQRT"; p = SELF -> ()
112 | SYMBOL "\\ROOT"; arg = pattern; SYMBOL "\\OF"; index = SELF -> ()
115 [ m = magic_pattern -> ()
116 | v = pattern_variable -> ()
117 | b = box_pattern -> ()
119 | SYMBOL "["; p = pattern; SYMBOL "]" -> ()
125 (* {{{ Grammar for ast patterns, notation level 2 *)
127 GLOBAL: level2_pattern;
128 level2_pattern: [ [ p = pattern -> () ] ];
130 [ SYMBOL "\\PROP" -> ()
131 | SYMBOL "\\SET" -> ()
132 | SYMBOL "\\TYPE" -> ()
136 [ (* TODO explicit substitution *)
140 [ (* TODO meta substitution *)
143 possibly_typed_name: [
144 [ SYMBOL "("; i = IDENT; SYMBOL ":"; typ = pattern; SYMBOL ")" -> ()
150 | SYMBOL "("; head = IDENT; vars = LIST1 possibly_typed_name; SYMBOL ")" ->
155 [ SYMBOL <:unicode<Pi>> (* Π *) -> ()
156 | SYMBOL <:unicode<exists>> (* ∃ *) -> ()
157 | SYMBOL <:unicode<forall>> (* ∀ *) -> ()
158 | SYMBOL <:unicode<lambda>> (* λ *) -> ()
162 [ vars = LIST1 IDENT SEP SYMBOL ",";
163 typ = OPT [ SYMBOL ":"; p = pattern -> () ] -> ()
165 SYMBOL "("; vars = LIST1 IDENT SEP SYMBOL ",";
166 typ = OPT [ SYMBOL ":"; p = pattern -> () ]; SYMBOL ")" -> ()
173 | IDENT "corec" -> ()
178 name = IDENT; args = bound_names;
179 index_name = OPT [ IDENT "on"; idx = IDENT -> () ];
180 ty = OPT [ SYMBOL ":" ; p = pattern -> () ];
181 SYMBOL <:unicode<def>> (* ≝ *); body = pattern ->
188 [ SYMBOL "\\NUM"; id = IDENT -> ()
189 | SYMBOL "\\IDENT"; id = IDENT -> ()
190 | SYMBOL "\\FRESH"; id = IDENT -> ()
194 [ SYMBOL "\\FOLD"; n = OPT NUMBER; [ IDENT "left" | IDENT "right" ];
195 LIST1 IDENT; SYMBOL "."; p1 = pattern;
196 OPT [ SYMBOL "\\LAMBDA"; LIST1 IDENT ]; p2 = pattern ->
198 | SYMBOL "\\DEFAULT"; id = IDENT; p1 = pattern; p2 = pattern -> ()
203 [ IDENT "let"; var = possibly_typed_name; SYMBOL <:unicode<def>> (* ≝ *);
204 p1 = pattern; "in"; p2 = pattern ->
206 | IDENT "let"; k = induction_kind; defs = let_defs; IDENT "in";
211 [ b = binder; bound_names; SYMBOL "."; body = pattern -> () ]
215 [ p1 = pattern; p2 = pattern -> () ]
218 | i = IDENT; s = explicit_subst -> ()
222 | m = META; s = meta_subst -> ()
226 | outtyp = OPT [ SYMBOL "["; typ = pattern; SYMBOL "]" ];
227 IDENT "match"; t = pattern;
228 indty_ident = OPT [ SYMBOL ":"; id = IDENT ];
229 IDENT "with"; SYMBOL "[";
231 lhs = match_pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *);
237 | SYMBOL "("; p1 = pattern; SYMBOL ":"; p2 = pattern; SYMBOL ")" -> ()
238 | SYMBOL "("; p = pattern; SYMBOL ")" -> ()
239 | v = pattern_variable -> ()
240 | m = magic_pattern -> ()
246 (* {{{ Grammar for interpretation, notation level 3 *)
248 GLOBAL: level3_interpretation;
249 level3_interpretation: [ [ i = interpretation -> () ] ];
252 | SYMBOL <:unicode<eta>> (* η *); SYMBOL "."; a = SELF -> ()
253 | SYMBOL <:unicode<eta>> (* η *); i = IDENT; SYMBOL "."; a = SELF -> ()
259 | SYMBOL "("; terms = LIST1 SELF; SYMBOL ")" -> ()
263 [ IDENT "interpretation"; s = SYMBOL; args = LIST1 argument; IDENT "as";
271 let exc_located_wrapper f =
275 | Stdpp.Exc_located (floc, Stream.Error msg) ->
276 raise (Parse_error (floc, msg))
277 | Stdpp.Exc_located (floc, exn) ->
278 raise (Parse_error (floc, (Printexc.to_string exn)))
280 let parse_syntax_pattern stream =
283 (Level1Parser.Entry.parse level1_pattern (Level1Parser.parsable stream)))
285 let parse_ast_pattern stream =
288 (Level2Parser.Entry.parse level2_pattern (Level2Parser.parsable stream)))
290 let parse_interpretation stream =
293 (Level3Parser.Entry.parse level3_interpretation
294 (Level3Parser.parsable stream)))
296 (* vim:set encoding=utf8 foldmethod=marker: *)