aboutsummaryrefslogtreecommitdiffhomepage
path: root/parsing/g_cases.ml4
blob: 679d174c2ae6ae716859d8fca5caf260167bbe26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
(***********************************************************************)
(*  v      *   The Coq Proof Assistant  /  The Coq Development Team    *)
(* <O___,, *        INRIA-Rocquencourt  &  LRI-CNRS-Orsay              *)
(*   \VV/  *************************************************************)
(*    //   *      This file is distributed under the terms of the      *)
(*         *       GNU Lesser General Public License Version 2.1       *)
(***********************************************************************)

(* $Id$ *)

open Pcoq
open Constr
open Topconstr
open Term
open Libnames

open Prim

let pair loc =
  Qualid (loc, Libnames.qualid_of_string "Coq.Init.Datatypes.pair")

GEXTEND Gram
  GLOBAL: operconstr pattern;

  pattern:
    [ [ r = Prim.reference -> CPatAtom (loc,Some r)
      | IDENT "_" -> CPatAtom (loc,None)
      (* Hack to parse syntax "(n)" as a natural number *)
      | "("; G_constr.test_int_rparen; n = bigint; ")" ->
          CPatDelimiters (loc,"N",CPatNumeral (loc,n))
      | "("; p = compound_pattern; ")" -> p
      | n = bigint -> CPatNumeral (loc,n)
      | "'"; G_constr.test_ident_colon; key = IDENT; ":"; c = pattern; "'" -> 
          CPatDelimiters (loc,key,c)
    ] ]
  ;
  compound_pattern:
    [ [ p = pattern ; lp = LIST1 pattern ->
        (match p with
          | CPatAtom (_, Some r) -> CPatCstr (loc, r, lp)
          | _ -> Util.user_err_loc 
              (loc, "compound_pattern", Pp.str "Constructor expected"))
      | p = pattern; "as"; id = base_ident ->
	  CPatAlias (loc, p, id)
      | p1 = pattern; ","; p2 = pattern ->
	  CPatCstr (loc, pair loc, [p1; p2])
      | p = pattern -> p ] ]
  ;
  equation:
    [ [ lhs = LIST1 pattern; "=>"; rhs = operconstr LEVEL "9" -> (loc,lhs,rhs) ] ]
  ;
  ne_eqn_list:
    [ [ leqn = LIST1 equation SEP "|" -> leqn ] ]
  ;
  operconstr: LEVEL "1"
    [ [ "<"; p = annot; ">"; "Cases"; lc = LIST1 constr; "of";
        OPT "|"; eqs = ne_eqn_list; "end" ->
	  CCases (loc, Some p, lc, eqs)
      | "Cases"; lc = LIST1 constr; "of";
	OPT "|"; eqs = ne_eqn_list; "end" ->
	  CCases (loc, None, lc, eqs)
      | "<"; p = annot; ">"; "Cases"; lc = LIST1 constr; "of"; "end" ->
	  CCases (loc, Some p, lc, [])
      | "Cases"; lc = LIST1 constr; "of"; "end" -> 
	  CCases (loc, None, lc, []) ] ]
  ;
END;