src/Pure/Isar/outer_syntax.ML
author wenzelm
Thu Dec 01 22:03:05 2005 +0100 (2005-12-01 ago)
changeset 18326 2f57579e618f
parent 18064 f5727fa16c77
child 18684 38d72231b41d
permissions -rw-r--r--
tuned msg;
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@5829
     6
*)
wenzelm@5829
     7
wenzelm@5829
     8
signature BASIC_OUTER_SYNTAX =
wenzelm@5829
     9
sig
wenzelm@15830
    10
  structure Isar:
wenzelm@15830
    11
    sig
wenzelm@18064
    12
      val state: unit -> Toplevel.state
wenzelm@18064
    13
      val exn: unit -> (exn * string) option
wenzelm@15830
    14
      val main: unit -> unit
wenzelm@15830
    15
      val loop: unit -> unit
wenzelm@15830
    16
      val sync_main: unit -> unit
wenzelm@15830
    17
      val sync_loop: unit -> unit
wenzelm@15830
    18
    end;
wenzelm@5829
    19
end;
wenzelm@5829
    20
wenzelm@5829
    21
signature OUTER_SYNTAX =
wenzelm@5829
    22
sig
wenzelm@5829
    23
  include BASIC_OUTER_SYNTAX
wenzelm@5829
    24
  type token
wenzelm@5829
    25
  type parser
wenzelm@17071
    26
  val command: string -> string -> OuterKeyword.T ->
