src/Pure/Isar/outer_syntax.ML
author wenzelm
Sat May 24 22:04:57 2008 +0200 (2008-05-24 ago)
changeset 26990 a91f7741967a
parent 26881 bb68f50644a9
child 27353 71c4dd53d4cb
permissions -rw-r--r--
added local_theory command wrappers;
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 OUTER_SYNTAX =
wenzelm@5829
    11
sig
wenzelm@24868
    12
  type parser_fn = OuterLex.token list ->
wenzelm@24868
    13
    (Toplevel.transition -> Toplevel.transition) * OuterLex.token list
wenzelm@23722
    14
  val get_lexicons: unit -> Scan.lexicon * Scan.lexicon
wenzelm@23796
    15
  val command_keyword: string -> OuterKeyword.T option
wenzelm@14687
    16
  val is_keyword: string -> bool
wenzelm@24868
    17
  val keywords: string list -> unit
wenzelm@24868
    18
  val command: string -> string -> OuterKeyword.T -> parser_fn -> unit
wenzelm@24868
    19
  val markup_command: ThyOutput.markup -> string -> string -> OuterKeyword.T -> parser_fn -> unit
wenzelm@24868
    20
  val improper_command: string -> string -> OuterKeyword.T -> parser_fn -> unit
wenzelm@26990
    21
  val local_theory: string -> string -> OuterKeyword.T ->
wenzelm@26990
    22
    (OuterParse.token list -> (local_theory -> local_theory) * OuterLex.token list) -> unit
wenzelm@26990
    23
  val local_theory_to_proof': string -> string -> OuterKeyword.T ->
wenzelm@26990
    24
    (OuterParse.token list -> (bool -> local_theory -> Proof.state) * OuterLex.token list) -> unit
wenzelm@26990
    25
  val local_theory_to_proof: string -> string -> OuterKeyword.T ->
wenzelm@26990
    26
    (OuterParse.token list -> (local_theory -> Proof.state) * OuterLex.token list) -> unit
wenzelm@7026
    27
  val dest_keywords: unit -> string list
wenzelm@7026
    28
  val dest_parsers: unit -> (string * string * string * bool) list
wenzelm@5883
    29
  val print_outer_syntax: unit -> unit
wenzelm@24872
    30
  val report: unit -> unit
wenzelm@19060
    31
  val check_text: string * Position.T -> Toplevel.node option -> unit
wenzelm@16195
    32
  val scan: string -> OuterLex.token list
wenzelm@25580
    33
  val parse: Position.T -> string -> Toplevel.transition list
wenzelm@26431
    34
  val process_file: Path.T -> theory -> theory
wenzelm@26600
    35
  type isar
wenzelm@26600
    36
  val isar: bool -> isar
wenzelm@26611
    37
  val load_thy: Path.T -> string -> Position.T -> string list -> bool -> unit
wenzelm@5829
    38
end;
wenzelm@5829
    39
wenzelm@26600
    40
structure OuterSyntax: OUTER_SYNTAX =
wenzelm@5829
    41
struct
wenzelm@5829
    42
wenzelm@7750
    43
structure T = OuterLex;
wenzelm@6860
    44
structure P = OuterParse;
wenzelm@6860
    45
wenzelm@5829
    46
wenzelm@5829
    47
(** outer syntax **)
wenzelm@5829
    48
wenzelm@24872
    49
(* diagnostics *)
wenzelm@24872
    50
wenzelm@24872
    51
fun report_keyword name =
wenzelm@26700
    52
  Pretty.mark (Markup.keyword_decl name)
wenzelm@26700
    53
    (Pretty.str ("Outer syntax keyword: " ^ quote name));
wenzelm@24872
    54
wenzelm@24872
    55
fun report_command name kind =
wenzelm@26700
    56
  Pretty.mark (Markup.command_decl name kind)
wenzelm@26700
    57
    (Pretty.str ("Outer syntax command: " ^ quote name ^ " (" ^ kind ^ ")"));
wenzelm@24872
    58
wenzelm@24872
    59
