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