src/Pure/Isar/outer_syntax.ML
author wenzelm
Tue, 10 Apr 2012 21:31:05 +0200
changeset 47416 df8fc0567a3d
parent 47014 e203b7d7e08d
child 48191 c1def7433a72
permissions -rw-r--r--
static relevance of proof via syntax keywords;
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
43711
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    12
  type outer_syntax
43712
3c2c912af2ef moved Outer_Syntax.load_thy to Thy_Load.load_thy;
wenzelm
parents: 43711
diff changeset
    13
  val is_markup: outer_syntax -> Thy_Output.markup -> string -> bool
43711
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    14
  val get_syntax: unit -> (Scan.lexicon * Scan.lexicon) * outer_syntax
46970
9667e0dcb5e2 check declared vs. defined commands at end of session;
wenzelm
parents: 46961
diff changeset
    15
  val check_syntax: unit -> unit
46961
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46958
diff changeset
    16
  type command_spec = string * Keyword.T
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46958
diff changeset
    17
  val command: command_spec -> string ->
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    18
    (Toplevel.transition -> Toplevel.transition) parser -> unit
46961
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46958
diff changeset
    19
  val markup_command: Thy_Output.markup -> command_spec -> string ->
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    20
    (Toplevel.transition -> Toplevel.transition) parser -> unit
46961
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46958
diff changeset
    21
  val improper_command: command_spec -> string ->
43711
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    22
    (Toplevel.transition -> Toplevel.transition) parser -> unit
46961
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46958
diff changeset
    23
  val local_theory': command_spec -> string ->
29380
a9ee3475abf4 added local_theory';
wenzelm
parents: 29315
diff changeset
    24
    (bool -> local_theory -> local_theory) parser -> unit
46961
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46958
diff changeset
    25
  val local_theory: command_spec -> string ->
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    26
    (local_theory -> local_theory) parser -> unit
46961
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46958
diff changeset
    27
  val local_theory_to_proof': command_spec -> string ->
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    28
    (bool -> local_theory -> Proof.state) parser -> unit
46961
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46958
diff changeset
    29
  val local_theory_to_proof: command_spec -> string ->
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    30
    (local_theory -> Proof.state) parser -> unit
5883
975c984526b0 tuned names;
wenzelm
parents: 5829
diff changeset
    31
  val print_outer_syntax: unit -> unit
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36955
diff changeset
    32
  val scan: Position.T -> string -> Token.T list
25580
6623674df897 added off-line parse;
wenzelm
parents: 25526
diff changeset
    33
  val parse: Position.T -> string -> Toplevel.transition list
26431
f1c79c00f1e4 added process_file;
wenzelm
parents: 26415
diff changeset
    34
  val process_file: Path.T -> theory -> theory
26600
f11515535c83 moved structure Isar to isar.ML;
wenzelm
parents: 26431
diff changeset
    35
  type isar
38253
3d4e521014f7 Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents: 38236
diff changeset
    36
  val isar: TextIO.instream -> bool -> isar
46876
8f3bb485f628 refined define_command vs. run_command: static tokenization vs. dynamic parsing, to increase the chance that the proper transaction is run after redefining commands (NB: requires slightly more space and time for document state);
wenzelm
parents: 44736
diff changeset
    37
  val read_span: outer_syntax -> Token.T list -> Toplevel.transition * bool
44478
4fdb1009a370 tuned signature -- emphasize traditional read/eval/print terminology, which is still relevant here;
wenzelm
parents: 44357
diff changeset
    38
  val read_element: outer_syntax -> (unit -> theory) -> Thy_Syntax.element ->
43712
3c2c912af2ef moved Outer_Syntax.load_thy to Thy_Load.load_thy;
wenzelm
parents: 43711
diff changeset
    39
    (Toplevel.transition * Toplevel.transition list) list
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    40
end;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    41
36953
2af1ad9aa1a3 renamed structure OuterSyntax to Outer_Syntax, keeping the old name as alias for some time;
wenzelm
parents: 36950
diff changeset
    42
