src/Pure/Isar/outer_syntax.ML
author wenzelm
Thu Mar 27 14:41:19 2008 +0100 (2008-03-27 ago)
changeset 26431 f1c79c00f1e4
parent 26415 1b624d6e9163
child 26600 f11515535c83
permissions -rw-r--r--
added process_file;
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@24868
     5
The global Isabelle/Isar outer syntax. Note: the syntax for files is
wenzelm@24868
     6
statically determined at the very beginning; for interactive processing
wenzelm@24868
     7
it may change dynamically.
wenzelm@5829
     8
*)
wenzelm@5829
     9
wenzelm@5829
    10
signature BASIC_OUTER_SYNTAX =
wenzelm@5829
    11
sig
wenzelm@15830
    12
  structure Isar:
wenzelm@15830
    13
    sig
wenzelm@18064
    14
      val state: unit -> Toplevel.state
wenzelm@21207
    15
      val exn: unit -> (exn * string) option
wenzelm@20023
    16
      val context: unit -> Proof.context
wenzelm@21401
    17
      val goal: unit -> thm list * thm
wenzelm@15830
    18
      val main: unit -> unit
wenzelm@15830
    19
      val loop: unit -> unit
wenzelm@15830
    20
      val sync_main: unit -> unit
wenzelm@15830
    21
      val sync_loop: unit -> unit
wenzelm@25526
    22
      val secure_main: unit -> unit
