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