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