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