]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_disambiguation/cicTextualParser2.ml
478e97d390531fe2df2e77f29570a0e0c17455cb
[helm.git] / helm / ocaml / cic_disambiguation / cicTextualParser2.ml
1 (* Copyright (C) 2004, 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 let debug = true
27 let debug_print s =
28   if debug then begin
29     prerr_endline "<NEW_TEXTUAL_PARSER>";
30     prerr_endline s;
31     prerr_endline "</NEW_TEXTUAL_PARSER>"
32   end
33
34   (** if set to true each number will have a different insance number and can
35   * thus be interpreted differently than others *)
36 let use_fresh_num_instances = false
37
38 open Printf
39
40 open DisambiguateTypes
41
42 exception Parse_error of Token.flocation * string
43
44 let fresh_num_instance =
45   let n = ref 0 in
46   if use_fresh_num_instances then
47     (fun () -> incr n; !n)
48   else
49     (fun () -> 0)
50
51 let choice_of_uri uri =
52   let term = CicUtil.term_of_uri uri in
53   (uri, (fun _ _ _ -> term))
54
55 let grammar = Grammar.gcreate CicTextualLexer2.cic_lexer
56
57 let term = Grammar.Entry.create grammar "term"
58 let term0 = Grammar.Entry.create grammar "term0"
59 let tactic = Grammar.Entry.create grammar "tactic"
60 let tactical = Grammar.Entry.create grammar "tactical"
61 let tactical0 = Grammar.Entry.create grammar "tactical0"
62 let command = Grammar.Entry.create grammar "command"
63 let script = Grammar.Entry.create grammar "script"
64
65 let return_term loc term = CicAst.AttributedTerm (`Loc loc, term)
66 let return_tactic loc tactic = TacticAst.LocatedTactic (loc, tactic)
67 let return_tactical loc tactical = TacticAst.LocatedTactical (loc, tactical)
68 let return_command loc cmd = cmd  (* TODO ZACK FIXME uhm ... why we drop loc? *)
69
70 let fail floc msg =
71   let (x, y) = CicAst.loc_of_floc floc in
72   failwith (Printf.sprintf "Error at characters %d - %d: %s" x y msg)
73
74 let name_of_string = function
75   | "_" -> Cic.Anonymous
76   | s -> Cic.Name s
77
78 let int_opt = function
79   | None -> None
80   | Some lexeme -> Some (int_of_string lexeme)
81
82   (** the uri of an inductive type (a ".ind" uri) is not meaningful without an
83   * xpointer. Still, it's likely that an user who wrote "cic:/blabla/foo.ind"
84   * actually meant "cic:/blabla/foo.ind#xpointer(1/1)", i.e. the first inductive
85   * type in a block of mutual inductive types.
86   *
87   * This function performs the expansion foo.ind -> foo#xpointer..., if needed
88   *)
89 let ind_expansion uri =
90   let len = String.length uri in
91   if len >= 4 && String.sub uri (len - 4) 4 = ".ind" then
92     uri ^ "#xpointer(1/1)"
93   else
94     uri
95
96 EXTEND
97   GLOBAL: term term0 tactic tactical tactical0 command script;
98   int: [
99     [ num = NUM ->
100         try
101           int_of_string num
102         with Failure _ -> raise (Parse_error (loc, "integer literal expected"))
103     ]
104   ];
105   meta_subst: [
106     [ s = SYMBOL "_" -> None
107     | t = term -> Some t ]
108   ];
109   binder: [
110     [ SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda
111     | SYMBOL <:unicode<Pi>>     (* Π *) -> `Pi
112     | SYMBOL <:unicode<exists>> (* ∃ *) -> `Exists
113     | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall ]
114   ];
115   sort: [
116     [ "Prop" -> `Prop
117     | "Set" -> `Set
118     | "Type" -> `Type
119     | "CProp" -> `CProp ]
120   ];
121   typed_name: [
122     [ PAREN "("; i = IDENT; SYMBOL ":"; typ = term; PAREN ")" ->
123         (name_of_string i, Some typ)
124     | i = IDENT -> (name_of_string i, None)
125     ]
126   ];
127   subst: [
128     [ subst = OPT [
129         SYMBOL "\\subst";  (* to avoid catching frequent "a [1]" cases *)
130         PAREN "[";
131         substs = LIST1 [
132           i = IDENT; SYMBOL <:unicode<Assign>> (* ≔ *); t = term -> (i, t)
133         ] SEP SYMBOL ";";
134         PAREN "]" ->
135           substs
136       ] -> subst
137     ]
138   ];
139   substituted_name: [ (* a subs.name is an explicit substitution subject *)
140     [ s = IDENT; subst = subst -> CicAst.Ident (s, subst)
141     | s = URI; subst = subst -> CicAst.Uri (ind_expansion s, subst)
142     ]
143   ];
144   name: [ (* as substituted_name with no explicit substitution *)
145     [ s = [ IDENT | SYMBOL ] -> s ]
146   ];
147   pattern: [
148     [ n = name -> (n, [])
149     | PAREN "("; head = name; vars = LIST1 typed_name; PAREN ")" ->
150         (head, vars)
151     ]
152   ];
153   constructor: [ [ name = IDENT; SYMBOL ":"; typ = term -> (name, typ) ] ];
154   term0: [ [ t = term; EOI -> return_term loc t ] ];
155   term:
156     [ "letin" NONA
157       [ "let"; var = typed_name;
158         SYMBOL "="; (* SYMBOL <:unicode<def>> (* ≝ *); *)
159         t1 = term; "in"; t2 = term ->
160           return_term loc (CicAst.LetIn (var, t1, t2))
161       | "let"; ind_kind = [ "corec" -> `CoInductive | "rec"-> `Inductive ];
162           defs = LIST1 [
163             var = typed_name;
164             index = OPT [ PAREN "("; index = NUM; PAREN ")" ->
165               int_of_string index
166             ];
167             SYMBOL "="; (* SYMBOL <:unicode<def>> (* ≝ *); *)
168             t1 = term ->
169               (var, t1, (match index with None -> 0 | Some i -> i))
170           ] SEP "and";
171           "in"; body = term ->
172             return_term loc (CicAst.LetRec (ind_kind, defs, body))
173       ]
174     | "binder" RIGHTA
175       [
176         b = binder;
177         (vars, typ) =
178           [ vars = LIST1 IDENT SEP SYMBOL ",";
179             typ = OPT [ SYMBOL ":"; t = term -> t ] -> (vars, typ)
180           | PAREN "("; vars = LIST1 IDENT SEP SYMBOL ",";
181             typ = OPT [ SYMBOL ":"; t = term -> t ]; PAREN ")" -> (vars, typ)
182           ];
183         SYMBOL "."; body = term ->
184           let binder =
185             List.fold_right
186               (fun var body ->
187                 let name = name_of_string var in
188                 CicAst.Binder (b, (name, typ), body))
189               vars body
190           in
191           return_term loc binder
192       | t1 = term; SYMBOL <:unicode<to>> (* → *); t2 = term ->
193             return_term loc
194               (CicAst.Binder (`Pi, (Cic.Anonymous, Some t1), t2))
195       ]
196     | "logic_add" LEFTA   [ (* nothing here by default *) ]
197     | "logic_mult" LEFTA  [ (* nothing here by default *) ]
198     | "logic_inv" NONA    [ (* nothing here by default *) ]
199     | "relop" LEFTA
200       [ t1 = term; SYMBOL "="; t2 = term ->
201         return_term loc (CicAst.Appl [CicAst.Symbol ("eq", 0); t1; t2])
202       ]
203     | "add" LEFTA     [ (* nothing here by default *) ]
204     | "mult" LEFTA    [ (* nothing here by default *) ]
205     | "power" LEFTA   [ (* nothing here by default *) ]
206     | "inv" NONA      [ (* nothing here by default *) ]
207     | "apply" LEFTA
208       [ t1 = term; t2 = term ->
209         let rec aux = function
210           | CicAst.Appl (hd :: tl) -> aux hd @ tl
211           | term -> [term]
212         in
213         CicAst.Appl (aux t1 @ [t2])
214       ]
215     | "simple" NONA
216       [ sort = sort -> CicAst.Sort sort
217       | n = substituted_name -> return_term loc n
218       | i = NUM -> return_term loc (CicAst.Num (i, (fresh_num_instance ())))
219       | IMPLICIT -> return_term loc CicAst.Implicit
220       | m = META;
221         substs = [
222           PAREN "["; substs = LIST0 meta_subst SEP SYMBOL ";" ; PAREN "]" ->
223             substs
224         ] ->
225             let index =
226               try
227                 int_of_string (String.sub m 1 (String.length m - 1))
228               with Failure "int_of_string" ->
229                 fail loc ("Invalid meta variable number: " ^ m)
230             in
231             return_term loc (CicAst.Meta (index, substs))
232       | outtyp = OPT [ PAREN "["; typ = term; PAREN "]" -> typ ];
233         "match"; t = term;
234         indty_ident = OPT [ SYMBOL ":"; id = IDENT -> id ];
235         "with";
236         PAREN "[";
237         patterns = LIST0 [
238           lhs = pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *); rhs = term ->
239             ((lhs: CicAst.case_pattern), rhs)
240         ] SEP SYMBOL "|";
241         PAREN "]" ->
242           return_term loc
243             (CicAst.Case (t, indty_ident, outtyp, patterns))
244       | PAREN "("; t1 = term; SYMBOL ":"; t2 = term; PAREN ")" ->
245           return_term loc (CicAst.Appl [CicAst.Symbol ("cast", 0); t1; t2])
246       | PAREN "("; t = term; PAREN ")" -> return_term loc t
247       ]
248     ];
249   tactic_where: [
250     [ where = OPT [ "in"; ident = IDENT -> ident ] -> where ]
251   ];
252   tactic_term: [ [ t = term -> t ] ];
253   ident_list0: [
254     [ PAREN "["; idents = LIST0 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
255   ];
256   ident_list1: [
257     [ PAREN "["; idents = LIST1 IDENT SEP SYMBOL ";"; PAREN "]" -> idents ]
258   ];
259   reduction_kind: [
260     [ [ IDENT "reduce" | IDENT "Reduce" ] -> `Reduce
261     | [ IDENT "simplify" | IDENT "Simplify" ] -> `Simpl
262     | [ IDENT "whd" | IDENT "Whd" ] -> `Whd ]
263   ];
264   tactic: [
265     [ [ IDENT "absurd" | IDENT "Absurd" ]; t = tactic_term ->
266         return_tactic loc (TacticAst.Absurd t)
267     | [ IDENT "apply" | IDENT "Apply" ]; t = tactic_term ->
268         return_tactic loc (TacticAst.Apply t)
269     | [ IDENT "assumption" | IDENT "Assumption" ] ->
270         return_tactic loc TacticAst.Assumption
271     | [ IDENT "auto" | IDENT "Auto" ] -> return_tactic loc TacticAst.Auto
272     | [ IDENT "change" | IDENT "Change" ];
273       t1 = tactic_term; "with"; t2 = tactic_term;
274       where = tactic_where ->
275         return_tactic loc (TacticAst.Change (t1, t2, where))
276     (* TODO Change_pattern *)
277     | [ IDENT "contradiction" | IDENT "Contradiction" ] ->
278         return_tactic loc TacticAst.Contradiction
279     | [ IDENT "cut" | IDENT "Cut" ];
280       t = tactic_term -> return_tactic loc (TacticAst.Cut t)
281     | [ IDENT "decompose" | IDENT "Decompose" ];
282       principles = ident_list1; where = IDENT ->
283         return_tactic loc (TacticAst.Decompose (where, principles))
284     | [ IDENT "discriminate" | IDENT "Discriminate" ];
285       hyp = IDENT ->
286         return_tactic loc (TacticAst.Discriminate hyp)
287     | [ IDENT "elimType" | IDENT "ElimType" ]; t = tactic_term ->
288         return_tactic loc (TacticAst.ElimType t)
289     | [ IDENT "elim" | IDENT "Elim" ];
290       t1 = tactic_term;
291       using = OPT [ "using"; using = tactic_term -> using ] ->
292         return_tactic loc (TacticAst.Elim (t1, using))
293     | [ IDENT "exact" | IDENT "Exact" ]; t = tactic_term ->
294         return_tactic loc (TacticAst.Exact t)
295     | [ IDENT "exists" | IDENT "Exists" ] ->
296         return_tactic loc TacticAst.Exists
297     | [ IDENT "fold" | IDENT "Fold" ];
298       kind = reduction_kind; t = tactic_term ->
299         return_tactic loc (TacticAst.Fold (kind, t))
300     | [ IDENT "fourier" | IDENT "Fourier" ] ->
301         return_tactic loc TacticAst.Fourier
302     | [ IDENT "hint" | IDENT "Hint" ] -> return_tactic loc TacticAst.Hint
303     | [ IDENT "injection" | IDENT "Injection" ]; ident = IDENT ->
304         return_tactic loc (TacticAst.Injection ident)
305     | [ IDENT "intros" | IDENT "Intros" ];
306       num = OPT [ num = int -> num ];
307       idents = OPT ident_list0 ->
308         let idents = match idents with None -> [] | Some idents -> idents in
309         return_tactic loc (TacticAst.Intros (num, idents))
310     | [ IDENT "intro" | IDENT "Intro" ] ->
311         return_tactic loc (TacticAst.Intros (Some 1, []))
312     | [ IDENT "left" | IDENT "Left" ] -> return_tactic loc TacticAst.Left
313     | [ "let" | "Let" ];
314       t = tactic_term; "in"; where = IDENT ->
315         return_tactic loc (TacticAst.LetIn (t, where))
316     | kind = reduction_kind;
317       pat = OPT [
318         "in"; pat = [ IDENT "goal" -> `Goal | IDENT "hyp" -> `Everywhere ] ->
319           pat
320       ];
321       terms = LIST0 term SEP SYMBOL "," ->
322         let tac =
323           (match (pat, terms) with
324           | None, [] -> TacticAst.Reduce (kind, None)
325           | None, terms -> TacticAst.Reduce (kind, Some (terms, `Goal))
326           | Some pat, [] -> TacticAst.Reduce (kind, Some ([], pat))
327           | Some pat, terms -> TacticAst.Reduce (kind, Some (terms, pat)))
328         in
329         return_tactic loc tac
330     | [ IDENT "reflexivity" | IDENT "Reflexivity" ] ->
331         return_tactic loc TacticAst.Reflexivity
332     | [ IDENT "replace" | IDENT "Replace" ];
333       t1 = tactic_term; "with"; t2 = tactic_term ->
334         return_tactic loc (TacticAst.Replace (t1, t2))
335     (* TODO Rewrite *)
336     (* TODO Replace_pattern *)
337     | [ IDENT "right" | IDENT "Right" ] -> return_tactic loc TacticAst.Right
338     | [ IDENT "ring" | IDENT "Ring" ] -> return_tactic loc TacticAst.Ring
339     | [ IDENT "split" | IDENT "Split" ] -> return_tactic loc TacticAst.Split
340     | [ IDENT "symmetry" | IDENT "Symmetry" ] ->
341         return_tactic loc TacticAst.Symmetry
342     | [ IDENT "transitivity" | IDENT "Transitivity" ];
343       t = tactic_term ->
344         return_tactic loc (TacticAst.Transitivity t)
345     ]
346   ];
347   tactical0: [ [ t = tactical; SYMBOL "." -> return_tactical loc t ] ];
348   tactical:
349     [ "command" NONA
350       [ cmd = command -> return_tactical loc (TacticAst.Command cmd) ]
351     | "sequence" LEFTA
352       [ tactics = LIST1 NEXT SEP SYMBOL ";" ->
353           return_tactical loc (TacticAst.Seq tactics)
354       ]
355     | "then" NONA
356       [ tac = tactical;
357         PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
358           return_tactical loc (TacticAst.Then (tac, tacs))
359       ]
360     | "loops" RIGHTA
361       [ [ IDENT "do" | IDENT "Do" ]; count = int; tac = tactical ->
362           return_tactical loc (TacticAst.Do (count, tac))
363       | [ IDENT "repeat" | IDENT "Repeat" ]; tac = tactical ->
364           return_tactical loc (TacticAst.Repeat tac)
365       ]
366     | "simple" NONA
367       [ [ IDENT "tries" | IDENT "Tries" ];
368         PAREN "["; tacs = LIST0 tactical SEP SYMBOL ";"; PAREN "]" ->
369           return_tactical loc (TacticAst.Tries tacs)
370       | [ IDENT "try" | IDENT "Try" ]; tac = NEXT ->
371           return_tactical loc (TacticAst.Try tac)
372       | [ IDENT "fail" | IDENT "Fail" ] -> return_tactical loc TacticAst.Fail
373       | [ IDENT "id" | IDENT "Id" ] -> return_tactical loc TacticAst.IdTac
374       | PAREN "("; tac = tactical; PAREN ")" -> return_tactical loc tac
375       | tac = tactic -> return_tactical loc (TacticAst.Tactic tac)
376       ]
377     ];
378   theorem_flavour: [  (* all flavours but Goal *)
379     [ [ IDENT "definition"  | IDENT "Definition"  ] -> `Definition
380     | [ IDENT "fact"        | IDENT "Fact"        ] -> `Fact
381     | [ IDENT "lemma"       | IDENT "Lemma"       ] -> `Lemma
382     | [ IDENT "remark"      | IDENT "Remark"      ] -> `Remark
383     | [ IDENT "theorem"     | IDENT "Theorem"     ] -> `Theorem
384     ]
385   ];
386   inductive_spec: [ [
387     fst_name = IDENT; params = LIST0 [
388       PAREN "("; names = LIST1 IDENT SEP SYMBOL ","; SYMBOL ":";
389       typ = term; PAREN ")" -> (names, typ) ];
390     SYMBOL ":"; fst_typ = term; SYMBOL <:unicode<def>>; OPT SYMBOL "|";
391     fst_constructors = LIST0 constructor SEP SYMBOL "|";
392     tl = OPT [ "with";
393       types = LIST1 [
394         name = IDENT; SYMBOL ":"; typ = term; SYMBOL <:unicode<def>>;
395        OPT SYMBOL "|"; constructors = LIST0 constructor SEP SYMBOL "|" ->
396           (name, true, typ, constructors) ] SEP "with" -> types
397     ] ->
398       let params =
399         List.fold_right
400           (fun (names, typ) acc ->
401             (List.map (fun name -> (name, typ)) names) @ acc)
402           params []
403       in
404       let fst_ind_type = (fst_name, true, fst_typ, fst_constructors) in
405       let tl_ind_types = match tl with None -> [] | Some types -> types in
406       let ind_types = fst_ind_type :: tl_ind_types in
407       (params, ind_types)
408   ] ];
409   command: [
410     [ [ IDENT "abort" | IDENT "Abort" ] -> return_command loc TacticAst.Abort
411     | [ IDENT "proof" | IDENT "Proof" ] -> return_command loc TacticAst.Proof
412     | [ IDENT "quit"  | IDENT "Quit"  ] -> return_command loc TacticAst.Quit
413     | [ IDENT "qed"   | IDENT "Qed"   ] ->
414         return_command loc (TacticAst.Qed None)
415     | [ IDENT "save"  | IDENT "Save"  ]; name = IDENT ->
416         return_command loc (TacticAst.Qed (Some name))
417     | flavour = theorem_flavour; name = OPT IDENT; SYMBOL ":"; typ = term;
418       body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
419         return_command loc (TacticAst.Theorem (flavour, name, typ, body))
420     | [ IDENT "inductive" | IDENT "Inductive" ]; spec = inductive_spec ->
421         let (params, ind_types) = spec in
422         return_command loc (TacticAst.Inductive (params, ind_types))
423     | [ IDENT "coinductive" | IDENT "CoInductive" ]; spec = inductive_spec ->
424         let (params, ind_types) = spec in
425         let ind_types = (* set inductive flags to false (coinductive) *)
426           List.map (fun (name, _, term, ctors) -> (name, false, term, ctors))
427             ind_types
428         in
429         return_command loc (TacticAst.Inductive (params, ind_types))
430     | [ IDENT "coercion" | IDENT "Coercion" ] ; name = IDENT -> 
431         return_command loc (TacticAst.Coercion (CicAst.Ident (name,Some [])))
432     | [ IDENT "coercion" | IDENT "Coercion" ] ; name = URI -> 
433         return_command loc (TacticAst.Coercion (CicAst.Uri (name,Some [])))
434     | [ IDENT "goal" | IDENT "Goal" ]; typ = term;
435       body = OPT [ SYMBOL <:unicode<def>> (* ≝ *); body = term -> body ] ->
436         return_command loc (TacticAst.Theorem (`Goal, None, typ, body))
437     | [ IDENT "undo"   | IDENT "Undo" ]; steps = OPT NUM ->
438         return_command loc (TacticAst.Undo (int_opt steps))
439     | [ IDENT "redo"   | IDENT "Redo" ]; steps = OPT NUM ->
440         return_command loc (TacticAst.Redo (int_opt steps))
441     | [ IDENT "baseuri"   | IDENT "Baseuri" ]; uri = OPT QSTRING ->
442         return_command loc (TacticAst.Baseuri uri)
443     | [ IDENT "check"   | IDENT "Check" ]; t = term ->
444         return_command loc (TacticAst.Check t)
445 (*
446     | [ IDENT "alias"   | IDENT "Alias" ]; spec = alias_spec ->
447         return_command loc (TacticAst.Alias spec)
448 *)
449     ]
450   ];
451   script_entry: [
452     [ cmd = tactical0 -> Command cmd
453     | s = COMMENT -> Comment (loc, s)
454     ]
455   ];
456   script: [ [ entries = LIST0 script_entry; EOI -> (loc, entries) ] ];
457 END
458
459 let exc_located_wrapper f =
460   try
461     f ()
462   with
463   | Stdpp.Exc_located (floc, Stream.Error msg) ->
464       raise (Parse_error (floc, msg))
465   | Stdpp.Exc_located (floc, exn) ->
466       raise (Parse_error (floc, (Printexc.to_string exn)))
467
468 let parse_term stream =
469   exc_located_wrapper (fun () -> (Grammar.Entry.parse term0 stream))
470 let parse_tactic stream =
471   exc_located_wrapper (fun () -> (Grammar.Entry.parse tactic stream))
472 let parse_tactical stream =
473   exc_located_wrapper (fun () -> (Grammar.Entry.parse tactical0 stream))
474 let parse_script stream =
475   exc_located_wrapper (fun () -> (Grammar.Entry.parse script stream))
476
477 (**/**)
478
479 (** {2 Interface for gTopLevel} *)
480
481 module EnvironmentP3 =
482   struct
483     type t = environment
484
485     let empty = ""
486
487     let aliases_grammar = Grammar.gcreate CicTextualLexer2.cic_lexer
488     let aliases = Grammar.Entry.create aliases_grammar "aliases"
489
490     let to_string env =
491       let aliases =
492         Environment.fold
493           (fun domain_item (dsc, _) acc ->
494             let s =
495               match domain_item with
496               | Id id -> sprintf "alias id %s = %s" id dsc
497               | Symbol (symb, instance) ->
498                   sprintf "alias symbol \"%s\" (instance %d) = \"%s\""
499                     symb instance dsc
500               | Num instance ->
501                   sprintf "alias num (instance %d) = \"%s\"" instance dsc
502             in
503             s :: acc)
504           env []
505       in
506       String.concat "\n" (List.sort compare aliases)
507
508     EXTEND
509       GLOBAL: aliases;
510       aliases: [  (* build an environment from an aliases list *)
511         [ aliases = LIST0 alias; EOI ->
512             List.fold_left
513               (fun env (domain_item, codomain_item) ->
514                 Environment.add domain_item codomain_item env)
515               Environment.empty aliases
516         ]
517       ];
518       alias: [  (* return a pair <domain_item, codomain_item> from an alias *)
519         [ IDENT "alias";
520           choice =
521             [ IDENT "id"; id = IDENT; SYMBOL "="; uri = URI ->
522                 (Id id, choice_of_uri uri)
523             | IDENT "symbol"; symbol = QSTRING;
524               PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
525               SYMBOL "="; dsc = QSTRING ->
526                 (Symbol (symbol, int_of_string instance),
527                  DisambiguateChoices.lookup_symbol_by_dsc symbol dsc)
528             | IDENT "num";
529               PAREN "("; IDENT "instance"; instance = NUM; PAREN ")";
530               SYMBOL "="; dsc = QSTRING ->
531                 (Num (int_of_string instance),
532                  DisambiguateChoices.lookup_num_by_dsc dsc)
533             ] -> choice ]
534       ];
535     END
536
537     let of_string s =
538       if s = empty then
539         Environment.empty
540       else
541         exc_located_wrapper
542           (fun () -> Grammar.Entry.parse aliases (Stream.of_string s))
543   end
544
545 (* vim:set encoding=utf8: *)