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