src/Pure/Isar/outer_syntax.ML
author wenzelm
Wed, 03 Feb 1999 17:20:09 +0100
changeset 6199 9b1be867e21a
parent 6107 1418bc571f23
child 6220 5a29b53eca45
permissions -rw-r--r--
removed load; detect old vs. new header; provide deps_thy, load_thy primitives for ThyLoad;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Isar/outer_syntax.ML
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
     3
    Author:     Markus Wenzel, TU Muenchen
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
     4
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
     5
The global Isabelle/Isar outer syntax.
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
     6
*)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
     7
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
     8
signature BASIC_OUTER_SYNTAX =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
     9
sig
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    10
  val main: unit -> unit
5883
975c984526b0 tuned names;
wenzelm
parents: 5829
diff changeset
    11
  val loop: unit -> unit
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    12
  val help: unit -> unit
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    13
end;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    14
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    15
signature OUTER_SYNTAX =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    16
sig
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    17
  include BASIC_OUTER_SYNTAX
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    18
  type token
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    19
  type parser
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    20
  val parser: bool -> string -> string ->
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    21
    (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
5883
975c984526b0 tuned names;
wenzelm
parents: 5829
diff changeset
    22
  val print_outer_syntax: unit -> unit
5952
7d4ec8992b23 added commands;
wenzelm
parents: 5923
diff changeset
    23
  val commands: unit -> string list
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    24
  val add_keywords: string list -> unit
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    25
  val add_parsers: parser list -> unit
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
    26
  val deps_thy: string -> bool -> Path.T -> string list * Path.T list
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
    27
  val load_thy: string -> bool -> bool -> Path.T -> unit
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    28
  val isar: Toplevel.isar
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    29
end;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    30
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    31
structure OuterSyntax: OUTER_SYNTAX =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    32
struct
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    33
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    34
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    35
(** outer syntax **)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    36
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    37
(* parsers *)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    38
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    39
type token = OuterLex.token;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    40
type parser_fn = token list -> (Toplevel.transition -> Toplevel.transition) * token list;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    41
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    42
datatype parser =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    43
  Parser of string * string * bool * parser_fn;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    44
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    45
fun parser int_only name comment parse = Parser (name, comment, int_only, parse);
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    46
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    47
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    48
(* parse command *)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    49
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
    50
local open OuterParse in
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
    51
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    52
fun command_name cmd =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    53
  group "command"
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    54
    (position (Scan.one (OuterLex.keyword_pred (is_some o cmd)) >> OuterLex.val_of));
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    55
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    56
fun command_body cmd (name, _) =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    57
  let val (int_only, parse) = the (cmd name)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    58
  in !!! (Scan.prompt (name ^ "# ") (parse >> pair int_only)) end;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    59
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    60
fun command cmd =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    61
  $$$ ";" >> K None ||
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    62
  command_name cmd :-- command_body cmd >> (fn ((name, pos), (int_only, f)) =>
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    63
    Some (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |>
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    64
      Toplevel.interactive int_only |> f));
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    65
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
    66
end;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
    67
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    68
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    69
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    70
(** global syntax state **)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    71
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    72
val global_lexicon = ref Scan.empty_lexicon;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    73
val global_parsers = ref (Symtab.empty: (string * (bool * parser_fn)) Symtab.table);
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    74
5952
7d4ec8992b23 added commands;
wenzelm
parents: 5923
diff changeset
    75
fun commands () = Symtab.keys (! global_parsers);
7d4ec8992b23 added commands;
wenzelm
parents: 5923
diff changeset
    76
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    77
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    78
(* print syntax *)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    79
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    80
fun print_outer_syntax () =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    81
  let
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    82
    val keywords = map implode (Scan.dest_lexicon (! global_lexicon));
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    83
    fun pretty_cmd (name, (comment, _)) =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    84
      Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    85
    val (int_cmds, cmds) = partition (#1 o #2 o #2) (Symtab.dest (! global_parsers));
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    86
  in
6107
1418bc571f23 tuned msg;
wenzelm
parents: 6095
diff changeset
    87
    Pretty.writeln (Pretty.strs ("syntax keywords:" :: map quote keywords));
6095
9f75a45384dd tuned msg;
wenzelm
parents: 5992
diff changeset
    88
    Pretty.writeln (Pretty.big_list "proper commands:" (map pretty_cmd cmds));
9f75a45384dd tuned msg;
wenzelm
parents: 5992
diff changeset
    89
    Pretty.writeln (Pretty.big_list "improper commands (interactive-only):"
9f75a45384dd tuned msg;
wenzelm
parents: 5992
diff changeset
    90
      (map pretty_cmd int_cmds))
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    91
  end;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    92
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    93
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    94
(* augment syntax *)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    95
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    96
fun add_keywords keywords =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    97
  global_lexicon := Scan.extend_lexicon (! global_lexicon) (map Symbol.explode keywords);
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    98
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    99
fun add_parser (tab, Parser (name, comment, int_only, parse)) =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   100
 (if is_none (Symtab.lookup (tab, name)) then ()
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   101
  else warning ("Redefined outer syntax command " ^ quote name);
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   102
  Symtab.update ((name, (comment, (int_only, parse))), tab));
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   103
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   104
fun add_parsers parsers =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   105
  (global_parsers := foldl add_parser (! global_parsers, parsers);
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   106
    add_keywords (map (fn Parser (name, _, _, _) => name) parsers));
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   107
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   108
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   109
(* get current lexer / parser *)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   110
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   111
(*Note: the syntax for files is statically determined at the very
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   112
  beginning; for interactive processing it may change dynamically.*)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   113
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   114
fun get_lexicon () = ! global_lexicon;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   115
fun get_parser () = apsome snd o curry Symtab.lookup (! global_parsers);
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   116
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   117
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   118
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   119
(** read theory **)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   120
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   121
(* source *)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   122
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   123
fun no_command cmd =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   124
  Scan.one ((not o OuterLex.keyword_pred ((is_some o cmd) orf equal ";")) andf OuterLex.not_eof);
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   125
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   126
fun recover cmd =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   127
  Scan.prompt "recover# " (Scan.one OuterLex.not_eof -- Scan.repeat (no_command cmd));
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   128
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   129
fun source do_recover cmd src =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   130
  src
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   131
  |> Source.source OuterLex.stopper (Scan.bulk (fn xs => OuterParse.!!! (command (cmd ())) xs))
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   132
    (if do_recover then Some (fn xs => recover (cmd ()) xs) else None)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   133
  |> Source.mapfilter I;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   134
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   135
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   136
(* detect header *)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   137
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   138
fun scan_header get_lexicon scan (src, pos) =
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   139
  src
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   140
  |> Symbol.source false
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   141
  |> OuterLex.source false get_lexicon pos
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   142
  |> Source.source OuterLex.stopper (Scan.single scan) None
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   143
  |> (fst o the o Source.get_single);
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   144
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   145
val check_header_lexicon = Scan.make_lexicon [Symbol.explode "theory"];
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   146
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   147
fun is_old_theory src =
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   148
  is_none (scan_header (K check_header_lexicon) (Scan.option (OuterParse.$$$ "theory")) src);
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   149
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   150
fun warn_theory_style path is_old =
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   151
  let
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   152
    val style = if is_old then "old" else "new";
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   153
    val _ = warning ("Assuming " ^ style ^ "-style theory format for " ^ quote (Path.pack path));
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   154
  in is_old end;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   155
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   156
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   157
(* deps_thy --- inspect theory header *)
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   158
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   159
val new_header_lexicon =
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   160
  Scan.make_lexicon (map Symbol.explode ["+", ":", "=", "files", "theory"]);
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   161
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   162
local open OuterParse in
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   163
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   164
val new_header =
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   165
  $$$ "theory" |-- !!! (name -- ($$$ "=" |-- enum1 "+" name) --
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   166
    Scan.optional ($$$ "files" |-- !!! (Scan.repeat1 name)) [] --| (Scan.ahead eof || $$$ ":"));
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   167
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   168
val old_header =
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   169
  name -- ($$$ "=" |-- name -- Scan.repeat ($$$ "+" |-- name))
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   170
  >> (fn (A, (B, Bs)) => ((A, B :: Bs), []));
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   171
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   172
end;
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   173
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   174
fun deps_thy name ml path =
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   175
  let
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   176
    val src = Source.of_file path;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   177
    val is_old = warn_theory_style path (is_old_theory src);
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   178
    val ((name', parents), files) =
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   179
      (*Note: old style headers dynamically depend on the current lexicon :-( *)
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   180
      if is_old then scan_header ThySyn.get_lexicon (Scan.error old_header) src
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   181
      else scan_header (K new_header_lexicon) (Scan.error new_header) src;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   182
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   183
    val ml_path = ThyLoad.ml_path name;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   184
    val ml_file = if not ml orelse is_none (ThyLoad.check_file ml_path) then [] else [ml_path];
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   185
  in
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   186
    if name <> name' then
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   187
      error ("Filename " ^ quote (Path.pack path) ^ " does not match theory name " ^ quote name)
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   188
    else (parents, map Path.unpack files @ ml_file)
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   189
  end;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   190
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   191
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   192
(* load_thy --- read text (including header) *)
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   193
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   194
fun try_ml_file name ml =
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   195
  let
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   196
    val path = ThyLoad.ml_path name;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   197
    val tr = Toplevel.imperative (fn () => ThyInfo.load_file path);
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   198
  in
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   199
    if not ml orelse is_none (ThyLoad.check_file path) then ()
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   200
    else Toplevel.excursion [Toplevel.empty |> Toplevel.name "use" |> tr]
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   201
  end;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   202
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   203
fun parse_thy (src, pos) =
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   204
  src
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   205
  |> Symbol.source false
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   206
  |> OuterLex.source false (K (get_lexicon ())) pos
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   207
  |> source false (K (get_parser ()))
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   208
  |> Source.exhaust;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   209
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   210
fun read_thy name ml path =
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   211
  let
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   212
    val (src, pos) = Source.of_file path;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   213
    val _ =
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   214
      if is_old_theory (src, pos) then ThySyn.load_thy name (Source.exhaust src)
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   215
      else (Toplevel.excursion (parse_thy (src, pos))
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   216
        handle exn => error (Toplevel.exn_message exn));
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   217
    val theory = ThyInfo.get_theory name;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   218
  in Context.setmp theory try_ml_file name ml end;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   219
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   220
fun load_thy name ml time path =
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   221
  if not time then read_thy name ml path
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   222
  else timeit (fn () =>
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   223
   (writeln ("\n**** Starting Theory " ^ quote name ^ " ****");
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   224
    setmp Goals.proof_timing true (read_thy name ml) path;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   225
    writeln ("**** Finished Theory " ^ quote name ^ " ****\n")));
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   226
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   227
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   228
(* interactive source of state transformers *)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   229
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   230
val isar =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   231
  Source.tty
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   232
  |> Symbol.source true
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   233
  |> OuterLex.source true get_lexicon (Position.line_name 1 "stdin")
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   234
  |> source true get_parser;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   235
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   236
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   237
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   238
(** the read-eval-print loop **)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   239
5923
wenzelm
parents: 5883
diff changeset
   240
(* main loop *)
wenzelm
parents: 5883
diff changeset
   241
5883
975c984526b0 tuned names;
wenzelm
parents: 5829
diff changeset
   242
fun loop () = (Context.reset_context (); Toplevel.loop isar);
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   243
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   244
fun main () =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   245
 (Toplevel.set_state Toplevel.toplevel;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   246
  ml_prompts "ML> " "ML# ";
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   247
  writeln (Session.welcome ());
5883
975c984526b0 tuned names;
wenzelm
parents: 5829
diff changeset
   248
  loop ());
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   249
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   250
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   251
(* help *)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   252
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   253
fun help () =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   254
  writeln ("This is Isabelle's underlying ML system (" ^ ml_system ^ ");\n\
5883
975c984526b0 tuned names;
wenzelm
parents: 5829
diff changeset
   255
    \invoke 'loop();' to enter the Isar loop.");
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   256
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   257
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   258
end;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   259
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   260
(*setup theory syntax dependent operations*)
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   261
ThyLoad.deps_thy_fn := OuterSyntax.deps_thy;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   262
ThyLoad.load_thy_fn := OuterSyntax.load_thy;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   263
structure ThyLoad: THY_LOAD = ThyLoad;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   264
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   265
structure BasicOuterSyntax: BASIC_OUTER_SYNTAX = OuterSyntax;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   266
open BasicOuterSyntax;