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