structure Outer_Syntax: OUTER_SYNTAX =
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    43
struct
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    44
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    45
(** outer syntax **)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    46
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    47
(* command parsers *)
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    48
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    49
datatype command = Command of
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
    50
 {comment: string,
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 36959
diff changeset
    51
  markup: Thy_Output.markup option,
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
    52
  int_only: bool,
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    53
  parse: (Toplevel.transition -> Toplevel.transition) parser};
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    54
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    55
fun make_command comment markup int_only parse =
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    56
  Command {comment = comment, markup = markup, int_only = int_only, parse = parse};
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    57
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    58
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    59
(* parse command *)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    60
6860
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
    61
local
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
    62
14925
0f86a8a694f8 added read (provides transition names and sources);
wenzelm
parents: 14687
diff changeset
    63
fun terminate false = Scan.succeed ()
44357
5f5649ac8235 tuned Parse.group: delayed failure message;
wenzelm
parents: 44187
diff changeset
    64
  | terminate true =
5f5649ac8235 tuned Parse.group: delayed failure message;
wenzelm
parents: 44187
diff changeset
    65
      Parse.group (fn () => "end of input")
5f5649ac8235 tuned Parse.group: delayed failure message;
wenzelm
parents: 44187
diff changeset
    66
        (Scan.option Parse.sync -- Parse.semicolon >> K ());
14925
0f86a8a694f8 added read (provides transition names and sources);
wenzelm
parents: 14687
diff changeset
    67
26620
722cf4fdd4dd eliminated unused trace, read;
wenzelm
parents: 26611
diff changeset
    68
fun body cmd (name, _) =
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
    69
  (case cmd name of
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    70
    SOME (Command {int_only, parse, ...}) =>
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 33223
diff changeset
    71
      Parse.!!! (Scan.prompt (name ^ "# ") (Parse.tags |-- parse >> pair int_only))
37852
a902f158b4fc eliminated old-style sys_error/SYS_ERROR in favour of exception Fail -- after careful checking that there is no overlap with existing handling of that;
wenzelm
parents: 37713
diff changeset
    72
  | NONE => raise Fail ("No parser for outer syntax command " ^ quote name));
6860
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
    73
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
    74
in
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    75
26620
722cf4fdd4dd eliminated unused trace, read;
wenzelm
parents: 26611
diff changeset
    76
fun parse_command do_terminate cmd =
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 33223
diff changeset
    77
  Parse.semicolon >> K NONE ||
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 33223
diff changeset
    78
  Parse.sync >> K NONE ||
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 33223
diff changeset
    79
  (Parse.position Parse.command :-- body cmd) --| terminate do_terminate
6860
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
    80
    >> (fn ((name, pos), (int_only, f)) =>
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15224
diff changeset
    81
      SOME (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |>
6860
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
    82
        Toplevel.interactive int_only |> f));
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    83
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
    84
end;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
    85
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    86
43711
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    87
(* type outer_syntax *)
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    88
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    89
datatype outer_syntax = Outer_Syntax of
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    90
 {commands: command Symtab.table,
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    91
  markups: (string * Thy_Output.markup) list};
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    92
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    93
fun make_outer_syntax commands markups =
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    94
  Outer_Syntax {commands = commands, markups = markups};
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    95
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    96
val empty_outer_syntax = make_outer_syntax Symtab.empty [];
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    97
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    98
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    99
fun map_commands f (Outer_Syntax {commands, ...}) =
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   100
  let
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   101
    val commands' = f commands;
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   102
    val markups' =
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   103
      Symtab.fold (fn (name, Command {markup = SOME m, ...}) => cons (name, m) | _ => I)
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   104
        commands' [];
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   105
  in make_outer_syntax commands' markups' end;
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   106
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   107
fun dest_commands (Outer_Syntax {commands, ...}) =
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   108
  commands |> Symtab.dest |> sort_wrt #1
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   109
  |> map (fn (name, Command {comment, int_only, ...}) => (name, comment, int_only));
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   110
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   111
fun lookup_commands (Outer_Syntax {commands, ...}) = Symtab.lookup commands;
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   112
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   113
fun is_markup (Outer_Syntax {markups, ...}) kind name =
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   114
  AList.lookup (op =) markups name = SOME kind;
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   115
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   116
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   117
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   118
(** global outer syntax **)
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   119
46961
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46958
diff changeset
   120
