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