src/Pure/Isar/outer_syntax.ML
author boehmes
Wed, 12 May 2010 23:53:59 +0200
changeset 36895 a96f9793d9c5
parent 33223 d27956b4d3b4
child 36950 75b8f26f2f07
permissions -rw-r--r--
split monolithic Z3 proof reconstruction structure into separate structures, use one set of schematic theorems for all uncertain proof rules (to extend proof reconstruction by missing cases), added several schematic theorems, improved abstraction of goals (abstract all uninterpreted sub-terms, only leave builtin symbols)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Isar/outer_syntax.ML
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
     2
    Author:     Markus Wenzel, TU Muenchen
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
     3
27353
71c4dd53d4cb moved global keywords from OuterSyntax to OuterKeyword, tuned interfaces;
wenzelm
parents: 26990
diff changeset
     4
The global Isabelle/Isar outer syntax.
71c4dd53d4cb moved global keywords from OuterSyntax to OuterKeyword, tuned interfaces;
wenzelm
parents: 26990
diff changeset
     5
71c4dd53d4cb moved global keywords from OuterSyntax to OuterKeyword, tuned interfaces;
wenzelm
parents: 26990
diff changeset
     6
Note: the syntax for files is statically determined at the very
71c4dd53d4cb moved global keywords from OuterSyntax to OuterKeyword, tuned interfaces;
wenzelm
parents: 26990
diff changeset
     7
beginning; for interactive processing it may change dynamically.
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
     8
*)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
     9
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    10
signature OUTER_SYNTAX =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    11
sig
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    12
  val command: string -> string -> OuterKeyword.T ->
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    13
    (Toplevel.transition -> Toplevel.transition) parser -> unit
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    14
  val markup_command: ThyOutput.markup -> string -> string -> OuterKeyword.T ->
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    15
    (Toplevel.transition -> Toplevel.transition) parser -> unit
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    16
  val improper_command: string -> string -> OuterKeyword.T ->
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    17
    (Toplevel.transition -> Toplevel.transition) parser -> unit
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    18
  val internal_command: string -> (Toplevel.transition -> Toplevel.transition) parser -> unit
29380
a9ee3475abf4 added local_theory';
wenzelm
parents: 29315
diff changeset
    19
  val local_theory': string -> string -> OuterKeyword.T ->
a9ee3475abf4 added local_theory';
wenzelm
parents: 29315
diff changeset
    20
    (bool -> local_theory -> local_theory) parser -> unit
26990
a91f7741967a added local_theory command wrappers;
wenzelm
parents: 26881
diff changeset
    21
  val local_theory: string -> string -> OuterKeyword.T ->
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    22
    (local_theory -> local_theory) parser -> unit
26990
a91f7741967a added local_theory command wrappers;
wenzelm
parents: 26881
diff changeset
    23
  val local_theory_to_proof': string -> string -> OuterKeyword.T ->
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    24
    (bool -> local_theory -> Proof.state) parser -> unit
26990
a91f7741967a added local_theory command wrappers;
wenzelm
parents: 26881
diff changeset
    25
  val local_theory_to_proof: string -> string -> OuterKeyword.T ->
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    26
    (local_theory -> Proof.state) parser -> unit
5883
975c984526b0 tuned names;
wenzelm
parents: 5829
diff changeset
    27
  val print_outer_syntax: unit -> unit
27839
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
    28
  val scan: Position.T -> string -> OuterLex.token list
25580
6623674df897 added off-line parse;
wenzelm
parents: 25526
diff changeset
    29
  val parse: Position.T -> string -> Toplevel.transition list
26431
f1c79c00f1e4 added process_file;
wenzelm
parents: 26415
diff changeset
    30
  val process_file: Path.T -> theory -> theory
26600
f11515535c83 moved structure Isar to isar.ML;
wenzelm
parents: 26431
diff changeset
    31
  type isar
f11515535c83 moved structure Isar to isar.ML;
wenzelm
parents: 26431
diff changeset
    32
  val isar: bool -> isar
27839
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
    33
  val prepare_command: Position.T -> string -> Toplevel.transition