type command_spec = string * Keyword.T;
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46958
diff changeset
   121
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   122
local
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   123
43711
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   124
(*synchronized wrt. Keywords*)
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   125
val global_outer_syntax = Unsynchronized.ref empty_outer_syntax;
5952
7d4ec8992b23 added commands;
wenzelm
parents: 5923
diff changeset
   126
46961
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46958
diff changeset
   127
fun add_command (name, kind) cmd = CRITICAL (fn () =>
46950
d0181abdbdac declare command keywords via theory header, including strict checking outside Pure;
wenzelm
parents: 46876
diff changeset
   128
  let
d0181abdbdac declare command keywords via theory header, including strict checking outside Pure;
wenzelm
parents: 46876
diff changeset
   129
    val thy = ML_Context.the_global_context ();
d0181abdbdac declare command keywords via theory header, including strict checking outside Pure;
wenzelm
parents: 46876
diff changeset
   130
    val _ =
d0181abdbdac declare command keywords via theory header, including strict checking outside Pure;
wenzelm
parents: 46876
diff changeset
   131
      (case try (Thy_Header.the_keyword thy) name of
46961
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46958
diff changeset
   132
        SOME spec =>
47014
e203b7d7e08d allow keyword tags to be redefined, but not the command category;
wenzelm
parents: 46970
diff changeset
   133
          if Option.map #1 spec = SOME (Keyword.kind_of kind) then ()
46950
d0181abdbdac declare command keywords via theory header, including strict checking outside Pure;
wenzelm
parents: 46876
diff changeset
   134
          else error ("Inconsistent outer syntax keyword declaration " ^ quote name)
d0181abdbdac declare command keywords via theory header, including strict checking outside Pure;
wenzelm
parents: 46876
diff changeset
   135
      | NONE =>
46961
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46958
diff changeset
   136
          if Context.theory_name thy = Context.PureN
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46958
diff changeset
   137
          then Keyword.define (name, SOME kind)
46957
0c15caf47040 clarified Keyword.is_keyword: union of minor and major;
wenzelm
parents: 46950
diff changeset
   138
          else error ("Undeclared outer syntax command " ^ quote name));
46950
d0181abdbdac declare command keywords via theory header, including strict checking outside Pure;
wenzelm
parents: 46876
diff changeset
   139
  in
d0181abdbdac declare command keywords via theory header, including strict checking outside Pure;
wenzelm
parents: 46876
diff changeset
   140
    Unsynchronized.change global_outer_syntax (map_commands (fn commands =>
d0181abdbdac declare command keywords via theory header, including strict checking outside Pure;
wenzelm
parents: 46876
diff changeset
   141
     (if not (Symtab.defined commands name) then ()
d0181abdbdac declare command keywords via theory header, including strict checking outside Pure;
wenzelm
parents: 46876
diff changeset
   142
      else warning ("Redefining outer syntax command " ^ quote name);
d0181abdbdac declare command keywords via theory header, including strict checking outside Pure;
wenzelm
parents: 46876
diff changeset
   143
      Symtab.update (name, cmd) commands)))
d0181abdbdac declare command keywords via theory header, including strict checking outside Pure;
wenzelm
parents: 46876
diff changeset
   144
  end);
6722
5e82c7196789 added keyword classification;
wenzelm
parents: 6685
diff changeset
   145
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   146
in
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   147
43711
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   148
fun get_syntax () = CRITICAL (fn () => (Keyword.get_lexicons (), ! global_outer_syntax));
7789
57d20133224e verbatim markup tokens;
wenzelm
parents: 7774
diff changeset
   149
46970
9667e0dcb5e2 check declared vs. defined commands at end of session;
wenzelm
parents: 46961
diff changeset
   150
fun check_syntax () =
9667e0dcb5e2 check declared vs. defined commands at end of session;
wenzelm
parents: 46961
diff changeset
   151
  let
9667e0dcb5e2 check declared vs. defined commands at end of session;
wenzelm
parents: 46961
diff changeset
   152
    val ((_, major), syntax) = CRITICAL (fn () => (Keyword.dest (), ! global_outer_syntax));
9667e0dcb5e2 check declared vs. defined commands at end of session;
wenzelm
parents: 46961
diff changeset
   153
  in
