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