29428
3ab54b42ded8 load_thy: explicit after_load phase for presentation;
wenzelm
parents: 29380
diff changeset
    34
  val load_thy: string -> Position.T -> string list -> bool -> unit -> unit
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    35
end;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    36
26600
f11515535c83 moved structure Isar to isar.ML;
wenzelm
parents: 26431
diff changeset
    37
structure OuterSyntax: OUTER_SYNTAX =
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    38
struct
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    39
7750
e63416829538 added markup_command;
wenzelm
parents: 7746
diff changeset
    40
structure T = OuterLex;
6860
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
    41
structure P = OuterParse;
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    42
type 'a parser = 'a P.parser;
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    43
6860
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
    44
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    45
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    46
(** outer syntax **)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    47
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    48
(* command parsers *)
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    49
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    50
datatype command = Command of
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
    51
 {comment: string,
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
    52
  markup: ThyOutput.markup option,
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
    53
  int_only: bool,
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    54
  parse: (Toplevel.transition -> Toplevel.transition) parser};
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    55
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    56
fun make_command comment markup int_only parse =
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    57
  Command {comment = comment, markup = markup, int_only = int_only, parse = parse};
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    58
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    59
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    60
(* parse command *)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    61
6860
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
    62
local
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
    63
14925
0f86a8a694f8 added read (provides transition names and sources);
wenzelm
parents: 14687
diff changeset
    64
fun terminate false = Scan.succeed ()
0f86a8a694f8 added read (provides transition names and sources);
wenzelm
parents: 14687
diff changeset
    65
  | terminate true = P.group "end of input" (Scan.option P.sync -- P.semicolon >> K ());
0f86a8a694f8 added read (provides transition names and sources);
wenzelm
parents: 14687
diff changeset
    66
26620
722cf4fdd4dd eliminated unused trace, read;
wenzelm
parents: 26611
diff changeset
    67
fun body cmd (name, _) =
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
    68
  (case cmd name of
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    69
    SOME (Command {int_only, parse, ...}) =>
26620
722cf4fdd4dd eliminated unused trace, read;
wenzelm
parents: 26611
diff changeset
    70
      P.!!! (Scan.prompt (name ^ "# ") (P.tags |-- parse >> pair int_only))
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15224
diff changeset
    71
  | NONE => sys_error ("no parser for outer syntax command " ^ quote name));
6860
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
    72
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
    73
in
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    74
26620
722cf4fdd4dd eliminated unused trace, read;
wenzelm
parents: 26611
diff changeset
    75
fun parse_command do_terminate cmd =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15224
diff changeset
    76
  P.semicolon >> K NONE ||
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15224
diff changeset
    77
  P.sync >> K NONE ||
26620
722cf4fdd4dd eliminated unused trace, read;
wenzelm
parents: 26611
diff changeset
    78
  (P.position P.command :-- body cmd) --| terminate do_terminate
6860
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
    79
    >> (fn ((name, pos), (int_only, f)) =>
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15224
diff changeset
    80
      SOME (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |>
6860
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
    81
        Toplevel.interactive int_only |> f));
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    82
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
    83
end;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
    84
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    85
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    86
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
    87
(** global outer syntax **)
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    88
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
    89
local
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
    90
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 30573
diff changeset
    91
val global_commands = Unsynchronized.ref (Symtab.empty: command Symtab.table);
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 30573
diff changeset
    92
val global_markups = Unsynchronized.ref ([]: (string * ThyOutput.markup) list);
5952
7d4ec8992b23 added commands;
wenzelm
parents: 5923
diff changeset
    93
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    94
fun change_commands f = CRITICAL (fn () =>
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 30573
diff changeset
    95
 (Unsynchronized.change global_commands f;
23939
e543359fe8b6 marked some CRITICAL sections;
wenzelm
parents: 23884
diff changeset
    96
  global_markups :=
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    97
    Symtab.fold (fn (name, Command {markup = SOME m, ...}) => cons (name, m) | _ => I)
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    98
      (! global_commands) []));
