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