wenzelm@18684
    23
      val toplevel: (unit -> 'a) -> 'a
wenzelm@15830
    24
    end;
wenzelm@5829
    25
end;
wenzelm@5829
    26
wenzelm@5829
    27
signature OUTER_SYNTAX =
wenzelm@5829
    28
sig
wenzelm@5829
    29
  include BASIC_OUTER_SYNTAX
wenzelm@24868
    30
  type parser_fn = OuterLex.token list ->
wenzelm@24868
    31
    (Toplevel.transition -> Toplevel.transition) * OuterLex.token list
wenzelm@23722
    32
  val get_lexicons: unit -> Scan.lexicon * Scan.lexicon
wenzelm@23796
    33
  val command_keyword: string -> OuterKeyword.T option
wenzelm@14687
    34
  val is_keyword: string -> bool
wenzelm@24868
    35
  val keywords: string list -> unit
wenzelm@24868
    36
  val command: string -> string -> OuterKeyword.T -> parser_fn -> unit
wenzelm@24868
    37
  val markup_command: ThyOutput.markup -> string -> string -> OuterKeyword.T -> parser_fn -> unit
wenzelm@24868
    38
  val improper_command: string -> string -> OuterKeyword.T -> parser_fn -> unit
wenzelm@7026
    39
  val dest_keywords: unit -> string list
wenzelm@7026
    40
  val dest_parsers: unit -> (string * string * string * bool) list
wenzelm@5883
    41
  val print_outer_syntax: unit -> unit
wenzelm@24872
    42
  val report: unit -> unit
wenzelm@19060
    43
  val check_text: string * Position.T -> Toplevel.node option -> unit
wenzelm@16195
    44
  val scan: string -> OuterLex.token list
wenzelm@16195
    45
  val read: OuterLex.token list -> (string * OuterLex.token list * Toplevel.transition) list
wenzelm@25580
    46
  val parse: Position.T -> string -> Toplevel.transition list
wenzelm@26431
    47
  val process_file: Path.T -> theory -> theory
wenzelm@24868
    48
  val isar: bool -> unit Toplevel.isar
wenzelm@5829
    49
end;
wenzelm@5829
    50
aspinall@15224
    51
structure OuterSyntax : OUTER_SYNTAX  =
wenzelm@5829
    52
struct
wenzelm@5829
    53
wenzelm@7750
    54
structure T = OuterLex;
wenzelm@6860
    55
structure P = OuterParse;
wenzelm@6860
    56
wenzelm@5829
    57
wenzelm@5829
    58
(** outer syntax **)
wenzelm@5829
    59
wenzelm@24872
    60
(* diagnostics *)
wenzelm@24872
    61
wenzelm@24872
    62
fun report_keyword name =
wenzelm@24872
    63
  Pretty.markup (Markup.keyword_decl name)
wenzelm@24872
    64
    [Pretty.str ("Outer syntax keyword: " ^ quote name)];
wenzelm@24872
    65
wenzelm@24872
    66
fun report_command name kind =
wenzelm@24872
    67
  Pretty.markup (Markup.command_decl name kind)
wenzelm@24872
    68
    [Pretty.str ("Outer syntax command: " ^ quote name ^ " (" ^ kind ^ ")")];
wenzelm@24872
    69
wenzelm@24872
    70
wenzelm@5829
    71
(* parsers *)
wenzelm@5829
    72
wenzelm@24868
    73
type parser_fn = T.token list -> (Toplevel.transition -> Toplevel.transition) * T.token list;
wenzelm@5829
    74
wenzelm@24868
    75
datatype parser = Parser of
wenzelm@24868
    76
 {comment: string,
wenzelm@24868
    77
  kind: OuterKeyword.T,
wenzelm@24868
    78
  markup: ThyOutput.markup option,
wenzelm@24868
    79
  int_only: bool,
wenzelm@24868
    80
  parse: parser_fn};
wenzelm@5829
    81
wenzelm@24868
    82
fun make_parser comment kind markup int_only parse =
wenzelm@24868
    83
  Parser {comment = comment, kind = kind, markup = markup, int_only = int_only, parse = parse};
wenzelm@5829
    84
wenzelm@5829
    85
wenzelm@5829
    86
(* parse command *)
wenzelm@5829
    87
wenzelm@6860
    88
local
wenzelm@6199
    89
wenzelm@14925
    90
fun terminate false = Scan.succeed ()
wenzelm@14925
    91
  | terminate true = P.group "end of input" (Scan.option P.sync -- P.semicolon >> K ());
wenzelm@14925
    92
wenzelm@14925
    93
fun trace false parse = parse
wenzelm@14925
    94
  | trace true parse = Scan.trace parse >> (fn (f, toks) => f o Toplevel.source toks);
wenzelm@14925
    95
wenzelm@17071
    96
fun body cmd do_trace (name, _) =
wenzelm@7026
    97
  (case cmd name of
wenzelm@24868
    98
    SOME (Parser {int_only, parse, ...}) =>
wenzelm@17118
    99
      P.!!! (Scan.prompt (name ^ "# ") (trace do_trace (P.tags |-- parse) >> pair int_only))
skalberg@15531
   100
  | NONE => sys_error ("no parser for outer syntax command " ^ quote name));
wenzelm@6860
   101
wenzelm@6860
   102
in
wenzelm@5829
   103
wenzelm@24868
   104
fun parse_command do_terminate do_trace cmd =
skalberg@15531
   105
  P.semicolon >> K NONE ||
skalberg@15531
   106
  P.sync >> K NONE ||
wenzelm@17118
   107
  (P.position P.command :-- body cmd do_trace) --| terminate do_terminate
wenzelm@6860
   108
    >> (fn ((name, pos), (int_only, f)) =>
skalberg@15531
   109
      SOME (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |>
wenzelm@6860
   110
        Toplevel.interactive int_only |> f));
wenzelm@5829
   111
wenzelm@6199
   112
end;
wenzelm@6199
   113
wenzelm@5829
   114
wenzelm@5829
   115
wenzelm@9132
   116
(** global outer syntax **)
wenzelm@5829
   117
wenzelm@7026
   118
local
wenzelm@7026
   119
wenzelm@7026
   120
val global_lexicons = ref (Scan.empty_lexicon, Scan.empty_lexicon);
wenzelm@24868
   121
val global_parsers = ref (Symtab.empty: parser Symtab.table);
wenzelm@22120
   122
val global_markups = ref ([]: (string * ThyOutput.markup) list);
wenzelm@5952
   123
wenzelm@23939
   124
fun change_lexicons f = CRITICAL (fn () =>
wenzelm@7026
   125
  let val lexs = f (! global_lexicons) in
wenzelm@7026
   126
    (case (op inter_string) (pairself Scan.dest_lexicon lexs) of
wenzelm@7026
   127
      [] => global_lexicons := lexs
wenzelm@7026
   128
    | bads => error ("Clash of outer syntax commands and keywords: " ^ commas_quote bads))
wenzelm@23939
   129
  end);
wenzelm@5829
   130
wenzelm@23939
   131
fun change_parsers f = CRITICAL (fn () =>
wenzelm@23939
   132
 (change global_parsers f;
wenzelm@23939
   133
  global_markups :=
wenzelm@24868
   134
    Symtab.fold (fn (name, Parser {markup = SOME m, ...}) => cons (name, m) | _ => I)
wenzelm@24868
   135
      (! global_parsers) []));
wenzelm@6722
   136
wenzelm@7026
   137
in
wenzelm@7026
   138
wenzelm@9132
   139
(* access current syntax *)
wenzelm@7026
   140
wenzelm@24868
   141
fun get_lexicons () = CRITICAL (fn () => ! global_lexicons);
wenzelm@24868
   142
fun get_parsers () = CRITICAL (fn () => ! global_parsers);
wenzelm@24868
   143
fun get_markups () = CRITICAL (fn () => ! global_markups);
wenzelm@7026
   144
wenzelm@24868
   145
fun get_parser () = Symtab.lookup (get_parsers ());
wenzelm@7789
   146
wenzelm@23796
   147
fun command_keyword name =
wenzelm@24868
   148
  (case Symtab.lookup (get_parsers ()) name of
wenzelm@24868
   149
    SOME (Parser {kind, ...}) => SOME kind
wenzelm@24868
   150
  | NONE => NONE);
wenzelm@24868
   151
wenzelm@23796
   152
fun command_tags name = these ((Option.map OuterKeyword.tags_of) (command_keyword name));
wenzelm@17071
   153
wenzelm@24868
   154
fun is_markup kind name = AList.lookup (op =) (get_markups ()) name = SOME kind;
wenzelm@5829
   155
wenzelm@5829
   156
wenzelm@5829
   157
(* augment syntax *)
wenzelm@5829
   158
wenzelm@24872
   159
fun keywords names =
wenzelm@24872
   160
 (change_lexicons (apfst (Scan.extend_lexicon (map Symbol.explode names)));
wenzelm@24872
   161
  List.app (Pretty.writeln o report_keyword) names);
wenzelm@24868
   162
wenzelm@24868
   163
wenzelm@24872
   164
fun add_parser (name, parser as Parser {kind, ...}) =
wenzelm@24868
   165
 (if not (Symtab.defined (get_parsers ()) name) then ()
wenzelm@24868
   166
  else warning ("Redefining outer syntax command " ^ quote name);
wenzelm@24868
   167
  change_parsers (Symtab.update (name, parser));
wenzelm@24872
   168
  change_lexicons (apsnd (Scan.extend_lexicon [Symbol.explode name]));
wenzelm@24872
   169
  Pretty.writeln (report_command name (OuterKeyword.kind_of kind)));
wenzelm@5829
   170
wenzelm@24868
   171
fun command name comment kind parse =
wenzelm@24868
   172
  add_parser (name, make_parser comment kind NONE false parse);
wenzelm@5829
   173
wenzelm@24868
   174
fun markup_command markup name comment kind parse =
wenzelm@24868
   175
  add_parser (name, make_parser comment kind (SOME markup) false parse);
wenzelm@24868
   176
wenzelm@24868
   177
fun improper_command name comment kind parse =
wenzelm@24868
   178
  add_parser (name, make_parser comment kind NONE true parse);
wenzelm@7026
   179
wenzelm@7026
   180
end;
wenzelm@5829
   181
wenzelm@5829
   182
wenzelm@24872
   183
(* inspect syntax *)
wenzelm@7026
   184
wenzelm@14687
   185
fun is_keyword s = Scan.is_literal (#1 (get_lexicons ())) (Symbol.explode s);
wenzelm@7026
   186
fun dest_keywords () = Scan.dest_lexicon (#1 (get_lexicons ()));
wenzelm@7026
   187
wenzelm@7026
   188
fun dest_parsers () =
wenzelm@16727
   189
  get_parsers () |> Symtab.dest |> sort_wrt #1
wenzelm@24868
   190
  |> map (fn (name, Parser {comment, kind, int_only, ...}) =>
wenzelm@24868
   191
    (name, comment, OuterKeyword.kind_of kind, int_only));
wenzelm@5829
   192
wenzelm@9223
   193
fun print_outer_syntax () =
wenzelm@7026
   194
  let
wenzelm@7026
   195
    fun pretty_cmd (name, comment, _, _) =
wenzelm@7026
   196
      Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
skalberg@15570
   197
    val (int_cmds, cmds) = List.partition #4 (dest_parsers ());
wenzelm@7026
   198
  in
wenzelm@8720
   199
    [Pretty.strs ("syntax keywords:" :: map quote (dest_keywords ())),
wenzelm@18326
   200
      Pretty.big_list "commands:" (map pretty_cmd cmds),
wenzelm@18326
   201
      Pretty.big_list "interactive-only commands:" (map pretty_cmd int_cmds)]
wenzelm@9223
   202
    |> Pretty.chunks |> Pretty.writeln
wenzelm@7026
   203
  end;
wenzelm@5829
   204
wenzelm@24872
   205
fun report () =
wenzelm@24872
   206
  (map report_keyword (dest_keywords ()) @
wenzelm@24872
   207
    map (fn (name, _, kind, _) => report_command name kind) (dest_parsers ()))
wenzelm@24872
   208
  |> Pretty.chunks |> Pretty.writeln;
wenzelm@7367
   209
wenzelm@5829
   210
wenzelm@5829
   211
wenzelm@9132
   212
(** toplevel parsing **)
wenzelm@5829
   213
wenzelm@9132
   214
(* basic sources *)
wenzelm@6860
   215
wenzelm@17071
   216
fun toplevel_source term do_trace do_recover cmd src =
wenzelm@9132
   217
  let
wenzelm@9132
   218
    val no_terminator =
wenzelm@9132
   219
      Scan.unless P.semicolon (Scan.one (T.not_sync andf T.not_eof));
wenzelm@23682
   220
    fun recover int =
wenzelm@23682
   221
      (int, fn _ => Scan.prompt "recover# " (Scan.repeat no_terminator) >> K [NONE]);
wenzelm@9132
   222
  in
wenzelm@9132
   223
    src
wenzelm@12876
   224
    |> T.source_proper
wenzelm@9132
   225
    |> Source.source T.stopper
skalberg@15531
   226
      (Scan.bulk (P.$$$ "--" -- P.!!! P.text >> K NONE || P.not_eof >> SOME))
wenzelm@23682
   227
        (Option.map recover do_recover)
wenzelm@19482
   228
    |> Source.map_filter I
wenzelm@24868
   229
    |> Source.source T.stopper
wenzelm@24868
   230
        (Scan.bulk (fn xs => P.!!! (parse_command term do_trace (cmd ())) xs))
wenzelm@23682
   231
        (Option.map recover do_recover)
wenzelm@19482
   232
    |> Source.map_filter I
wenzelm@9132
   233
  end;
wenzelm@5829
   234
wenzelm@7746
   235
wenzelm@25580
   236
(* off-line scanning/parsing *)
wenzelm@14925
   237
wenzelm@25580
   238
(*tokens*)
aspinall@15144
   239
fun scan str =
wenzelm@16195
   240
  Source.of_string str
wenzelm@16195
   241
  |> Symbol.source false
wenzelm@23679
   242
  |> T.source (SOME false) get_lexicons Position.none
wenzelm@16195
   243
  |> Source.exhaust;
wenzelm@16195
   244
wenzelm@25580
   245
(*commands from tokens, with trace*)
aspinall@15144
   246
fun read toks =
aspinall@15144
   247
  Source.of_list toks
wenzelm@25580
   248
  |> toplevel_source false true NONE get_parser
wenzelm@14925
   249
  |> Source.exhaust
wenzelm@15973
   250
  |> map (fn tr => (Toplevel.name_of tr, the (Toplevel.source_of tr), tr));
nipkow@14091
   251
wenzelm@25580
   252
(*commands from string, without trace*)
wenzelm@25580
   253
fun parse pos str =
wenzelm@25580
   254
  Source.of_string str
wenzelm@25580
   255
  |> Symbol.source false
wenzelm@25580
   256
  |> T.source (SOME false) get_lexicons pos
wenzelm@25580
   257
  |> toplevel_source false false NONE get_parser
wenzelm@25580
   258
  |> Source.exhaust;
wenzelm@25580
   259
nipkow@14091
   260
wenzelm@26431
   261
(* process file *)
wenzelm@26431
   262
wenzelm@26431
   263
fun process_file path thy =
wenzelm@26431
   264
  let
wenzelm@26431
   265
    val result = ref thy;
wenzelm@26431
   266
    val trs = parse (Position.path path) (File.read path);
wenzelm@26431
   267
    val init = Toplevel.init_theory (K thy) (fn thy' => result := thy') (K ());
wenzelm@26431
   268
    val _ = Toplevel.excursion (init Toplevel.empty :: trs @ [Toplevel.exit Toplevel.empty]);
wenzelm@26431
   269
  in ! result end;
wenzelm@26431
   270
wenzelm@26431
   271
wenzelm@24868
   272
(* interactive source of toplevel transformers *)
wenzelm@24868
   273
wenzelm@24868
   274
fun isar term =
wenzelm@24868
   275
  Source.tty
wenzelm@24868
   276
  |> Symbol.source true
wenzelm@24868
   277
  |> T.source (SOME true) get_lexicons Position.none
wenzelm@24868
   278
  |> toplevel_source term false (SOME true) get_parser;
wenzelm@24868
   279
wenzelm@24868
   280
wenzelm@16195
   281
wenzelm@9132
   282
(** read theory **)
wenzelm@6247
   283
wenzelm@12943
   284
(* check_text *)
wenzelm@12943
   285
wenzelm@22120
   286
fun check_text s state = (ThyOutput.eval_antiquote (#1 (get_lexicons ())) state s; ());
wenzelm@12943
   287
wenzelm@12943
   288
wenzelm@26323
   289
(* load_thy (backpatching) *)
wenzelm@6199
   290
wenzelm@7746
   291
local
wenzelm@7746
   292
wenzelm@26323
   293
fun load_thy dir name pos text time =
wenzelm@7683
   294
  let
wenzelm@24065
   295
    val text_src = Source.of_list (Library.untabify text);
wenzelm@23866
   296
wenzelm@17932
   297
    val _ = Present.init_theory name;
wenzelm@24065
   298
    val _ = Present.verbatim_source name (fn () => Source.exhaust (Symbol.source false text_src));
wenzelm@24065
   299
    val toks = text_src
wenzelm@17932
   300
      |> Symbol.source false
wenzelm@24065
   301
      |> T.source NONE (K (get_lexicons ())) pos
wenzelm@17932
   302
      |> Source.exhausted;
wenzelm@17932
   303
    val trs = toks
wenzelm@23679
   304
      |> toplevel_source false false NONE (K (get_parser ()))
wenzelm@17932
   305
      |> Source.exhaust;
wenzelm@23866
   306
wenzelm@23866
   307
    val _ = if time then writeln ("\n**** Starting theory " ^ quote name ^ " ****") else ();
wenzelm@25685
   308
    val _ = cond_timeit time "" (fn () =>
wenzelm@23866
   309
      ThyOutput.process_thy (#1 (get_lexicons ())) command_tags is_markup trs toks
wenzelm@23866
   310
      |> Buffer.content
wenzelm@23866
   311
      |> Present.theory_output name);
wenzelm@23866
   312
    val _ = if time then writeln ("**** Finished theory " ^ quote name ^ " ****\n") else ();
wenzelm@24065
   313
  in () end;
wenzelm@23866
   314
wenzelm@26323
   315
in val _ = ThyLoad.load_thy_fn := load_thy end;
wenzelm@7746
   316
wenzelm@5829
   317
wenzelm@5829
   318
wenzelm@5829
   319
(** the read-eval-print loop **)
wenzelm@5829
   320
wenzelm@5923
   321
(* main loop *)
wenzelm@5923
   322
wenzelm@25583
   323
fun gen_loop secure do_terminate =
wenzelm@26415
   324
 (CRITICAL (fn () => Context.set_thread_data NONE);
wenzelm@25583
   325
  Toplevel.loop secure (isar do_terminate));
wenzelm@5829
   326
wenzelm@25583
   327
fun gen_main secure do_terminate =
wenzelm@21957
   328
 (Toplevel.init_state ();
wenzelm@6199
   329
  writeln (Session.welcome ());
wenzelm@25583
   330
  gen_loop secure do_terminate);
wenzelm@6860
   331
wenzelm@15830
   332
structure Isar =
wenzelm@15830
   333
struct
wenzelm@18064
   334
  val state = Toplevel.get_state;
wenzelm@18064
   335
  val exn = Toplevel.exn;
wenzelm@21401
   336
wenzelm@21207
   337
  fun context () =
wenzelm@21506
   338
    Toplevel.context_of (state ())
wenzelm@21207
   339
      handle Toplevel.UNDEF => error "Unknown context";
wenzelm@21401
   340
wenzelm@21401
   341
  fun goal () =
wenzelm@21401
   342
    #2 (Proof.get_goal (Toplevel.proof_of (state ())))
wenzelm@21401
   343
      handle Toplevel.UNDEF => error "No goal present";
wenzelm@21401
   344
wenzelm@25526
   345
  fun main () = gen_main (Secure.is_secure ()) false;
wenzelm@25526
   346
  fun loop () = gen_loop (Secure.is_secure ()) false;
wenzelm@25526
   347
  fun sync_main () = gen_main (Secure.is_secure ()) true;
wenzelm@25526
   348
  fun sync_loop () = gen_loop (Secure.is_secure ()) true;
wenzelm@25839
   349
  fun secure_main () = (Toplevel.init_state (); gen_loop true true);
wenzelm@18684
   350
  val toplevel = Toplevel.program;
wenzelm@15830
   351
end;
wenzelm@5829
   352
wenzelm@5829
   353
end;
wenzelm@5829
   354
wenzelm@6199
   355
structure ThyLoad: THY_LOAD = ThyLoad;
wenzelm@5829
   356
structure BasicOuterSyntax: BASIC_OUTER_SYNTAX = OuterSyntax;
wenzelm@5829
   357
open BasicOuterSyntax;