6722
5e82c7196789 added keyword classification;
wenzelm
parents: 6685
diff changeset
    99
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   100
in
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   101
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   102
(* access current syntax *)
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   103
33223
d27956b4d3b4 non-critical atomic accesses;
wenzelm
parents: 32738
diff changeset
   104
fun get_commands () = ! global_commands;
d27956b4d3b4 non-critical atomic accesses;
wenzelm
parents: 32738
diff changeset
   105
fun get_markups () = ! global_markups;
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   106
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   107
fun get_command () = Symtab.lookup (get_commands ());
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   108
fun get_syntax () = CRITICAL (fn () => (OuterKeyword.get_lexicons (), get_command ()));
7789
57d20133224e verbatim markup tokens;
wenzelm
parents: 7774
diff changeset
   109
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   110
fun is_markup kind name = AList.lookup (op =) (get_markups ()) name = SOME kind;
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   111
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   112
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   113
(* augment syntax *)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   114
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   115
fun add_command name kind cmd = CRITICAL (fn () =>
27353
71c4dd53d4cb moved global keywords from OuterSyntax to OuterKeyword, tuned interfaces;
wenzelm
parents: 26990
diff changeset
   116
 (OuterKeyword.command name kind;
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   117
  if not (Symtab.defined (get_commands ()) name) then ()
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   118
  else warning ("Redefining outer syntax command " ^ quote name);
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   119
  change_commands (Symtab.update (name, cmd))));
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   120
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   121
fun command name comment kind parse =
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   122
  add_command name kind (make_command comment NONE false parse);
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   123
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   124
fun markup_command markup name comment kind parse =
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   125
  add_command name kind (make_command comment (SOME markup) false parse);
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   126
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   127
fun improper_command name comment kind parse =
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   128
  add_command name kind (make_command comment NONE true parse);
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   129
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   130
end;
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   131
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   132
fun internal_command name parse =
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   133
  command name "(internal)" OuterKeyword.control (parse >> (fn tr => Toplevel.no_timing o tr));
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   134
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   135
26990
a91f7741967a added local_theory command wrappers;
wenzelm
parents: 26881
diff changeset
   136
(* local_theory commands *)
a91f7741967a added local_theory command wrappers;
wenzelm
parents: 26881
diff changeset
   137
a91f7741967a added local_theory command wrappers;
wenzelm
parents: 26881
diff changeset
   138
fun local_theory_command do_print trans name comment kind parse =
a91f7741967a added local_theory command wrappers;
wenzelm
parents: 26881
diff changeset
   139
  command name comment kind (P.opt_target -- parse
a91f7741967a added local_theory command wrappers;
wenzelm
parents: 26881
diff changeset
   140
    >> (fn (loc, f) => (if do_print then Toplevel.print else I) o trans loc f));
a91f7741967a added local_theory command wrappers;
wenzelm
parents: 26881
diff changeset
   141
29380
a9ee3475abf4 added local_theory';
wenzelm
parents: 29315
diff changeset
   142
val local_theory' = local_theory_command false Toplevel.local_theory';
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   143
val local_theory = local_theory_command false Toplevel.local_theory;
26990
a91f7741967a added local_theory command wrappers;
wenzelm
parents: 26881
diff changeset
   144
val local_theory_to_proof' = local_theory_command true Toplevel.local_theory_to_proof';
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   145
val local_theory_to_proof = local_theory_command true Toplevel.local_theory_to_proof;
26990
a91f7741967a added local_theory command wrappers;
wenzelm
parents: 26881
diff changeset
   146
a91f7741967a added local_theory command wrappers;
wenzelm
parents: 26881
diff changeset
   147
24872
7fd1aa6671a4 report on keyword/command declarations;
wenzelm
parents: 24868
diff changeset
   148
(* inspect syntax *)
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   149
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   150
fun dest_commands () =
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   151
  get_commands () |> Symtab.dest |> sort_wrt #1
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   152
  |> map (fn (name, Command {comment, int_only, ...}) => (name, comment, int_only));
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   153
9223
eb752c2fac22 removed help;
wenzelm
parents: 9213
diff changeset
   154
