]> 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 let_in_prec = 10
41 let binder_prec = 20
42 let apply_prec = 70
43 let simple_prec = 90
44
45 let let_in_assoc = Gramext.NonA
46 let binder_assoc = Gramext.RightA
47 let apply_assoc = Gramext.LeftA
48 let simple_assoc = Gramext.NonA
49
50 let level1_pattern = Grammar.Entry.create grammar "level1_pattern"
51 let level2_pattern = Grammar.Entry.create grammar "level2_pattern"
52 let level3_term = Grammar.Entry.create grammar "level3_term"
53 let l2_pattern = Grammar.Entry.create grammar "l2_pattern"
54 let notation = Grammar.Entry.create grammar "notation" (* level1 <-> level 2 *)
55 let interpretation =
56   Grammar.Entry.create grammar "interpretation" (* level2 <-> level 3 *)
57 let phrase = Grammar.Entry.create grammar "phrase"
58
59 let return_term loc term = ()
60
61 let fail floc msg =
62   let (x, y) = loc_of_floc floc in
63   failwith (sprintf "Error at characters %d - %d: %s" x y msg)
64
65 let int_of_string s =
66   try
67     Pervasives.int_of_string s
68   with Failure _ ->
69     failwith (sprintf "Lexer failure: string_of_int \"%s\" failed" s)
70
71 (** {2 Grammar extension} *)
72
73 let symbol s = Gramext.Stoken ("SYMBOL", s)
74 let ident s = Gramext.Stoken ("IDENT", s)
75 let number s = Gramext.Stoken ("NUMBER", s)
76 let term = Gramext.Sself
77
78 let g_symbol_of_literal =
79   function
80   | `Symbol s -> symbol s
81   | `Keyword s -> ident s
82   | `Number s -> number s
83
84 type binding =
85   | NoBinding
86   | Binding of string * value_type
87   | Env of (string * value_type) list
88
89 let make_action action bindings =
90   let rec aux (vl : CicNotationEnv.t) =
91     function
92       [] -> Gramext.action (fun (loc: location) -> action vl loc)
93     | NoBinding :: tl -> Gramext.action (fun _ -> aux vl tl)
94     (* LUCA: DEFCON 5 BEGIN *)
95     | Binding (name, TermType) :: tl ->
96         Gramext.action
97           (fun (v:term) -> aux ((name, (TermType, TermValue v))::vl) tl)
98     | Binding (name, StringType) :: tl ->
99         Gramext.action
100           (fun (v:string) ->
101             aux ((name, (StringType, StringValue v)) :: vl) tl)
102     | Binding (name, NumType) :: tl ->
103         Gramext.action
104           (fun (v:string) -> aux ((name, (NumType, NumValue v)) :: vl) tl)
105     | Binding (name, OptType t) :: tl ->
106         Gramext.action
107           (fun (v:'a option) ->
108             aux ((name, (OptType t, OptValue v)) :: vl) tl)
109     | Binding (name, ListType t) :: tl ->
110         Gramext.action
111           (fun (v:'a list) ->
112             aux ((name, (ListType t, ListValue v)) :: vl) tl)
113     | Env _ :: tl ->
114         Gramext.action (fun (v:CicNotationEnv.t) -> aux (v @ vl) tl)
115     (* LUCA: DEFCON 5 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 : CicNotationEnv.t 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           List.map
178             (function
179               | (n', (ty, ListValue vl)) as entry ->
180                   if n' = n then n', (ty, ListValue (v :: vl)) else entry
181               | _ -> assert false)
182             env
183         in
184         let grow_env env_i env =
185           List.fold_left
186             (fun env (n, (_, v)) -> grow_env_entry env n v)
187             env env_i
188         in *)
189         let action (env_list : CicNotationEnv.t list) (loc : location) =
190           CicNotationEnv.coalesce_env p_names env_list
191         in
192         let g_symbol s =
193           match magic with
194           | List0 (_, None) -> Gramext.Slist0 s
195           | List1 (_, None) -> Gramext.Slist1 s
196           | List0 (_, Some l) -> Gramext.Slist0sep (s, g_symbol_of_literal l)
197           | List1 (_, Some l) -> Gramext.Slist1sep (s, g_symbol_of_literal l)
198           | _ -> assert false
199         in
200         [ Env (List.map list_declaration p_names),
201           Gramext.srules
202             [ [ g_symbol (Gramext.srules [ p_atoms, p_action ]) ],
203               Gramext.action action ] ]
204     | _ -> assert false
205   and aux_variable =
206     function
207     | NumVar s -> [Binding (s, NumType), number ""]
208     | TermVar s -> [Binding (s, TermType), term]
209     | IdentVar s -> [Binding (s, StringType), ident ""]
210     | Ascription (p, s) -> assert false (* TODO *)
211     | FreshVar _ -> assert false
212   and inner_pattern p =
213     let p_bindings, p_atoms = List.split (aux p) in
214     let p_names = flatten_opt p_bindings in
215     let _ = prerr_endline ("inner names: " ^ String.concat " " (List.map fst p_names)) in
216     let action =
217       make_action (fun (env : CicNotationEnv.t) (loc : location) -> env)
218         p_bindings
219     in
220     p_bindings, p_atoms, p_names, action
221   in
222   aux pattern
223
224 let level_of_int precedence =
225   if precedence < min_precedence || precedence > max_precedence then
226     raise (Level_not_found precedence);
227   string_of_int precedence
228
229 type rule_id = Token.t Gramext.g_symbol list
230
231 let extend level1_pattern ?(precedence = default_precedence)
232   ?associativity action
233 =
234   let p_bindings, p_atoms =
235     List.split (extract_term_production level1_pattern)
236   in
237   let level = level_of_int precedence in
238   let p_names = flatten_opt p_bindings in
239   let _ =
240     prerr_endline (string_of_int (List.length p_bindings));
241     Grammar.extend
242       [ Grammar.Entry.obj (l2_pattern: 'a Grammar.Entry.e),
243         Some (Gramext.Level level),
244         [ None,
245           associativity,
246           [ p_atoms, 
247             (make_action
248               (fun (env: CicNotationEnv.t) (loc: location) -> (action env loc))
249               p_bindings) ]]]
250   in
251   p_atoms
252
253 let delete atoms = Grammar.delete_rule l2_pattern atoms
254
255 (** {2 Grammar} *)
256
257 let fold_binder binder pt_names body =
258   let fold_cluster binder terms ty body =
259     List.fold_right
260       (fun term body -> Binder (binder, (term, ty), body))
261       terms body  (* terms are names: either Ident or FreshVar *)
262   in
263   List.fold_right
264     (fun (names, ty) body -> fold_cluster binder names ty body)
265     pt_names body
266
267 let return_term loc term = AttributedTerm (`Loc loc, term)
268
269 let _ = (* create empty precedence level for "l2_pattern" *)
270   let mk_level_list first last =
271     let rec aux acc = function
272       | i when i < first -> acc
273       | i -> aux ((Some (string_of_int i), None, []) :: acc) (i - 1)
274     in
275     aux [] last
276   in
277   Grammar.extend
278     [ Grammar.Entry.obj (l2_pattern: 'a Grammar.Entry.e),
279       None,
280       mk_level_list min_precedence max_precedence ]
281
282 EXTEND
283   GLOBAL: level1_pattern level2_pattern level3_term
284           l2_pattern
285           notation interpretation
286           phrase;
287 (* {{{ Grammar for concrete syntax patterns, notation level 1 *)
288   level1_pattern: [ [ p = l1_simple_pattern -> p ] ];
289   l1_pattern: [ [ p = LIST1 l1_simple_pattern -> p ] ];
290   literal: [
291     [ s = SYMBOL -> `Symbol s
292     | k = KEYWORD -> `Keyword k
293     | n = NUMBER -> `Number n
294     ]
295   ];
296   sep:       [ [ SYMBOL "\\SEP";      sep = literal -> sep ] ];
297 (*   row_sep:   [ [ SYMBOL "\\ROWSEP";   sep = literal -> sep ] ];
298   field_sep: [ [ SYMBOL "\\FIELDSEP"; sep = literal -> sep ] ]; *)
299   l1_magic_pattern: [
300     [ SYMBOL "\\LIST0"; p = l1_simple_pattern; sep = OPT sep -> List0 (p, sep)
301     | SYMBOL "\\LIST1"; p = l1_simple_pattern; sep = OPT sep -> List1 (p, sep)
302     | SYMBOL "\\OPT";   p = l1_simple_pattern -> Opt p
303     ]
304   ];
305   l1_pattern_variable: [
306     [ SYMBOL "\\TERM"; id = IDENT -> TermVar id
307     | SYMBOL "\\NUM"; id = IDENT -> NumVar id
308     | SYMBOL "\\IDENT"; id = IDENT -> IdentVar id
309     ]
310   ];
311   l1_simple_pattern:
312     [ "layout" LEFTA
313       [ p1 = SELF; SYMBOL "\\SUB"; p2 = SELF ->
314           return_term loc (Layout (Sub (p1, p2)))
315       | p1 = SELF; SYMBOL "\\SUP"; p2 = SELF ->
316           return_term loc (Layout (Sup (p1, p2)))
317       | p1 = SELF; SYMBOL "\\BELOW"; p2 = SELF ->
318           return_term loc (Layout (Below (p1, p2)))
319       | p1 = SELF; SYMBOL "\\ABOVE"; p2 = SELF ->
320           return_term loc (Layout (Above (p1, p2)))
321       | p1 = SELF; SYMBOL "\\OVER"; p2 = SELF ->
322           return_term loc (Layout (Over (p1, p2)))
323       | p1 = SELF; SYMBOL "\\ATOP"; p2 = SELF ->
324           return_term loc (Layout (Atop (p1, p2)))
325 (*       | SYMBOL "\\ARRAY"; p = SELF; csep = OPT field_sep; rsep = OPT row_sep ->
326           return_term loc (Array (p, csep, rsep)) *)
327       | SYMBOL "\\FRAC"; p1 = SELF; p2 = SELF ->
328           return_term loc (Layout (Frac (p1, p2)))
329       | SYMBOL "\\SQRT"; p = SELF -> return_term loc (Layout (Sqrt p))
330       | SYMBOL "\\ROOT"; index = SELF; SYMBOL "\\OF"; arg = SELF ->
331           return_term loc (Layout (Root (arg, index)));
332       | SYMBOL "\\HBOX"; DELIM "\\["; p = l1_pattern; DELIM "\\]" ->
333           return_term loc (Layout (Box ((H, false, false), p)))
334       | SYMBOL "\\VBOX"; DELIM "\\["; p = l1_pattern; DELIM "\\]" ->
335           return_term loc (Layout (Box ((V, false, false), p)))
336       | SYMBOL "\\HVBOX"; DELIM "\\["; p = l1_pattern; DELIM "\\]" ->
337           return_term loc (Layout (Box ((HV, false, false), p)))
338       | SYMBOL "\\HOVBOX"; DELIM "\\["; p = l1_pattern; DELIM "\\]" ->
339           return_term loc (Layout (Box ((HOV, false, false), p)))
340 (*       | SYMBOL "\\BREAK" -> return_term loc (Layout Break) *)
341 (*       | SYMBOL "\\SPACE" -> return_term loc (Layout Space) *)
342       | DELIM "\\["; p = l1_pattern; DELIM "\\]" ->
343           return_term loc (CicNotationUtil.boxify p)
344       | p = SELF; SYMBOL "\\AS"; id = IDENT ->
345           return_term loc (Variable (Ascription (p, id)))
346       ]
347     | "simple" NONA
348       [ i = IDENT -> return_term loc (Ident (i, None))
349       | m = l1_magic_pattern -> return_term loc (Magic m)
350       | v = l1_pattern_variable -> return_term loc (Variable v)
351       | l = literal -> return_term loc (Literal l)
352       ]
353     ];
354 (* }}} *)
355 (* {{{ Grammar for ast patterns, notation level 2 *)
356   level2_pattern: [ [ p = l2_pattern -> p ] ];
357   sort: [
358     [ SYMBOL "\\PROP" -> `Prop
359     | SYMBOL "\\SET" -> `Set
360     | SYMBOL "\\TYPE" -> `Type
361     ]
362   ];
363   explicit_subst: [
364     [ SYMBOL "\\subst";  (* to avoid catching frequent "a [1]" cases *)
365       SYMBOL "[";
366       substs = LIST1 [
367         i = IDENT; SYMBOL <:unicode<Assign>> (* ≔ *); t = l2_pattern -> (i, t)
368       ] SEP SYMBOL ";";
369       SYMBOL "]" ->
370         substs
371     ]
372   ];
373   meta_subst: [
374     [ s = SYMBOL "_" -> None
375     | p = l2_pattern -> Some p ]
376   ];
377   meta_substs: [
378     [ SYMBOL "["; substs = LIST0 meta_subst; SYMBOL "]" -> substs ]
379   ];
380   possibly_typed_name: [
381     [ SYMBOL "("; id = bound_name; SYMBOL ":"; typ = l2_pattern; SYMBOL ")" ->
382         id, Some typ
383     | id = bound_name -> id, None
384     ]
385   ];
386   match_pattern: [
387     [ id = IDENT -> id, []
388     | SYMBOL "("; id = IDENT; vars = LIST1 possibly_typed_name; SYMBOL ")" ->
389         id, vars
390     ]
391   ];
392   binder: [
393     [ SYMBOL <:unicode<Pi>>     (* Π *) -> `Pi
394     | SYMBOL <:unicode<exists>> (* ∃ *) -> `Exists
395     | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall
396     | SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda
397     ]
398   ];
399   bound_name: [
400     [ i = IDENT -> Ident (i, None)
401     | SYMBOL "\\FRESH"; i = IDENT -> Variable (FreshVar i)
402     ]
403   ];
404   bound_names: [
405     [ vars = LIST1 bound_name SEP SYMBOL ",";
406       ty = OPT [ SYMBOL ":"; p = l2_pattern -> p ] ->
407         [ vars, ty ]
408     | clusters = LIST1 [
409         SYMBOL "(";
410         vars = LIST1 bound_name SEP SYMBOL ",";
411         ty = OPT [ SYMBOL ":"; p = l2_pattern -> p ];
412         SYMBOL ")" ->
413           vars, ty
414       ] ->
415         clusters
416     ]
417   ];
418   induction_kind: [
419     [ IDENT "rec" -> `Inductive
420     | IDENT "corec" -> `CoInductive
421     ]
422   ];
423   let_defs: [
424     [ defs = LIST1 [
425         name = bound_name; args = bound_names;
426         index_name = OPT [ IDENT "on"; id = bound_name -> id ];
427         ty = OPT [ SYMBOL ":" ; p = l2_pattern -> p ];
428         SYMBOL <:unicode<def>> (* ≝ *); body = l2_pattern ->
429           let body = fold_binder `Lambda args body in
430           let ty = 
431             match ty with 
432             | None -> None
433             | Some ty -> Some (fold_binder `Pi args ty)
434           in
435           let rec position_of name p = function 
436             | [] -> None, p
437             | n :: _ when n = name -> Some p, p
438             | _ :: tl -> position_of name (p + 1) tl
439           in
440           let rec find_arg name n = function 
441             | [] ->
442                 fail loc (sprintf "Argument %s not found"
443                   (CicNotationPp.pp_term name))
444             | (l,_) :: tl -> 
445                 (match position_of name 0 l with
446                 | None, len -> find_arg name (n + len) tl
447                 | Some where, len -> n + where)
448           in
449           let index = 
450             match index_name with 
451             | None -> 0 
452             | Some name -> find_arg name 0 args
453           in
454           (name, ty), body, index
455       ] SEP IDENT "and" ->
456         defs
457     ]
458   ];
459   l2_pattern_variable: [
460     [ SYMBOL "\\TERM"; id = IDENT -> TermVar id
461     | SYMBOL "\\NUM"; id = IDENT -> NumVar id
462     | SYMBOL "\\IDENT"; id = IDENT -> IdentVar id
463     | SYMBOL "\\FRESH"; id = IDENT -> FreshVar id
464     ]
465   ];
466   l2_magic_pattern: [
467     [ SYMBOL "\\FOLD";
468       kind = [ IDENT "left" -> `Left | IDENT "right" -> `Right ];
469       DELIM "\\["; base = l2_pattern; DELIM "\\]";
470       SYMBOL "\\LAMBDA"; id = IDENT;
471       DELIM "\\["; recursive = l2_pattern; DELIM "\\]" ->
472         Fold (kind, base, [id], recursive)
473     | SYMBOL "\\DEFAULT";
474       DELIM "\\["; some = l2_pattern; DELIM "\\]";
475       DELIM "\\["; none = l2_pattern; DELIM "\\]" ->
476         Default (some, none)
477     ]
478   ];
479   l2_pattern: LEVEL "10"  (* let in *)
480     [ "10" NONA
481       [ IDENT "let"; var = possibly_typed_name; SYMBOL <:unicode<def>> (* ≝ *);
482         p1 = l2_pattern; "in"; p2 = l2_pattern ->
483           return_term loc (LetIn (var, p1, p2))
484       | IDENT "let"; k = induction_kind; defs = let_defs; IDENT "in";
485         body = l2_pattern ->
486           return_term loc (LetRec (k, defs, body))
487       ]
488     ];
489   l2_pattern: LEVEL "20"  (* binder *)
490     [ "20" RIGHTA
491       [ b = binder; names = bound_names; SYMBOL "."; body = l2_pattern ->
492           return_term loc (fold_binder b names body)
493       ]
494     ];
495   l2_pattern: LEVEL "70"  (* apply *)
496     [ "70" LEFTA
497       [ p1 = l2_pattern; p2 = l2_pattern ->
498           let rec aux = function
499             | Appl (hd :: tl)
500             | AttributedTerm (_, Appl (hd :: tl)) ->
501                 aux hd @ tl
502             | term -> [term]
503           in
504           return_term loc (Appl (aux p1 @ [p2]))
505       ]
506     ];
507   l2_pattern: LEVEL "90"  (* simple *)
508     [ "90" NONA
509       [ id = IDENT -> return_term loc (Ident (id, None))
510       | id = IDENT; s = explicit_subst -> return_term loc (Ident (id, Some s))
511       | s = CSYMBOL -> return_term loc (Symbol (s, 0))
512       | u = URI -> return_term loc (Uri (u, None))
513       | n = 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 (0, id)
540     | l = LIST1 [ SYMBOL <:unicode<eta>> (* η *) -> () ] SEP SYMBOL ".";
541       SYMBOL "."; id = IDENT ->
542         IdentArg (List.length l, id)
543     ]
544   ];
545   level3_term: [
546     [ u = URI -> UriPattern (UriManager.uri_of_string u)
547     | id = IDENT -> VarPattern id
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     [ p1 = level1_pattern;
568       assoc = OPT associativity; prec = OPT precedence;
569       IDENT "for"; p2 = level2_pattern ->
570         (p1, assoc, prec, p2)
571     ]
572   ];
573   interpretation: [
574     [ s = CSYMBOL; args = LIST1 argument; SYMBOL "="; t = level3_term ->
575         (s, args, t)
576     ]
577   ];
578 (* }}} *)
579 (* {{{ Top-level phrases *)
580   phrase: [
581     [ IDENT "print"; p2 = level2_pattern; SYMBOL "." -> Print p2
582     | IDENT "notation"; (l1, assoc, prec, l2) = notation; SYMBOL "." ->
583         Notation (l1, assoc, prec, l2)
584     | IDENT "interpretation"; (symbol, args, l3) = interpretation; SYMBOL "." ->
585         Interpretation ((symbol, args), l3)
586     | IDENT "render"; u = URI; SYMBOL "." -> Render (UriManager.uri_of_string u)
587     ]
588   ];
589 (* }}} *)
590 END
591
592 (** {2 API implementation} *)
593
594 let exc_located_wrapper f =
595   try
596     f ()
597   with
598   | Stdpp.Exc_located (floc, Stream.Error msg) ->
599       raise (Parse_error (floc, msg))
600   | Stdpp.Exc_located (floc, exn) ->
601       raise (Parse_error (floc, (Printexc.to_string exn)))
602
603 let parse_syntax_pattern stream =
604   exc_located_wrapper (fun () -> Grammar.Entry.parse level1_pattern stream)
605 let parse_ast_pattern stream =
606   exc_located_wrapper (fun () -> Grammar.Entry.parse level2_pattern stream)
607 let parse_interpretation stream =
608   exc_located_wrapper (fun () -> Grammar.Entry.parse level3_term stream)
609 let parse_phrase stream =
610   exc_located_wrapper (fun () -> Grammar.Entry.parse phrase stream)
611
612 (** {2 Debugging} *)
613
614 let print_l2_pattern () =
615   Grammar.print_entry Format.std_formatter (Grammar.Entry.obj l2_pattern);
616   Format.pp_print_flush Format.std_formatter ();
617   flush stdout
618
619 (* vim:set encoding=utf8 foldmethod=marker: *)