src/Pure/Isar/outer_syntax.ML
author wenzelm
Fri Mar 19 11:24:00 1999 +0100 (1999-03-19)
changeset 6404 2daaf2943c79
parent 6373 47b357194f32
child 6641 254ab03bd082
permissions -rw-r--r--
common qed and end of proofs;
     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 TODO:
     8   - cleanup;
     9   - avoid string constants;
    10 *)
    11 
    12 signature BASIC_OUTER_SYNTAX =
    13 sig
    14   val main: unit -> unit
    15   val loop: unit -> unit
    16   val help: unit -> unit
    17 end;
    18 
    19 signature OUTER_SYNTAX =
    20 sig
    21   include BASIC_OUTER_SYNTAX
    22   type token
    23   type parser
    24   val parser: bool -> string -> string ->
    25     (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
    26   val command: string -> string ->
    27     (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
    28   val improper_command: string -> string ->
    29     (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
    30   val print_outer_syntax: unit -> unit
    31   val commands: unit -> string list
    32   val add_keywords: string list -> unit
    33   val add_parsers: parser list -> unit
    34   val theory_header: token list -> (string * string list * (string * bool) list) * token list
    35   val deps_thy: string -> bool -> Path.T -> string list * Path.T list
    36   val load_thy: string -> bool -> bool -> Path.T -> unit
    37   val isar: Toplevel.isar
    38 end;
    39 
    40 structure OuterSyntax: OUTER_SYNTAX =
    41 struct
    42 
    43 
    44 (** outer syntax **)
    45 
    46 (* parsers *)
    47 
    48 type token = OuterLex.token;
    49 type parser_fn = token list -> (Toplevel.transition -> Toplevel.transition) * token list;
    50 
    51 datatype parser =
    52   Parser of string * string * bool * parser_fn;
    53 
    54 fun parser int_only name comment parse = Parser (name, comment, int_only, parse);
    55 
    56 val command_parser = parser false;
    57 val improper_command_parser = parser true;
    58 
    59 
    60 (* parse command *)
    61 
    62 local open OuterParse in
    63 
    64 fun command_name cmd =
    65   group "command"
    66     (position (Scan.one (OuterLex.keyword_pred (is_some o cmd)) >> OuterLex.val_of));
    67 
    68 fun command_body cmd (name, _) =
    69   let val (int_only, parse) = the (cmd name)
    70   in !!! (Scan.prompt (name ^ "# ") (parse >> pair int_only)) end;
    71 
    72 fun command cmd =
    73   $$$ ";" >> K None ||
    74   command_name cmd :-- command_body cmd >> (fn ((name, pos), (int_only, f)) =>
    75     Some (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |>
    76       Toplevel.interactive int_only |> f));
    77 
    78 end;
    79 
    80 
    81 
    82 (** global syntax state **)
    83 
    84 val global_lexicon = ref Scan.empty_lexicon;
    85 val global_parsers = ref (Symtab.empty: (string * (bool * parser_fn)) Symtab.table);
    86 
    87 fun commands () = Symtab.keys (! global_parsers);
    88 
    89 
    90 (* print syntax *)
    91 
    92 fun print_outer_syntax () =
    93   let
    94     val keywords = map implode (Scan.dest_lexicon (! global_lexicon));
    95     fun pretty_cmd (name, (comment, _)) =
    96       Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
    97     val (int_cmds, cmds) = partition (#1 o #2 o #2) (Symtab.dest (! global_parsers));
    98   in
    99     Pretty.writeln (Pretty.strs ("syntax keywords:" :: map quote keywords));
   100     Pretty.writeln (Pretty.big_list "proper commands:" (map pretty_cmd cmds));
   101     Pretty.writeln (Pretty.big_list "improper commands (interactive-only):"
   102       (map pretty_cmd int_cmds))
   103   end;
   104 
   105 
   106 (* augment syntax *)
   107 
   108 fun add_keywords keywords =
   109   global_lexicon := Scan.extend_lexicon (! global_lexicon) (map Symbol.explode keywords);
   110 
   111 fun add_parser (tab, Parser (name, comment, int_only, parse)) =
   112  (if is_none (Symtab.lookup (tab, name)) then ()
   113   else warning ("Redefined outer syntax command " ^ quote name);
   114   Symtab.update ((name, (comment, (int_only, parse))), tab));
   115 
   116 fun add_parsers parsers =
   117   (global_parsers := foldl add_parser (! global_parsers, parsers);
   118     add_keywords (map (fn Parser (name, _, _, _) => name) parsers));
   119 
   120 
   121 (* get current lexer / parser *)
   122 
   123 (*Note: the syntax for files is statically determined at the very
   124   beginning; for interactive processing it may change dynamically.*)
   125 
   126 fun get_lexicon () = ! global_lexicon;
   127 fun get_parser () = apsome snd o curry Symtab.lookup (! global_parsers);
   128 
   129 
   130 
   131 (** read theory **)
   132 
   133 (* theory keyword *)
   134 
   135 val theoryN = "theory";
   136 val theory_keyword = OuterParse.$$$ theoryN;
   137 
   138 
   139 (* source *)
   140 
   141 fun no_command cmd =
   142   Scan.one ((not o OuterLex.keyword_pred ((is_some o cmd) orf equal ";")) andf OuterLex.not_eof);
   143 
   144 fun recover cmd =
   145   Scan.prompt "recover# " (Scan.one OuterLex.not_eof -- Scan.repeat (no_command cmd));
   146 
   147 fun source do_recover cmd src =
   148   src
   149   |> Source.source OuterLex.stopper (Scan.bulk (fn xs => OuterParse.!!! (command (cmd ())) xs))
   150     (if do_recover then Some (fn xs => recover (cmd ()) xs) else None)
   151   |> Source.mapfilter I;
   152 
   153 
   154 (* detect header *)
   155 
   156 fun scan_header get_lexicon scan (src, pos) =
   157   src
   158   |> Symbol.source false
   159   |> OuterLex.source false get_lexicon pos
   160   |> Source.source OuterLex.stopper (Scan.single scan) None
   161   |> (fst o the o Source.get_single);
   162 
   163 val check_header_lexicon = Scan.make_lexicon [Symbol.explode theoryN];
   164 
   165 fun is_old_theory src =
   166   is_none (scan_header (K check_header_lexicon) (Scan.option theory_keyword) src);
   167 
   168 fun warn_theory_style path is_old =
   169   let
   170     val style = if is_old then "old" else "new";
   171     val _ = warning ("Assuming " ^ style ^ "-style theory format for " ^ quote (Path.pack path));
   172   in is_old end;
   173 
   174 
   175 (* deps_thy --- inspect theory header *)
   176 
   177 val header_lexicon =
   178   Scan.make_lexicon (map Symbol.explode ["(", ")", "+", ":", "=", "files", theoryN]);
   179 
   180 local open OuterParse in
   181 
   182 val file_name = ($$$ "(" |-- !!! (name --| $$$ ")")) >> rpair false || name >> rpair true;
   183 
   184 val theory_head =
   185   (name -- ($$$ "=" |-- enum1 "+" name) --
   186     Scan.optional ($$$ "files" |-- !!! (Scan.repeat1 file_name)) [])
   187   >> (fn ((A, Bs), files) => (A, Bs, files));
   188 
   189 val theory_header = theory_head --| (Scan.ahead eof || $$$ ":");
   190 val only_header = theory_keyword |-- theory_head --| Scan.ahead eof;
   191 val new_header = theory_keyword |-- !!! theory_header;
   192 
   193 val old_header =
   194   name -- ($$$ "=" |-- name -- Scan.repeat ($$$ "+" |-- name))
   195   >> (fn (A, (B, Bs)) => (A, B :: Bs, []: (string * bool) list));
   196 
   197 end;
   198 
   199 fun deps_thy name ml path =
   200   let
   201     val src = Source.of_file path;
   202     val is_old = warn_theory_style path (is_old_theory src);
   203     val (name', parents, files) =
   204       (*Note: old style headers dynamically depend on the current lexicon :-( *)
   205       if is_old then scan_header ThySyn.get_lexicon (Scan.error old_header) src
   206       else scan_header (K header_lexicon) (Scan.error new_header) src;
   207 
   208     val ml_path = ThyLoad.ml_path name;
   209     val ml_file = if not ml orelse is_none (ThyLoad.check_file ml_path) then [] else [ml_path];
   210   in
   211     if name <> name' then
   212       error ("Filename " ^ quote (Path.pack path) ^ " does not match theory name " ^ quote name)
   213     else (parents, map (Path.unpack o #1) files @ ml_file)
   214   end;
   215 
   216 
   217 (* load_thy --- read text (including header) *)
   218 
   219 fun try_ml_file name ml time =
   220   let
   221     val path = ThyLoad.ml_path name;
   222     val tr = Toplevel.imperative (fn () => ThyInfo.load_file time path);
   223     val tr_name = if time then "time_use" else "use";
   224   in
   225     if not ml orelse is_none (ThyLoad.check_file path) then ()
   226     else Toplevel.excursion [Toplevel.empty |> Toplevel.name tr_name |> tr]
   227   end;
   228 
   229 fun parse_thy (src, pos) =
   230   let
   231     val lex_src =
   232       src
   233       |> Symbol.source false
   234       |> OuterLex.source false (K (get_lexicon ())) pos;
   235     val only_head =
   236       lex_src
   237       |> Source.source OuterLex.stopper (Scan.single (Scan.option only_header)) None
   238       |> (fst o the o Source.get_single);
   239   in
   240     (case only_head of
   241       None =>
   242         lex_src
   243         |> source false (K (get_parser ()))
   244         |> Source.exhaust
   245     | Some spec =>
   246         [Toplevel.empty |> Toplevel.name theoryN |> IsarThy.theory spec,
   247           Toplevel.empty |> Toplevel.name "end" |> Toplevel.exit])
   248   end;
   249 
   250 fun run_thy name path =
   251   let val (src, pos) = Source.of_file path in
   252     Present.theory_source name src;
   253     if is_old_theory (src, pos) then ThySyn.load_thy name (Source.exhaust src)
   254     else (Toplevel.excursion (parse_thy (src, pos))
   255       handle exn => error (Toplevel.exn_message exn))
   256   end;
   257 
   258 fun load_thy name ml time path =
   259  (if time then
   260     timeit (fn () =>
   261      (writeln ("\n**** Starting theory " ^ quote name ^ " ****");
   262       setmp Goals.proof_timing true (run_thy name) path;
   263       writeln ("**** Finished theory " ^ quote name ^ " ****\n")))
   264   else run_thy name path;
   265   Context.context (ThyInfo.get_theory name);
   266   try_ml_file name ml time);
   267 
   268 
   269 (* interactive source of state transformers *)
   270 
   271 val isar =
   272   Source.tty
   273   |> Symbol.source true
   274   |> OuterLex.source true get_lexicon (Position.line_name 1 "stdin")
   275   |> source true get_parser;
   276 
   277 
   278 
   279 (** the read-eval-print loop **)
   280 
   281 (* main loop *)
   282 
   283 fun loop () = (Context.reset_context (); Toplevel.loop isar);
   284 
   285 fun main () =
   286  (Toplevel.set_state Toplevel.toplevel;
   287   ml_prompts "ML> " "ML# ";
   288   writeln (Session.welcome ());
   289   loop ());
   290 
   291 
   292 (* help *)
   293 
   294 fun help () =
   295   writeln ("This is Isabelle's underlying ML system (" ^ ml_system ^ ");\n\
   296     \invoke 'loop();' to enter the Isar loop.");
   297 
   298 
   299 (*final declarations of this structure!*)
   300 val command = command_parser;
   301 val improper_command = improper_command_parser;
   302 
   303 end;
   304 
   305 (*setup theory syntax dependent operations*)
   306 ThyLoad.deps_thy_fn := OuterSyntax.deps_thy;
   307 ThyLoad.load_thy_fn := OuterSyntax.load_thy;
   308 structure ThyLoad: THY_LOAD = ThyLoad;
   309 
   310 structure BasicOuterSyntax: BASIC_OUTER_SYNTAX = OuterSyntax;
   311 open BasicOuterSyntax;