fun print_outer_syntax () =
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   155
  let
27353
71c4dd53d4cb moved global keywords from OuterSyntax to OuterKeyword, tuned interfaces;
wenzelm
parents: 26990
diff changeset
   156
    fun pretty_cmd (name, comment, _) =
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   157
      Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   158
    val (int_cmds, cmds) = List.partition #3 (dest_commands ());
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   159
  in
27353
71c4dd53d4cb moved global keywords from OuterSyntax to OuterKeyword, tuned interfaces;
wenzelm
parents: 26990
diff changeset
   160
    [Pretty.strs ("syntax keywords:" :: map quote (OuterKeyword.dest_keywords ())),
18326
2f57579e618f tuned msg;
wenzelm
parents: 18064
diff changeset
   161
      Pretty.big_list "commands:" (map pretty_cmd cmds),
2f57579e618f tuned msg;
wenzelm
parents: 18064
diff changeset
   162
      Pretty.big_list "interactive-only commands:" (map pretty_cmd int_cmds)]
9223
eb752c2fac22 removed help;
wenzelm
parents: 9213
diff changeset
   163
    |> Pretty.chunks |> Pretty.writeln
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   164
  end;
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   165
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   166
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   167
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   168
(** toplevel parsing **)
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   169
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   170
(* basic sources *)
6860
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
   171
26620
722cf4fdd4dd eliminated unused trace, read;
wenzelm
parents: 26611
diff changeset
   172
fun toplevel_source term do_recover cmd src =
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   173
  let
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   174
    val no_terminator =
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   175
      Scan.unless P.semicolon (Scan.one (T.not_sync andf T.not_eof));
23682
cf4773532006 nested source: explicit interactive flag for recover avoids duplicate errors;
wenzelm
parents: 23679
diff changeset
   176
    fun recover int =
cf4773532006 nested source: explicit interactive flag for recover avoids duplicate errors;
wenzelm
parents: 23679
diff changeset
   177
      (int, fn _ => Scan.prompt "recover# " (Scan.repeat no_terminator) >> K [NONE]);
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   178
  in
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   179
    src
12876
a70df1e5bf10 got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents: 10749
diff changeset
   180
    |> T.source_proper
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   181
    |> Source.source T.stopper
27872
631371a02b8c P.doc_source and P.ml_sorce for proper SymbolPos.text;
wenzelm
parents: 27855
diff changeset
   182
      (Scan.bulk (P.$$$ "--" -- P.!!! P.doc_source >> K NONE || P.not_eof >> SOME))
23682
cf4773532006 nested source: explicit interactive flag for recover avoids duplicate errors;
wenzelm
parents: 23679
diff changeset
   183
        (Option.map recover do_recover)
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19060
diff changeset
   184
    |> Source.map_filter I
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   185
    |> Source.source T.stopper
26620
722cf4fdd4dd eliminated unused trace, read;
wenzelm
parents: 26611
diff changeset
   186
        (Scan.bulk (fn xs => P.!!! (parse_command term (cmd ())) xs))
23682
cf4773532006 nested source: explicit interactive flag for recover avoids duplicate errors;
wenzelm
parents: 23679
diff changeset
   187
        (Option.map recover do_recover)
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19060
diff changeset
   188
    |> Source.map_filter I
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   189
  end;
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   190
7746
b52c1a632e6a simplified;
wenzelm
parents: 7735
diff changeset
   191
25580
6623674df897 added off-line parse;
wenzelm
parents: 25526
diff changeset
   192
(* off-line scanning/parsing *)
14925
0f86a8a694f8 added read (provides transition names and sources);
wenzelm
parents: 14687
diff changeset
   193
27839
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   194
fun scan pos str =
16195
wenzelm
parents: 15989
diff changeset
   195
  Source.of_string str
27839
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   196
  |> Symbol.source {do_recover = false}
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   197
  |> T.source {do_recover = SOME false} OuterKeyword.get_lexicons pos
16195
wenzelm
parents: 15989
diff changeset
   198
  |> Source.exhaust;
