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)
44 let level1_pattern = Level1Parser.Entry.create "level1_pattern"
45 let level2_pattern = Level2Parser.Entry.create "level2_pattern"
47 let return_term loc term = ()
49 (*let fail floc msg =*)
50 (* let (x, y) = CicAst.loc_of_floc floc in*)
51 (* failwith (sprintf "Error at characters %d - %d: %s" x y msg)*)
55 Pervasives.int_of_string s
57 failwith (sprintf "Lexer failure: string_of_int \"%s\" failed" s)
59 (* {{{ Grammar for concrete syntax patterns, notation level 1 *)
61 GLOBAL: level1_pattern;
62 level1_pattern: [ [ p = pattern -> () ] ];
63 pattern: [ [ p = LIST1 simple_pattern -> () ] ];
69 sep: [ [ SYMBOL "\\SEP"; sep = literal -> () ] ];
70 row_sep: [ [ SYMBOL "\\ROWSEP"; sep = literal -> () ] ];
71 field_sep: [ [ SYMBOL "\\FIELDSEP"; sep = literal -> () ] ];
73 [ SYMBOL "\\HBOX"; p = simple_pattern -> ()
74 | SYMBOL "\\VBOX"; p = simple_pattern -> ()
75 | SYMBOL "\\BREAK" -> ()
79 [ SYMBOL "\\LIST0"; p = simple_pattern; sep = OPT sep -> ()
80 | SYMBOL "\\LIST1"; p = simple_pattern; sep = OPT sep -> ()
81 | SYMBOL "\\OPT"; p = simple_pattern -> ()
86 | SYMBOL "\\NUM"; id = IDENT -> ()
87 | SYMBOL "\\IDENT"; id = IDENT -> ()
92 [ p1 = SELF; SYMBOL "\\SUB"; p2 = SELF -> ()
93 | p1 = SELF; SYMBOL "\\SUP"; p2 = SELF -> ()
94 | p1 = SELF; SYMBOL "\\BELOW"; p2 = SELF -> ()
95 | p1 = SELF; SYMBOL "\\ABOVE"; p2 = SELF -> ()
96 | SYMBOL "["; p1 = pattern; SYMBOL "\\OVER"; p2 = pattern; SYMBOL "]" ->
98 | SYMBOL "["; p1 = pattern; SYMBOL "\\ATOP"; p2 = pattern; SYMBOL "]" ->
100 | SYMBOL "\\ARRAY"; p = SELF; fsep = OPT field_sep; rsep = OPT row_sep ->
102 | SYMBOL "\\FRAC"; p1 = SELF; p2 = SELF -> ()
103 | SYMBOL "\\SQRT"; p = SELF -> ()
104 | SYMBOL "\\ROOT"; arg = pattern; SYMBOL "\\OF"; index = SELF -> ()
107 [ m = magic_pattern -> ()
108 | v = pattern_variable -> ()
109 | b = box_pattern -> ()
111 | SYMBOL "["; p = pattern; SYMBOL "]" -> ()
117 (* {{{ Grammar for ast patterns, notation level 2 *)
119 GLOBAL: level2_pattern;
120 level2_pattern: [ [ p = pattern -> () ] ];
122 [ SYMBOL "\\PROP" -> ()
123 | SYMBOL "\\SET" -> ()
124 | SYMBOL "\\TYPE" -> ()
128 [ (* TODO explicit substitution *)
132 [ (* TODO meta substitution *)
135 possibly_typed_name: [
136 [ SYMBOL "("; i = IDENT; SYMBOL ":"; typ = pattern; SYMBOL ")" -> ()
142 | SYMBOL "("; head = IDENT; vars = LIST1 possibly_typed_name; SYMBOL ")" ->
147 [ SYMBOL <:unicode<Pi>> (* Π *) -> ()
148 | SYMBOL <:unicode<exists>> (* ∃ *) -> ()
149 | SYMBOL <:unicode<forall>> (* ∀ *) -> ()
150 | SYMBOL <:unicode<lambda>> (* λ *) -> ()
154 [ vars = LIST1 IDENT SEP SYMBOL ",";
155 typ = OPT [ SYMBOL ":"; p = pattern -> () ] -> ()
157 SYMBOL "("; vars = LIST1 IDENT SEP SYMBOL ",";
158 typ = OPT [ SYMBOL ":"; p = pattern -> () ]; SYMBOL ")" -> ()
165 | IDENT "corec" -> ()
170 name = IDENT; args = bound_names;
171 index_name = OPT [ IDENT "on"; idx = IDENT -> () ];
172 ty = OPT [ SYMBOL ":" ; p = pattern -> () ];
173 SYMBOL <:unicode<def>> (* ≝ *); body = pattern ->
180 [ SYMBOL "\\NUM"; id = IDENT -> ()
181 | SYMBOL "\\IDENT"; id = IDENT -> ()
182 | SYMBOL "\\FRESH"; id = IDENT -> ()
186 [ SYMBOL "\\FOLD"; n = OPT NUMBER; [ IDENT "left" | IDENT "right" ];
187 LIST1 IDENT; SYMBOL "."; p1 = pattern;
188 OPT [ SYMBOL "\\LAMBDA"; LIST1 IDENT ]; p2 = pattern ->
190 | SYMBOL "\\DEFAULT"; id = IDENT; p1 = pattern; p2 = pattern -> ()
195 [ IDENT "let"; var = possibly_typed_name; SYMBOL <:unicode<def>> (* ≝ *);
196 p1 = pattern; "in"; p2 = pattern ->
198 | IDENT "let"; k = induction_kind; defs = let_defs; IDENT "in";
203 [ b = binder; bound_names; SYMBOL "."; body = pattern -> () ]
207 [ p1 = pattern; p2 = pattern -> () ]
210 | i = IDENT; s = explicit_subst -> ()
214 | m = META; s = meta_subst -> ()
218 | outtyp = OPT [ SYMBOL "["; typ = pattern; SYMBOL "]" ];
219 IDENT "match"; t = pattern;
220 indty_ident = OPT [ SYMBOL ":"; id = IDENT ];
221 IDENT "with"; SYMBOL "[";
223 lhs = match_pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *);
229 | SYMBOL "("; p1 = pattern; SYMBOL ":"; p2 = pattern; SYMBOL ")" -> ()
230 | SYMBOL "("; p = pattern; SYMBOL ")" -> ()
231 | v = pattern_variable -> ()
232 | m = magic_pattern -> ()
238 let exc_located_wrapper f =
242 | Stdpp.Exc_located (floc, Stream.Error msg) ->
243 raise (Parse_error (floc, msg))
244 | Stdpp.Exc_located (floc, exn) ->
245 raise (Parse_error (floc, (Printexc.to_string exn)))
247 let parse_syntax_pattern stream =
250 (Level1Parser.Entry.parse level1_pattern (Level1Parser.parsable stream)))
252 let parse_ast_pattern stream =
255 (Level2Parser.Entry.parse level2_pattern (Level2Parser.parsable stream)))
257 (* vim:set encoding=utf8 foldmethod=marker: *)