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