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