wenzelm@5829
    60
(* parsers *)
wenzelm@5829
    61
wenzelm@24868
    62
type parser_fn = T.token list -> (Toplevel.transition -> Toplevel.transition) * T.token list;
wenzelm@5829
    63
wenzelm@24868
    64
datatype parser = Parser of
wenzelm@24868
    65
 {comment: string,
wenzelm@24868
    66
  kind: OuterKeyword.T,
wenzelm@24868
    67
  markup: ThyOutput.markup option,
wenzelm@24868
    68
  int_only: bool,
wenzelm@24868
    69
  parse: parser_fn};
wenzelm@5829
    70
wenzelm@24868
    71
fun make_parser comment kind markup int_only parse =
wenzelm@24868
    72
  Parser {comment = comment, kind = kind, markup = markup, int_only = int_only, parse = parse};
wenzelm@5829
    73
wenzelm@5829
    74
wenzelm@5829
    75
(* parse command *)
wenzelm@5829
    76
wenzelm@6860
    77
local
wenzelm@6199
    78
wenzelm@14925
    79
fun terminate false = Scan.succeed ()
wenzelm@14925
    80
  | terminate true = P.group "end of input" (Scan.option P.sync -- P.semicolon >> K ());
wenzelm@14925
    81
wenzelm@26620
    82
fun body cmd (name, _) =
wenzelm@7026
    83
  (case cmd name of
wenzelm@24868
    84
    SOME (Parser {int_only, parse, ...}) =>
wenzelm@26620
    85
      P.!!! (Scan.prompt (name ^ "# ") (P.tags |-- parse >> pair int_only))
skalberg@15531
    86
  | NONE => sys_error ("no parser for outer syntax command " ^ quote name));
wenzelm@6860
    87
wenzelm@6860
    88
in
wenzelm@5829
    89
wenzelm@26620
    90
fun parse_command do_terminate cmd =
skalberg@15531
    91
  P.semicolon >> K NONE ||
skalberg@15531
    92
  P.sync >> K NONE ||
wenzelm@26620
    93
  (P.position P.command :-- body cmd) --| terminate do_terminate
wenzelm@6860
    94
    >> (fn ((name, pos), (int_only, f)) =>
skalberg@15531
    95
      SOME (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |>
wenzelm@6860
    96
        Toplevel.interactive int_only |> f));
wenzelm@5829
    97
wenzelm@6199
    98
end;
wenzelm@6199
    99
wenzelm@5829
   100
wenzelm@5829
   101
wenzelm@9132
   102
(** global outer syntax **)
wenzelm@5829
   103
wenzelm@7026
   104
local
wenzelm@7026
   105
wenzelm@7026
   106
val global_lexicons = ref (Scan.empty_lexicon, Scan.empty_lexicon);
wenzelm@24868
   107
val global_parsers = ref (Symtab.empty: parser Symtab.table);
wenzelm@22120
   108
val global_markups = ref ([]: (string * ThyOutput.markup) list);
wenzelm@5952
   109
wenzelm@23939
   110
fun change_lexicons f = CRITICAL (fn () =>
wenzelm@7026
   111
  let val lexs = f (! global_lexicons) in
wenzelm@7026
   112
    (case (op inter_string) (pairself Scan.dest_lexicon lexs) of
wenzelm@7026
   113
      [] => global_lexicons := lexs
wenzelm@7026
   114
    | bads => error ("Clash of outer syntax commands and keywords: " ^ commas_quote bads))
wenzelm@23939
   115
  end);
wenzelm@5829
   116
wenzelm@23939
   117
fun change_parsers f = CRITICAL (fn () =>
wenzelm@23939
   118
 (change global_parsers f;
wenzelm@23939
   119
  global_markups :=
wenzelm@24868
   120
    Symtab.fold (fn (name, Parser {markup = SOME m, ...}) => cons (name, m) | _ => I)
wenzelm@24868
   121
      (! global_parsers) []));
wenzelm@6722
   122
wenzelm@7026
   123