wenzelm
parents: 15989
diff changeset
   199
25580
6623674df897 added off-line parse;
wenzelm
parents: 25526
diff changeset
   200
fun parse pos str =
6623674df897 added off-line parse;
wenzelm
parents: 25526
diff changeset
   201
  Source.of_string str
27839
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   202
  |> Symbol.source {do_recover = false}
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   203
  |> T.source {do_recover = SOME false} OuterKeyword.get_lexicons pos
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   204
  |> toplevel_source false NONE get_command
25580
6623674df897 added off-line parse;
wenzelm
parents: 25526
diff changeset
   205
  |> Source.exhaust;
6623674df897 added off-line parse;
wenzelm
parents: 25526
diff changeset
   206
14091
ad6ba9c55190 A patch by david aspinall
nipkow
parents: 12943
diff changeset
   207
26431
f1c79c00f1e4 added process_file;
wenzelm
parents: 26415
diff changeset
   208
(* process file *)
f1c79c00f1e4 added process_file;
wenzelm
parents: 26415
diff changeset
   209
f1c79c00f1e4 added process_file;
wenzelm
parents: 26415
diff changeset
   210
fun process_file path thy =
f1c79c00f1e4 added process_file;
wenzelm
parents: 26415
diff changeset
   211
  let
26881
bb68f50644a9 renamed Position.path to Path.position;
wenzelm
parents: 26700
diff changeset
   212
    val trs = parse (Path.position path) (File.read path);
28424
fc6ce1c4d5b7 simplified process_file, eliminated Toplevel.excursion;
wenzelm
parents: 27872
diff changeset
   213
    val init = Toplevel.init_theory "" (K thy) (K ()) Toplevel.empty;
fc6ce1c4d5b7 simplified process_file, eliminated Toplevel.excursion;
wenzelm
parents: 27872
diff changeset
   214
    val result = fold Toplevel.command (init :: trs) Toplevel.toplevel;
fc6ce1c4d5b7 simplified process_file, eliminated Toplevel.excursion;
wenzelm
parents: 27872
diff changeset
   215
  in
