aboutsummaryrefslogtreecommitdiffhomepage
path: root/parsing/g_cases.ml4
blob: 7ad53edb9d1c36519d72c68ac9a7080e64915929 (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
68
69
70
71
72
73
74
75
76
77
78
79
(************************************************************************)
(*  v      *   The Coq Proof Assistant  /  The Coq Development Team     *)
(* <O___,, * CNRS-Ecole Polytechnique-INRIA Futurs-Universite Paris Sud *)
(*   \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")

if !Options.v7 then
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 = INT; ")" ->
	  (* Delimiter "N" moved to "nat" in V7 *)
          CPatDelimiters (loc,"nat",CPatNumeral (loc,Bigint.of_string n))
      | "("; p = compound_pattern; ")" -> p
      | n = INT -> CPatNumeral (loc,Bigint.of_string n)
      | "-"; n = INT ->
          let n = Bigint.of_string n in
          if n = Bigint.zero then
            CPatNotation(loc,"( _ )",[CPatNumeral (loc,n)])
          else
            CPatNumeral (loc,Bigint.neg 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" ->
          let lc = List.map (fun c -> c,(None,None)) lc in
	  CCases (loc, (Some p,None), lc, eqs)
      | "Cases"; lc = LIST1 constr; "of";
	OPT "|"; eqs = ne_eqn_list; "end" ->
          let lc = List.map (fun c -> c,(None,None)) lc in
	  CCases (loc, (None,None), lc, eqs)
      | "<"; p = annot; ">"; "Cases"; lc = LIST1 constr; "of"; "end" ->
          let lc = List.map (fun c -> c,(None,None)) lc in
	  CCases (loc, (Some p,None), lc, [])
      | "Cases"; lc = LIST1 constr; "of"; "end" -> 
          let lc = List.map (fun c -> c,(None,None)) lc in
	  CCases (loc, (None,None), lc, []) ] ]
  ;
END;