src/Pure/Isar/outer_syntax.ML
author wenzelm
Wed Oct 06 00:33:53 1999 +0200 (1999-10-06)
changeset 7750 e63416829538
parent 7746 b52c1a632e6a
child 7755 01e3d545ced8
permissions -rw-r--r--
added markup_command;
     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   val main: unit -> unit
    11   val loop: unit -> unit
    12   val sync_main: unit -> unit
    13   val sync_loop: unit -> unit
    14   val help: 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 prf_goal: string
    33       val prf_block: string
    34       val prf_chain: string
    35       val prf_decl: string
    36       val prf_asm: string
    37       val prf_asm_goal: string
    38       val prf_script: string
    39       val kinds: string list
    40     end
    41   type token
    42   type parser
    43   val command: string -> string -> string ->
    44     (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
    45   val markup_command: string -> string -> string ->
    46     (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
    47   val improper_command: string -> string -> string ->
    48     (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
    49   val dest_keywords: unit -> string list
    50   val dest_parsers: unit -> (string * string * string * bool) list
    51   val print_outer_syntax: unit -> unit
    52   val print_help: Toplevel.transition -> Toplevel.transition
    53   val add_keywords: string list -> unit
    54   val add_parsers: parser list -> unit
    55   val theory_header: token list -> (string * string list * (string * bool) list) * token list
    56   val deps_thy: string -> Path.T -> string list * Path.T list
    57   val load_thy: string -> bool -> bool -> Path.T -> unit
    58   val isar: bool -> bool -> Toplevel.isar
    59 end;
    60 
    61 structure OuterSyntax: OUTER_SYNTAX =
    62 struct
    63 
    64 structure T = OuterLex;
    65 structure P = OuterParse;
    66 
    67 
    68 (** outer syntax **)
    69 
    70 (* command keyword classification *)
    71 
    72 structure Keyword =
    73 struct
    74   val control = "control";
    75   val diag = "diag";
    76   val thy_begin = "theory-begin";
    77   val thy_switch = "theory-switch";
    78   val thy_end = "theory-end";
    79   val thy_heading = "theory-heading";
    80   val thy_decl = "theory-decl";
    81   val thy_goal = "theory-goal";
    82   val qed = "qed";
    83   val qed_block = "qed-block";
    84   val prf_goal = "proof-goal";
    85   val prf_block = "proof-block";
    86   val prf_chain = "proof-chain";
    87   val prf_decl = "proof-decl";
    88   val prf_asm = "proof-asm";
    89   val prf_asm_goal = "proof-asm-goal";
    90   val prf_script = "proof-script";
    91 
    92   val kinds = [control, diag, thy_begin, thy_switch, thy_end, thy_heading, thy_decl, thy_goal,
    93     qed, qed_block, prf_goal, prf_block, prf_chain, prf_decl, prf_asm, prf_asm_goal, prf_script];
    94 end;
    95 
    96 
    97 (* parsers *)
    98 
    99 type token = T.token;
   100 type parser_fn = token list -> (Toplevel.transition -> Toplevel.transition) * token list;
   101 
   102 datatype parser =
   103   Parser of string * (string * string * bool) * bool * parser_fn;
   104 
   105 fun parser int_only markup name comment kind parse =
   106   Parser (name, (comment, kind, markup), int_only, parse);
   107 
   108 
   109 (* parse command *)
   110 
   111 local
   112 
   113 fun command_body cmd (name, _) =
   114   (case cmd name of
   115     Some (int_only, parse) => P.!!! (Scan.prompt (name ^ "# ") (parse >> pair int_only))
   116   | None => sys_error ("no parser for outer syntax command " ^ quote name));
   117 
   118 fun terminator false = Scan.succeed ()
   119   | terminator true = P.group "terminator" (Scan.option P.sync -- P.$$$ ";" >> K ());
   120 
   121 in
   122 
   123 fun command term cmd =
   124   P.$$$ ";" >> K None ||
   125   P.sync >> K None ||
   126   (P.position P.command :-- command_body cmd) --| terminator term
   127     >> (fn ((name, pos), (int_only, f)) =>
   128       Some (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |>
   129         Toplevel.interactive int_only |> f));
   130 
   131 end;
   132 
   133 
   134 
   135 (** global syntax state **)
   136 
   137 local
   138 
   139 val global_lexicons = ref (Scan.empty_lexicon, Scan.empty_lexicon);
   140 val global_parsers =
   141   ref (Symtab.empty: (((string * string) * (bool * parser_fn)) * bool) Symtab.table);
   142 val global_markups = ref ([]: string list);
   143 
   144 fun change_lexicons f =
   145   let val lexs = f (! global_lexicons) in
   146     (case (op inter_string) (pairself Scan.dest_lexicon lexs) of
   147       [] => global_lexicons := lexs
   148     | bads => error ("Clash of outer syntax commands and keywords: " ^ commas_quote bads))
   149   end;
   150 
   151 fun get_markup (names, (name, (_, true))) = name :: names
   152   | get_markup (names, _) = names;
   153 
   154 fun make_markups () = global_markups := Symtab.foldl get_markup ([], ! global_parsers);
   155 fun change_parsers f = (global_parsers := f (! global_parsers); make_markups ());
   156 
   157 in
   158 
   159 
   160 (* get current syntax *)
   161 
   162 (*Note: the syntax for files is statically determined at the very
   163   beginning; for interactive processing it may change dynamically.*)
   164 
   165 fun get_lexicons () = ! global_lexicons;
   166 fun get_parsers () = ! global_parsers;
   167 fun get_parser () = apsome (#2 o #1) o curry Symtab.lookup (! global_parsers);
   168 fun is_markup name = name mem_string ! global_markups;
   169 
   170 
   171 (* augment syntax *)
   172 
   173 fun add_keywords keywords = change_lexicons (apfst (fn lex =>
   174   (Scan.extend_lexicon lex (map Symbol.explode keywords))));
   175 
   176 fun add_parser (tab, Parser (name, (comment, kind, markup), int_only, parse)) =
   177  (if is_none (Symtab.lookup (tab, name)) then ()
   178   else warning ("Redefined outer syntax command " ^ quote name);
   179   Symtab.update ((name, (((comment, kind), (int_only, parse)), markup)), tab));
   180 
   181 fun add_parsers parsers =
   182   (change_parsers (fn tab => foldl add_parser (tab, parsers));
   183     change_lexicons (apsnd (fn lex => Scan.extend_lexicon lex
   184       (map (fn Parser (name, _, _, _) => Symbol.explode name) parsers))));
   185 
   186 end;
   187 
   188 
   189 (* print syntax *)
   190 
   191 fun dest_keywords () = Scan.dest_lexicon (#1 (get_lexicons ()));
   192 
   193 fun dest_parsers () =
   194   map (fn (name, (((cmt, kind), (int_only, _)), _)) => (name, cmt, kind, int_only))
   195     (Symtab.dest (get_parsers ()));
   196 
   197 fun print_outer_syntax () =
   198   let
   199     fun pretty_cmd (name, comment, _, _) =
   200       Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
   201     val (int_cmds, cmds) = partition #4 (dest_parsers ());
   202   in
   203     Pretty.writeln (Pretty.strs ("syntax keywords:" :: map quote (dest_keywords ())));
   204     Pretty.writeln (Pretty.big_list "proper commands:" (map pretty_cmd cmds));
   205     Pretty.writeln (Pretty.big_list "improper commands (interactive-only):"
   206       (map pretty_cmd int_cmds))
   207   end;
   208 
   209 val print_help =
   210   Toplevel.keep (fn state =>
   211     let val opt_thy = try Toplevel.theory_of state in
   212       print_outer_syntax ();
   213       Method.help_methods opt_thy;
   214       Attrib.help_attributes opt_thy
   215     end);
   216 
   217 
   218 
   219 (** read theory **)
   220 
   221 (* special keywords *)
   222 
   223 val headerN = "header";
   224 val theoryN = "theory";
   225 
   226 val theory_keyword = P.$$$ theoryN;
   227 val header_keyword = P.$$$ headerN;
   228 val semicolon = P.$$$ ";";
   229 
   230 
   231 (* sources *)
   232 
   233 local
   234 
   235 val no_terminator =
   236   Scan.unless semicolon (Scan.one (T.not_sync andf T.not_eof));
   237 
   238 val recover = Scan.prompt "recover# " (Scan.repeat no_terminator);
   239 
   240 in
   241 
   242 fun source term do_recover cmd src =
   243   src
   244   |> Source.source T.stopper
   245     (Scan.bulk (fn xs => P.!!! (command term (cmd ())) xs))
   246     (if do_recover then Some recover else None)
   247   |> Source.mapfilter I;
   248 
   249 end;
   250 
   251 fun token_source (src, pos) =
   252   src
   253   |> Symbol.source false
   254   |> T.source false (K (get_lexicons ())) pos;
   255 
   256 fun filter_proper src =
   257   src
   258   |> Source.filter T.is_proper;
   259 
   260 
   261 (* scan header *)
   262 
   263 fun scan_header get_lex scan (src, pos) =
   264   src
   265   |> Symbol.source false
   266   |> T.source false (fn () => (get_lex (), Scan.empty_lexicon)) pos
   267   |> filter_proper
   268   |> Source.source T.stopper (Scan.single scan) None
   269   |> (fst o the o Source.get_single);
   270 
   271 
   272 (* detect new/old header *)
   273 
   274 local
   275 
   276 val check_header_lexicon = Scan.make_lexicon [Symbol.explode headerN, Symbol.explode theoryN];
   277 val check_header = Scan.option (header_keyword || theory_keyword);
   278 
   279 in
   280 
   281 fun is_old_theory src = is_none (scan_header (K check_header_lexicon) check_header src);
   282 
   283 end;
   284 
   285 
   286 (* deps_thy --- inspect theory header *)
   287 
   288 local
   289 
   290 val header_lexicon =
   291   Scan.make_lexicon (map Symbol.explode ["(", ")", "+", ":", ";", "=", "files", headerN, theoryN]);
   292 
   293 val file_name =
   294   (P.$$$ "(" |-- P.!!! (P.name --| P.$$$ ")")) >> rpair false || P.name >> rpair true;
   295 
   296 in
   297 
   298 val theory_header =
   299   (P.name -- (P.$$$ "=" |-- P.enum1 "+" P.name) --
   300     Scan.optional (P.$$$ "files" |-- P.!!! (Scan.repeat1 file_name)) [] --|  P.$$$ ":")
   301   >> (fn ((A, Bs), files) => (A, Bs, files));
   302 
   303 val new_header =
   304   header_keyword |-- (P.!!! (P.text -- Scan.option semicolon -- theory_keyword |-- theory_header))
   305     || theory_keyword |-- P.!!! theory_header;
   306 
   307 val old_header =
   308   P.name -- (P.$$$ "=" |-- P.name -- Scan.repeat (P.$$$ "+" |-- P.name))
   309   >> (fn (A, (B, Bs)) => (A, B :: Bs, []: (string * bool) list));
   310 
   311 fun deps_thy name path =
   312   let
   313     val src = Source.of_string (File.read path);
   314     val pos = Path.position path;
   315     val (name', parents, files) =
   316       (*Note: old style headers dynamically depend on the current lexicon :-( *)
   317       if is_old_theory (src, pos) then
   318         scan_header ThySyn.get_lexicon (Scan.error old_header) (src, pos)
   319       else scan_header (K header_lexicon) (Scan.error new_header) (src, pos);
   320 
   321     val ml_path = ThyLoad.ml_path name;
   322     val ml_file = if is_none (ThyLoad.check_file ml_path) then [] else [ml_path];
   323   in
   324     if name <> name' then
   325       error ("Filename " ^ quote (Path.pack path) ^ " does not match theory name " ^ quote name)
   326     else (parents, map (Path.unpack o #1) files @ ml_file)
   327   end;
   328 
   329 end;
   330 
   331 
   332 (* present theory source *)
   333 
   334 local
   335 
   336 val scan_markup =
   337   Scan.one T.not_eof :-- (fn tok =>
   338     if T.is_kind T.Command tok andalso is_markup (T.val_of tok) then
   339       (Scan.any (not o T.is_proper) |-- P.text) >> Some
   340     else Scan.succeed None);
   341 
   342 fun invisible_token (tok, arg) =
   343   is_none arg andalso T.keyword_with (equal ";") tok orelse T.is_kind T.Comment tok;
   344 
   345 in
   346 
   347 (*note: lazy evaluation ahead*)
   348 
   349 fun present_toks text pos () =
   350   token_source (Source.of_list (Library.untabify text), pos)
   351   |> Source.source T.stopper (Scan.bulk scan_markup) None
   352   |> Source.exhaust
   353   |> filter_out invisible_token;
   354 
   355 fun present_text text () =
   356   Source.exhaust (Symbol.source false (Source.of_list (Library.untabify text)));
   357 
   358 end;
   359 
   360 
   361 (* load_thy --- read text (including header) *)
   362 
   363 local
   364 
   365 fun try_ml_file name ml time =
   366   let
   367     val path = ThyLoad.ml_path name;
   368     val tr = Toplevel.imperative (fn () => ThyInfo.may_load_file ml time path);
   369     val tr_name = if time then "time_use" else "use";
   370   in
   371     if is_none (ThyLoad.check_file path) then ()
   372     else Toplevel.excursion_error [Toplevel.empty |> Toplevel.name tr_name |> tr]
   373   end;
   374 
   375 fun parse_thy src_pos =
   376   src_pos
   377   |> token_source
   378   |> filter_proper
   379   |> source false false (K (get_parser ()))
   380   |> Source.exhaust;
   381 
   382 fun run_thy name path =
   383   let
   384     val text = explode (File.read path);
   385     val src = Source.of_list text;
   386     val pos = Path.position path;
   387   in
   388     Present.init_theory name;
   389     if is_old_theory (src, pos) then ThySyn.load_thy name text
   390     else
   391      (Toplevel.excursion_error (parse_thy (src, pos));
   392       Present.token_source name (present_toks text pos));
   393     Present.verbatim_source name (present_text text)
   394   end;
   395 
   396 in
   397 
   398 fun load_thy name ml time path =
   399  (if time then
   400     timeit (fn () =>
   401      (writeln ("\n**** Starting theory " ^ quote name ^ " ****");
   402       setmp Goals.proof_timing true (run_thy name) path;
   403       writeln ("**** Finished theory " ^ quote name ^ " ****\n")))
   404   else run_thy name path;
   405   Context.context (ThyInfo.get_theory name);
   406   try_ml_file name ml time);
   407 
   408 end;
   409 
   410 
   411 (* interactive source of state transformers *)
   412 
   413 fun isar term no_pos =
   414   Source.tty
   415   |> Symbol.source true
   416   |> T.source true get_lexicons
   417     (if no_pos then Position.none else Position.line_name 1 "stdin")
   418   |> filter_proper
   419   |> source term true get_parser;
   420 
   421 
   422 
   423 (** the read-eval-print loop **)
   424 
   425 (* main loop *)
   426 
   427 fun gen_loop term no_pos =
   428  (Context.reset_context ();
   429   Toplevel.loop (isar term no_pos));
   430 
   431 fun gen_main term no_pos =
   432  (Toplevel.set_state Toplevel.toplevel;
   433   ml_prompts "ML> " "ML# ";
   434   writeln (Session.welcome ());
   435   gen_loop term no_pos);
   436 
   437 fun main () = gen_main false false;
   438 fun loop () = gen_loop false false;
   439 fun sync_main () = gen_main true true;
   440 fun sync_loop () = gen_loop true true;
   441 
   442 
   443 (* help *)
   444 
   445 fun help () =
   446   writeln ("This is Isabelle's underlying ML system (" ^ ml_system ^ ");\n\
   447     \invoke 'loop();' to enter the Isar loop.");
   448 
   449 
   450 (*final declarations of this structure!*)
   451 val command = parser false false;
   452 val markup_command = parser false true;
   453 val improper_command = parser true false;
   454 
   455 
   456 end;
   457 
   458 (*setup theory syntax dependent operations*)
   459 ThyLoad.deps_thy_fn := OuterSyntax.deps_thy;
   460 ThyLoad.load_thy_fn := OuterSyntax.load_thy;
   461 structure ThyLoad: THY_LOAD = ThyLoad;
   462 
   463 structure BasicOuterSyntax: BASIC_OUTER_SYNTAX = OuterSyntax;
   464 open BasicOuterSyntax;