in
wenzelm@7026
   124
wenzelm@9132
   125
(* access current syntax *)
wenzelm@7026
   126
wenzelm@24868
   127
fun get_lexicons () = CRITICAL (fn () => ! global_lexicons);
wenzelm@24868
   128
fun get_parsers () = CRITICAL (fn () => ! global_parsers);
wenzelm@24868
   129
fun get_markups () = CRITICAL (fn () => ! global_markups);
wenzelm@7026
   130
wenzelm@24868
   131
fun get_parser () = Symtab.lookup (get_parsers ());
wenzelm@7789
   132
wenzelm@23796
   133
fun command_keyword name =
wenzelm@24868
   134
  (case Symtab.lookup (get_parsers ()) name of
wenzelm@24868
   135
    SOME (Parser {kind, ...}) => SOME kind
wenzelm@24868
   136
  | NONE => NONE);
wenzelm@24868
   137
wenzelm@26600
   138
fun command_tags name = these (Option.map OuterKeyword.tags_of (command_keyword name));
wenzelm@17071
   139
wenzelm@24868
   140
fun is_markup kind name = AList.lookup (op =) (get_markups ()) name = SOME kind;
wenzelm@5829
   141
wenzelm@5829
   142
wenzelm@5829
   143
(* augment syntax *)
wenzelm@5829
   144
wenzelm@24872
   145
fun keywords names =
wenzelm@24872
   146
 (change_lexicons (apfst (Scan.extend_lexicon (map Symbol.explode names)));
wenzelm@24872
   147
  List.app (Pretty.writeln o report_keyword) names);
wenzelm@24868
   148
wenzelm@24868
   149
wenzelm@24872
   150
fun add_parser (name, parser as Parser {kind, ...}) =
wenzelm@24868
   151
 (if not (Symtab.defined (get_parsers ()) name) then ()
wenzelm@24868
   152
  else warning ("Redefining outer syntax command " ^ quote name);
wenzelm@24868
   153
  change_parsers (Symtab.update (name, parser));
wenzelm@24872
   154
  change_lexicons (apsnd (Scan.extend_lexicon [Symbol.explode name]));
wenzelm@24872
   155
  Pretty.writeln (report_command name (OuterKeyword.kind_of kind)));
wenzelm@5829
   156
wenzelm@24868
   157
fun command name comment kind parse =
wenzelm@24868
   158
  add_parser (name, make_parser comment kind NONE false parse);
wenzelm@5829
   159
wenzelm@24868
   160
fun markup_command markup name comment kind parse =
wenzelm@24868
   161
  add_parser (name, make_parser comment kind (SOME markup) false parse);
wenzelm@24868
   162
wenzelm@24868
   163
fun improper_command name comment kind parse =
wenzelm@24868
   164
  add_parser (name, make_parser comment kind NONE true parse);
wenzelm@7026
   165
wenzelm@7026
   166
end;
wenzelm@5829
   167
wenzelm@5829
   168
wenzelm@26990
   169
(* local_theory commands *)
wenzelm@26990
   170
wenzelm@26990
   171
fun local_theory_command do_print trans name comment kind parse =
wenzelm@26990
   172
  command name comment kind (P.opt_target -- parse
wenzelm@26990
   173
    >> (fn (loc, f) => (if do_print then Toplevel.print else I) o trans loc f));
wenzelm@26990
   174
wenzelm@26990
   175
val local_theory           = local_theory_command false Toplevel.local_theory;
wenzelm@26990
   176
val local_theory_to_proof' = local_theory_command true Toplevel.local_theory_to_proof';
wenzelm@26990
   177
val local_theory_to_proof  = local_theory_command true Toplevel.local_theory_to_proof;
wenzelm@26990
   178
wenzelm@26990
   179
wenzelm@24872
   180
(* inspect syntax *)
wenzelm@7026
   181
wenzelm@14687
   182
