]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/ocaml/cic_notation/cicNotationParser.ml
ocaml 3.09 transition
[helm.git] / helm / ocaml / cic_notation / cicNotationParser.ml
index b0529dd7a570d82d72f458189130fbb0dd7d845a..32b6b0a9068312564dcc8c65648b12892a6f4bc3 100644 (file)
 
 open Printf
 
+module Ast = CicNotationPt
+module Env = CicNotationEnv
+
 exception Parse_error of Token.flocation * string
 exception Level_not_found of int
 
-let grammar = Grammar.gcreate CicNotationLexer.notation_lexer
-
-let level1_pattern = Grammar.Entry.create grammar "level1_pattern"
-let level2_pattern = Grammar.Entry.create grammar "level2_pattern"
-let level3_term = Grammar.Entry.create grammar "level3_term"
-let notation = Grammar.Entry.create grammar "notation" (* level1 <-> level 2 *)
-let interpretation =
-  Grammar.Entry.create grammar "interpretation" (* level2 <-> level 3 *)
-
-let return_term loc term = ()
+let level1_pattern_grammar =
+  Grammar.gcreate CicNotationLexer.level1_pattern_lexer
+let level2_ast_grammar = Grammar.gcreate CicNotationLexer.level2_ast_lexer
+let level2_meta_grammar = Grammar.gcreate CicNotationLexer.level2_meta_lexer
 
-let loc_of_floc = function
-  | { Lexing.pos_cnum = loc_begin }, { Lexing.pos_cnum = loc_end } ->
-      (loc_begin, loc_end)
+let min_precedence = 0
+let max_precedence = 100
 
-let fail floc msg =
-  let (x, y) = loc_of_floc floc in
-  failwith (sprintf "Error at characters %d - %d: %s" x y msg)
+let level1_pattern =
+  Grammar.Entry.create level1_pattern_grammar "level1_pattern"
+let level2_ast = Grammar.Entry.create level2_ast_grammar "level2_ast"
+let term = Grammar.Entry.create level2_ast_grammar "term"
+let let_defs = Grammar.Entry.create level2_ast_grammar "let_defs"
+let level2_meta = Grammar.Entry.create level2_meta_grammar "level2_meta"
 
 let int_of_string s =
   try
@@ -53,152 +52,453 @@ let int_of_string s =
   with Failure _ ->
     failwith (sprintf "Lexer failure: string_of_int \"%s\" failed" s)
 
-open CicNotationPt
+(** {2 Grammar extension} *)
 
-let boxify = function
-  | [ a ] -> a
-  | l -> Layout (Box (H, l))
+let gram_symbol s = Gramext.Stoken ("SYMBOL", s)
+let gram_ident s = Gramext.Stoken ("IDENT", s)
+let gram_number s = Gramext.Stoken ("NUMBER", s)
+let gram_keyword s = Gramext.Stoken ("", s)
+let gram_term = Gramext.Sself
 
