src/Pure/Isar/outer_syntax.ML
author wenzelm
Tue Jul 27 21:58:59 1999 +0200 (1999-07-27)
changeset 7104 247e4247b64e
parent 7062 e992884b256d
child 7192 30a67acd0d7e
permissions -rw-r--r--
added thy_switch kind;
     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 -> bool -> 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 fun warn_theory_style path is_old =
   240   let
   241     val style = if is_old then "old" else "new";
   242     val _ = warning ("Assuming " ^ style ^ "-style theory format for " ^ quote (Path.pack path));
   243   in is_old end;
   244 
   245 
   246 (* deps_thy --- inspect theory header *)
   247 
   248 val header_lexicon =
   249   Scan.make_lexicon (map Symbol.explode ["(", ")", "+", ":", "=", "files", theoryN]);
   250 
   251 local
   252 
   253 val file_name =
   254   (P.$$$ "(" |-- P.!!! (P.name --| P.$$$ ")")) >> rpair false || P.name >> rpair true;
   255 
   256 val theory_head =
   257   (P.name -- (P.$$$ "=" |-- P.enum1 "+" P.name) --
   258     Scan.optional (P.$$$ "files" |-- P.!!! (Scan.repeat1 file_name)) [])
   259   >> (fn ((A, Bs), files) => (A, Bs, files));
   260 
   261 in
   262 
   263 val theory_header = theory_head --| (Scan.ahead P.eof || P.$$$ ":");
   264 val only_header = theory_keyword |-- theory_head --| Scan.ahead P.eof;
   265 val new_header = theory_keyword |-- P.!!! theory_header;
   266 
   267 val old_header =
   268   P.name -- (P.$$$ "=" |-- P.name -- Scan.repeat (P.$$$ "+" |-- P.name))
   269   >> (fn (A, (B, Bs)) => (A, B :: Bs, []: (string * bool) list));
   270 
   271 end;
   272 
   273 fun deps_thy name ml path =
   274   let
   275     val src = File.source path;
   276     val is_old = warn_theory_style path (is_old_theory src);
   277     val (name', parents, files) =
   278       (*Note: old style headers dynamically depend on the current lexicon :-( *)
   279       if is_old then scan_header ThySyn.get_lexicon (Scan.error old_header) src
   280       else scan_header (K header_lexicon) (Scan.error new_header) src;
   281 
   282     val ml_path = ThyLoad.ml_path name;
   283     val ml_file = if not ml orelse is_none (ThyLoad.check_file ml_path) then [] else [ml_path];
   284   in
   285     if name <> name' then
   286       error ("Filename " ^ quote (Path.pack path) ^ " does not match theory name " ^ quote name)
   287     else (parents, map (Path.unpack o #1) files @ ml_file)
   288   end;
   289 
   290 
   291 (* load_thy --- read text (including header) *)
   292 
   293 fun try_ml_file name ml time =
   294   let
   295     val path = ThyLoad.ml_path name;
   296     val tr = Toplevel.imperative (fn () => ThyInfo.load_file time path);
   297     val tr_name = if time then "time_use" else "use";
   298   in
   299     if not ml orelse is_none (ThyLoad.check_file path) then ()
   300     else Toplevel.excursion_error [Toplevel.empty |> Toplevel.name tr_name |> tr]
   301   end;
   302 
   303 fun parse_thy (src, pos) =
   304   let
   305     val lex_src =
   306       src
   307       |> Symbol.source false
   308       |> OuterLex.source false (K (get_lexicons ())) pos;
   309     val only_head =
   310       lex_src
   311       |> Source.source OuterLex.stopper (Scan.single (Scan.option only_header)) None
   312       |> (fst o the o Source.get_single);
   313   in
   314     (case only_head of
   315       None =>
   316         lex_src
   317         |> source false false (K (get_parser ()))
   318         |> Source.exhaust
   319     | Some spec =>
   320         [Toplevel.empty |> Toplevel.name theoryN |> IsarThy.theory spec,
   321           Toplevel.empty |> Toplevel.name "end" |> Toplevel.exit])
   322   end;
   323 
   324 fun run_thy name path =
   325   let val (src, pos) = File.source path in
   326     Present.theory_source name src;
   327     if is_old_theory (src, pos) then ThySyn.load_thy name (Source.exhaust src)
   328     else Toplevel.excursion_error (parse_thy (src, pos))
   329   end;
   330 
   331 fun load_thy name ml time path =
   332  (if time then
   333     timeit (fn () =>
   334      (writeln ("\n**** Starting theory " ^ quote name ^ " ****");
   335       setmp Goals.proof_timing true (run_thy name) path;
   336       writeln ("**** Finished theory " ^ quote name ^ " ****\n")))
   337   else run_thy name path;
   338   Context.context (ThyInfo.get_theory name);
   339   try_ml_file name ml time);
   340 
   341 
   342 (* interactive source of state transformers *)
   343 
   344 fun isar term =
   345   Source.tty
   346   |> Symbol.source true
   347   |> OuterLex.source true get_lexicons (Position.line_name 1 "stdin")
   348   |> source term true get_parser;
   349 
   350 
   351 
   352 (** the read-eval-print loop **)
   353 
   354 (* main loop *)
   355 
   356 fun gen_loop term = (Context.reset_context (); Toplevel.loop (isar term));
   357 
   358 fun gen_main term =
   359  (Toplevel.set_state Toplevel.toplevel;
   360   ml_prompts "ML> " "ML# ";
   361   writeln (Session.welcome ());
   362   gen_loop term);
   363 
   364 fun main () = gen_main false;
   365 fun loop () = gen_loop false;
   366 fun sync_main () = gen_main true;
   367 fun sync_loop () = gen_loop true;
   368 
   369 
   370 (* help *)
   371 
   372 fun help () =
   373   writeln ("This is Isabelle's underlying ML system (" ^ ml_system ^ ");\n\
   374     \invoke 'loop();' to enter the Isar loop.");
   375 
   376 
   377 (*final declarations of this structure!*)
   378 val command = parser false;
   379 val improper_command = parser true;
   380 
   381 
   382 end;
   383 
   384 (*setup theory syntax dependent operations*)
   385 ThyLoad.deps_thy_fn := OuterSyntax.deps_thy;
   386 ThyLoad.load_thy_fn := OuterSyntax.load_thy;
   387 structure ThyLoad: THY_LOAD = ThyLoad;
   388 
   389 structure BasicOuterSyntax: BASIC_OUTER_SYNTAX = OuterSyntax;
   390 open BasicOuterSyntax;