wenzelm@6373
    27
    (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
wenzelm@17071
    28
  val markup_command: IsarOutput.markup -> string -> string -> OuterKeyword.T ->
wenzelm@7789
    29
    (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
wenzelm@17071
    30
  val improper_command: string -> string -> OuterKeyword.T ->
wenzelm@6373
    31
    (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
wenzelm@14687
    32
  val is_keyword: string -> bool
wenzelm@7026
    33
  val dest_keywords: unit -> string list
wenzelm@7026
    34
  val dest_parsers: unit -> (string * string * string * bool) list
wenzelm@5883
    35
  val print_outer_syntax: unit -> unit
wenzelm@9223
    36
  val print_commands: Toplevel.transition -> Toplevel.transition
wenzelm@5829
    37
  val add_keywords: string list -> unit
wenzelm@5829
    38
  val add_parsers: parser list -> unit
wenzelm@17265
    39
  val check_text: string * Position.T -> Toplevel.state -> unit
wenzelm@7940
    40
  val deps_thy: string -> bool -> Path.T -> string list * Path.T list
wenzelm@6199
    41
  val load_thy: string -> bool -> bool -> Path.T -> unit
nipkow@14091
    42
  val isar: bool -> bool -> unit Toplevel.isar
wenzelm@16195
    43
  val scan: string -> OuterLex.token list
wenzelm@16195
    44
  val read: OuterLex.token list -> (string * OuterLex.token list * Toplevel.transition) list
wenzelm@5829
    45
end;
wenzelm@5829
    46
aspinall@15224
    47
structure OuterSyntax : OUTER_SYNTAX  =
wenzelm@5829
    48
struct
wenzelm@5829
    49
wenzelm@7750
    50
structure T = OuterLex;
wenzelm@6860
    51
structure P = OuterParse;
wenzelm@6860
    52
wenzelm@5829
    53
wenzelm@5829
    54
(** outer syntax **)
wenzelm@5829
    55
wenzelm@5829
    56
(* parsers *)
wenzelm@5829
    57
wenzelm@7750
    58
type token = T.token;
wenzelm@5829
    59
type parser_fn = token list -> (Toplevel.transition -> Toplevel.transition) * token list;
wenzelm@5829
    60
wenzelm@5829
    61
datatype parser =
wenzelm@17071
    62
  Parser of string * (string * OuterKeyword.T * IsarOutput.markup option) * bool * parser_fn;
wenzelm@5829
    63
wenzelm@7750
    64
fun parser int_only markup name comment kind parse =
wenzelm@7750
    65
  Parser (name, (comment, kind, markup), int_only, parse);
wenzelm@5829
    66
wenzelm@5829
    67
wenzelm@5829
    68
(* parse command *)
wenzelm@5829
    69
wenzelm@6860
    70
local
wenzelm@6199
    71
wenzelm@14925
    72
fun terminate false = Scan.succeed ()
wenzelm@14925
    73
  | terminate true = P.group "end of input" (Scan.option P.sync -- P.semicolon >> K ());
wenzelm@14925
    74
wenzelm@14925
    75
fun trace false parse = parse
wenzelm@14925
    76
  | trace true parse = Scan.trace parse >> (fn (f, toks) => f o Toplevel.source toks);
wenzelm@14925
    77
wenzelm@17071
    78
fun body cmd do_trace (name, _) =
wenzelm@7026
    79
  (case cmd name of
skalberg@15531
    80
    SOME (int_only, parse) =>
wenzelm@17118
    81
      P.!!! (Scan.prompt (name ^ "# ") (trace do_trace (P.tags |-- parse) >> pair int_only))
skalberg@15531
    82
  | NONE => sys_error ("no parser for outer syntax command " ^ quote name));
wenzelm@6860
    83
wenzelm@6860
    84
in
wenzelm@5829
    85
wenzelm@14925
    86
fun command do_terminate do_trace cmd =
skalberg@15531
    87
  P.semicolon >> K NONE ||
skalberg@15531
    88
  P.sync >> K NONE ||
wenzelm@17118
    89
  (P.position P.command :-- body cmd do_trace) --| terminate do_terminate
wenzelm@6860
    90
    >> (fn ((name, pos), (int_only, f)) =>
skalberg@15531
    91
      SOME (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |>
wenzelm@6860
    92
        Toplevel.interactive int_only |> f));
wenzelm@5829
    93
wenzelm@6199
    94
end;
wenzelm@6199
    95
wenzelm@5829
    96
wenzelm@5829
    97
wenzelm@9132
    98
(** global outer syntax **)
wenzelm@5829
    99
wenzelm@7026
   100
local
wenzelm@7026
   101
wenzelm@7026
   102
val global_lexicons = ref (Scan.empty_lexicon, Scan.empty_lexicon);
wenzelm@7750
   103
val global_parsers =
wenzelm@17071
   104
  ref (Symtab.empty: (((string * OuterKeyword.T) * (bool * parser_fn)) * IsarOutput.markup option)
wenzelm@9132
   105
    Symtab.table);
wenzelm@9132
   106
val global_markups = ref ([]: (string * IsarOutput.markup) list);
wenzelm@5952
   107
wenzelm@7026
   108
fun change_lexicons f =
wenzelm@7026
   109
  let val lexs = f (! global_lexicons) in
wenzelm@7026
   110
    (case (op inter_string) (pairself Scan.dest_lexicon lexs) of
wenzelm@7026
   111
      [] => global_lexicons := lexs
wenzelm@7026
   112
    | bads => error ("Clash of outer syntax commands and keywords: " ^ commas_quote bads))
wenzelm@7026
   113
  end;
wenzelm@5829
   114
wenzelm@17071
   115
fun make_markups () = global_markups :=
wenzelm@17071
   116
  Symtab.fold (fn (name, (_, SOME m)) => cons (name, m) | _ => I) (! global_parsers) [];
wenzelm@7750
   117
wenzelm@9132
   118
fun change_parsers f = (Library.change global_parsers f; make_markups ());
wenzelm@6722
   119
wenzelm@7026
   120
in
wenzelm@7026
   121
wenzelm@7750
   122
wenzelm@9132
   123
(* access current syntax *)
wenzelm@7026
   124
wenzelm@7026
   125
(*Note: the syntax for files is statically determined at the very
wenzelm@7026
   126
  beginning; for interactive processing it may change dynamically.*)
wenzelm@7026
   127
wenzelm@7026
   128
fun get_lexicons () = ! global_lexicons;
wenzelm@7026
   129
fun get_parsers () = ! global_parsers;
wenzelm@17412
   130
fun get_parser () = Option.map (#2 o #1) o Symtab.lookup (get_parsers ());
wenzelm@7789
   131
wenzelm@17071
   132
fun command_tags name =
wenzelm@17412
   133
  (case Symtab.lookup (get_parsers ()) name of
wenzelm@17071
   134
    SOME (((_, k), _), _) => OuterKeyword.tags_of k
wenzelm@17071
   135
  | NONE => []);
wenzelm@17071
   136
wenzelm@17184
   137
fun is_markup kind name = (AList.lookup (op =) (! global_markups) name = SOME kind);
wenzelm@5829
   138
wenzelm@5829
   139
wenzelm@5829
   140
(* augment syntax *)
wenzelm@5829
   141
wenzelm@7026
   142
fun add_keywords keywords = change_lexicons (apfst (fn lex =>
wenzelm@7026
   143
  (Scan.extend_lexicon lex (map Symbol.explode keywords))));
wenzelm@5829
   144
wenzelm@17071
   145
fun add_parser (Parser (name, (comment, kind, markup), int_only, parse)) tab =
wenzelm@16894
   146
 (if not (Symtab.defined tab name) then ()
wenzelm@5829
   147
  else warning ("Redefined outer syntax command " ^ quote name);
wenzelm@17412
   148
  Symtab.update (name, (((comment, kind), (int_only, parse)), markup)) tab);
wenzelm@5829
   149
wenzelm@5829
   150
fun add_parsers parsers =
wenzelm@17071
   151
  (change_parsers (fold add_parser parsers);
wenzelm@7026
   152
    change_lexicons (apsnd (fn lex => Scan.extend_lexicon lex
wenzelm@7026
   153
      (map (fn Parser (name, _, _, _) => Symbol.explode name) parsers))));
wenzelm@7026
   154
wenzelm@7026
   155
end;
wenzelm@5829
   156
wenzelm@5829
   157
wenzelm@7026
   158
(* print syntax *)
wenzelm@7026
   159
wenzelm@14687
   160
fun is_keyword s = Scan.is_literal (#1 (get_lexicons ())) (Symbol.explode s);
wenzelm@7026
   161
fun dest_keywords () = Scan.dest_lexicon (#1 (get_lexicons ()));
wenzelm@7026
   162
wenzelm@7026
   163
fun dest_parsers () =
wenzelm@16727
   164
  get_parsers () |> Symtab.dest |> sort_wrt #1
wenzelm@17071
   165
  |> map (fn (name, (((cmt, kind), (int_only, _)), _)) =>
wenzelm@17071
   166
    (name, cmt, OuterKeyword.kind_of kind, int_only));
wenzelm@5829
   167
wenzelm@9223
   168
fun print_outer_syntax () =
wenzelm@7026
   169
  let
wenzelm@7026
   170
    fun pretty_cmd (name, comment, _, _) =
wenzelm@7026
   171
      Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
skalberg@15570
   172
    val (int_cmds, cmds) = List.partition #4 (dest_parsers ());
wenzelm@7026
   173
  in
wenzelm@8720
   174
    [Pretty.strs ("syntax keywords:" :: map quote (dest_keywords ())),
wenzelm@18326
   175
      Pretty.big_list "commands:" (map pretty_cmd cmds),
wenzelm@18326
   176
      Pretty.big_list "interactive-only commands:" (map pretty_cmd int_cmds)]
wenzelm@9223
   177
    |> Pretty.chunks |> Pretty.writeln
wenzelm@7026
   178
  end;
wenzelm@5829
   179
wenzelm@9223
   180
val print_commands = Toplevel.imperative print_outer_syntax;
wenzelm@7367
   181
wenzelm@5829
   182
wenzelm@5829
   183
wenzelm@9132
   184
(** toplevel parsing **)
wenzelm@5829
   185
wenzelm@9132
   186
(* basic sources *)
wenzelm@6860
   187
wenzelm@17071
   188
fun toplevel_source term do_trace do_recover cmd src =
wenzelm@9132
   189
  let
wenzelm@9132
   190
    val no_terminator =
wenzelm@9132
   191
      Scan.unless P.semicolon (Scan.one (T.not_sync andf T.not_eof));
skalberg@15531
   192
    fun recover x = (Scan.prompt "recover# " (Scan.repeat no_terminator) >> K [NONE]) x;
wenzelm@9132
   193
  in
wenzelm@9132
   194
    src
wenzelm@12876
   195
    |> T.source_proper
wenzelm@9132
   196
    |> Source.source T.stopper
skalberg@15531
   197
      (Scan.bulk (P.$$$ "--" -- P.!!! P.text >> K NONE || P.not_eof >> SOME))
skalberg@15531
   198
      (if do_recover then SOME recover else NONE)
wenzelm@12876
   199
    |> Source.mapfilter I
wenzelm@17071
   200
    |> Source.source T.stopper (Scan.bulk (fn xs => P.!!! (command term do_trace (cmd ())) xs))
skalberg@15531
   201
      (if do_recover then SOME recover else NONE)
wenzelm@9132
   202
    |> Source.mapfilter I
wenzelm@9132
   203
  end;
wenzelm@5829
   204
wenzelm@7746
   205
wenzelm@9132
   206
(* interactive source of toplevel transformers *)
wenzelm@5829
   207
wenzelm@9132
   208
fun isar term no_pos =
wenzelm@9132
   209
  Source.tty
wenzelm@9132
   210
  |> Symbol.source true
wenzelm@9132
   211
  |> T.source true get_lexicons
wenzelm@9132
   212
    (if no_pos then Position.none else Position.line_name 1 "stdin")
wenzelm@14925
   213
  |> toplevel_source term false true get_parser;
wenzelm@6199
   214
wenzelm@6199
   215
wenzelm@16195
   216
(* scan text *)
wenzelm@14925
   217
aspinall@15144
   218
fun scan str =
wenzelm@16195
   219
  Source.of_string str
wenzelm@16195
   220
  |> Symbol.source false
wenzelm@16195
   221
  |> T.source true get_lexicons Position.none
wenzelm@16195
   222
  |> Source.exhaust;
wenzelm@16195
   223
wenzelm@16195
   224
wenzelm@16195
   225
(* read tokens with trace *)
wenzelm@14925
   226
aspinall@15144
   227
fun read toks =
aspinall@15144
   228
  Source.of_list toks
wenzelm@14925
   229
  |> toplevel_source false true true get_parser
wenzelm@14925
   230
  |> Source.exhaust
wenzelm@15973
   231
  |> map (fn tr => (Toplevel.name_of tr, the (Toplevel.source_of tr), tr));
nipkow@14091
   232
nipkow@14091
   233
wenzelm@16195
   234
wenzelm@9132
   235
(** read theory **)
wenzelm@6247
   236
wenzelm@12943
   237
(* check_text *)
wenzelm@12943
   238
wenzelm@17265
   239
fun check_text s state = (IsarOutput.eval_antiquote (#1 (get_lexicons ())) state s; ());
wenzelm@12943
   240
wenzelm@12943
   241
wenzelm@9132
   242
(* deps_thy *)
wenzelm@6199
   243
wenzelm@7940
   244
fun deps_thy name ml path =
wenzelm@6199
   245
  let
wenzelm@7735
   246
    val src = Source.of_string (File.read path);
wenzelm@7735
   247
    val pos = Path.position path;
wenzelm@17932
   248
    val (name', parents, files) = ThyHeader.read (src, pos);
wenzelm@6199
   249
    val ml_path = ThyLoad.ml_path name;
wenzelm@15973
   250
    val ml_file = if ml andalso is_some (ThyLoad.check_file NONE ml_path) then [ml_path] else [];
wenzelm@6199
   251
  in
wenzelm@6199
   252
    if name <> name' then
wenzelm@7940
   253
      error ("Filename " ^ quote (Path.pack path) ^
wenzelm@8078
   254
        " does not agree with theory name " ^ quote name')
wenzelm@6247
   255
    else (parents, map (Path.unpack o #1) files @ ml_file)
wenzelm@6199
   256
  end;
wenzelm@6199
   257
wenzelm@7746
   258
wenzelm@9132
   259
(* load_thy *)
wenzelm@6199
   260
wenzelm@7746
   261
local
wenzelm@7746
   262
wenzelm@7940
   263
fun try_ml_file name time =
wenzelm@6199
   264
  let
wenzelm@6199
   265
    val path = ThyLoad.ml_path name;
wenzelm@17975
   266
    val tr = Toplevel.imperative (fn () =>
wenzelm@17975
   267
      setmp OldGoals.legacy true (fn () => ThyInfo.load_file time path) ());
wenzelm@6247
   268
    val tr_name = if time then "time_use" else "use";
wenzelm@6199
   269
  in
skalberg@15531
   270
    if is_none (ThyLoad.check_file NONE path) then ()
wenzelm@9132
   271
    else Toplevel.excursion [Toplevel.empty |> Toplevel.name tr_name |> tr]
wenzelm@6199
   272
  end;
wenzelm@6199
   273
wenzelm@6247
   274
fun run_thy name path =
wenzelm@7683
   275
  let
wenzelm@17932
   276
    val text = Source.of_list (Library.untabify (explode (File.read path)));
wenzelm@17932
   277
    val _ = Present.init_theory name;
wenzelm@17932
   278
    val _ = Present.verbatim_source name (fn () => Source.exhaust (Symbol.source false text));
wenzelm@17932
   279
    val toks = text
wenzelm@17932
   280
      |> Symbol.source false
wenzelm@17932
   281
      |> T.source false (K (get_lexicons ())) (Path.position path)
wenzelm@17932
   282
      |> Source.exhausted;
wenzelm@17932
   283
    val trs = toks
wenzelm@17932
   284
      |> toplevel_source false false false (K (get_parser ()))
wenzelm@17932
   285
      |> Source.exhaust;
wenzelm@7683
   286
  in
wenzelm@17932
   287
    IsarOutput.present_thy (#1 (get_lexicons ())) command_tags is_markup trs toks
wenzelm@17932
   288
    |> Buffer.content
wenzelm@17932
   289
    |> Present.theory_output name
wenzelm@6247
   290
  end;
wenzelm@6199
   291
wenzelm@7746
   292
in
wenzelm@7746
   293
wenzelm@6199
   294
fun load_thy name ml time path =
wenzelm@6247
   295
 (if time then
wenzelm@6247
   296
    timeit (fn () =>
wenzelm@6247
   297
     (writeln ("\n**** Starting theory " ^ quote name ^ " ****");
wenzelm@9036
   298
      run_thy name path;
wenzelm@6247
   299
      writeln ("**** Finished theory " ^ quote name ^ " ****\n")))
wenzelm@6247
   300
  else run_thy name path;
wenzelm@6247
   301
  Context.context (ThyInfo.get_theory name);
wenzelm@7940
   302
  if ml then try_ml_file name time else ());
wenzelm@5829
   303
wenzelm@7746
   304
end;
wenzelm@7746
   305
wenzelm@5829
   306
wenzelm@5829
   307
wenzelm@5829
   308
(** the read-eval-print loop **)
wenzelm@5829
   309
wenzelm@5923
   310
(* main loop *)
wenzelm@5923
   311
wenzelm@7333
   312
fun gen_loop term no_pos =
wenzelm@7333
   313
 (Context.reset_context ();
wenzelm@15989
   314
  Toplevel.loop (isar term no_pos);
wenzelm@15989
   315
  ml_prompts "ML> " "ML# ");
wenzelm@5829
   316
wenzelm@7333
   317
fun gen_main term no_pos =
wenzelm@5829
   318
 (Toplevel.set_state Toplevel.toplevel;
wenzelm@6199
   319
  writeln (Session.welcome ());
wenzelm@7333
   320
  gen_loop term no_pos);
wenzelm@6860
   321
wenzelm@15830
   322
structure Isar =
wenzelm@15830
   323
struct
wenzelm@18064
   324
  val state = Toplevel.get_state;
wenzelm@18064
   325
  val exn = Toplevel.exn;
wenzelm@15830
   326
  fun main () = gen_main false false;
wenzelm@15830
   327
  fun loop () = gen_loop false false;
wenzelm@15830
   328
  fun sync_main () = gen_main true true;
wenzelm@15830
   329
  fun sync_loop () = gen_loop true true;
wenzelm@15830
   330
end;
wenzelm@5829
   331
wenzelm@5829
   332
wenzelm@6373
   333
(*final declarations of this structure!*)
skalberg@15531
   334
val command = parser false NONE;
skalberg@15531
   335
val markup_command = parser false o SOME;
skalberg@15531
   336
val improper_command = parser true NONE;
wenzelm@6685
   337
wenzelm@5829
   338
end;
wenzelm@5829
   339
wenzelm@6199
   340
(*setup theory syntax dependent operations*)
wenzelm@6199
   341
ThyLoad.deps_thy_fn := OuterSyntax.deps_thy;
wenzelm@6199
   342
ThyLoad.load_thy_fn := OuterSyntax.load_thy;
wenzelm@6199
   343
structure ThyLoad: THY_LOAD = ThyLoad;
wenzelm@6199
   344
wenzelm@5829
   345
structure BasicOuterSyntax: BASIC_OUTER_SYNTAX = OuterSyntax;
wenzelm@5829
   346
open BasicOuterSyntax;