let min_precedence = 0
let max_precedence = 100
-let default_precedence = 50
-
-let let_in_prec = 10
-let binder_prec = 20
-let apply_prec = 70
-let simple_prec = 90
-
-let let_in_assoc = Gramext.NonA
-let binder_assoc = Gramext.RightA
-let apply_assoc = Gramext.LeftA
-let simple_assoc = Gramext.NonA
let level1_pattern =
Grammar.Entry.create level1_pattern_grammar "level1_pattern"
let return_term loc term = ()
-let fail floc msg =
- let (x, y) = loc_of_floc floc in
- failwith (sprintf "Error at characters %d - %d: %s" x y msg)
-
let int_of_string s =
try
Pervasives.int_of_string s
in
aux pattern
-let level_of_int precedence =
+let level_of precedence associativity =
if precedence < min_precedence || precedence > max_precedence then
raise (Level_not_found precedence);
- string_of_int 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 = default_precedence)
- ?associativity action
-=
+let extend level1_pattern ~precedence ~associativity action =
let p_bindings, p_atoms =
List.split (extract_term_production level1_pattern)
in
- let level = level_of_int precedence 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,
- associativity,
+ Some associativity,
[ p_atoms,
(make_action
(fun (env: CicNotationEnv.t) (loc: location) -> (action env loc))
let mk_level_list first last =
let rec aux acc = function
| i when i < first -> acc
- | i -> aux ((Some (string_of_int i), None, []) :: acc) (i - 1)
+ | i ->
+ aux
+ ((Some (string_of_int i ^ "N"), Some Gramext.NonA, [])
+ :: (Some (string_of_int i ^ "L"), Some Gramext.LeftA, [])
+ :: (Some (string_of_int i ^ "R"), Some Gramext.RightA, [])
+ :: acc)
+ (i - 1)
in
aux [] last
in
| 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 = SELF; SYMBOL "\\OF"; arg = SELF ->
+ | SYMBOL "\\root"; index = SELF; SYMBOL "\\of"; arg = SELF ->
return_term loc (Layout (Root (arg, index)))
| "hbox"; LPAREN; p = l1_pattern; RPAREN ->
return_term loc (Layout (Box ((H, false, false), p)))
END
(* }}} *)
-
+(* {{{ Grammar for ast magics, notation level 2 *)
EXTEND
GLOBAL: level2_meta;
l2_variable: [
]
];
END
+(* }}} *)
+(* {{{ Grammar for ast patterns, notation level 2 *)
EXTEND
GLOBAL: level2_ast term let_defs;
-(* {{{ Grammar for ast patterns, notation level 2 *)
level2_ast: [ [ p = term -> p ] ];
sort: [
[ "Prop" -> `Prop
| "Set" -> `Set
| "Type" -> `Type
+ | "CProp" -> `CProp
]
];
explicit_subst: [
RPAREN -> (vars, typ)
]
];
- term: LEVEL "10" (* let in *)
- [ "10" NONA
- [ "let"; var = possibly_typed_name; SYMBOL <:unicode<def>> (* ≝ *);
- p1 = term; "in"; p2 = term ->
- return_term loc (LetIn (var, p1, p2))
- | "let"; k = induction_kind; defs = let_defs; "in";
- body = term ->
- return_term loc (LetRec (k, defs, body))
- ]
- ];
- term: LEVEL "20" (* binder *)
- [ "20" RIGHTA
+ term: LEVEL "10N" [ (* let in *)
+ [ "let"; var = possibly_typed_name; SYMBOL <:unicode<def>> (* ≝ *);
+ p1 = term; "in"; p2 = term ->
+ return_term loc (LetIn (var, p1, p2))
+ | "let"; k = induction_kind; defs = let_defs; "in";
+ body = term ->
+ 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)
]
];
- term: LEVEL "70" (* apply *)
- [ "70" LEFTA
+ term: LEVEL "70L" (* apply *)
+ [
[ p1 = term; p2 = term ->
let rec aux = function
| Appl (hd :: tl)
return_term loc (Appl (aux p1 @ [p2]))
]
];
- term: LEVEL "90" (* simple *)
- [ "90" NONA
+ term: LEVEL "90N" (* simple *)
+ [
[ id = IDENT -> return_term loc (Ident (id, None))
| id = IDENT; s = explicit_subst -> return_term loc (Ident (id, Some s))
| s = CSYMBOL -> return_term loc (Symbol (s, 0))
SYMBOL "]" ->
return_term loc (Case (t, indty_ident, outtyp, patterns))
| LPAREN; p1 = term; SYMBOL ":"; p2 = term; RPAREN ->
- return_term loc (Appl [ Symbol ("cast", 0); p1; p2 ])
+ return_term loc (Cast (p1, p2))
| LPAREN; p = term; RPAREN -> p
| blob = UNPARSED_META -> !parse_level2_meta_ref (Stream.of_string blob)
]
];
-(* }}} *)
END
+(* }}} *)
(** {2 API implementation} *)