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