src/Pure/Thy/ml_context.ML
changeset 23939 e543359fe8b6
parent 23033 a7e23f993c5e
child 24056 e134e757fc64
equal deleted inserted replaced
23938:977d14aeb4d5 23939:e543359fe8b6
    66 fun pass_context context f x =
    66 fun pass_context context f x =
    67   (case pass (SOME context) f x of
    67   (case pass (SOME context) f x of
    68     (y, SOME context') => (y, context')
    68     (y, SOME context') => (y, context')
    69   | (_, NONE) => error "Lost context in ML");
    69   | (_, NONE) => error "Lost context in ML");
    70 
    70 
    71 fun save f x = setmp (get_context ()) f x;
    71 fun save f x = CRITICAL (fn () => setmp (get_context ()) f x);
    72 
    72 
    73 fun change_theory f =
    73 fun change_theory f = CRITICAL (fn () =>
    74   set_context (SOME (Context.map_theory f (the_generic_context ())));
    74   set_context (SOME (Context.map_theory f (the_generic_context ()))));
       
    75 
    75 
    76 
    76 
    77 
    77 (** ML antiquotations **)
    78 (** ML antiquotations **)
    78 
    79 
    79 (* maintain keywords *)
    80 (* maintain keywords *)
    80 
    81 
    81 val global_lexicon = ref Scan.empty_lexicon;
    82 val global_lexicon = ref Scan.empty_lexicon;
    82 
    83 
    83 fun add_keywords keywords =
    84 fun add_keywords keywords = CRITICAL (fn () =>
    84   change global_lexicon (Scan.extend_lexicon (map Symbol.explode keywords));
    85   change global_lexicon (Scan.extend_lexicon (map Symbol.explode keywords)));
    85 
    86 
    86 
    87 
    87 (* maintain commands *)
    88 (* maintain commands *)
    88 
    89 
    89 datatype antiq = Inline of string | ProjValue of string * string * string;
    90 datatype antiq = Inline of string | ProjValue of string * string * string;
    90 val global_parsers = ref (Symtab.empty:
    91 val global_parsers = ref (Symtab.empty:
    91   (Context.generic * Args.T list -> antiq * (Context.generic * Args.T list)) Symtab.table);
    92   (Context.generic * Args.T list -> antiq * (Context.generic * Args.T list)) Symtab.table);
    92 
    93 
    93 local
    94 local
    94 
    95 
    95 fun add_item kind name scan = change global_parsers (fn tab =>
    96 fun add_item kind name scan = CRITICAL (fn () =>
    96  (if not (Symtab.defined tab name) then ()
    97   change global_parsers (fn tab =>
    97   else warning ("Redefined ML antiquotation: " ^ quote name);
    98    (if not (Symtab.defined tab name) then ()
    98   Symtab.update (name, scan >> kind) tab));
    99     else warning ("Redefined ML antiquotation: " ^ quote name);
       
   100     Symtab.update (name, scan >> kind) tab)));
    99 
   101 
   100 in
   102 in
   101 
   103 
   102 val inline_antiq = add_item Inline;
   104 val inline_antiq = add_item Inline;
   103 val proj_value_antiq = add_item ProjValue;
   105 val proj_value_antiq = add_item ProjValue;
   113 
   115 
   114 fun command src =
   116 fun command src =
   115   let val ((name, _), pos) = Args.dest_src src in
   117   let val ((name, _), pos) = Args.dest_src src in
   116     (case Symtab.lookup (! global_parsers) name of
   118     (case Symtab.lookup (! global_parsers) name of
   117       NONE => error ("Unknown ML antiquotation command: " ^ quote name ^ Position.str_of pos)
   119       NONE => error ("Unknown ML antiquotation command: " ^ quote name ^ Position.str_of pos)
   118     | SOME scan => transform_failure (curry Antiquote.ANTIQUOTE_FAIL (name, pos))
   120     | SOME scan => syntax scan src)
   119         (fn () => syntax scan src) ())
       
   120   end;
   121   end;
   121 
   122 
   122 
   123 
   123 (* outer syntax *)
   124 (* outer syntax *)
   124 
   125 
   135 local
   136 local
   136 
   137 
   137 val isabelle_struct = enclose "structure Isabelle =\nstruct\n" "end;";
   138 val isabelle_struct = enclose "structure Isabelle =\nstruct\n" "end;";
   138 val isabelle_struct0 = isabelle_struct "";
   139 val isabelle_struct0 = isabelle_struct "";
   139 
   140 
   140 fun eval_antiquotes txt =
   141 fun eval_antiquotes txt = CRITICAL (fn () =>
   141   let
   142   let
   142     val ants = Antiquote.scan_antiquotes (txt, Position.line 1);
   143     val ants = Antiquote.scan_antiquotes (txt, Position.line 1);
   143 
   144 
   144     fun expand (Antiquote.Text s) names = (("", Symbol.escape s), names)
   145     fun expand (Antiquote.Text s) names = (("", Symbol.escape s), names)
   145       | expand (Antiquote.Antiq x) names =
   146       | expand (Antiquote.Antiq x) names =
   156               in ((binding, value), names') end);
   157               in ((binding, value), names') end);
   157 
   158 
   158     val (decls, body) =
   159     val (decls, body) =
   159       fold_map expand ants ML_Syntax.reserved
   160       fold_map expand ants ML_Syntax.reserved
   160       |> #1 |> split_list |> pairself implode;
   161       |> #1 |> split_list |> pairself implode;
   161   in (isabelle_struct decls, body) end;
   162   in (isabelle_struct decls, body) end);
   162 
   163 
   163 fun eval name verbose txt =
   164 fun eval name verbose txt =
   164   Output.ML_errors (use_text name Output.ml_output verbose) txt;
   165   Output.ML_errors (use_text name Output.ml_output verbose) txt;
   165 
   166 
   166 in
   167 in