-let fold_binder binder pt_names body =
-  let fold_cluster binder names ty body =
-    List.fold_right
-      (fun name body -> Binder (binder, (Cic.Name name, ty), body))
-      names body
+let gram_of_literal =
+  function
+  | `Symbol s -> gram_symbol s
+  | `Keyword s -> gram_keyword s
+  | `Number s -> gram_number s
+
+type binding =
+  | NoBinding
+  | Binding of string * Env.value_type
+  | Env of (string * Env.value_type) list
+
+let make_action action bindings =
+  let rec aux (vl : CicNotationEnv.t) =
+    function
+      [] -> Gramext.action (fun (loc: Ast.location) -> action vl loc)
+    | NoBinding :: tl -> Gramext.action (fun _ -> aux vl tl)
+    (* LUCA: DEFCON 3 BEGIN *)
+    | Binding (name, Env.TermType) :: tl ->
+        Gramext.action
+          (fun (v:Ast.term) ->
+            aux ((name, (Env.TermType, Env.TermValue v))::vl) tl)
+    | Binding (name, Env.StringType) :: tl ->
+        Gramext.action
+          (fun (v:string) ->
+            aux ((name, (Env.StringType, Env.StringValue v)) :: vl) tl)
+    | Binding (name, Env.NumType) :: tl ->
+        Gramext.action
+          (fun (v:string) ->
+            aux ((name, (Env.NumType, Env.NumValue v)) :: vl) tl)
+    | Binding (name, Env.OptType t) :: tl ->
+        Gramext.action
+          (fun (v:'a option) ->
+            aux ((name, (Env.OptType t, Env.OptValue v)) :: vl) tl)
+    | Binding (name, Env.ListType t) :: tl ->
+        Gramext.action
+          (fun (v:'a list) ->
+            aux ((name, (Env.ListType t, Env.ListValue v)) :: vl) tl)
+    | Env _ :: tl ->
+        Gramext.action (fun (v:CicNotationEnv.t) -> aux (v @ vl) tl)
+    (* LUCA: DEFCON 3 END *)
+  in
+    aux [] (List.rev bindings)
+
+let flatten_opt =
+  let rec aux acc =
+    function
+      [] -> List.rev acc
+    | NoBinding :: tl -> aux acc tl
+    | Env names :: tl -> aux (List.rev names @ acc) tl
+    | Binding (name, ty) :: tl -> aux ((name, ty) :: acc) tl
+  in
+  aux []
+
+  (* given a level 1 pattern computes the new RHS of "term" grammar entry *)
+let extract_term_production pattern =
+  let rec aux = function
+    | Ast.AttributedTerm (_, t) -> aux t
+    | Ast.Literal l -> aux_literal l
+    | Ast.Layout l -> aux_layout l
+    | Ast.Magic m -> aux_magic m
+    | Ast.Variable v -> aux_variable v
+    | t ->
+        prerr_endline (CicNotationPp.pp_term t);
+        assert false
+  and aux_literal =
+    function
+    | `Symbol s -> [NoBinding, gram_symbol s]
+    | `Keyword s ->
+        (* assumption: s will be registered as a keyword with the lexer *)
+        [NoBinding, gram_keyword s]
+    | `Number s -> [NoBinding, gram_number s]
+  and aux_layout = function
+    | Ast.Sub (p1, p2) -> aux p1 @ [NoBinding, gram_symbol "\\sub"] @ aux p2
+    | Ast.Sup (p1, p2) -> aux p1 @ [NoBinding, gram_symbol "\\sup"] @ aux p2
+    | Ast.Below (p1, p2) -> aux p1 @ [NoBinding, gram_symbol "\\below"] @ aux p2
+    | Ast.Above (p1, p2) -> aux p1 @ [NoBinding, gram_symbol "\\above"] @ aux p2
+    | Ast.Frac (p1, p2) -> aux p1 @ [NoBinding, gram_symbol "\\frac"] @ aux p2
+    | Ast.Atop (p1, p2) -> aux p1 @ [NoBinding, gram_symbol "\\atop"] @ aux p2
+    | Ast.Over (p1, p2) -> aux p1 @ [NoBinding, gram_symbol "\\over"] @ aux p2
+    | Ast.Root (p1, p2) ->
+        [NoBinding, gram_symbol "\\root"] @ aux p2
+        @ [NoBinding, gram_symbol "\\of"] @ aux p1
+    | Ast.Sqrt p -> [NoBinding, gram_symbol "\\sqrt"] @ aux p
+    | Ast.Break -> []
+    | Ast.Box (_, pl) -> List.flatten (List.map aux pl)
+    | Ast.Group pl -> List.flatten (List.map aux pl)
+  and aux_magic magic =
+    match magic with
+    | Ast.Opt p ->
+        let p_bindings, p_atoms, p_names, p_action = inner_pattern p in
+        let action (env_opt : CicNotationEnv.t option) (loc : Ast.location) =
+          match env_opt with
+          | Some env -> List.map Env.opt_binding_some env
+          | None -> List.map Env.opt_binding_of_name p_names
+        in
+        [ Env (List.map Env.opt_declaration p_names),
+          Gramext.srules
+            [ [ Gramext.Sopt (Gramext.srules [ p_atoms, p_action ]) ],
+              Gramext.action action ] ]
+    | Ast.List0 (p, _)
+    | Ast.List1 (p, _) ->
+        let p_bindings, p_atoms, p_names, p_action = inner_pattern p in
+(*         let env0 = List.map list_binding_of_name p_names in
+        let grow_env_entry env n v =
+          List.map
+            (function
+              | (n', (ty, ListValue vl)) as entry ->
+                  if n' = n then n', (ty, ListValue (v :: vl)) else entry
+              | _ -> assert false)
+            env
+        in
+        let grow_env env_i env =
+          List.fold_left
+            (fun env (n, (_, v)) -> grow_env_entry env n v)
+            env env_i
+        in *)
+        let action (env_list : CicNotationEnv.t list) (loc : Ast.location) =
+          CicNotationEnv.coalesce_env p_names env_list
+        in
+        let gram_of_list s =
+          match magic with
+          | Ast.List0 (_, None) -> Gramext.Slist0 s
+          | Ast.List1 (_, None) -> Gramext.Slist1 s
+          | Ast.List0 (_, Some l) -> Gramext.Slist0sep (s, gram_of_literal l)
+          | Ast.List1 (_, Some l) -> Gramext.Slist1sep (s, gram_of_literal l)
+          | _ -> assert false
+        in
+        [ Env (List.map Env.list_declaration p_names),
+          Gramext.srules
+            [ [ gram_of_list (Gramext.srules [ p_atoms, p_action ]) ],
+              Gramext.action action ] ]
+    | _ -> assert false
+  and aux_variable =
+    function
+    | Ast.NumVar s -> [Binding (s, Env.NumType), gram_number ""]
+    | Ast.TermVar s -> [Binding (s, Env.TermType), gram_term]
+    | Ast.IdentVar s -> [Binding (s, Env.StringType), gram_ident ""]
+    | Ast.Ascription (p, s) -> assert false (* TODO *)
+    | Ast.FreshVar _ -> assert false
+  and inner_pattern p =
+    let p_bindings, p_atoms = List.split (aux p) in
+    let p_names = flatten_opt p_bindings in
+    let action =
+      make_action (fun (env : CicNotationEnv.t) (loc : Ast.location) -> env)
+        p_bindings
+    in
+    p_bindings, p_atoms, p_names, action
   in
+  aux pattern
+
+let level_of precedence associativity =
+  if precedence < min_precedence || precedence > max_precedence then
+    raise (Level_not_found precedence);
+  let assoc_string =
+    match associativity with
+    | Gramext.NonA -> "N"
+    | Gramext.LeftA -> "L"
+    | Gramext.RightA -> "R"
+  in
+  string_of_int precedence ^ assoc_string
+
+type rule_id = Token.t Gramext.g_symbol list
+
+  (* mapping: rule_id -> owned keywords. (rule_id, string list) Hashtbl.t *)
+let owned_keywords = Hashtbl.create 23
+
+let extend level1_pattern ~precedence ~associativity action =
+  let p_bindings, p_atoms =
+    List.split (extract_term_production level1_pattern)
+  in
+  let level = level_of precedence associativity in
+  let p_names = flatten_opt p_bindings in
+  let _ =
+    Grammar.extend
+      [ Grammar.Entry.obj (term: 'a Grammar.Entry.e),
+        Some (Gramext.Level level),
+        [ None,
+          Some associativity,
+          [ p_atoms, 
+            (make_action
+              (fun (env: CicNotationEnv.t) (loc: Ast.location) ->
+                (action env loc))
+              p_bindings) ]]]
+  in
+  let keywords = CicNotationUtil.keywords_of_term level1_pattern in
+  let rule_id = p_atoms in
+  List.iter CicNotationLexer.add_level2_ast_keyword keywords;
+  Hashtbl.add owned_keywords rule_id keywords;  (* keywords may be [] *)
+  rule_id
+
+let delete rule_id =
+  let atoms = rule_id in
+  (try
+    let keywords = Hashtbl.find owned_keywords rule_id in
+    List.iter CicNotationLexer.remove_level2_ast_keyword keywords
+  with Not_found -> assert false);
+  Grammar.delete_rule term atoms
+
+(** {2 Grammar} *)
+
+let parse_level1_pattern_ref = ref (fun _ -> assert false)
+let parse_level2_ast_ref = ref (fun _ -> assert false)
+let parse_level2_meta_ref = ref (fun _ -> assert false)
+
+let fold_cluster binder terms ty body =
+  List.fold_right
+    (fun term body -> Ast.Binder (binder, (term, ty), body))
+    terms body  (* terms are names: either Ident or FreshVar *)
+
+let fold_exists terms ty body =
+  List.fold_right
+    (fun term body ->
+      let lambda = Ast.Binder (`Lambda, (term, ty), body) in
+      Ast.Appl [ Ast.Symbol ("exists", 0); lambda ])
+    terms body
+
+let fold_binder binder pt_names body =
   List.fold_right
     (fun (names, ty) body -> fold_cluster binder names ty body)
     pt_names body
 
-let return_term loc term = AttributedTerm (`Loc loc, term)
+let return_term loc term = Ast.AttributedTerm (`Loc loc, term)
+
+  (* create empty precedence level for "term" *)
+let _ =
+  let dummy_action =
+    Gramext.action (fun _ ->
+      failwith "internal error, lexer generated a dummy token")
+  in
+  (* Needed since campl4 on "delete_rule" remove the precedence level if it gets
+   * empty after the deletion. The lexer never generate the Stoken below. *)
+  let dummy_prod = [ [ Gramext.Stoken ("DUMMY", "") ], dummy_action ] in
+  let mk_level_list first last =
+    let rec aux acc = function
+      | i when i < first -> acc
+      | i ->
+          aux
+            ((Some (string_of_int i ^ "N"), Some Gramext.NonA, dummy_prod)
+             :: (Some (string_of_int i ^ "L"), Some Gramext.LeftA, dummy_prod)
+             :: (Some (string_of_int i ^ "R"), Some Gramext.RightA, dummy_prod)
+             :: acc)
+            (i - 1)
+    in
+    aux [] last
+  in
+  Grammar.extend
+    [ Grammar.Entry.obj (term: 'a Grammar.Entry.e),
+      None,
+      mk_level_list min_precedence max_precedence ]
 
-EXTEND
-  GLOBAL: level1_pattern level2_pattern level3_term
-          notation interpretation;
 (* {{{ Grammar for concrete syntax patterns, notation level 1 *)
-  level1_pattern: [ [ p = l1_pattern; EOI -> boxify p ] ];
-  l1_pattern: [ [ p = LIST0 l1_simple_pattern -> p ] ];
+EXTEND
+  GLOBAL: level1_pattern;
+
+  level1_pattern: [ [ p = l1_pattern; EOI -> CicNotationUtil.boxify p ] ];
+  l1_pattern: [ [ p = LIST1 l1_simple_pattern -> p ] ];
   literal: [
     [ s = SYMBOL -> `Symbol s
-    | k = KEYWORD -> `Keyword k
+    | k = QKEYWORD -> `Keyword k
     | n = NUMBER -> `Number n
     ]
   ];
-  sep:       [ [ SYMBOL "\\SEP";      sep = literal -> sep ] ];
-(*   row_sep:   [ [ SYMBOL "\\ROWSEP";   sep = literal -> sep ] ];
-  field_sep: [ [ SYMBOL "\\FIELDSEP"; sep = literal -> sep ] ]; *)
+  sep:       [ [ "sep";      sep = literal -> sep ] ];
+(*   row_sep:   [ [ "rowsep";   sep = literal -> sep ] ];
+  field_sep: [ [ "fieldsep"; sep = literal -> sep ] ]; *)
   l1_magic_pattern: [
-    [ SYMBOL "\\LIST0"; p = l1_simple_pattern; sep = OPT sep -> List0 (p, sep)
-    | SYMBOL "\\LIST1"; p = l1_simple_pattern; sep = OPT sep -> List1 (p, sep)
-    | SYMBOL "\\OPT";   p = l1_simple_pattern -> Opt p
+    [ "list0"; p = l1_simple_pattern; sep = OPT sep -> Ast.List0 (p, sep)
+    | "list1"; p = l1_simple_pattern; sep = OPT sep -> Ast.List1 (p, sep)
+    | "opt";   p = l1_simple_pattern -> Ast.Opt p
     ]
   ];
   l1_pattern_variable: [
-    [ id = IDENT -> TermVar id
-    | SYMBOL "\\TERM"; id = IDENT -> TermVar id
-    | SYMBOL "\\NUM"; id = IDENT -> NumVar id
-    | SYMBOL "\\IDENT"; id = IDENT -> IdentVar id
+    [ "term"; id = IDENT -> Ast.TermVar id
+    | "number"; id = IDENT -> Ast.NumVar id
+    | "ident"; id = IDENT -> Ast.IdentVar id
     ]
   ];
   l1_simple_pattern:
     [ "layout" LEFTA
-      [ p1 = SELF; SYMBOL "\\SUB"; p2 = SELF ->
-          return_term loc (Layout (Sub (p1, p2)))
-      | p1 = SELF; SYMBOL "\\SUP"; p2 = SELF ->
-          return_term loc (Layout (Sup (p1, p2)))
-      | p1 = SELF; SYMBOL "\\BELOW"; p2 = SELF ->
-          return_term loc (Layout (Below (p1, p2)))
-      | p1 = SELF; SYMBOL "\\ABOVE"; p2 = SELF ->
-          return_term loc (Layout (Above (p1, p2)))
-      | SYMBOL "["; p1 = l1_pattern; SYMBOL "\\OVER"; p2 = l1_pattern;
-        SYMBOL "]" ->
-          return_term loc (Layout (Over (boxify p1, boxify p2)))
-      | SYMBOL "["; p1 = l1_pattern; SYMBOL "\\ATOP"; p2 = l1_pattern;
-        SYMBOL "]" ->
-          return_term loc (Layout (Atop (boxify p1, boxify p2)))
-(*       | SYMBOL "\\ARRAY"; p = SELF; csep = OPT field_sep; rsep = OPT row_sep ->
+      [ p1 = SELF; SYMBOL "\\sub"; p2 = SELF ->
+          return_term loc (Ast.Layout (Ast.Sub (p1, p2)))
+      | p1 = SELF; SYMBOL "\\sup"; p2 = SELF ->
+          return_term loc (Ast.Layout (Ast.Sup (p1, p2)))
+      | p1 = SELF; SYMBOL "\\below"; p2 = SELF ->
+          return_term loc (Ast.Layout (Ast.Below (p1, p2)))
+      | p1 = SELF; SYMBOL "\\above"; p2 = SELF ->
+          return_term loc (Ast.Layout (Ast.Above (p1, p2)))
+      | p1 = SELF; SYMBOL "\\over"; p2 = SELF ->
+          return_term loc (Ast.Layout (Ast.Over (p1, p2)))
+      | p1 = SELF; SYMBOL "\\atop"; p2 = SELF ->
+          return_term loc (Ast.Layout (Ast.Atop (p1, p2)))
+(*       | "array"; p = SELF; csep = OPT field_sep; rsep = OPT row_sep ->
           return_term loc (Array (p, csep, rsep)) *)
-      | SYMBOL "\\FRAC"; p1 = SELF; p2 = SELF ->
-          return_term loc (Layout (Frac (p1, p2)))
-      | SYMBOL "\\SQRT"; p = SELF -> return_term loc (Layout (Sqrt p))
-      | SYMBOL "\\ROOT"; index = l1_pattern; SYMBOL "\\OF"; arg = SELF ->
-          return_term loc (Layout (Root (arg, Layout (Box (H, index)))))
-      | SYMBOL "\\HBOX"; SYMBOL "["; p = l1_pattern; SYMBOL "]" ->
-          return_term loc (Layout (Box (H, p)))
-      | SYMBOL "\\VBOX"; SYMBOL "["; p = l1_pattern; SYMBOL "]" ->
-          return_term loc (Layout (Box (V, p)))
-      | SYMBOL "\\BREAK" -> return_term loc (Layout Break)
-      | SYMBOL "["; p = l1_pattern; SYMBOL "]" ->
-          return_term loc (boxify p)
-      | SYMBOL "["; p = l1_pattern; SYMBOL "\\AS"; id = IDENT; SYMBOL "]" ->
-          return_term loc (Variable (Ascription (Layout (Box (H, p)), id)))
+      | SYMBOL "\\frac"; p1 = SELF; p2 = SELF ->
+          return_term loc (Ast.Layout (Ast.Frac (p1, p2)))
+      | SYMBOL "\\sqrt"; p = SELF -> return_term loc (Ast.Layout (Ast.Sqrt p))
+      | SYMBOL "\\root"; index = SELF; SYMBOL "\\of"; arg = SELF ->
+          return_term loc (Ast.Layout (Ast.Root (arg, index)))
+      | "hbox"; LPAREN; p = l1_pattern; RPAREN ->
+          return_term loc (Ast.Layout (Ast.Box ((Ast.H, false, false), p)))
+      | "vbox"; LPAREN; p = l1_pattern; RPAREN ->
+          return_term loc (Ast.Layout (Ast.Box ((Ast.V, false, false), p)))
+      | "hvbox"; LPAREN; p = l1_pattern; RPAREN ->
+          return_term loc (Ast.Layout (Ast.Box ((Ast.HV, false, false), p)))
+      | "hovbox"; LPAREN; p = l1_pattern; RPAREN ->
+          return_term loc (Ast.Layout (Ast.Box ((Ast.HOV, false, false), p)))
+      | "break" -> return_term loc (Ast.Layout Ast.Break)
+(*       | SYMBOL "\\SPACE" -> return_term loc (Layout Space) *)
+      | LPAREN; p = l1_pattern; RPAREN ->
+          return_term loc (CicNotationUtil.group p)
       ]
     | "simple" NONA
-      [ m = l1_magic_pattern -> return_term loc (Magic m)
-      | v = l1_pattern_variable -> return_term loc (Variable v)
-      | l = literal -> return_term loc (Literal l)
+      [ i = IDENT -> return_term loc (Ast.Variable (Ast.TermVar i))
+      | m = l1_magic_pattern -> return_term loc (Ast.Magic m)
+      | v = l1_pattern_variable -> return_term loc (Ast.Variable v)
+      | l = literal -> return_term loc (Ast.Literal l)
       ]
     ];
+  END
 (* }}} *)
+
+(* {{{ Grammar for ast magics, notation level 2 *)
+EXTEND
+  GLOBAL: level2_meta;
+  l2_variable: [
+    [ "term"; id = IDENT -> Ast.TermVar id
+    | "number"; id = IDENT -> Ast.NumVar id
+    | "ident"; id = IDENT -> Ast.IdentVar id
+    | "fresh"; id = IDENT -> Ast.FreshVar id
+    | "anonymous" -> Ast.TermVar "_"
+    | id = IDENT -> Ast.TermVar id
+    ]
+  ];
+  l2_magic: [
+    [ "fold"; kind = [ "left" -> `Left | "right" -> `Right ];
+      base = level2_meta; "rec"; id = IDENT; recursive = level2_meta ->
+        Ast.Fold (kind, base, [id], recursive)
+    | "default"; some = level2_meta; none = level2_meta ->
+        Ast.Default (some, none)
+    | "if"; p_test = level2_meta;
+      "then"; p_true = level2_meta;
+      "else"; p_false = level2_meta ->
+        Ast.If (p_test, p_true, p_false)
+    | "fail" -> Ast.Fail
+    ]
+  ];
+  level2_meta: [
+    [ magic = l2_magic -> Ast.Magic magic
+    | var = l2_variable -> Ast.Variable var
+    | blob = UNPARSED_AST ->
+        !parse_level2_ast_ref (Ulexing.from_utf8_string blob)
+    ]
+  ];
+END
+(* }}} *)
+
 (* {{{ Grammar for ast patterns, notation level 2 *)
-  level2_pattern: [ [ p = l2_pattern -> p ] ];
+EXTEND
+  GLOBAL: level2_ast term let_defs;
+  level2_ast: [ [ p = term -> p ] ];
   sort: [
-    [ SYMBOL "\\PROP" -> `Prop
-    | SYMBOL "\\SET" -> `Set
-    | SYMBOL "\\TYPE" -> `Type
+    [ "Prop" -> `Prop
+    | "Set" -> `Set
+    | "Type" -> `Type (CicUniv.fresh ()) 
+    | "CProp" -> `CProp
     ]
   ];
   explicit_subst: [
-    [ (* TODO explicit substitution *)
+    [ SYMBOL "\\subst";  (* to avoid catching frequent "a [1]" cases *)
+      SYMBOL "[";
+      substs = LIST1 [
+        i = IDENT; SYMBOL <:unicode<Assign>> (* ≔ *); t = term -> (i, t)
+      ] SEP SYMBOL ";";
+      SYMBOL "]" ->
+        substs
     ]
   ];
   meta_subst: [
-    [ (* TODO meta substitution *)
-    ]
+    [ s = SYMBOL "_" -> None
+    | p = term -> Some p ]
+  ];
+  meta_substs: [
+    [ SYMBOL "["; substs = LIST0 meta_subst; SYMBOL "]" -> substs ]
   ];
   possibly_typed_name: [
-    [ SYMBOL "("; id = IDENT; SYMBOL ":"; typ = l2_pattern; SYMBOL ")" ->
-        Cic.Name id, Some typ
-    | id = IDENT -> Cic.Name id, None
+    [ LPAREN; id = single_arg; SYMBOL ":"; typ = term; RPAREN ->
+        id, Some typ
+    | arg = single_arg -> arg, None
     ]
   ];
   match_pattern: [
-    [ id = IDENT -> id, []
-    | SYMBOL "("; id = IDENT; vars = LIST1 possibly_typed_name; SYMBOL ")" ->
-        id, vars
+    [ id = IDENT -> id, None, []
+    | LPAREN; id = IDENT; vars = LIST1 possibly_typed_name; RPAREN ->
+        id, None, vars
     ]
   ];
   binder: [
     [ SYMBOL <:unicode<Pi>>     (* Π *) -> `Pi
-    | SYMBOL <:unicode<exists>> (* ∃ *) -> `Exists
+(*     | SYMBOL <:unicode<exists>> |+ ∃ +| -> `Exists *)
     | SYMBOL <:unicode<forall>> (* ∀ *) -> `Forall
     | SYMBOL <:unicode<lambda>> (* λ *) -> `Lambda
     ]
   ];
-  bound_names: [
-    [ vars = LIST1 IDENT SEP SYMBOL ",";
-      ty = OPT [ SYMBOL ":"; p = l2_pattern -> p ] ->
-        [ vars, ty ]
-    | clusters = LIST1 [
-        SYMBOL "(";
-        vars = LIST1 IDENT SEP SYMBOL ",";
-        ty = OPT [ SYMBOL ":"; p = l2_pattern -> p ];
-        SYMBOL ")" ->
-          vars, ty
-      ] ->
-        clusters
+  arg: [
+    [ LPAREN; names = LIST1 IDENT SEP SYMBOL ",";
+      SYMBOL ":"; ty = term; RPAREN ->
+        List.map (fun n -> Ast.Ident (n, None)) names, Some ty
+    | name = IDENT -> [Ast.Ident (name, None)], None
+    | blob = UNPARSED_META ->
+        let meta = !parse_level2_meta_ref (Ulexing.from_utf8_string blob) in
+        match meta with
+        | Ast.Variable (Ast.FreshVar _) -> [meta], None
+        | Ast.Variable (Ast.TermVar "_") -> [Ast.Ident ("_", None)], None
+        | _ -> failwith "Invalid bound name."
+   ]
+  ];
+  single_arg: [
+    [ name = IDENT -> Ast.Ident (name, None)
+    | blob = UNPARSED_META ->
+        let meta = !parse_level2_meta_ref (Ulexing.from_utf8_string blob) in
+        match meta with
+        | Ast.Variable (Ast.FreshVar _)
+        | Ast.Variable (Ast.IdentVar _) -> meta
+        | Ast.Variable (Ast.TermVar "_") -> Ast.Ident ("_", None)
+        | _ -> failwith "Invalid index name."
     ]
   ];
   induction_kind: [
-    [ IDENT "rec" -> `Inductive
-    | IDENT "corec" -> `CoInductive
+    [ "rec" -> `Inductive
+    | "corec" -> `CoInductive
     ]
   ];
   let_defs: [
     [ defs = LIST1 [
-        name = IDENT; args = bound_names;
-        index_name = OPT [ IDENT "on"; id = IDENT -> id ];
-        ty = OPT [ SYMBOL ":" ; p = l2_pattern -> p ];
-        SYMBOL <:unicode<def>> (* ≝ *); body = l2_pattern ->
+        name = single_arg;
+        args = LIST1 arg;
+        index_name = OPT [ "on"; id = single_arg -> id ];
+        ty = OPT [ SYMBOL ":" ; p = term -> p ];
+        SYMBOL <:unicode<def>> (* ≝ *); body = term ->
           let body = fold_binder `Lambda args body in
           let ty = 
             match ty with 
@@ -211,7 +511,9 @@ EXTEND
             | _ :: tl -> position_of name (p + 1) tl
           in
           let rec find_arg name n = function 
-            | [] -> fail loc (sprintf "Argument %s not found" name)
+            | [] ->
+                Ast.fail loc (sprintf "Argument %s not found"
+                  (CicNotationPp.pp_term name))
             | (l,_) :: tl -> 
                 (match position_of name 0 l with
                 | None, len -> find_arg name (n + len) tl
@@ -220,128 +522,92 @@ EXTEND
           let index = 
             match index_name with 
             | None -> 0 
-            | Some name -> find_arg name 0 args
+            | Some index_name -> find_arg index_name 0 args
           in
-          (Cic.Name name, ty), body, index
-      ] SEP IDENT "and" ->
+          (name, ty), body, index
+      ] SEP "and" ->
         defs
     ]
   ];
-  l2_pattern_variable: [
-    [ SYMBOL "\\NUM"; id = IDENT -> NumVar id
-    | SYMBOL "\\IDENT"; id = IDENT -> IdentVar id
-    | SYMBOL "\\FRESH"; id = IDENT -> FreshVar id
+  binder_vars: [
+    [ vars = [
+          l = LIST1 single_arg SEP SYMBOL "," -> l
+        | SYMBOL "_" -> [Ast.Ident ("_", None)] ];
+      typ = OPT [ SYMBOL ":"; t = term -> t ] -> (vars, typ)
+    | LPAREN; 
+        vars = [
+            l =  LIST1 single_arg SEP SYMBOL "," -> l
+          | SYMBOL "_" -> [Ast.Ident ("_", None)] ];
+      typ = OPT [ SYMBOL ":"; t = term -> t ]; 
+      RPAREN -> (vars, typ)
     ]
   ];
-  l2_magic_pattern: [
-    [ SYMBOL "\\FOLD";
-      kind = [ IDENT "left" -> `Left | IDENT "right" -> `Right ];
-      base = l2_pattern;
-      SYMBOL "\\LAMBDA"; id = IDENT; recursive = l2_pattern ->
-        Fold (kind, base, [id], recursive)
-    | SYMBOL "\\DEFAULT"; some = l2_pattern; none = l2_pattern ->
-        Default (some, none)
+  term: LEVEL "10N" [ (* let in *)
+    [ "let"; var = possibly_typed_name; SYMBOL <:unicode<def>> (* ≝ *);
+      p1 = term; "in"; p2 = term ->
+        return_term loc (Ast.LetIn (var, p1, p2))
+    | "let"; k = induction_kind; defs = let_defs; "in";
+      body = term ->
+        return_term loc (Ast.LetRec (k, defs, body))
     ]
   ];
-  l2_pattern:
-    [ "0" [ ]
-    | "10" NONA (* let in *)
-      [ IDENT "let"; var = possibly_typed_name; SYMBOL <:unicode<def>> (* ≝ *);
-        p1 = l2_pattern; "in"; p2 = l2_pattern ->
-          return_term loc (LetIn (var, p1, p2))
-      | IDENT "let"; k = induction_kind; defs = let_defs; IDENT "in";
-        body = l2_pattern ->
-          return_term loc (LetRec (k, defs, body))
+  term: LEVEL "20R"  (* binder *)
+    [
+      [ b = binder; (vars, typ) = binder_vars; SYMBOL "."; body = term ->
+          return_term loc (fold_cluster b vars typ body)
+      | SYMBOL <:unicode<exists>> (* ∃ *);
+        (vars, typ) = binder_vars; SYMBOL "."; body = term ->
+          return_term loc (fold_exists vars typ body)
       ]
-    | "20" RIGHTA (* binder *)
-      [ b = binder; names = bound_names; SYMBOL "."; body = l2_pattern ->
-          return_term loc (fold_binder b names body)
-      ]
-    | "30" [ ]
-    | "40" [ ]
-    | "50" [ ]
-    | "60" [ ]
-    | "70" LEFTA (* apply *)
-      [ p1 = l2_pattern; p2 = l2_pattern ->
+    ];
+  term: LEVEL "70L"  (* apply *)
+    [
+      [ p1 = term; p2 = term ->
           let rec aux = function
-            | Appl (hd :: tl)
-            | AttributedTerm (_, Appl (hd :: tl)) ->
+            | Ast.Appl (hd :: tl)
+            | Ast.AttributedTerm (_, Ast.Appl (hd :: tl)) ->
                 aux hd @ tl
             | term -> [term]
           in
-          return_term loc (Appl (aux p1 @ [p2]))
+          return_term loc (Ast.Appl (aux p1 @ [p2]))
       ]
-    | "80" [ ]
-    | "90" NONA (* simple *)
-      [ id = IDENT -> return_term loc (Ident (id, None))
-      | id = IDENT; s = explicit_subst -> return_term loc (Ident (id, Some s))
-      | u = URI -> return_term loc (Uri (u, None))
-      | n = NUMBER -> return_term loc (Num (n, 0))
-      | IMPLICIT -> return_term loc (Implicit)
-      | m = META -> return_term loc (Meta (int_of_string m, []))
-      | m = META; s = meta_subst -> return_term loc (Meta (int_of_string m, s))
-      | s = sort -> return_term loc (Sort s)
-      | s = SYMBOL -> return_term loc (Symbol (s, 0))
-      | outtyp = OPT [ SYMBOL "["; ty = l2_pattern; SYMBOL "]" -> ty ];
-        IDENT "match"; t = l2_pattern;
-        indty_ident = OPT [ SYMBOL ":"; id = IDENT -> id ];
-        IDENT "with"; SYMBOL "[";
+    ];
+  term: LEVEL "90N"  (* simple *)
+    [
+      [ id = IDENT -> return_term loc (Ast.Ident (id, None))
+      | id = IDENT; s = explicit_subst ->
+          return_term loc (Ast.Ident (id, Some s))
+      | s = CSYMBOL -> return_term loc (Ast.Symbol (s, 0))
+      | u = URI -> return_term loc (Ast.Uri (u, None))
+      | n = NUMBER -> return_term loc (Ast.Num (n, 0))
+      | IMPLICIT -> return_term loc (Ast.Implicit)
+      | PLACEHOLDER -> return_term loc Ast.UserInput
+      | m = META -> return_term loc (Ast.Meta (int_of_string m, []))
+      | m = META; s = meta_substs ->
+          return_term loc (Ast.Meta (int_of_string m, s))
+      | s = sort -> return_term loc (Ast.Sort s)
+      | "match"; t = term;
+        indty_ident = OPT [ "in"; id = IDENT -> id, None ];
+        outtyp = OPT [ "return"; ty = term -> ty ];
+        "with"; SYMBOL "[";
         patterns = LIST0 [
           lhs = match_pattern; SYMBOL <:unicode<Rightarrow>> (* ⇒ *);
-          rhs = l2_pattern ->
+          rhs = term ->
             lhs, rhs
         ] SEP SYMBOL "|";
         SYMBOL "]" ->
-          return_term loc (Case (t, indty_ident, outtyp, patterns))
-      | SYMBOL "("; p1 = l2_pattern; SYMBOL ":"; p2 = l2_pattern; SYMBOL ")" ->
-          return_term loc (Appl [ Symbol ("cast", 0); p1; p2 ])
-      | SYMBOL "("; p = l2_pattern; SYMBOL ")" -> p
-      | v = l2_pattern_variable -> return_term loc (Variable v)
-      | m = l2_magic_pattern -> return_term loc (Magic m)
+          return_term loc (Ast.Case (t, indty_ident, outtyp, patterns))
+      | LPAREN; p1 = term; SYMBOL ":"; p2 = term; RPAREN ->
+          return_term loc (Ast.Cast (p1, p2))
+      | LPAREN; p = term; RPAREN -> p
+      | blob = UNPARSED_META ->
+          !parse_level2_meta_ref (Ulexing.from_utf8_string blob)
       ]
-    | "100" [ ]
     ];
-(* }}} *)
-(* {{{ Grammar for interpretation, notation level 3 *)
-  argument: [
-    [ id = IDENT -> IdentArg id
-    | SYMBOL <:unicode<eta>> (* η *); SYMBOL "."; a = SELF -> EtaArg (None, a)
-    | SYMBOL <:unicode<eta>> (* η *); id = IDENT; SYMBOL "."; a = SELF ->
-        EtaArg (Some id, a)
-    ]
-  ];
-  level3_term: [
-    [ u = URI -> UriPattern u
-    | a = argument -> ArgPattern a
-    | SYMBOL "("; terms = LIST1 SELF; SYMBOL ")" ->
-        (match terms with
-        | [] -> assert false
-        | [term] -> term
-        | terms -> ApplPattern terms)
-    ]
-  ];
-(* }}} *)
-(* {{{ Notation glues *)
-  associativity: [
-    [ IDENT "left";  IDENT "associative" -> `Left
-    | IDENT "right"; IDENT "associative" -> `Right
-    ]
-  ];
-  precedence: [ [ IDENT "at"; IDENT "precedence"; n = NUMBER -> n ] ];
-  notation: [
-    [ IDENT "notation"; p1 = level1_pattern; IDENT "for"; p2 = level2_pattern;
-      assoc = OPT associativity; prec = OPT precedence ->
-        ()
-    ]
-  ];
-  interpretation: [
-    [ IDENT "interpretation"; s = SYMBOL; args = LIST1 argument; IDENT "as";
-      t = level3_term ->
-        ()
-    ]
-  ];
-(* }}} *)
 END
+(* }}} *)
+
+(** {2 API implementation} *)
 
 let exc_located_wrapper f =
   try
@@ -352,139 +618,28 @@ let exc_located_wrapper f =
   | Stdpp.Exc_located (floc, exn) ->
       raise (Parse_error (floc, (Printexc.to_string exn)))
 
-let parse_syntax_pattern stream =
-  exc_located_wrapper (fun () -> Grammar.Entry.parse level1_pattern stream)
-
-let parse_ast_pattern stream =
-  exc_located_wrapper (fun () -> Grammar.Entry.parse level2_pattern stream)
-
-let parse_interpretation stream =
-  exc_located_wrapper (fun () -> Grammar.Entry.parse level3_term stream)
-
-(** {2 Grammar extension} *)
-
-type associativity_kind = [ `Left | `Right | `None ]
-
-let symbol s = Gramext.Stoken ("SYMBOL", s)
-let ident s = Gramext.Stoken ("IDENT", s)
-let number s = Gramext.Stoken ("NUMBER", s)
-let term = Gramext.Sself
+let parse_level1_pattern lexbuf =
+  exc_located_wrapper
+    (fun () -> Grammar.Entry.parse level1_pattern (Obj.magic lexbuf))
 
-type env_type = (string * (value_type * value)) list
+let parse_level2_ast lexbuf =
+  exc_located_wrapper
+    (fun () -> Grammar.Entry.parse level2_ast (Obj.magic lexbuf))
 
-let make_action action =
-  let rec aux (vl : env_type) =
-    function
-      [] -> Gramext.action (fun (loc: location) -> action vl loc)
-    | None :: tl -> Gramext.action (fun _ -> aux vl tl)
-    | Some (name, typ) :: tl ->
-        (* i tipi servono? Magari servono solo quando si verifica la
-         * correttezza della notazione?
-         *)
-        Gramext.action (fun (v: value) -> aux ((name, (typ, v))::vl) tl)
-  in
-    aux []
-
-let flatten_opt =
-  let rec aux acc =
-    function
-      [] -> List.rev acc
-    | None::tl -> aux acc tl
-    | Some hd::tl -> aux (hd::acc) tl
-  in
-  aux []
+let parse_level2_meta lexbuf =
+  exc_located_wrapper
+    (fun () -> Grammar.Entry.parse level2_meta (Obj.magic lexbuf))
 
-  (* given a level 1 pattern computes the new RHS of "term" grammar entry *)
-let extract_term_production pattern =
-  let rec aux = function
-    | Literal l -> aux_literal l
-    | Layout l -> aux_layout l
-    | Magic m -> aux_magic m
-    | Variable v -> aux_variable v
-    | _ -> assert false
-  and aux_literal = function
-    | `Symbol s -> [None, symbol s]
-    | `Keyword s -> [None, ident s]
-    | `Number s -> [None, number s]
-  and aux_layout = function
-    | Sub (p1, p2) -> aux p1 @ [None, symbol "\\SUB"] @ aux p2
-    | Sup (p1, p2) -> aux p1 @ [None, symbol "\\SUP"] @ aux p2
-    | Below (p1, p2) -> aux p1 @ [None, symbol "\\BELOW"] @ aux p2
-    | Above (p1, p2) -> aux p1 @ [None, symbol "\\ABOVE"] @ aux p2
-    | Frac (p1, p2) -> aux p1 @ [None, symbol "\\FRAC"] @ aux p2
-    | Atop (p1, p2) -> aux p1 @ [None, symbol "\\ATOP"] @ aux p2
-    | Over (p1, p2) -> aux p1 @ [None, symbol "\\OVER"] @ aux p2
-    | Root (p1, p2) ->
-        [None, symbol "\\ROOT"] @ aux p2 @ [None, symbol "\\OF"] @ aux p1
-    | Sqrt p -> [None, symbol "\\SQRT"] @ aux p
-    | Break -> []
-    | Box (_, pl) -> List.flatten (List.map aux pl)
-  and aux_magic = function
-    | Opt p ->
-        let p_bindings, p_atoms = List.split (aux p) in
-        let p_names = flatten_opt p_bindings in
-        [ None,
-          Gramext.srules
-            [ [ Gramext.Sopt
-                  (Gramext.srules
-                    [ p_atoms,
-                      (make_action
-                        (fun (env : env_type) (loc : location) -> env)
-                        p_bindings)])],
-              Gramext.action
-                (fun (env_opt : env_type option) (loc : location) ->
-                  match env_opt with
-                    Some env ->
-                      List.map
-                        (fun (name, (typ, v)) ->
-                          (name, (OptType typ, OptValue (Some v))))
-                        env
-                  | None -> 
-                      List.map
-                        (fun (name, typ) ->
-                          (name, (OptType typ, OptValue None)))
-                        p_names) ]]
-    | _ -> assert false
-  and aux_variable = function
-    | NumVar s -> [Some (s, NumType), number ""]
-    | TermVar s -> [Some (s, TermType), term]
-    | IdentVar s -> [Some (s, StringType), ident ""]
-    | Ascription (p, s) -> assert false (* TODO *)
-    | FreshVar _ -> assert false
-  in
-  aux pattern
-
-let level_of_int precedence =
-  (* TODO "mod" test to be removed as soon as we add all 100 levels *)
-  if precedence mod 10 <> 0 || precedence < 0 || precedence > 100 then
-    raise (Level_not_found precedence);
-  string_of_int precedence
-
-type rule_id = term Grammar.Entry.e * Token.t Gramext.g_symbol list
-
-let extend level1_pattern ?(precedence = 0) ?associativity action =
-  let p_bindings, p_atoms =
-    List.split (extract_term_production level1_pattern)
-  in
-  let level = level_of_int precedence in
-  let p_names = flatten_opt p_bindings in
-  let entry = Grammar.Entry.obj (level2_pattern: 'a Grammar.Entry.e) in
-  let _ =
-    Grammar.extend
-      [ entry, Some (Gramext.Level level),
-        [ Some level,  (* TODO should we put None here? *)
-          associativity,
-          [ p_atoms, 
-            (make_action
-              (fun (env: env_type)(loc: location) -> TermValue (action env loc))
-              p_bindings) ]]]
-  in
-  (level2_pattern, p_atoms)
+let _ =
+  parse_level1_pattern_ref := parse_level1_pattern;
+  parse_level2_ast_ref := parse_level2_ast;
+  parse_level2_meta_ref := parse_level2_meta
 
-let delete (entry, atoms) = Grammar.delete_rule entry atoms
+(** {2 Debugging} *)
 
-let print_level2_pattern () =
-  Grammar.print_entry Format.std_formatter (Grammar.Entry.obj level2_pattern);
-  Format.pp_print_flush Format.std_formatter ()
+let print_l2_pattern () =
+  Grammar.print_entry Format.std_formatter (Grammar.Entry.obj term);
+  Format.pp_print_flush Format.std_formatter ();
+  flush stdout
 
 (* vim:set encoding=utf8 foldmethod=marker: *)