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