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