fc6ce1c4d5b7 simplified process_file, eliminated Toplevel.excursion;
wenzelm
parents: 27872
diff changeset
   216
    (case (Toplevel.is_theory result, Toplevel.generic_theory_of result) of
fc6ce1c4d5b7 simplified process_file, eliminated Toplevel.excursion;
wenzelm
parents: 27872
diff changeset
   217
      (true, Context.Theory thy') => thy'
fc6ce1c4d5b7 simplified process_file, eliminated Toplevel.excursion;
wenzelm
parents: 27872
diff changeset
   218
    | _ => error "Bad result state: global theory expected")
fc6ce1c4d5b7 simplified process_file, eliminated Toplevel.excursion;
wenzelm
parents: 27872
diff changeset
   219
  end;
26431
f1c79c00f1e4 added process_file;
wenzelm
parents: 26415
diff changeset
   220
f1c79c00f1e4 added process_file;
wenzelm
parents: 26415
diff changeset
   221
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   222
(* interactive source of toplevel transformers *)
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   223
26600
f11515535c83 moved structure Isar to isar.ML;
wenzelm
parents: 26431
diff changeset
   224
type isar =
f11515535c83 moved structure Isar to isar.ML;
wenzelm
parents: 26431
diff changeset
   225
  (Toplevel.transition, (Toplevel.transition option,
f11515535c83 moved structure Isar to isar.ML;
wenzelm
parents: 26431
diff changeset
   226
    (OuterLex.token, (OuterLex.token option, (OuterLex.token, (OuterLex.token,
30573
49899f26fbd1 de-camelized Symbol_Pos;
wenzelm
parents: 30513
diff changeset
   227
      (Symbol_Pos.T, Position.T * (Symbol.symbol, (string, unit) Source.source)
27770
10d84e124a2f updated type of nested sources;
wenzelm
parents: 27720
diff changeset
   228
  Source.source) Source.source) Source.source) Source.source)
10d84e124a2f updated type of nested sources;
wenzelm
parents: 27720
diff changeset
   229
  Source.source) Source.source) Source.source) Source.source;
26600
f11515535c83 moved structure Isar to isar.ML;
wenzelm
parents: 26431
diff changeset
   230
f11515535c83 moved structure Isar to isar.ML;
wenzelm
parents: 26431
diff changeset
   231
fun isar term : isar =
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   232
  Source.tty
27839
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   233
  |> Symbol.source {do_recover = true}
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   234
  |> T.source {do_recover = SOME true} OuterKeyword.get_lexicons Position.none
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   235
  |> toplevel_source term (SOME true) get_command;
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   236
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   237
27839
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   238
(* prepare toplevel commands -- fail-safe *)
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   239
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   240
val not_singleton = "Exactly one command expected";
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   241
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   242
fun prepare_span commands span =
27839
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   243
  let
29315
b074c05f00ad renamed ThyEdit (in thy_edit.ML) to ThySyntax (in thy_syntax.ML);
wenzelm
parents: 29311
diff changeset
   244
    val range_pos = Position.encode_range (ThySyntax.span_range span);
b074c05f00ad renamed ThyEdit (in thy_edit.ML) to ThySyntax (in thy_syntax.ML);
wenzelm
parents: 29311
diff changeset
   245
    val toks = ThySyntax.span_content span;
b074c05f00ad renamed ThyEdit (in thy_edit.ML) to ThySyntax (in thy_syntax.ML);
wenzelm
parents: 29311
diff changeset
   246
    val _ = List.app ThySyntax.report_token toks;
27839
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   247
  in
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   248
    (case Source.exhaust (toplevel_source false NONE (K commands) (Source.of_list toks)) of
27839
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   249
      [tr] => (tr, true)
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   250
    | [] => (Toplevel.ignored range_pos, false)
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   251
    | _ => (Toplevel.malformed range_pos not_singleton, true))
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   252
    handle ERROR msg => (Toplevel.malformed range_pos msg, true)
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   253
  end;
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   254
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   255
fun prepare_unit commands (cmd, proof, proper_proof) =
28436
4faf705a177d load_thy: more precise treatment of improper cmd or proof (notably 'oops');
wenzelm
parents: 28432
diff changeset
   256
  let
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   257
    val (tr, proper_cmd) = prepare_span commands cmd;
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   258
    val proof_trs = map (prepare_span commands) proof |> filter #2 |> map #1;
28436
4faf705a177d load_thy: more precise treatment of improper cmd or proof (notably 'oops');
wenzelm
parents: 28432
diff changeset
   259
  in
4faf705a177d load_thy: more precise treatment of improper cmd or proof (notably 'oops');
wenzelm
parents: 28432
diff changeset
   260
    if proper_cmd andalso proper_proof then [(tr, proof_trs)]
4faf705a177d load_thy: more precise treatment of improper cmd or proof (notably 'oops');
wenzelm
parents: 28432
diff changeset
   261
    else map (rpair []) (if proper_cmd then tr :: proof_trs else proof_trs)
4faf705a177d load_thy: more precise treatment of improper cmd or proof (notably 'oops');
wenzelm
parents: 28432
diff changeset
   262
  end;
28432
944cb67f809a load_thy: Toplevel.excursion based on units of command/proof pairs;
wenzelm
parents: 28424
diff changeset
   263
27839
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   264
fun prepare_command pos str =
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   265
  let val (lexs, commands) = get_syntax () in
29315
b074c05f00ad renamed ThyEdit (in thy_edit.ML) to ThySyntax (in thy_syntax.ML);
wenzelm
parents: 29311
diff changeset
   266
    (case ThySyntax.parse_spans lexs pos str of
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   267
      [span] => #1 (prepare_span commands span)
27839
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   268
    | _ => Toplevel.malformed pos not_singleton)
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   269
  end;
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   270
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   271
26611
03455add4801 export load_thy -- no backpatching;
wenzelm
parents: 26600
diff changeset
   272
(* load_thy *)
7746
b52c1a632e6a simplified;
wenzelm
parents: 7735
diff changeset
   273
27839
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   274
fun load_thy name pos text time =
7683
e74f43f1d8a3 export token_source;
wenzelm
parents: 7676
diff changeset
   275
  let
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   276
    val (lexs, commands) = get_syntax ();
23866
5295671034f8 moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents: 23796
diff changeset
   277
17932
677f7bec354e removed old-style theory format;
wenzelm
parents: 17412
diff changeset
   278
    val _ = Present.init_theory name;
27855
b1bf607e06c2 load_thy: no untabify (preserve position information!), present spans instead of verbatim source;
wenzelm
parents: 27839
diff changeset
   279
29315
b074c05f00ad renamed ThyEdit (in thy_edit.ML) to ThySyntax (in thy_syntax.ML);
wenzelm
parents: 29311
diff changeset
   280
    val toks = Source.exhausted (ThySyntax.token_source lexs pos (Source.of_list text));
b074c05f00ad renamed ThyEdit (in thy_edit.ML) to ThySyntax (in thy_syntax.ML);
wenzelm
parents: 29311
diff changeset
   281
    val spans = Source.exhaust (ThySyntax.span_source toks);
b074c05f00ad renamed ThyEdit (in thy_edit.ML) to ThySyntax (in thy_syntax.ML);
wenzelm
parents: 29311
diff changeset
   282
    val _ = List.app ThySyntax.report_span spans;
b074c05f00ad renamed ThyEdit (in thy_edit.ML) to ThySyntax (in thy_syntax.ML);
wenzelm
parents: 29311
diff changeset
   283
    val units = Source.exhaust (ThySyntax.unit_source (Source.of_list spans))
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   284
      |> maps (prepare_unit commands);
23866
5295671034f8 moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents: 23796
diff changeset
   285
27855
b1bf607e06c2 load_thy: no untabify (preserve position information!), present spans instead of verbatim source;
wenzelm
parents: 27839
diff changeset
   286
    val _ = Present.theory_source name
29315
b074c05f00ad renamed ThyEdit (in thy_edit.ML) to ThySyntax (in thy_syntax.ML);
wenzelm
parents: 29311
diff changeset
   287
      (fn () => HTML.html_mode (implode o map ThySyntax.present_span) spans);
27855
b1bf607e06c2 load_thy: no untabify (preserve position information!), present spans instead of verbatim source;
wenzelm
parents: 27839
diff changeset
   288
23866
5295671034f8 moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents: 23796
diff changeset
   289
    val _ = if time then writeln ("\n**** Starting theory " ^ quote name ^ " ****") else ();
29428
3ab54b42ded8 load_thy: explicit after_load phase for presentation;
wenzelm
parents: 29380
diff changeset
   290
    val results = cond_timeit time "" (fn () => Toplevel.excursion units);
23866
5295671034f8 moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents: 23796
diff changeset
   291
    val _ = if time then writeln ("**** Finished theory " ^ quote name ^ " ****\n") else ();
29428
3ab54b42ded8 load_thy: explicit after_load phase for presentation;
wenzelm
parents: 29380
diff changeset
   292
3ab54b42ded8 load_thy: explicit after_load phase for presentation;
wenzelm
parents: 29380
diff changeset
   293
    fun after_load () =
3ab54b42ded8 load_thy: explicit after_load phase for presentation;
wenzelm
parents: 29380
diff changeset
   294
      ThyOutput.present_thy (#1 lexs) OuterKeyword.command_tags is_markup (Lazy.force results) toks
3ab54b42ded8 load_thy: explicit after_load phase for presentation;
wenzelm
parents: 29380
diff changeset
   295
      |> Buffer.content
3ab54b42ded8 load_thy: explicit after_load phase for presentation;
wenzelm
parents: 29380
diff changeset
   296
      |> Present.theory_output name;
3ab54b42ded8 load_thy: explicit after_load phase for presentation;
wenzelm
parents: 29380
diff changeset
   297
  in after_load end;
23866
5295671034f8 moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents: 23796
diff changeset
   298
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   299
end;