]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_notation/cicNotationParser.ml
snapshot
[helm.git] / helm / ocaml / cic_notation / cicNotationParser.ml
1 (* Copyright (C) 2005, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 open Printf
27
28 open CicNotationEnv
29 open CicNotationPt
30
31 exception Parse_error of Token.flocation * string
32 exception Level_not_found of int
33
34 let grammar = Grammar.gcreate CicNotationLexer.notation_lexer
35
36 let min_precedence = 0
37 let max_precedence = 100
38 let default_precedence = 50
39
40 let level1_pattern = Grammar.Entry.create grammar "level1_pattern"
41 let level2_pattern = Grammar.Entry.create grammar "level2_pattern"
42 let level3_term = Grammar.Entry.create grammar "level3_term"
43 let l2_pattern = Grammar.Entry.create grammar "l2_pattern"
44 let notation = Grammar.Entry.create grammar "notation" (* level1 <-> level 2 *)
45 let interpretation =
46   Grammar.Entry.create grammar "interpretation" (* level2 <-> level 3 *)
47 let phrase = Grammar.Entry.create grammar "phrase"
48
49 let return_term loc term = ()
50
51 let fail floc msg =
52   let (x, y) = loc_of_floc floc in
53   failwith (sprintf "Error at characters %d - %d: %s" x y msg)
54
55 let int_of_string s =
56   try
57     Pervasives.int_of_string s
58   with Failure _ ->
59     failwith (sprintf "Lexer failure: string_of_int \"%s\" failed" s)
60
61 (** {2 Grammar extension} *)
62
63 let symbol s = Gramext.Stoken ("SYMBOL", s)
64 let ident s = Gramext.Stoken ("IDENT", s)
65 let number s = Gramext.Stoken ("NUMBER", s)
66 let term = Gramext.Sself
67
68 let g_symbol_of_literal =
69   function
70   | `Symbol s -> symbol s
71   | `Keyword s -> ident s
72   | `Number s -> number s
73
74 type binding =
75   | NoBinding
76   | Binding of string * value_type
77   | Env of (string * value_type) list
78
79 let make_action action bindings =
80   let rec aux (vl : env_type) =
81     function
82       [] ->
83         prerr_endline "aux: make_action";
84         Gramext.action (fun (loc: location) -> action vl loc)
85     | NoBinding :: tl ->
86         prerr_endline "aux: none";
87         Gramext.action (fun _ -> aux vl tl)
88     (* LUCA: DEFCON 2 BEGIN *)
89     | Binding (name, TermType) :: tl ->
90         prerr_endline "aux: term";
91         Gramext.action
92           (fun (v:term) -> aux ((name, (TermType, (TermValue v)))::vl) tl)
93     | Binding (name, StringType) :: tl ->
94         prerr_endline "aux: string";
95         Gramext.action
96           (fun (v:string) ->
97             aux ((name, (StringType, (StringValue v))) :: vl) tl)
98     | Binding (name, NumType) :: tl ->
99         prerr_endline "aux: num";
100         Gramext.action
101           (fun (v:string) -> aux ((name, (NumType, (NumValue v))) :: vl) tl)
102     | Binding (name, OptType t) :: tl ->
103         prerr_endline "aux: opt";
104         Gramext.action
105           (fun (v:'a option) ->
106             aux ((name, (OptType t, (OptValue v))) :: vl) tl)
107     | Binding (name, ListType t) :: tl ->
108         prerr_endline "aux: list";
109         Gramext.action
110           (fun (v:'a list) ->
111             aux ((name, (ListType t, (ListValue v))) :: vl) tl)
112     | Env _ :: tl ->
113         prerr_endline "aux: env";
114         Gramext.action (fun (v:env_type) -> aux (v @ vl) tl)
115     (* LUCA: DEFCON 2 END *)
116   in
117     aux [] (List.rev bindings)
118
119 let flatten_opt =
120   let rec aux acc =
121     function
122       [] -> List.rev acc
123     | NoBinding :: tl -> aux acc tl
124     | Env names :: tl -> aux (List.rev names @ acc) tl
125     | Binding (name, ty) :: tl -> aux ((name, ty) :: acc) tl
126   in
127   aux []
128
129   (* given a level 1 pattern computes the new RHS of "term" grammar entry *)
130 let extract_term_production pattern =
131   let rec aux = function
132     | AttributedTerm (_, t) -> aux t
133     | Literal l -> aux_literal l
134     | Layout l -> aux_layout l
135     | Magic m -> aux_magic m
136     | Variable v -> aux_variable v
137     | t ->
138         prerr_endline (CicNotationPp.pp_term t);
139         assert false
140   and aux_literal =
141     function
142     | `Symbol s -> [NoBinding, symbol s]
143     | `Keyword s -> [NoBinding, ident s]
144     | `Number s -> [NoBinding, number s]
145   and aux_layout = function
146     | Sub (p1, p2) -> aux p1 @ [NoBinding, symbol "\\SUB"] @ aux p2
147     | Sup (p1, p2) -> aux p1 @ [NoBinding, symbol "\\SUP"] @ aux p2
148     | Below (p1, p2) -> aux p1 @ [NoBinding, symbol "\\BELOW"] @ aux p2
149     | Above (p1, p2) -> aux p1 @ [NoBinding, symbol "\\ABOVE"] @ aux p2
150     | Frac (p1, p2) -> aux p1 @ [NoBinding, symbol "\\FRAC"] @ aux p2
151     | Atop (p1, p2) -> aux p1 @ [NoBinding, symbol "\\ATOP"] @ aux p2
152     | Over (p1, p2) -> aux p1 @ [NoBinding, symbol "\\OVER"] @ aux p2
153     | Root (p1, p2) ->
154         [NoBinding, symbol "\\ROOT"] @ aux p2 @ [NoBinding, symbol "\\OF"]
155         @ aux p1
156     | Sqrt p -> [NoBinding, symbol "\\SQRT"] @ aux p
157     | Break -> []
158     | Box (_, pl) -> List.flatten (List.map aux pl)
159   and aux_magic magic =
160     match magic with
161     | Opt p ->
162         let p_bindings, p_atoms, p_names, p_action = inner_pattern p in
163         let action (env_opt : env_type option) (loc : location) =
164           match env_opt with
165           | Some env -> List.map opt_binding_some env
166           | None -> List.map opt_binding_of_name p_names
167         in
168         [ Env (List.map opt_declaration p_names),
169           Gramext.srules
170             [ [ Gramext.Sopt (Gramext.srules [ p_atoms, p_action ]) ],
171               Gramext.action action ] ]
172     | List0 (p, _)
173     | List1 (p, _) ->
174         let p_bindings, p_atoms, p_names, p_action = inner_pattern p in
175         let env0 = List.map list_binding_of_name p_names in
176         let grow_env_entry env n v =
177           prerr_endline "grow_env_entry";
178           List.map
179             (function
180               | (n', (ty, ListValue vl)) as entry ->
181                   if n' = n then n', (ty, ListValue (v :: vl)) else entry
182               | _ -> assert false)
183             env
184         in
185         let grow_env env_i env =
186           prerr_endline "grow_env";
187           List.fold_left
188             (fun env (n, (_, v)) -> grow_env_entry env n v)
189             env env_i
190         in
191         let action (env_list : env_type list) (loc : location) =
192           prerr_endline "list action";
193           List.fold_right grow_env env_list env0
194         in
195         let g_symbol s =
196           match magic with
197           | List0 (_, None) -> Gramext.Slist0 s
198           | List1 (_, None) -> Gramext.Slist1 s
199           | List0 (_, Some l) -> Gramext.Slist0sep (s, g_symbol_of_literal l)
200           | List1 (_, Some l) -> Gramext.Slist1sep (s, g_symbol_of_literal l)
201           | _ -> assert false
202         in
203         [ Env (List.map list_declaration p_names),
204           Gramext.srules
205             [ [ g_symbol (Gramext.srules [ p_atoms, p_action ]) ],
206               Gramext.action action ] ]
207     | _ -> assert false
208   and aux_variable =
209     function
210     | NumVar s -> [Binding (s, NumType), number ""]
211     | TermVar s -> [Binding (s, TermType), term]
212     | IdentVar s -> [Binding (s, StringType), ident ""]
213     | Ascription (p, s) -> assert false (* TODO *)
214     | FreshVar _ -> assert false
215   and inner_pattern p =
216     let p_bindings, p_atoms = List.split (aux p) in
217     let p_names = flatten_opt p_bindings in
218     let _ = prerr_endline ("inner names: " ^ String.concat " " (List.map fst p_names)) in
219     let action =
220       make_action (fun (env : env_type) (loc : location) -> prerr_endline "inner action"; env) p_bindings
221     in
222     p_bindings, p_atoms, p_names, action
223   in
224   aux pattern
225
226 let level_of_int precedence =
227   if precedence < min_precedence || precedence > max_precedence then
228     raise (Level_not_found precedence);
229   string_of_int precedence
230
231 type rule_id = Token.t Gramext.g_symbol list
232
233 let extend level1_pattern ?(precedence = default_precedence)
234   ?associativity action
235 =
236   let p_bindings, p_atoms =
237     List.split (extract_term_production level1_pattern)
238   in
239   let level = level_of_int precedence in
240   let p_names = flatten_opt p_bindings in
241   let _ =
242     prerr_endline (string_of_int (List.length p_bindings));
243     Grammar.extend
244       [ Grammar.Entry.obj (l2_pattern: 'a Grammar.Entry.e),
245         Some (Gramext.Level level),
246         [ None,
247           associativity,
248           [ p_atoms, 
249             (make_action
250               (fun (env: env_type) (loc: location) -> (action env loc))
251               p_bindings) ]]]
252   in
253   p_atoms
254
255 let delete atoms = Grammar.delete_rule l2_pattern atoms
256
257 (** {2 Grammar} *)
258
259 let boxify = function
260   | [ a ] -> a
261   | l -> Layout (Box (H, l))
262
263 let fold_binder binder pt_names body =
264   let fold_cluster binder terms ty body =
265     List.fold_right
266       (fun term body -> Binder (binder, (term, ty), body))
267       terms body  (* terms are names: either Ident or FreshVar *)
268   in
269   List.fold_right
270     (fun (names, ty) body -> fold_cluster binder names ty body)
271     pt_names body
272
273 let return_term loc term = AttributedTerm (`Loc loc, term)
274
275 let _ = (* create empty precedence level for "l2_pattern" *)
276   let mk_level_list first last =
277     let rec aux acc = function
278       | i when i < first -> acc
279       | i -> aux ((Some (string_of_int i), None, []) :: acc) (i - 1)
280     in
281     aux [] last
282   in
283   Grammar.extend
284     [ Grammar.Entry.obj (l2_pattern: 'a Grammar.Entry.e),
285       None,
286       mk_level_list min_precedence max_precedence ]
287
288 EXTEND
289   GLOBAL: level1_pattern level2_pattern level3_term
290           l2_pattern
291           notation interpretation
292           phrase;
293 (* {{{ Grammar for concrete syntax patterns, notation level 1 *)
294   level1_pattern: [ [ p = l1_simple_pattern -> p ] ];
295   l1_pattern: [ [ p = LIST1 l1_simple_pattern -> p ] ];
296   literal: [
297     [ s = SYMBOL -> `Symbol s
298     | k = KEYWORD -> `Keyword k
299     | n = NUMBER -> `Number n
300     ]
301   ];
302   sep:       [ [ SYMBOL "\\SEP";      sep = literal -> sep ] ];
303 (*   row_sep:   [ [ SYMBOL "\\ROWSEP";   sep = literal -> sep ] ];
304   field_sep: [ [ SYMBOL "\\FIELDSEP"; sep = literal -> sep ] ]; *)
305   l1_magic_pattern: [
306     [ SYMBOL "\\LIST0"; p = l1_simple_pattern; sep = OPT sep -> List0 (p, sep)
307     | SYMBOL "\\LIST1"; p = l1_simple_pattern; sep = OPT sep -> List1 (p, sep)
308     | SYMBOL "\\OPT";   p = l1_simple_pattern -> Opt p
309     ]
310   ];
311   l1_pattern_variable: [
312     [ SYMBOL "\\TERM"; id = IDENT -> TermVar id
313     | SYMBOL "\\NUM"; id = IDENT -> NumVar id
314     | SYMBOL "\\IDENT"; id = IDENT -> IdentVar id
315     ]
316   ];
317   l1_simple_pattern:
318     [ "layout" LEFTA
319       [ p1 = SELF; SYMBOL "\\SUB"; p2 = SELF ->
320           return_term loc (Layout (Sub (p1, p2)))
321       | p1 = SELF; SYMBOL "\\SUP"; p2 = SELF ->
322           return_term loc (Layout (Sup (p1, p2)))
323       | p1 = SELF; SYMBOL "\\BELOW"; p2 = SELF ->
324           return_term loc (Layout (Below (p1, p2)))
325       | p1 = SELF; SYMBOL "\\ABOVE"; p2 = SELF ->
326           return_term loc (Layout (Above (p1, p2)))
327       | p1 = SELF; SYMBOL "\\OVER"; p2 = SELF ->
328           return_term loc (Layout (Over (p1, p2)))
329       | p1 = SELF; SYMBOL "\\ATOP"; p2 = SELF ->
330           return_term loc (Layout (Atop (p1, p2)))
331 (*       | SYMBOL "\\ARRAY"; p = SELF; csep = OPT field_sep; rsep = OPT row_sep ->
332           return_term loc (Array (p, csep, rsep)) *)
333       | SYMBOL "\\FRAC"; p1 = SELF; p2 = SELF ->
334           return_term loc (Layout (Frac (p1, p2)))
335       | SYMBOL "\\SQRT"; p = SELF -> return_term loc (Layout (Sqrt p))
336       | SYMBOL "\\ROOT"; index = SELF; SYMBOL "\\OF"; arg = SELF ->
337           return_term loc (Layout (Root (arg, index)));
338       | SYMBOL "\\HBOX"; DELIM "\\["; p = l1_pattern; DELIM "\\]" ->
339           return_term loc (Layout (Box (H, p)))
340       | SYMBOL "\\VBOX"; DELIM "\\["; p = l1_pattern; DELIM "\\]" ->
341           return_term loc (Layout (Box (V, p)))
342       | SYMBOL "\\BREAK" -> return_term loc (Layout Break)
343       | DELIM "\\["; p = l1_pattern; DELIM "\\]" ->
344           return_term loc (boxify p)
345       | p = SELF; SYMBOL "\\AS"; id = IDENT ->
346           return_term loc (Variable (Ascription (p, id)))
347       ]
348     | "simple" NONA
349       [ i = IDENT -> return_term loc (Ident (i, None))
350       | m = l1_magic_pattern -> return_term loc (Magic m)
351       | v = l1_pattern_variable -> return_term loc (Variable v)
352       | l = literal -> return_term loc (Literal l)
353       ]
354     ];
355 (* }}} *)
356 (* {{{ Grammar for ast patterns, notation level 2 *)
357   level2_pattern: [ [ p = l2_pattern -> p ] ];
358   sort: [
359     [ SYMBOL "\\PROP" -> `Prop
360     | SYMBOL "\\SET" -> `Set
361     | SYMBOL "\\TYPE" -> `Type
362     ]
363   ];
364   explicit_subst: [
365     [ SYMBOL "\\subst";  (* to avoid catching frequent "a [1]" cases *)
366       SYMBOL "[";
367       substs = LIST1 [
368         i = IDENT; SYMBOL <:unicode<Assign>> (* ≔ *); t = l2_pattern -> (i, t)
369       ] SEP SYMBOL ";";
370       SYMBOL "]" ->
371         substs
372     ]
373   ];
374   meta_subst: [
375     [ s = SYMBOL "_" -> None
376     | p = l2_pattern -> Some p ]
377   ];
378   meta_substs: [
379     [ SYMBOL "["; substs = LIST0 meta_subst; SYMBOL "]" -> substs ]
380   ];
381   possibly_typed_name: [
382     [ SYMBOL "("; id = bound_name; SYMBOL ":"; typ = l2_pattern; SYMBOL ")" ->
383         id, Some typ
384     | id = bound_name -> id, None
385     ]
386   ];
387   match_pattern: [
388     [ id = IDENT -> id, []
389     | SYMBOL "("; id = IDENT; vars = LIST1 possibly_typed_name; SYMBOL ")" ->
390         id, vars
391     ]
392   ];
393   binder: [
394     [ SYMBOL <:unicode<Pi>>     (* Π *) -> `Pi
395     | SYMBOL <:unicode<exists>> (* ∃ *) -> `Exists
396     | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall
397     | SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda
398     ]
399   ];
400   bound_name: [
401     [ i = IDENT -> Ident (i, None)
402     | SYMBOL "\\FRESH"; i = IDENT -> Variable (FreshVar i)
403     ]
404   ];
405   bound_names: [
406     [ vars = LIST1 bound_name SEP SYMBOL ",";
407       ty = OPT [ SYMBOL ":"; p = l2_pattern -> p ] ->
408         [ vars, ty ]
409     | clusters = LIST1 [
410         SYMBOL "(";
411         vars = LIST1 bound_name SEP SYMBOL ",";
412         ty = OPT [ SYMBOL ":"; p = l2_pattern -> p ];
413         SYMBOL ")" ->
414           vars, ty
415       ] ->
416         clusters
417     ]
418   ];
419   induction_kind: [
420     [ IDENT "rec" -> `Inductive
421     | IDENT "corec" -> `CoInductive
422     ]
423   ];
424   let_defs: [
425     [ defs = LIST1 [
426         name = bound_name; args = bound_names;
427         index_name = OPT [ IDENT "on"; id = bound_name -> id ];
428         ty = OPT [ SYMBOL ":" ; p = l2_pattern -> p ];
429         SYMBOL <:unicode<def>> (* ≝ *); body = l2_pattern ->
430           let body = fold_binder `Lambda args body in
431           let ty = 
432             match ty with 
433             | None -> None
434             | Some ty -> Some (fold_binder `Pi args ty)
435           in
436           let rec position_of name p = function 
437             | [] -> None, p
438             | n :: _ when n = name -> Some p, p
439             | _ :: tl -> position_of name (p + 1) tl
440           in
441           let rec find_arg name n = function 
442             | [] ->
443                 fail loc (sprintf "Argument %s not found"
444                   (CicNotationPp.pp_term name))
445             | (l,_) :: tl -> 
446                 (match position_of name 0 l with
447                 | None, len -> find_arg name (n + len) tl
448                 | Some where, len -> n + where)
449           in
450           let index = 
451             match index_name with 
452             | None -> 0 
453             | Some name -> find_arg name 0 args
454           in
455           (name, ty), body, index
456       ] SEP IDENT "and" ->
457         defs
458     ]
459   ];
460   l2_pattern_variable: [
461     [ SYMBOL "\\TERM"; id = IDENT -> TermVar id
462     | SYMBOL "\\NUM"; id = IDENT -> NumVar id
463     | SYMBOL "\\IDENT"; id = IDENT -> IdentVar id
464     | SYMBOL "\\FRESH"; id = IDENT -> FreshVar id
465     ]
466   ];
467   l2_magic_pattern: [
468     [ SYMBOL "\\FOLD";
469       kind = [ IDENT "left" -> `Left | IDENT "right" -> `Right ];
470       DELIM "\\["; base = l2_pattern; DELIM "\\]";
471       SYMBOL "\\LAMBDA"; id = IDENT;
472       DELIM "\\["; recursive = l2_pattern; DELIM "\\]" ->
473         Fold (kind, base, [id], recursive)
474     | SYMBOL "\\DEFAULT";
475       DELIM "\\["; some = l2_pattern; DELIM "\\]";
476       DELIM "\\["; none = l2_pattern; DELIM "\\]" ->
477         Default (some, none)
478     ]
479   ];
480   l2_pattern: LEVEL "10"  (* let in *)
481     [ "10" NONA
482       [ IDENT "let"; var = possibly_typed_name; SYMBOL <:unicode<def>> (* ≝ *);
483         p1 = l2_pattern; "in"; p2 = l2_pattern ->
484           return_term loc (LetIn (var, p1, p2))
485       | IDENT "let"; k = induction_kind; defs = let_defs; IDENT "in";
486         body = l2_pattern ->
487           return_term loc (LetRec (k, defs, body))
488       ]
489     ];
490   l2_pattern: LEVEL "20"  (* binder *)
491     [ "20" RIGHTA
492       [ b = binder; names = bound_names; SYMBOL "."; body = l2_pattern ->
493           return_term loc (fold_binder b names body)
494       ]
495     ];
496   l2_pattern: LEVEL "70"  (* apply *)
497     [ "70" LEFTA
498       [ p1 = l2_pattern; p2 = l2_pattern ->
499           let rec aux = function
500             | Appl (hd :: tl)
501             | AttributedTerm (_, Appl (hd :: tl)) ->
502                 aux hd @ tl
503             | term -> [term]
504           in
505           return_term loc (Appl (aux p1 @ [p2]))
506       ]
507     ];
508   l2_pattern: LEVEL "90"  (* simple *)
509     [ "90" NONA
510       [ id = IDENT -> return_term loc (Ident (id, None))
511       | id = IDENT; s = explicit_subst -> return_term loc (Ident (id, Some s))
512       | u = URI -> return_term loc (Uri (u, None))
513       | n = NUMBER -> prerr_endline "number"; return_term loc (Num (n, 0))
514       | IMPLICIT -> return_term loc (Implicit)
515       | m = META -> return_term loc (Meta (int_of_string m, []))
516       | m = META; s = meta_substs -> return_term loc (Meta (int_of_string m, s))
517       | s = sort -> return_term loc (Sort s)
518       | outtyp = OPT [ SYMBOL "["; ty = l2_pattern; SYMBOL "]" -> ty ];
519         IDENT "match"; t = l2_pattern;
520         indty_ident = OPT [ SYMBOL ":"; id = IDENT -> id ];
521         IDENT "with"; SYMBOL "[";
522         patterns = LIST0 [
523           lhs = match_pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *);
524           rhs = l2_pattern ->
525             lhs, rhs
526         ] SEP SYMBOL "|";
527         SYMBOL "]" ->
528           return_term loc (Case (t, indty_ident, outtyp, patterns))
529       | SYMBOL "("; p1 = l2_pattern; SYMBOL ":"; p2 = l2_pattern; SYMBOL ")" ->
530           return_term loc (Appl [ Symbol ("cast", 0); p1; p2 ])
531       | SYMBOL "("; p = l2_pattern; SYMBOL ")" -> p
532       | v = l2_pattern_variable -> return_term loc (Variable v)
533       | m = l2_magic_pattern -> return_term loc (Magic m)
534       ]
535     ];
536 (* }}} *)
537 (* {{{ Grammar for interpretation, notation level 3 *)
538   argument: [
539     [ id = IDENT -> IdentArg id
540     | SYMBOL <:unicode<eta>> (* η *); SYMBOL "."; a = SELF -> EtaArg (None, a)
541     | SYMBOL <:unicode<eta>> (* η *); id = IDENT; SYMBOL "."; a = SELF ->
542         EtaArg (Some id, a)
543     ]
544   ];
545   level3_term: [
546     [ u = URI -> UriPattern u
547     | a = argument -> ArgPattern a
548     | SYMBOL "("; terms = LIST1 SELF; SYMBOL ")" ->
549         (match terms with
550         | [] -> assert false
551         | [term] -> term
552         | terms -> ApplPattern terms)
553     ]
554   ];
555 (* }}} *)
556 (* {{{ Notation glues *)
557   associativity: [
558     [ IDENT "left";  IDENT "associative" -> Gramext.LeftA
559     | IDENT "right"; IDENT "associative" -> Gramext.RightA
560     | IDENT "non"; IDENT "associative" -> Gramext.NonA
561     ]
562   ];
563   precedence: [
564     [ IDENT "with"; IDENT "precedence"; n = NUMBER -> int_of_string n ]
565   ];
566   notation: [
567     [ IDENT "notation";
568       p1 = level1_pattern;
569       assoc = OPT associativity; prec = OPT precedence;
570       IDENT "for"; p2 = level2_pattern ->
571         (p1, assoc, prec, p2)
572     ]
573   ];
574   interpretation: [
575     [ IDENT "interpretation"; s = SYMBOL; args = LIST1 argument; IDENT "as";
576       t = level3_term ->
577         ()
578     ]
579   ];
580 (* }}} *)
581 (* {{{ Top-level phrases *)
582   phrase: [
583     [ IDENT "print"; p2 = level2_pattern; SYMBOL "." -> Print p2
584     | (l1, assoc, prec, l2) = notation; SYMBOL "." ->
585         Notation (l1, assoc, prec, l2)
586     ]
587   ];
588 (* }}} *)
589 END
590
591 (** {2 API implementation} *)
592
593 let exc_located_wrapper f =
594   try
595     f ()
596   with
597   | Stdpp.Exc_located (floc, Stream.Error msg) ->
598       raise (Parse_error (floc, msg))
599   | Stdpp.Exc_located (floc, exn) ->
600       raise (Parse_error (floc, (Printexc.to_string exn)))
601
602 let parse_syntax_pattern stream =
603   exc_located_wrapper (fun () -> Grammar.Entry.parse level1_pattern stream)
604 let parse_ast_pattern stream =
605   exc_located_wrapper (fun () -> Grammar.Entry.parse level2_pattern stream)
606 let parse_interpretation stream =
607   exc_located_wrapper (fun () -> Grammar.Entry.parse level3_term stream)
608 let parse_phrase stream =
609   exc_located_wrapper (fun () -> Grammar.Entry.parse phrase stream)
610
611 (** {2 Debugging} *)
612
613 let print_l2_pattern () =
614   Grammar.print_entry Format.std_formatter (Grammar.Entry.obj l2_pattern);
615   Format.pp_print_flush Format.std_formatter ();
616   flush stdout
617
618 (* vim:set encoding=utf8 foldmethod=marker: *)