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