X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=helm%2Focaml%2Fcic_notation%2FcicNotationParser.ml;h=f4651d6374e0452583be24f117eee5e6794f83aa;hb=57b43a967eaf3b0747350cd775d4301a53af2820;hp=2b5598c42a0873b5c2fa2c420a6cb2529465144f;hpb=879797d6505bc39489009d9ae1e2506022bde9e2;p=helm.git diff --git a/helm/ocaml/cic_notation/cicNotationParser.ml b/helm/ocaml/cic_notation/cicNotationParser.ml index 2b5598c42..f4651d637 100644 --- a/helm/ocaml/cic_notation/cicNotationParser.ml +++ b/helm/ocaml/cic_notation/cicNotationParser.ml @@ -25,17 +25,49 @@ open Printf +open CicNotationEnv +open CicNotationPt + exception Parse_error of Token.flocation * string +exception Level_not_found of int + +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 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 grammar = Grammar.gcreate CicNotationLexer.level1_lexer +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 level2_meta = Grammar.Entry.create level2_meta_grammar "level2_meta" -let level1 = Grammar.Entry.create grammar "level1" +let level3_term = Grammar.Entry.create level2_ast_grammar "level3_term" +let notation = (* level1 <-> level 2 *) + Grammar.Entry.create level2_ast_grammar "notation" +let interpretation = (* level2 <-> level 3 *) + Grammar.Entry.create level2_ast_grammar "interpretation" +let phrase = Grammar.Entry.create level2_ast_grammar "phrase" let return_term loc term = () -(*let fail floc msg =*) -(* let (x, y) = CicAst.loc_of_floc floc in*) -(* failwith (sprintf "Error at characters %d - %d: %s" x y msg)*) +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 @@ -43,63 +75,567 @@ let int_of_string s = with Failure _ -> failwith (sprintf "Lexer failure: string_of_int \"%s\" failed" s) -EXTEND - GLOBAL: level1; +(** {2 Grammar extension} *) + +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 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 * value_type + | Env of (string * value_type) list + +let make_action action bindings = + let rec aux (vl : CicNotationEnv.t) = + function + [] -> Gramext.action (fun (loc: location) -> action vl loc) + | NoBinding :: tl -> Gramext.action (fun _ -> aux vl tl) + (* LUCA: DEFCON 5 BEGIN *) + | Binding (name, TermType) :: tl -> + Gramext.action + (fun (v:term) -> aux ((name, (TermType, TermValue v))::vl) tl) + | Binding (name, StringType) :: tl -> + Gramext.action + (fun (v:string) -> + aux ((name, (StringType, StringValue v)) :: vl) tl) + | Binding (name, NumType) :: tl -> + Gramext.action + (fun (v:string) -> aux ((name, (NumType, NumValue v)) :: vl) tl) + | Binding (name, OptType t) :: tl -> + Gramext.action + (fun (v:'a option) -> + aux ((name, (OptType t, OptValue v)) :: vl) tl) + | Binding (name, ListType t) :: tl -> + Gramext.action + (fun (v:'a list) -> + aux ((name, (ListType t, ListValue v)) :: vl) tl) + | Env _ :: tl -> + Gramext.action (fun (v:CicNotationEnv.t) -> aux (v @ vl) tl) + (* LUCA: DEFCON 5 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 + | AttributedTerm (_, t) -> aux t + | Literal l -> aux_literal l + | Layout l -> aux_layout l + | Magic m -> aux_magic m + | 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 + | Sub (p1, p2) -> aux p1 @ [NoBinding, gram_symbol "\\sub"] @ aux p2 + | Sup (p1, p2) -> aux p1 @ [NoBinding, gram_symbol "\\sup"] @ aux p2 + | Below (p1, p2) -> aux p1 @ [NoBinding, gram_symbol "\\below"] @ aux p2 + | Above (p1, p2) -> aux p1 @ [NoBinding, gram_symbol "\\above"] @ aux p2 + | Frac (p1, p2) -> aux p1 @ [NoBinding, gram_symbol "\\frac"] @ aux p2 + | Atop (p1, p2) -> aux p1 @ [NoBinding, gram_symbol "\\atop"] @ aux p2 + | Over (p1, p2) -> aux p1 @ [NoBinding, gram_symbol "\\over"] @ aux p2 + | Root (p1, p2) -> + [NoBinding, gram_symbol "\\root"] @ aux p2 + @ [NoBinding, gram_symbol "\\of"] @ aux p1 + | Sqrt p -> [NoBinding, gram_symbol "\\sqrt"] @ aux p + | Break -> [] + | Box (_, pl) -> List.flatten (List.map aux pl) + and aux_magic magic = + match magic with + | Opt p -> + let p_bindings, p_atoms, p_names, p_action = inner_pattern p in + let action (env_opt : CicNotationEnv.t option) (loc : location) = + match env_opt with + | Some env -> List.map opt_binding_some env + | None -> List.map opt_binding_of_name p_names + in + [ Env (List.map opt_declaration p_names), + Gramext.srules + [ [ Gramext.Sopt (Gramext.srules [ p_atoms, p_action ]) ], + Gramext.action action ] ] + | List0 (p, _) + | 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 : location) = + CicNotationEnv.coalesce_env p_names env_list + in + let gram_of_list s = + match magic with + | List0 (_, None) -> Gramext.Slist0 s + | List1 (_, None) -> Gramext.Slist1 s + | List0 (_, Some l) -> Gramext.Slist0sep (s, gram_of_literal l) + | List1 (_, Some l) -> Gramext.Slist1sep (s, gram_of_literal l) + | _ -> assert false + in + [ Env (List.map list_declaration p_names), + Gramext.srules + [ [ gram_of_list (Gramext.srules [ p_atoms, p_action ]) ], + Gramext.action action ] ] + | _ -> assert false + and aux_variable = + function + | NumVar s -> [Binding (s, NumType), gram_number ""] + | TermVar s -> [Binding (s, TermType), gram_term] + | IdentVar s -> [Binding (s, StringType), gram_ident ""] + | Ascription (p, s) -> assert false (* TODO *) + | 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 : location) -> env) + p_bindings + in + p_bindings, p_atoms, p_names, action + in + aux pattern + +let level_of_int precedence = + if precedence < min_precedence || precedence > max_precedence then + raise (Level_not_found precedence); + string_of_int precedence + +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 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 _ = + Grammar.extend + [ Grammar.Entry.obj (term: 'a Grammar.Entry.e), + Some (Gramext.Level level), + [ None, + associativity, + [ p_atoms, + (make_action + (fun (env: CicNotationEnv.t) (loc: 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 - level1: [ [ p = pattern -> () ] ]; +(** {2 Grammar} *) - pattern: [ [ p = LIST1 simple_pattern -> () ] ]; +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_binder binder pt_names body = + let fold_cluster binder terms ty body = + List.fold_right + (fun term body -> Binder (binder, (term, ty), body)) + terms body (* terms are names: either Ident or FreshVar *) + in + 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 _ = (* create empty precedence level for "term" *) + 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) + in + aux [] last + in + Grammar.extend + [ Grammar.Entry.obj (term: 'a Grammar.Entry.e), + None, + mk_level_list min_precedence max_precedence ] + +(* {{{ Grammar for concrete syntax patterns, notation level 1 *) +EXTEND + GLOBAL: level1_pattern; + + level1_pattern: [ [ p = l1_pattern; EOI -> CicNotationUtil.boxify p ] ]; + l1_pattern: [ [ p = LIST1 l1_simple_pattern -> p ] ]; literal: [ - [ s = SYMBOL -> () - | k = KEYWORD -> () + [ s = SYMBOL -> `Symbol s + | k = QKEYWORD -> `Keyword k + | n = NUMBER -> `Number n + ] + ]; + sep: [ [ "sep"; sep = literal -> sep ] ]; +(* row_sep: [ [ "rowsep"; sep = literal -> sep ] ]; + field_sep: [ [ "fieldsep"; sep = literal -> sep ] ]; *) + l1_magic_pattern: [ + [ "list0"; p = l1_simple_pattern; sep = OPT sep -> List0 (p, sep) + | "list1"; p = l1_simple_pattern; sep = OPT sep -> List1 (p, sep) + | "opt"; p = l1_simple_pattern -> Opt p + ] + ]; + l1_pattern_variable: [ + [ "term"; id = IDENT -> TermVar id + | "number"; id = IDENT -> NumVar id + | "ident"; id = IDENT -> 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))) + | p1 = SELF; SYMBOL "\\over"; p2 = SELF -> + return_term loc (Layout (Over (p1, p2))) + | p1 = SELF; SYMBOL "\\atop"; p2 = SELF -> + return_term loc (Layout (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 = SELF; SYMBOL "\\OF"; arg = SELF -> + return_term loc (Layout (Root (arg, index))) + | "hbox"; LPAREN; p = l1_pattern; RPAREN -> + return_term loc (CicNotationUtil.boxify p) + | "vbox"; LPAREN; p = l1_pattern; RPAREN -> + return_term loc (CicNotationUtil.boxify p) + | "hvbox"; LPAREN; p = l1_pattern; RPAREN -> + return_term loc (CicNotationUtil.boxify p) + | "hovbox"; LPAREN; p = l1_pattern; RPAREN -> + return_term loc (CicNotationUtil.boxify p) + | "break" -> return_term loc (Layout Break) +(* | SYMBOL "\\SPACE" -> return_term loc (Layout Space) *) + | LPAREN; p = l1_pattern; RPAREN -> + return_term loc (CicNotationUtil.boxify p) + ] + | "simple" NONA + [ i = IDENT -> return_term loc (Variable (TermVar i)) + | 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) + ] + ]; + END +(* }}} *) - sep: [ [ SYMBOL "\\SEP"; sep = literal -> () ] ]; - row_sep: [ [ SYMBOL "\\ROWSEP"; sep = literal -> () ] ]; - field_sep: [ [ SYMBOL "\\FIELDSEP"; sep = literal -> () ] ]; - box_token: [ - [ SYMBOL "\\HBOX"; p = simple_pattern -> () - | SYMBOL "\\VBOX"; p = simple_pattern -> () - | SYMBOL "\\BREAK" -> () +EXTEND + GLOBAL: level2_meta; + l2_variable: [ + [ "term"; id = IDENT -> TermVar id + | "number"; id = IDENT -> NumVar id + | "ident"; id = IDENT -> IdentVar id + | "fresh"; id = IDENT -> FreshVar id + | "anonymous" -> TermVar "_" + | id = IDENT -> TermVar id ] ]; - - layout_schemata: [ - [ SYMBOL "\\ARRAY"; p = simple_pattern; fsep = OPT field_sep; - rsep = OPT row_sep -> - () - | SYMBOL "\\FRAC"; p1 = simple_pattern; p2 = simple_pattern -> () - | SYMBOL "\\SQRT"; p = simple_pattern -> () - | SYMBOL "\\ROOT"; p1 = simple_pattern; SYMBOL "\\OF"; - p2 = simple_pattern -> - () - (* TODO XXX many issues here: - * - "^^" is lexed as two "^" symbols - * - "a_b" is lexed as IDENT "a_b" *) - | p1 = simple_pattern; SYMBOL "^"; p2 = simple_pattern -> () - | p1 = simple_pattern; SYMBOL "^"; SYMBOL "^"; p2 = simple_pattern -> () - | p1 = simple_pattern; SYMBOL "_"; p2 = simple_pattern -> () - | p1 = simple_pattern; SYMBOL "_"; SYMBOL "_"; p2 = simple_pattern -> () + l2_magic: [ + [ "fold"; kind = [ "left" -> `Left | "right" -> `Right ]; + base = level2_meta; "rec"; id = IDENT; recursive = level2_meta -> + Fold (kind, base, [id], recursive) + | "default"; some = level2_meta; none = level2_meta -> Default (some, none) + | "if"; p_test = level2_meta; + "then"; p_true = level2_meta; + "else"; p_false = level2_meta -> + If (p_test, p_true, p_false) + | "fail" -> Fail + ] + ]; + level2_meta: [ + [ magic = l2_magic -> Magic magic + | var = l2_variable -> Variable var + | blob = UNPARSED_AST -> !parse_level2_ast_ref (Stream.of_string blob) ] ]; +END - simple_pattern: [ - [ SYMBOL "\\LIST0"; p = simple_pattern; sep = OPT sep -> () - | SYMBOL "\\LIST1"; p = simple_pattern; sep = OPT sep -> () - | b = box_token -> () - | id = IDENT -> () - | SYMBOL "\\NUM"; id = IDENT -> () - | SYMBOL "\\IDENT"; id = IDENT -> () - | SYMBOL "\\OPT"; p = simple_pattern -> () - | l = layout_schemata -> () - | SYMBOL "["; p = pattern; SYMBOL "]" -> () +EXTEND + GLOBAL: level2_ast term + level3_term + notation interpretation + phrase; +(* {{{ Grammar for ast patterns, notation level 2 *) + level2_ast: [ [ p = term -> p ] ]; + sort: [ + [ "Prop" -> `Prop + | "Set" -> `Set + | "Type" -> `Type + ] + ]; + explicit_subst: [ + [ SYMBOL "\\subst"; (* to avoid catching frequent "a [1]" cases *) + SYMBOL "["; + substs = LIST1 [ + i = IDENT; SYMBOL <:unicode> (* ≔ *); t = term -> (i, t) + ] SEP SYMBOL ";"; + SYMBOL "]" -> + substs ] ]; + meta_subst: [ + [ s = SYMBOL "_" -> None + | p = term -> Some p ] + ]; + meta_substs: [ + [ SYMBOL "["; substs = LIST0 meta_subst; SYMBOL "]" -> substs ] + ]; + possibly_typed_name: [ + [ LPAREN; id = bound_name; SYMBOL ":"; typ = term; RPAREN -> + id, Some typ + | id = bound_name -> id, None + ] + ]; + match_pattern: [ + [ id = IDENT -> id, [] + | LPAREN; id = IDENT; vars = LIST1 possibly_typed_name; RPAREN -> + id, vars + ] + ]; + binder: [ + [ SYMBOL <:unicode> (* Π *) -> `Pi + | SYMBOL <:unicode> (* ∃ *) -> `Exists + | SYMBOL <:unicode> (* ∀ *) -> `Forall + | SYMBOL <:unicode> (* λ *) -> `Lambda + ] + ]; + bound_name: [ + [ i = IDENT -> Ident (i, None) + | SYMBOL "\\FRESH"; i = IDENT -> Variable (FreshVar i) + ] + ]; + bound_names: [ + [ vars = LIST1 bound_name SEP SYMBOL ","; + ty = OPT [ SYMBOL ":"; p = term -> p ] -> + [ vars, ty ] + | clusters = LIST1 [ + LPAREN; + vars = LIST1 bound_name SEP SYMBOL ","; + ty = OPT [ SYMBOL ":"; p = term -> p ]; + RPAREN -> + vars, ty + ] -> + clusters + ] + ]; + induction_kind: [ + [ "rec" -> `Inductive + | "corec" -> `CoInductive + ] + ]; + let_defs: [ + [ defs = LIST1 [ + name = bound_name; args = bound_names; + index_name = OPT [ "on"; id = bound_name -> id ]; + ty = OPT [ SYMBOL ":" ; p = term -> p ]; + SYMBOL <:unicode> (* ≝ *); body = term -> + let body = fold_binder `Lambda args body in + let ty = + match ty with + | None -> None + | Some ty -> Some (fold_binder `Pi args ty) + in + let rec position_of name p = function + | [] -> None, p + | n :: _ when n = name -> Some p, p + | _ :: tl -> position_of name (p + 1) tl + in + let rec find_arg name n = function + | [] -> + 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 + | Some where, len -> n + where) + in + let index = + match index_name with + | None -> 0 + | Some name -> find_arg name 0 args + in + (name, ty), body, index + ] SEP "and" -> + defs + ] + ]; + term: LEVEL "10" (* let in *) + [ "10" NONA + [ "let"; var = possibly_typed_name; SYMBOL <:unicode> (* ≝ *); + 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 + [ b = binder; names = bound_names; SYMBOL "."; body = term -> + return_term loc (fold_binder b names body) + ] + ]; + term: LEVEL "70" (* apply *) + [ "70" LEFTA + [ p1 = term; p2 = term -> + let rec aux = function + | Appl (hd :: tl) + | AttributedTerm (_, Appl (hd :: tl)) -> + aux hd @ tl + | term -> [term] + in + return_term loc (Appl (aux p1 @ [p2])) + ] + ]; + term: LEVEL "90" (* simple *) + [ "90" NONA + [ 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)) + | 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_substs -> return_term loc (Meta (int_of_string m, s)) + | s = sort -> return_term loc (Sort s) + | outtyp = OPT [ SYMBOL "["; ty = term; SYMBOL "]" -> ty ]; + "match"; t = term; + indty_ident = OPT [ SYMBOL ":"; id = IDENT -> id ]; + "with"; SYMBOL "["; + patterns = LIST0 [ + lhs = match_pattern; SYMBOL <:unicode> (* ⇒ *); + rhs = term -> + lhs, rhs + ] SEP SYMBOL "|"; + 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 ]) + | LPAREN; p = term; RPAREN -> p + | blob = UNPARSED_META -> !parse_level2_meta_ref (Stream.of_string blob) + ] + ]; +(* }}} *) +(* {{{ Grammar for interpretation, notation level 3 *) + argument: [ + [ id = IDENT -> IdentArg (0, id) + | l = LIST1 [ SYMBOL <:unicode> (* η *) -> () ] SEP SYMBOL "."; + SYMBOL "."; id = IDENT -> + IdentArg (List.length l, id) + ] + ]; + level3_term: [ + [ u = URI -> UriPattern (UriManager.uri_of_string u) + | id = IDENT -> VarPattern id + | LPAREN; terms = LIST1 SELF; RPAREN -> + (match terms with + | [] -> assert false + | [term] -> term + | terms -> ApplPattern terms) + ] + ]; +(* }}} *) +(* {{{ Notation glues *) + associativity: [ + [ IDENT "left"; IDENT "associative" -> Gramext.LeftA + | IDENT "right"; IDENT "associative" -> Gramext.RightA + | IDENT "non"; IDENT "associative" -> Gramext.NonA + ] + ]; + precedence: [ + [ "with"; IDENT "precedence"; n = NUMBER -> int_of_string n ] + ]; + notation: [ + [ s = QSTRING; + assoc = OPT associativity; prec = OPT precedence; + IDENT "for"; + p2 = + [ blob = UNPARSED_AST -> !parse_level2_ast_ref (Stream.of_string blob) + | blob = UNPARSED_META -> + !parse_level2_meta_ref (Stream.of_string blob) ] + -> + (!parse_level1_pattern_ref (Stream.of_string s), assoc, prec, p2) + ] + ]; + interpretation: [ + [ s = CSYMBOL; args = LIST1 argument; SYMBOL "="; t = level3_term -> + (s, args, t) + ] + ]; +(* }}} *) +(* {{{ Top-level phrases *) + phrase: [ + [ IDENT "print"; t = term; SYMBOL "." -> Print t + | IDENT "notation"; (l1, assoc, prec, l2) = notation; SYMBOL "." -> + Notation (l1, assoc, prec, l2) + | IDENT "interpretation"; (symbol, args, l3) = interpretation; SYMBOL "." -> + Interpretation ((symbol, args), l3) + | IDENT "render"; u = URI; SYMBOL "." -> Render (UriManager.uri_of_string u) + | IDENT "dump"; SYMBOL "." -> Dump + ] + ]; +(* }}} *) END +(** {2 API implementation} *) + let exc_located_wrapper f = try f () @@ -110,6 +646,26 @@ let exc_located_wrapper f = raise (Parse_error (floc, (Printexc.to_string exn))) let parse_level1_pattern stream = - exc_located_wrapper (fun () -> (Grammar.Entry.parse level1 stream)) + exc_located_wrapper (fun () -> Grammar.Entry.parse level1_pattern stream) +let parse_level2_ast stream = + exc_located_wrapper (fun () -> Grammar.Entry.parse level2_ast stream) +let parse_level2_meta stream = + exc_located_wrapper (fun () -> Grammar.Entry.parse level2_meta stream) +let parse_interpretation stream = + exc_located_wrapper (fun () -> Grammar.Entry.parse level3_term stream) +let parse_phrase stream = + exc_located_wrapper (fun () -> Grammar.Entry.parse phrase stream) + +let _ = + parse_level1_pattern_ref := parse_level1_pattern; + parse_level2_ast_ref := parse_level2_ast; + parse_level2_meta_ref := parse_level2_meta + +(** {2 Debugging} *) + +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: *) +(* vim:set encoding=utf8 foldmethod=marker: *)