src/Pure/Isar/outer_syntax.ML
author wenzelm
Tue May 17 18:10:42 2005 +0200 (2005-05-17)
changeset 15989 80dd2f5780df
parent 15973 5fd94d84470f
child 16195 0eb3c15298cd
permissions -rw-r--r--
re-init ml_prompts after loop termination;
     1 (*  Title:      Pure/Isar/outer_syntax.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 The global Isabelle/Isar outer syntax.
     6 *)
     7 
     8 signature BASIC_OUTER_SYNTAX =
     9 sig
    10   structure Isar:
    11     sig
    12       val main: unit -> unit
    13       val loop: unit -> unit
    14       val sync_main: unit -> unit
    15       val sync_loop: unit -> unit
    16     end;
    17 end;
    18 
    19 signature OUTER_SYNTAX =
    20 sig
    21   include BASIC_OUTER_SYNTAX
    22   structure Keyword:
    23     sig
    24       val control: string
    25       val diag: string
    26       val thy_begin: string
    27       val thy_switch: string
    28       val thy_end: string
    29       val thy_heading: string
    30       val thy_decl: string
    31       val thy_script: string
    32       val thy_goal: string
    33       val qed: string
    34       val qed_block: string
    35       val qed_global: string
    36       val prf_heading: string
    37       val prf_goal: string
    38       val prf_block: string
    39       val prf_open: string
    40       val prf_close: string
    41       val prf_chain: string
    42       val prf_decl: string
    43       val prf_asm: string
    44       val prf_asm_goal: string
    45       val prf_script: string
    46       val kinds: string list
    47     end
    48   type token
    49   type parser
    50   val command: string -> string -> string ->
    51     (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
    52   val markup_command: IsarOutput.markup -> string -> string -> string ->
    53     (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
    54   val improper_command: string -> string -> string ->
    55     (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
    56   val is_keyword: string -> bool
    57   val dest_keywords: unit -> string list
    58   val dest_parsers: unit -> (string * string * string * bool) list
    59   val print_outer_syntax: unit -> unit
    60   val print_commands: Toplevel.transition -> Toplevel.transition
    61   val add_keywords: string list -> unit
    62   val add_parsers: parser list -> unit
    63   val check_text: string * Position.T -> bool -> Toplevel.state -> unit
    64   val deps_thy: string -> bool -> Path.T -> string list * Path.T list
    65   val load_thy: string -> bool -> bool -> Path.T -> unit
    66   val isar: bool -> bool -> unit Toplevel.isar
    67   val scan: string -> OuterLex.token list  
    68   val read: OuterLex.token list -> 
    69 		(string * OuterLex.token list * Toplevel.transition) list
    70 end;
    71 
    72 structure OuterSyntax : OUTER_SYNTAX  =
    73 struct
    74 
    75 structure T = OuterLex;
    76 structure P = OuterParse;
    77 
    78 
    79 (** outer syntax **)
    80 
    81 (* command keyword classification *)
    82 
    83 structure Keyword =
    84 struct
    85   val control = "control";
    86   val diag = "diag";
    87   val thy_begin = "theory-begin";
    88   val thy_switch = "theory-switch";
    89   val thy_end = "theory-end";
    90   val thy_heading = "theory-heading";
    91   val thy_decl = "theory-decl";
    92   val thy_script = "theory-script";
    93   val thy_goal = "theory-goal";
    94   val qed = "qed";
    95   val qed_block = "qed-block";
    96   val qed_global = "qed-global";
    97   val prf_heading = "proof-heading";
    98   val prf_goal = "proof-goal";
    99   val prf_block = "proof-block";
   100   val prf_open = "proof-open";
   101   val prf_close = "proof-close";
   102   val prf_chain = "proof-chain";
   103   val prf_decl = "proof-decl";
   104   val prf_asm = "proof-asm";
   105   val prf_asm_goal = "proof-asm-goal";
   106   val prf_script = "proof-script";
   107 
   108   val kinds = [control, diag, thy_begin, thy_switch, thy_end, thy_heading, thy_decl, thy_script,
   109     thy_goal, qed, qed_block, qed_global, prf_heading, prf_goal, prf_block, prf_open, prf_close,
   110     prf_chain, prf_decl, prf_asm, prf_asm_goal, prf_script];
   111 end;
   112 
   113 
   114 (* parsers *)
   115 
   116 type token = T.token;
   117 type parser_fn = token list -> (Toplevel.transition -> Toplevel.transition) * token list;
   118 
   119 datatype parser =
   120   Parser of string * (string * string * IsarOutput.markup option) * bool * parser_fn;
   121 
   122 fun parser int_only markup name comment kind parse =
   123   Parser (name, (comment, kind, markup), int_only, parse);
   124 
   125 
   126 (* parse command *)
   127 
   128 local
   129 
   130 fun terminate false = Scan.succeed ()
   131   | terminate true = P.group "end of input" (Scan.option P.sync -- P.semicolon >> K ());
   132 
   133 fun trace false parse = parse
   134   | trace true parse = Scan.trace parse >> (fn (f, toks) => f o Toplevel.source toks);
   135 
   136 fun body cmd trc (name, _) =
   137   (case cmd name of
   138     SOME (int_only, parse) =>
   139       P.!!! (Scan.prompt (name ^ "# ") (trace trc parse >> pair int_only))
   140   | NONE => sys_error ("no parser for outer syntax command " ^ quote name));
   141 
   142 in
   143 
   144 fun command do_terminate do_trace cmd =
   145   P.semicolon >> K NONE ||
   146   P.sync >> K NONE ||
   147   (P.position P.command :-- body cmd do_trace) --| terminate do_terminate
   148     >> (fn ((name, pos), (int_only, f)) =>
   149       SOME (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |>
   150         Toplevel.interactive int_only |> f));
   151 
   152 end;
   153 
   154 
   155 
   156 (** global outer syntax **)
   157 
   158 local
   159 
   160 val global_lexicons = ref (Scan.empty_lexicon, Scan.empty_lexicon);
   161 val global_parsers =
   162   ref (Symtab.empty: (((string * string) * (bool * parser_fn)) * IsarOutput.markup option)
   163     Symtab.table);
   164 val global_markups = ref ([]: (string * IsarOutput.markup) list);
   165 
   166 fun change_lexicons f =
   167   let val lexs = f (! global_lexicons) in
   168     (case (op inter_string) (pairself Scan.dest_lexicon lexs) of
   169       [] => global_lexicons := lexs
   170     | bads => error ("Clash of outer syntax commands and keywords: " ^ commas_quote bads))
   171   end;
   172 
   173 fun get_markup (ms, (name, (_, SOME m))) = (name, m) :: ms
   174   | get_markup (ms, _) = ms;
   175 
   176 fun make_markups () = global_markups := Symtab.foldl get_markup ([], ! global_parsers);
   177 fun change_parsers f = (Library.change global_parsers f; make_markups ());
   178 
   179 in
   180 
   181 
   182 (* access current syntax *)
   183 
   184 (*Note: the syntax for files is statically determined at the very
   185   beginning; for interactive processing it may change dynamically.*)
   186 
   187 fun get_lexicons () = ! global_lexicons;
   188 fun get_parsers () = ! global_parsers;
   189 fun get_parser () = Option.map (#2 o #1) o curry Symtab.lookup (! global_parsers);
   190 
   191 fun is_markup kind name =
   192   (case assoc (! global_markups, name) of SOME k => k = kind | NONE => false);
   193 fun markup kind = Scan.one (T.is_kind T.Command andf is_markup kind o T.val_of);
   194 
   195 
   196 (* augment syntax *)
   197 
   198 fun add_keywords keywords = change_lexicons (apfst (fn lex =>
   199   (Scan.extend_lexicon lex (map Symbol.explode keywords))));
   200 
   201 fun add_parser (tab, Parser (name, (comment, kind, markup), int_only, parse)) =
   202  (if is_none (Symtab.lookup (tab, name)) then ()
   203   else warning ("Redefined outer syntax command " ^ quote name);
   204   Symtab.update ((name, (((comment, kind), (int_only, parse)), markup)), tab));
   205 
   206 fun add_parsers parsers =
   207   (change_parsers (fn tab => Library.foldl add_parser (tab, parsers));
   208     change_lexicons (apsnd (fn lex => Scan.extend_lexicon lex
   209       (map (fn Parser (name, _, _, _) => Symbol.explode name) parsers))));
   210 
   211 end;
   212 
   213 
   214 (* print syntax *)
   215 
   216 fun is_keyword s = Scan.is_literal (#1 (get_lexicons ())) (Symbol.explode s);
   217 fun dest_keywords () = Scan.dest_lexicon (#1 (get_lexicons ()));
   218 
   219 fun dest_parsers () =
   220   map (fn (name, (((cmt, kind), (int_only, _)), _)) => (name, cmt, kind, int_only))
   221     (Symtab.dest (get_parsers ()));
   222 
   223 fun print_outer_syntax () =
   224   let
   225     fun pretty_cmd (name, comment, _, _) =
   226       Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
   227     val (int_cmds, cmds) = List.partition #4 (dest_parsers ());
   228   in
   229     [Pretty.strs ("syntax keywords:" :: map quote (dest_keywords ())),
   230       Pretty.big_list "proper commands:" (map pretty_cmd cmds),
   231       Pretty.big_list "improper commands (interactive-only):" (map pretty_cmd int_cmds)]
   232     |> Pretty.chunks |> Pretty.writeln
   233   end;
   234 
   235 val print_commands = Toplevel.imperative print_outer_syntax;
   236 
   237 
   238 
   239 (** toplevel parsing **)
   240 
   241 (* basic sources *)
   242 
   243 fun toplevel_source term trc do_recover cmd src =
   244   let
   245     val no_terminator =
   246       Scan.unless P.semicolon (Scan.one (T.not_sync andf T.not_eof));
   247     fun recover x = (Scan.prompt "recover# " (Scan.repeat no_terminator) >> K [NONE]) x;
   248   in
   249     src
   250     |> T.source_proper
   251     |> Source.source T.stopper
   252       (Scan.bulk (P.$$$ "--" -- P.!!! P.text >> K NONE || P.not_eof >> SOME))
   253       (if do_recover then SOME recover else NONE)
   254     |> Source.mapfilter I
   255     |> Source.source T.stopper (Scan.bulk (fn xs => P.!!! (command term trc (cmd ())) xs))
   256       (if do_recover then SOME recover else NONE)
   257     |> Source.mapfilter I
   258   end;
   259 
   260 
   261 (* interactive source of toplevel transformers *)
   262 
   263 fun isar term no_pos =
   264   Source.tty
   265   |> Symbol.source true
   266   |> T.source true get_lexicons
   267     (if no_pos then Position.none else Position.line_name 1 "stdin")
   268   |> toplevel_source term false true get_parser;
   269 
   270 
   271 (* scan text,  read tokens with trace (for Proof General) *)
   272 
   273 fun scan str =
   274  Source.of_string str
   275  |> Symbol.source false
   276  |> T.source true get_lexicons Position.none
   277  |> Source.exhaust
   278 
   279 fun read toks =
   280   Source.of_list toks
   281   |> toplevel_source false true true get_parser
   282   |> Source.exhaust
   283   |> map (fn tr => (Toplevel.name_of tr, the (Toplevel.source_of tr), tr));
   284 
   285 
   286 (** read theory **)
   287 
   288 (* check_text *)
   289 
   290 fun check_text s true state = (IsarOutput.eval_antiquote (#1 (get_lexicons ())) state s; ())
   291   | check_text _ false _ = ();
   292 
   293 
   294 (* deps_thy *)
   295 
   296 fun deps_thy name ml path =
   297   let
   298     val src = Source.of_string (File.read path);
   299     val pos = Path.position path;
   300     val (name', parents, files) = ThyHeader.scan (src, pos);
   301     val ml_path = ThyLoad.ml_path name;
   302     val ml_file = if ml andalso is_some (ThyLoad.check_file NONE ml_path) then [ml_path] else [];
   303   in
   304     if name <> name' then
   305       error ("Filename " ^ quote (Path.pack path) ^
   306         " does not agree with theory name " ^ quote name')
   307     else (parents, map (Path.unpack o #1) files @ ml_file)
   308   end;
   309 
   310 
   311 (* load_thy *)
   312 
   313 local
   314 
   315 fun try_ml_file name time =
   316   let
   317     val path = ThyLoad.ml_path name;
   318     val tr = Toplevel.imperative (fn () => ThyInfo.load_file time path);
   319     val tr_name = if time then "time_use" else "use";
   320   in
   321     if is_none (ThyLoad.check_file NONE path) then ()
   322     else Toplevel.excursion [Toplevel.empty |> Toplevel.name tr_name |> tr]
   323   end;
   324 
   325 fun parse_thy src =
   326   src
   327   |> toplevel_source false false false (K (get_parser ()))
   328   |> Source.exhaust;
   329 
   330 fun run_thy name path =
   331   let
   332     val pos = Path.position path;
   333     val text = Library.untabify (explode (File.read path));
   334     val text_src = Source.of_list text;
   335     fun present_text () = Source.exhaust (Symbol.source false text_src);
   336   in
   337     Present.init_theory name;
   338     Present.verbatim_source name present_text;
   339     if ThyHeader.is_old (text_src, pos) then (ThySyn.load_thy name text;
   340       Present.old_symbol_source name present_text)   (*note: text presented twice*)
   341     else
   342       let
   343         val tok_src = text_src
   344           |> Symbol.source false
   345           |> T.source false (K (get_lexicons ())) pos
   346           |> Source.exhausted;
   347         val out = Toplevel.excursion_result
   348           (IsarOutput.parse_thy markup (#1 (get_lexicons ()))
   349             (parse_thy tok_src) tok_src);
   350       in Present.theory_output name (Buffer.content out) end
   351   end;
   352 
   353 in
   354 
   355 fun load_thy name ml time path =
   356  (if time then
   357     timeit (fn () =>
   358      (writeln ("\n**** Starting theory " ^ quote name ^ " ****");
   359       run_thy name path;
   360       writeln ("**** Finished theory " ^ quote name ^ " ****\n")))
   361   else run_thy name path;
   362   Context.context (ThyInfo.get_theory name);
   363   if ml then try_ml_file name time else ());
   364 
   365 end;
   366 
   367 
   368 
   369 (** the read-eval-print loop **)
   370 
   371 (* main loop *)
   372 
   373 fun gen_loop term no_pos =
   374  (Context.reset_context ();
   375   Toplevel.loop (isar term no_pos);
   376   ml_prompts "ML> " "ML# ");
   377 
   378 fun gen_main term no_pos =
   379  (Toplevel.set_state Toplevel.toplevel;
   380   writeln (Session.welcome ());
   381   gen_loop term no_pos);
   382 
   383 structure Isar =
   384 struct
   385   fun main () = gen_main false false;
   386   fun loop () = gen_loop false false;
   387   fun sync_main () = gen_main true true;
   388   fun sync_loop () = gen_loop true true;
   389 end;
   390 
   391 
   392 (*final declarations of this structure!*)
   393 val command = parser false NONE;
   394 val markup_command = parser false o SOME;
   395 val improper_command = parser true NONE;
   396 
   397 end;
   398 
   399 (*setup theory syntax dependent operations*)
   400 ThyLoad.deps_thy_fn := OuterSyntax.deps_thy;
   401 ThyLoad.load_thy_fn := OuterSyntax.load_thy;
   402 structure ThyLoad: THY_LOAD = ThyLoad;
   403 
   404 structure BasicOuterSyntax: BASIC_OUTER_SYNTAX = OuterSyntax;
   405 open BasicOuterSyntax;
   406 open Isar;