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