fun is_keyword s = Scan.is_literal (#1 (get_lexicons ())) (Symbol.explode s);
wenzelm@7026
   183
fun dest_keywords () = Scan.dest_lexicon (#1 (get_lexicons ()));
wenzelm@7026
   184
wenzelm@7026
   185
fun dest_parsers () =
wenzelm@16727
   186
  get_parsers () |> Symtab.dest |> sort_wrt #1
wenzelm@24868
   187
  |> map (fn (name, Parser {comment, kind, int_only, ...}) =>
wenzelm@24868
   188
    (name, comment, OuterKeyword.kind_of kind, int_only));
wenzelm@5829
   189
wenzelm@9223
   190
fun print_outer_syntax () =
wenzelm@7026
   191
  let
wenzelm@7026
   192
    fun pretty_cmd (name, comment, _, _) =
wenzelm@7026
   193
      Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
skalberg@15570
   194
    val (int_cmds, cmds) = List.partition #4 (dest_parsers ());
wenzelm@7026
   195
  in
wenzelm@8720
   196
    [Pretty.strs ("syntax keywords:" :: map quote (dest_keywords ())),
wenzelm@18326
   197
      Pretty.big_list "commands:" (map pretty_cmd cmds),
wenzelm@18326
   198
      Pretty.big_list "interactive-only commands:" (map pretty_cmd int_cmds)]
wenzelm@9223
   199
    |> Pretty.chunks |> Pretty.writeln
wenzelm@7026
   200
  end;
wenzelm@5829
   201
wenzelm@24872
   202
fun report () =
wenzelm@24872
   203
  (map report_keyword (dest_keywords ()) @
wenzelm@24872
   204
    map (fn (name, _, kind, _) => report_command name kind) (dest_parsers ()))
wenzelm@24872
   205
  |> Pretty.chunks |> Pretty.writeln;
wenzelm@7367
   206
wenzelm@5829
   207
wenzelm@5829
   208
wenzelm@9132
   209
(** toplevel parsing **)
wenzelm@5829
   210
wenzelm@9132
   211
(* basic sources *)
wenzelm@6860
   212
wenzelm@26620
   213
fun toplevel_source term do_recover cmd src =
wenzelm@9132
   214
  let
wenzelm@9132
   215
    val no_terminator =
wenzelm@9132
   216
      Scan.unless P.semicolon (Scan.one (T.not_sync andf T.not_eof));
wenzelm@23682
   217
    fun recover int =
wenzelm@23682
   218
      (int, fn _ => Scan.prompt "recover# " (Scan.repeat no_terminator) >> K [NONE]);
wenzelm@9132
   219
  in
wenzelm@9132
   220
    src
wenzelm@12876
   221
    |> T.source_proper
wenzelm@9132
   222
    |> Source.source T.stopper
skalberg@15531
   223
      (Scan.bulk (P.$$$ "--" -- P.!!! P.text >> K NONE || P.not_eof >> SOME))
wenzelm@23682
   224
        (Option.map recover do_recover)
wenzelm@19482
   225
    |> Source.map_filter I
wenzelm@24868
   226
    |> Source.source T.stopper
wenzelm@26620
   227
        (Scan.bulk (fn xs => P.!!! (parse_command term (cmd ())) xs))
wenzelm@23682
   228
        (Option.map recover do_recover)
wenzelm@19482
   229
    |> Source.map_filter I
wenzelm@9132
   230
  end;
wenzelm@5829
   231
wenzelm@7746
   232
wenzelm@25580
   233
(* off-line scanning/parsing *)
wenzelm@14925
   234
aspinall@15144
   235
fun scan str =
wenzelm@16195
   236
  Source.of_string str
wenzelm@16195
   237
  |> Symbol.source false
wenzelm@23679
   238
  |> T.source (SOME false) get_lexicons Position.none
wenzelm@16195
   239
  |> Source.exhaust;
wenzelm@16195
   240
wenzelm@25580
   241
fun parse pos str =
wenzelm@25580
   242
  Source.of_string str
wenzelm@25580
   243
  |> Symbol.source false
wenzelm@25580
   244
  |> T.source (SOME false) get_lexicons pos
wenzelm@26620
   245
  |> toplevel_source false NONE get_parser
wenzelm@25580
   246
  |> Source.exhaust;
wenzelm@25580
   247
nipkow@14091
   248
wenzelm@26431
   249
(* process file *)
wenzelm@26431
   250
wenzelm@26431
   251
fun process_file path thy =
wenzelm@26431
   252
  let
wenzelm@26431
   253
    val result = ref thy;
wenzelm@26881
   254
    val trs = parse (Path.position path) (File.read path);
wenzelm@26431
   255
    val init = Toplevel.init_theory (K thy) (fn thy' => result := thy') (K ());
wenzelm@26431
   256
    val _ = Toplevel.excursion (init Toplevel.empty :: trs @ [Toplevel.exit Toplevel.empty]);
wenzelm@26431
   257
  in ! result end;
wenzelm@26431
   258
wenzelm@26431
   259
wenzelm@24868
   260
(* interactive source of toplevel transformers *)
wenzelm@24868
   261
wenzelm@26600
   262
type isar =
wenzelm@26600
   263
  (Toplevel.transition, (Toplevel.transition option,
wenzelm@26600
   264
    (OuterLex.token, (OuterLex.token option, (OuterLex.token, (OuterLex.token,
wenzelm@26600
   265
      Position.T * (Symbol.symbol, (string, unit) Source.source) Source.source)
wenzelm@26600
   266
          Source.source) Source.source) Source.source) Source.source) Source.source) Source.source;
wenzelm@26600
   267
wenzelm@26600
   268
fun isar term : isar =
wenzelm@24868
   269
  Source.tty
wenzelm@24868
   270
  |> Symbol.source true
wenzelm@24868
   271
  |> T.source (SOME true) get_lexicons Position.none
wenzelm@26620
   272
  |> toplevel_source term (SOME true) get_parser;
wenzelm@24868
   273
wenzelm@24868
   274
wenzelm@16195
   275
wenzelm@9132
   276
(** read theory **)
wenzelm@6247
   277
wenzelm@12943
   278
(* check_text *)
wenzelm@12943
   279
wenzelm@22120
   280
fun check_text s state = (ThyOutput.eval_antiquote (#1 (get_lexicons ())) state s; ());
wenzelm@12943
   281
wenzelm@12943
   282
wenzelm@26611
   283
(* load_thy *)
wenzelm@7746
   284
wenzelm@26323
   285
fun load_thy dir name pos text time =
wenzelm@7683
   286
  let
wenzelm@24065
   287
    val text_src = Source.of_list (Library.untabify text);
wenzelm@23866
   288
wenzelm@17932
   289
    val _ = Present.init_theory name;
wenzelm@24065
   290
    val _ = Present.verbatim_source name (fn () => Source.exhaust (Symbol.source false text_src));
wenzelm@24065
   291
    val toks = text_src
wenzelm@17932
   292
      |> Symbol.source false
wenzelm@24065
   293
      |> T.source NONE (K (get_lexicons ())) pos
wenzelm@17932
   294
      |> Source.exhausted;
wenzelm@17932
   295
    val trs = toks
wenzelm@26620
   296
      |> toplevel_source false NONE (K (get_parser ()))
wenzelm@17932
   297
      |> Source.exhaust;
wenzelm@23866
   298
wenzelm@23866
   299
    val _ = if time then writeln ("\n**** Starting theory " ^ quote name ^ " ****") else ();
wenzelm@25685
   300
    val _ = cond_timeit time "" (fn () =>
wenzelm@23866
   301
      ThyOutput.process_thy (#1 (get_lexicons ())) command_tags is_markup trs toks
wenzelm@23866
   302
      |> Buffer.content
wenzelm@23866
   303
      |> Present.theory_output name);
wenzelm@23866
   304
    val _ = if time then writeln ("**** Finished theory " ^ quote name ^ " ****\n") else ();
wenzelm@24065
   305
  in () end;
wenzelm@23866
   306
wenzelm@5829
   307
end;
wenzelm@5829
   308