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