9667e0dcb5e2 check declared vs. defined commands at end of session;
wenzelm
parents: 46961
diff changeset
   154
    (case subtract (op =) (map #1 (dest_commands syntax)) major of
9667e0dcb5e2 check declared vs. defined commands at end of session;
wenzelm
parents: 46961
diff changeset
   155
      [] => ()
9667e0dcb5e2 check declared vs. defined commands at end of session;
wenzelm
parents: 46961
diff changeset
   156
    | missing => error ("Missing outer syntax command(s) " ^ commas_quote missing))
9667e0dcb5e2 check declared vs. defined commands at end of session;
wenzelm
parents: 46961
diff changeset
   157
  end;
9667e0dcb5e2 check declared vs. defined commands at end of session;
wenzelm
parents: 46961
diff changeset
   158
43711
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   159
fun lookup_commands_dynamic () = lookup_commands (! global_outer_syntax);
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   160
46961
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46958
diff changeset
   161
fun command command_spec comment parse =
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46958
diff changeset
   162
  add_command command_spec (make_command comment NONE false parse);
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   163
46961
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46958
diff changeset
   164
fun markup_command markup command_spec comment parse =
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46958
diff changeset
   165
  add_command command_spec (make_command comment (SOME markup) false parse);
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   166
46961
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46958
diff changeset
   167
fun improper_command command_spec comment parse =
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46958
diff changeset
   168
  add_command command_spec (make_command comment NONE true parse);
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   169
43711
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   170
end;
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   171
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   172
26990
a91f7741967a added local_theory command wrappers;
wenzelm
parents: 26881
diff changeset
   173
(* local_theory commands *)
a91f7741967a added local_theory command wrappers;
wenzelm
parents: 26881
diff changeset
   174
46961
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46958
diff changeset
   175
fun local_theory_command do_print trans command_spec comment parse =
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46958
diff changeset
   176
  command command_spec comment (Parse.opt_target -- parse
26990
a91f7741967a added local_theory command wrappers;
wenzelm
parents: 26881
diff changeset
   177
    >> (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
   178
29380
a9ee3475abf4 added local_theory';
wenzelm
parents: 29315
diff changeset
   179
val local_theory' = local_theory_command false Toplevel.local_theory';
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   180
val local_theory = local_theory_command false Toplevel.local_theory;
26990
a91f7741967a added local_theory command wrappers;
wenzelm
parents: 26881
diff changeset
   181
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
   182
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
   183
a91f7741967a added local_theory command wrappers;
wenzelm
parents: 26881
diff changeset
   184
24872
7fd1aa6671a4 report on keyword/command declarations;
wenzelm
parents: 24868
diff changeset
   185
(* inspect syntax *)
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   186
9223
eb752c2fac22 removed help;
wenzelm
parents: 9213
diff changeset
   187
fun print_outer_syntax () =
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   188
  let
46957
0c15caf47040 clarified Keyword.is_keyword: union of minor and major;
wenzelm
parents: 46950
diff changeset
   189
    val ((keywords, _), outer_syntax) =
0c15caf47040 clarified Keyword.is_keyword: union of minor and major;
wenzelm
parents: 46950
diff changeset
   190
      CRITICAL (fn () => (Keyword.dest (), #2 (get_syntax ())));
27353
71c4dd53d4cb moved global keywords from OuterSyntax to OuterKeyword, tuned interfaces;
wenzelm
parents: 26990
diff changeset
   191
    fun pretty_cmd (name, comment, _) =
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   192
      Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
43711
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   193
    val (int_cmds, cmds) = List.partition #3 (dest_commands outer_syntax);
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   194
  in
43711
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   195
    [Pretty.strs ("syntax keywords:" :: map quote keywords),
18326
2f57579e618f tuned msg;
wenzelm
parents: 18064
diff changeset
   196
      Pretty.big_list "commands:" (map pretty_cmd cmds),
2f57579e618f tuned msg;
wenzelm
parents: 18064
diff changeset
   197
      Pretty.big_list "interactive-only commands:" (map pretty_cmd int_cmds)]
9223
eb752c2fac22 removed help;
wenzelm
parents: 9213
diff changeset
   198
    |> Pretty.chunks |> Pretty.writeln
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   199
  end;
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   200
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   201
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   202
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   203
(** toplevel parsing **)
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   204
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   205
(* basic sources *)
6860
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
   206
26620
722cf4fdd4dd eliminated unused trace, read;
wenzelm
parents: 26611
diff changeset
   207
fun toplevel_source term do_recover cmd src =
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   208
  let
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   209
    val no_terminator =
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36955
diff changeset
   210
      Scan.unless Parse.semicolon (Scan.one (Token.not_sync andf Token.not_eof));
23682
cf4773532006 nested source: explicit interactive flag for recover avoids duplicate errors;
wenzelm
parents: 23679
diff changeset
   211
    fun recover int =
cf4773532006 nested source: explicit interactive flag for recover avoids duplicate errors;
wenzelm
parents: 23679
diff changeset
   212
      (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
   213
  in
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   214
    src
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36955
diff changeset
   215
    |> Token.source_proper
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36955
diff changeset
   216
    |> Source.source Token.stopper
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 33223
diff changeset
   217
      (Scan.bulk (Parse.$$$ "--" -- Parse.!!! Parse.doc_source >> K NONE || Parse.not_eof >> SOME))
23682
cf4773532006 nested source: explicit interactive flag for recover avoids duplicate errors;
wenzelm
parents: 23679
diff changeset
   218
        (Option.map recover do_recover)
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19060
diff changeset
   219
    |> Source.map_filter I
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36955
diff changeset
   220
    |> Source.source Token.stopper
36950
75b8f26f2f07 refer directly to structure Keyword and Parse;
wenzelm
parents: 33223
diff changeset
   221
        (Scan.bulk (fn xs => Parse.!!! (parse_command term (cmd ())) xs))
23682
cf4773532006 nested source: explicit interactive flag for recover avoids duplicate errors;
wenzelm
parents: 23679
diff changeset
   222
        (Option.map recover do_recover)
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19060
diff changeset
   223
    |> Source.map_filter I
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   224
  end;
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   225
7746
b52c1a632e6a simplified;
wenzelm
parents: 7735
diff changeset
   226
25580
6623674df897 added off-line parse;
wenzelm
parents: 25526
diff changeset
   227
(* off-line scanning/parsing *)
14925
0f86a8a694f8 added read (provides transition names and sources);
wenzelm
parents: 14687
diff changeset
   228
27839
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   229
fun scan pos str =
16195
wenzelm
parents: 15989
diff changeset
   230
  Source.of_string str
40523
1050315f6ee2 simplified/robustified treatment of malformed symbols, which are now fully internalized (total Symbol.explode etc.);
wenzelm
parents: 38422
diff changeset
   231
  |> Symbol.source
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36955
diff changeset
   232
  |> Token.source {do_recover = SOME false} Keyword.get_lexicons pos
16195
wenzelm
parents: 15989
diff changeset
   233
  |> Source.exhaust;
wenzelm
parents: 15989
diff changeset
   234
25580
6623674df897 added off-line parse;
wenzelm
parents: 25526
diff changeset
   235
fun parse pos str =
6623674df897 added off-line parse;
wenzelm
parents: 25526
diff changeset
   236
  Source.of_string str
40523
1050315f6ee2 simplified/robustified treatment of malformed symbols, which are now fully internalized (total Symbol.explode etc.);
wenzelm
parents: 38422
diff changeset
   237
  |> Symbol.source
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36955
diff changeset
   238
  |> Token.source {do_recover = SOME false} Keyword.get_lexicons pos
43711
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   239
  |> toplevel_source false NONE lookup_commands_dynamic
25580
6623674df897 added off-line parse;
wenzelm
parents: 25526
diff changeset
   240
  |> Source.exhaust;
6623674df897 added off-line parse;
wenzelm
parents: 25526
diff changeset
   241
14091
ad6ba9c55190 A patch by david aspinall
nipkow
parents: 12943
diff changeset
   242
26431
f1c79c00f1e4 added process_file;
wenzelm
parents: 26415
diff changeset
   243
(* process file *)
f1c79c00f1e4 added process_file;
wenzelm
parents: 26415
diff changeset
   244
f1c79c00f1e4 added process_file;
wenzelm
parents: 26415
diff changeset
   245
fun process_file path thy =
f1c79c00f1e4 added process_file;
wenzelm
parents: 26415
diff changeset
   246
  let
26881
bb68f50644a9 renamed Position.path to Path.position;
wenzelm
parents: 26700
diff changeset
   247
    val trs = parse (Path.position path) (File.read path);
44187
88d770052bac simplified Toplevel.init_theory: discontinued special name argument;
wenzelm
parents: 44186
diff changeset
   248
    val init = Toplevel.init_theory (K thy) Toplevel.empty;
28424
fc6ce1c4d5b7 simplified process_file, eliminated Toplevel.excursion;
wenzelm
parents: 27872
diff changeset
   249
    val result = fold Toplevel.command (init :: trs) Toplevel.toplevel;
fc6ce1c4d5b7 simplified process_file, eliminated Toplevel.excursion;
wenzelm
parents: 27872
diff changeset
   250
  in
fc6ce1c4d5b7 simplified process_file, eliminated Toplevel.excursion;
wenzelm
parents: 27872
diff changeset
   251
    (case (Toplevel.is_theory result, Toplevel.generic_theory_of result) of
fc6ce1c4d5b7 simplified process_file, eliminated Toplevel.excursion;
wenzelm
parents: 27872
diff changeset
   252
      (true, Context.Theory thy') => thy'
fc6ce1c4d5b7 simplified process_file, eliminated Toplevel.excursion;
wenzelm
parents: 27872
diff changeset
   253
    | _ => error "Bad result state: global theory expected")
fc6ce1c4d5b7 simplified process_file, eliminated Toplevel.excursion;
wenzelm
parents: 27872
diff changeset
   254
  end;
26431
f1c79c00f1e4 added process_file;
wenzelm
parents: 26415
diff changeset
   255
f1c79c00f1e4 added process_file;
wenzelm
parents: 26415
diff changeset
   256
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   257
(* interactive source of toplevel transformers *)
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   258
26600
f11515535c83 moved structure Isar to isar.ML;
wenzelm
parents: 26431
diff changeset
   259
type isar =
f11515535c83 moved structure Isar to isar.ML;
wenzelm
parents: 26431
diff changeset
   260
  (Toplevel.transition, (Toplevel.transition option,
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36955
diff changeset
   261
    (Token.T, (Token.T option, (Token.T, (Token.T,
30573
49899f26fbd1 de-camelized Symbol_Pos;
wenzelm
parents: 30513
diff changeset
   262
      (Symbol_Pos.T, Position.T * (Symbol.symbol, (string, unit) Source.source)
27770
10d84e124a2f updated type of nested sources;
wenzelm
parents: 27720
diff changeset
   263
  Source.source) Source.source) Source.source) Source.source)
10d84e124a2f updated type of nested sources;
wenzelm
parents: 27720
diff changeset
   264
  Source.source) Source.source) Source.source) Source.source;
26600
f11515535c83 moved structure Isar to isar.ML;
wenzelm
parents: 26431
diff changeset
   265
38253
3d4e521014f7 Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents: 38236
diff changeset
   266
fun isar in_stream term : isar =
3d4e521014f7 Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents: 38236
diff changeset
   267
  Source.tty in_stream
40523
1050315f6ee2 simplified/robustified treatment of malformed symbols, which are now fully internalized (total Symbol.explode etc.);
wenzelm
parents: 38422
diff changeset
   268
  |> Symbol.source
36959
f5417836dbea renamed structure OuterLex to Token and type token to Token.T, keeping legacy aliases for some time;
wenzelm
parents: 36955
diff changeset
   269
  |> Token.source {do_recover = SOME true} Keyword.get_lexicons Position.none
43711
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   270
  |> toplevel_source term (SOME true) lookup_commands_dynamic;
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   271
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   272
44478
4fdb1009a370 tuned signature -- emphasize traditional read/eval/print terminology, which is still relevant here;
wenzelm
parents: 44357
diff changeset
   273
(* read toplevel commands -- fail-safe *)
27839
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   274
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   275
val not_singleton = "Exactly one command expected";
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   276
44658
5bec9c15ef29 more direct Token.range_pos and Outer_Syntax.read_command, bypassing Thy_Syntax.span;
wenzelm
parents: 44478
diff changeset
   277
fun read_span outer_syntax toks =
27839
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   278
  let
43711
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   279
    val commands = lookup_commands outer_syntax;
44658
5bec9c15ef29 more direct Token.range_pos and Outer_Syntax.read_command, bypassing Thy_Syntax.span;
wenzelm
parents: 44478
diff changeset
   280
    val range_pos = Position.set_range (Token.range toks);
44736
c2a3f1c84179 bulk reports for improved message throughput;
wenzelm
parents: 44659
diff changeset
   281
    val _ = Position.reports (maps Thy_Syntax.reports_of_token toks);
27839
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   282
  in
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   283
    (case Source.exhaust (toplevel_source false NONE (K commands) (Source.of_list toks)) of
37713
c82cf6e11669 Outer_Syntax.prepare_command: disallow control commands here, and consequently in Isar documents;
wenzelm
parents: 37216
diff changeset
   284
      [tr] =>
c82cf6e11669 Outer_Syntax.prepare_command: disallow control commands here, and consequently in Isar documents;
wenzelm
parents: 37216
diff changeset
   285
        if Keyword.is_control (Toplevel.name_of tr) then
44658
5bec9c15ef29 more direct Token.range_pos and Outer_Syntax.read_command, bypassing Thy_Syntax.span;
wenzelm
parents: 44478
diff changeset
   286
          (Toplevel.malformed (Toplevel.pos_of tr) "Illegal control command", true)
37713
c82cf6e11669 Outer_Syntax.prepare_command: disallow control commands here, and consequently in Isar documents;
wenzelm
parents: 37216
diff changeset
   287
        else (tr, true)
27839
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   288
    | [] => (Toplevel.ignored range_pos, false)
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   289
    | _ => (Toplevel.malformed range_pos not_singleton, true))
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   290
    handle ERROR msg => (Toplevel.malformed range_pos msg, true)
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   291
  end;
0be8644c45eb Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents: 27831
diff changeset
   292
44478
4fdb1009a370 tuned signature -- emphasize traditional read/eval/print terminology, which is still relevant here;
wenzelm
parents: 44357
diff changeset
   293
fun read_element outer_syntax init {head, proof, proper_proof} =
28436
4faf705a177d load_thy: more precise treatment of improper cmd or proof (notably 'oops');
wenzelm
parents: 28432
diff changeset
   294
  let
44658
5bec9c15ef29 more direct Token.range_pos and Outer_Syntax.read_command, bypassing Thy_Syntax.span;
wenzelm
parents: 44478
diff changeset
   295
    val read = read_span outer_syntax o Thy_Syntax.span_content;
5bec9c15ef29 more direct Token.range_pos and Outer_Syntax.read_command, bypassing Thy_Syntax.span;
wenzelm
parents: 44478
diff changeset
   296
    val (tr, proper_head) = read head |>> Toplevel.modify_init init;
5bec9c15ef29 more direct Token.range_pos and Outer_Syntax.read_command, bypassing Thy_Syntax.span;
wenzelm
parents: 44478
diff changeset
   297
    val proof_trs = map read proof |> filter #2 |> map #1;
28436
4faf705a177d load_thy: more precise treatment of improper cmd or proof (notably 'oops');
wenzelm
parents: 28432
diff changeset
   298
  in
47416
df8fc0567a3d static relevance of proof via syntax keywords;
wenzelm
parents: 47014
diff changeset
   299
    if proper_head andalso proper_proof andalso
df8fc0567a3d static relevance of proof via syntax keywords;
wenzelm
parents: 47014
diff changeset
   300
      not (Keyword.is_schematic_goal (Toplevel.name_of tr)) then [(tr, proof_trs)]
43621
e8858190cccd clarified Thy_Syntax.element;
wenzelm
parents: 42129
diff changeset
   301
    else map (rpair []) (if proper_head then tr :: proof_trs else proof_trs)
28436
4faf705a177d load_thy: more precise treatment of improper cmd or proof (notably 'oops');
wenzelm
parents: 28432
diff changeset
   302
  end;
28432
944cb67f809a load_thy: Toplevel.excursion based on units of command/proof pairs;
wenzelm
parents: 28424
diff changeset
   303
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   304
end;
36953
2af1ad9aa1a3 renamed structure OuterSyntax to Outer_Syntax, keeping the old name as alias for some time;
wenzelm
parents: 36950
diff changeset
   305