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