src/Pure/Isar/outer_syntax.ML
author wenzelm
Thu, 06 Nov 2014 11:44:41 +0100
changeset 58918 8d36bc5eaed3
parent 58908 58bedbc18915
child 58928 23d0ffd48006
permissions -rw-r--r--
simplified keyword kinds; more explicit bootstrap syntax;
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
58862
63a16e98cc14 tuned signature, in accordance to Scala version;
wenzelm
parents: 58861
diff changeset
    12
  type syntax
55448
e42a3fc18458 explicit indication that redefining outer syntax commands is not supposed to happen -- NB: interactive mode requires global change of syntax;
wenzelm
parents: 54734
diff changeset
    13
  val batch_mode: bool Unsynchronized.ref
58862
63a16e98cc14 tuned signature, in accordance to Scala version;
wenzelm
parents: 58861
diff changeset
    14
  val is_markup: syntax -> Thy_Output.markup -> string -> bool
58903
38c72f5f6c2e explicit type Keyword.keywords;
wenzelm
parents: 58893
diff changeset
    15
  val get_syntax: unit -> Keyword.keywords * syntax
46970
9667e0dcb5e2 check declared vs. defined commands at end of session;
wenzelm
parents: 46961
diff changeset
    16
  val check_syntax: unit -> unit
48646
91281e9472d8 more official command specifications, including source position;
wenzelm
parents: 48638
diff changeset
    17
  type command_spec = (string * Keyword.T) * Position.T
46961
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46958
diff changeset
    18
  val command: command_spec -> string ->
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    19
    (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
    20
  val markup_command: Thy_Output.markup -> command_spec -> string ->
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    21
    (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
    22
  val local_theory': command_spec -> string ->
29380
a9ee3475abf4 added local_theory';
wenzelm
parents: 29315
diff changeset
    23
    (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
    24
  val local_theory: command_spec -> string ->
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    25
    (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
    26
  val local_theory_to_proof': command_spec -> string ->
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    27
    (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
    28
  val local_theory_to_proof: command_spec -> string ->
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    29
    (local_theory -> Proof.state) parser -> unit
58862
63a16e98cc14 tuned signature, in accordance to Scala version;
wenzelm
parents: 58861
diff changeset
    30
  val help_syntax: string list -> unit
63a16e98cc14 tuned signature, in accordance to Scala version;
wenzelm
parents: 58861
diff changeset
    31
  val print_syntax: unit -> unit
58903
38c72f5f6c2e explicit type Keyword.keywords;
wenzelm
parents: 58893
diff changeset
    32
  val scan: Keyword.keywords -> Position.T -> string -> Token.T list
38c72f5f6c2e explicit type Keyword.keywords;
wenzelm
parents: 58893
diff changeset
    33
  val parse: Keyword.keywords * syntax -> Position.T -> string -> Toplevel.transition list
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
    34
  val parse_spans: Token.T list -> Command_Span.span list
52510
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
    35
  val side_comments: Token.T list -> Token.T list
58862
63a16e98cc14 tuned signature, in accordance to Scala version;
wenzelm
parents: 58861
diff changeset
    36
  val command_reports: syntax -> Token.T -> Position.report_text list
63a16e98cc14 tuned signature, in accordance to Scala version;
wenzelm
parents: 58861
diff changeset
    37
  val read_spans: syntax -> Token.T list -> Toplevel.transition list
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    38
end;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    39
36953
2af1ad9aa1a3 renamed structure OuterSyntax to Outer_Syntax, keeping the old name as alias for some time;
wenzelm
parents: 36950
diff changeset
    40
structure Outer_Syntax: OUTER_SYNTAX =
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    41
struct
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    42
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    43
(** outer syntax **)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    44
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
    45
(* command parsers *)
5829
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
datatype command = Command of
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
    48
 {comment: string,
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 36959
diff changeset
    49
  markup: Thy_Output.markup option,
48647
a5144c4c26a2 report commands as formal entities, with def/ref positions;
wenzelm
parents: 48646
diff changeset
    50
  parse: (Toplevel.transition -> Toplevel.transition) parser,
a5144c4c26a2 report commands as formal entities, with def/ref positions;
wenzelm
parents: 48646
diff changeset
    51
  pos: Position.T,
a5144c4c26a2 report commands as formal entities, with def/ref positions;
wenzelm
parents: 48646
diff changeset
    52
  id: serial};
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    53
58893
9e0ecb66d6a7 eliminated unused int_only flag (see also c12484a27367);
wenzelm
parents: 58864
diff changeset
    54
fun new_command comment markup parse pos =
9e0ecb66d6a7 eliminated unused int_only flag (see also c12484a27367);
wenzelm
parents: 58864
diff changeset
    55
  Command {comment = comment, markup = markup, parse = parse, pos = pos, id = serial ()};
48647
a5144c4c26a2 report commands as formal entities, with def/ref positions;
wenzelm
parents: 48646
diff changeset
    56
a5144c4c26a2 report commands as formal entities, with def/ref positions;
wenzelm
parents: 48646
diff changeset
    57
fun command_markup def (name, Command {pos, id, ...}) =
a5144c4c26a2 report commands as formal entities, with def/ref positions;
wenzelm
parents: 48646
diff changeset
    58
  Markup.properties (Position.entity_properties_of def id pos)
50201
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 49564
diff changeset
    59
    (Markup.entity Markup.commandN name);
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    60
50213
7b73c0509835 refined outer syntax 'help' command;
wenzelm
parents: 50201
diff changeset
    61
fun pretty_command (cmd as (name, Command {comment, ...})) =
7b73c0509835 refined outer syntax 'help' command;
wenzelm
parents: 50201
diff changeset
    62
  Pretty.block
7b73c0509835 refined outer syntax 'help' command;
wenzelm
parents: 50201
diff changeset
    63
    (Pretty.marks_str
50450
358b6020f8b6 generalized notion of active area, where sendback is just one application;
wenzelm
parents: 50215
diff changeset
    64
      ([Active.make_markup Markup.sendbackN {implicit = true, properties = [Markup.padding_line]},
50215
97959912840a more general sendback properties;
wenzelm
parents: 50213
diff changeset
    65
        command_markup false cmd], name) :: Pretty.str ":" :: Pretty.brk 2 :: Pretty.text comment);
50213
7b73c0509835 refined outer syntax 'help' command;
wenzelm
parents: 50201
diff changeset
    66
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    67
58862
63a16e98cc14 tuned signature, in accordance to Scala version;
wenzelm
parents: 58861
diff changeset
    68
(* type syntax *)
43711
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    69
58862
63a16e98cc14 tuned signature, in accordance to Scala version;
wenzelm
parents: 58861
diff changeset
    70
datatype syntax = Syntax of
43711
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    71
 {commands: command Symtab.table,
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    72
  markups: (string * Thy_Output.markup) list};
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    73
58862
63a16e98cc14 tuned signature, in accordance to Scala version;
wenzelm
parents: 58861
diff changeset
    74
fun make_syntax commands markups =
63a16e98cc14 tuned signature, in accordance to Scala version;
wenzelm
parents: 58861
diff changeset
    75
  Syntax {commands = commands, markups = markups};
43711
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    76
58862
63a16e98cc14 tuned signature, in accordance to Scala version;
wenzelm
parents: 58861
diff changeset
    77
val empty_syntax = make_syntax Symtab.empty [];
43711
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    78
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    79
58862
63a16e98cc14 tuned signature, in accordance to Scala version;
wenzelm
parents: 58861
diff changeset
    80
fun map_commands f (Syntax {commands, ...}) =
43711
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    81
  let
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    82
    val commands' = f commands;
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    83
    val markups' =
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    84
      Symtab.fold (fn (name, Command {markup = SOME m, ...}) => cons (name, m) | _ => I)
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    85
        commands' [];
58862
63a16e98cc14 tuned signature, in accordance to Scala version;
wenzelm
parents: 58861
diff changeset
    86
  in make_syntax commands' markups' end;
43711
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    87
58862
63a16e98cc14 tuned signature, in accordance to Scala version;
wenzelm
parents: 58861
diff changeset
    88
fun dest_commands (Syntax {commands, ...}) =
50213
7b73c0509835 refined outer syntax 'help' command;
wenzelm
parents: 50201
diff changeset
    89
  commands |> Symtab.dest |> sort_wrt #1;
43711
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    90
58862
63a16e98cc14 tuned signature, in accordance to Scala version;
wenzelm
parents: 58861
diff changeset
    91
fun lookup_commands (Syntax {commands, ...}) = Symtab.lookup commands;
43711
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    92
58862
63a16e98cc14 tuned signature, in accordance to Scala version;
wenzelm
parents: 58861
diff changeset
    93
fun is_markup (Syntax {markups, ...}) kind name =
43711
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    94
  AList.lookup (op =) markups name = SOME kind;
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    95
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
    96
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    97
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
    98
(** global outer syntax **)
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    99
48646
91281e9472d8 more official command specifications, including source position;
wenzelm
parents: 48638
diff changeset
   100
type command_spec = (string * Keyword.T) * Position.T;
46961
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46958
diff changeset
   101
55448
e42a3fc18458 explicit indication that redefining outer syntax commands is not supposed to happen -- NB: interactive mode requires global change of syntax;
wenzelm
parents: 54734
diff changeset
   102
val batch_mode = Unsynchronized.ref false;
e42a3fc18458 explicit indication that redefining outer syntax commands is not supposed to happen -- NB: interactive mode requires global change of syntax;
wenzelm
parents: 54734
diff changeset
   103
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   104
local
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   105
43711
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   106
(*synchronized wrt. Keywords*)
58862
63a16e98cc14 tuned signature, in accordance to Scala version;
wenzelm
parents: 58861
diff changeset
   107
val global_syntax = Unsynchronized.ref empty_syntax;
5952
7d4ec8992b23 added commands;
wenzelm
parents: 5923
diff changeset
   108
48647
a5144c4c26a2 report commands as formal entities, with def/ref positions;
wenzelm
parents: 48646
diff changeset
   109
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
   110
  let
56294
85911b8a6868 prefer Context_Position where a context is available;
wenzelm
parents: 56203
diff changeset
   111
    val context = ML_Context.the_generic_context ();
85911b8a6868 prefer Context_Position where a context is available;
wenzelm
parents: 56203
diff changeset
   112
    val thy = Context.theory_of context;
48647
a5144c4c26a2 report commands as formal entities, with def/ref positions;
wenzelm
parents: 48646
diff changeset
   113
    val Command {pos, ...} = cmd;
57623
249c0297cf10 more markup;
wenzelm
parents: 57105
diff changeset
   114
    val command_name = quote (Markup.markup Markup.keyword1 name);
46950
d0181abdbdac declare command keywords via theory header, including strict checking outside Pure;
wenzelm
parents: 46876
diff changeset
   115
    val _ =
d0181abdbdac declare command keywords via theory header, including strict checking outside Pure;
wenzelm
parents: 46876
diff changeset
   116
      (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
   117
        SOME spec =>
48864
3ee314ae1e0a added keyword kind "thy_load" (with optional list of file extensions);
wenzelm
parents: 48771
diff changeset
   118
          if Option.map #1 spec = SOME (Keyword.kind_files_of kind) then ()
58918
8d36bc5eaed3 simplified keyword kinds;
wenzelm
parents: 58908
diff changeset
   119
          else
8d36bc5eaed3 simplified keyword kinds;
wenzelm
parents: 58908
diff changeset
   120
            error ("Inconsistent outer syntax keyword declaration " ^
8d36bc5eaed3 simplified keyword kinds;
wenzelm
parents: 58908
diff changeset
   121
              command_name ^ Position.here pos)
46950
d0181abdbdac declare command keywords via theory header, including strict checking outside Pure;
wenzelm
parents: 46876
diff changeset
   122
      | NONE =>
58918
8d36bc5eaed3 simplified keyword kinds;
wenzelm
parents: 58908
diff changeset
   123
          error ("Undeclared outer syntax command " ^ command_name ^ Position.here pos));
56294
85911b8a6868 prefer Context_Position where a context is available;
wenzelm
parents: 56203
diff changeset
   124
    val _ = Context_Position.report_generic context pos (command_markup true (name, cmd));
46950
d0181abdbdac declare command keywords via theory header, including strict checking outside Pure;
wenzelm
parents: 46876
diff changeset
   125
  in
58862
63a16e98cc14 tuned signature, in accordance to Scala version;
wenzelm
parents: 58861
diff changeset
   126
    Unsynchronized.change global_syntax (map_commands (fn commands =>
46950
d0181abdbdac declare command keywords via theory header, including strict checking outside Pure;
wenzelm
parents: 46876
diff changeset
   127
     (if not (Symtab.defined commands name) then ()
57026
90a3e39be0ca afford strict check (see also AFP/a8e08d947f0a);
wenzelm
parents: 56938
diff changeset
   128
      else if ! batch_mode then
57623
249c0297cf10 more markup;
wenzelm
parents: 57105
diff changeset
   129
        error ("Attempt to redefine outer syntax command " ^ command_name)
55448
e42a3fc18458 explicit indication that redefining outer syntax commands is not supposed to happen -- NB: interactive mode requires global change of syntax;
wenzelm
parents: 54734
diff changeset
   130
      else
57623
249c0297cf10 more markup;
wenzelm
parents: 57105
diff changeset
   131
        warning ("Redefining outer syntax command " ^ command_name ^
57026
90a3e39be0ca afford strict check (see also AFP/a8e08d947f0a);
wenzelm
parents: 56938
diff changeset
   132
          Position.here (Position.thread_data ()));
46950
d0181abdbdac declare command keywords via theory header, including strict checking outside Pure;
wenzelm
parents: 46876
diff changeset
   133
      Symtab.update (name, cmd) commands)))
d0181abdbdac declare command keywords via theory header, including strict checking outside Pure;
wenzelm
parents: 46876
diff changeset
   134
  end);
6722
5e82c7196789 added keyword classification;
wenzelm
parents: 6685
diff changeset
   135
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   136
in
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   137
58903
38c72f5f6c2e explicit type Keyword.keywords;
wenzelm
parents: 58893
diff changeset
   138
fun get_syntax () = CRITICAL (fn () => (Keyword.get_keywords (), ! global_syntax));
7789
57d20133224e verbatim markup tokens;
wenzelm
parents: 7774
diff changeset
   139
46970
9667e0dcb5e2 check declared vs. defined commands at end of session;
wenzelm
parents: 46961
diff changeset
   140
fun check_syntax () =
9667e0dcb5e2 check declared vs. defined commands at end of session;
wenzelm
parents: 46961
diff changeset
   141
  let
58903
38c72f5f6c2e explicit type Keyword.keywords;
wenzelm
parents: 58893
diff changeset
   142
    val (keywords, syntax) = CRITICAL (fn () => (Keyword.get_keywords (), ! global_syntax));
38c72f5f6c2e explicit type Keyword.keywords;
wenzelm
parents: 58893
diff changeset
   143
    val major = Keyword.major_keywords keywords;
46970
9667e0dcb5e2 check declared vs. defined commands at end of session;
wenzelm
parents: 46961
diff changeset
   144
  in
58903
38c72f5f6c2e explicit type Keyword.keywords;
wenzelm
parents: 58893
diff changeset
   145
    (case subtract (op =) (map #1 (dest_commands syntax)) (Scan.dest_lexicon major) of
46970
9667e0dcb5e2 check declared vs. defined commands at end of session;
wenzelm
parents: 46961
diff changeset
   146
      [] => ()
9667e0dcb5e2 check declared vs. defined commands at end of session;
wenzelm
parents: 46961
diff changeset
   147
    | 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
   148
  end;
9667e0dcb5e2 check declared vs. defined commands at end of session;
wenzelm
parents: 46961
diff changeset
   149
48647
a5144c4c26a2 report commands as formal entities, with def/ref positions;
wenzelm
parents: 48646
diff changeset
   150
fun command (spec, pos) comment parse =
58893
9e0ecb66d6a7 eliminated unused int_only flag (see also c12484a27367);
wenzelm
parents: 58864
diff changeset
   151
  add_command spec (new_command comment NONE parse pos);
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   152
48647
a5144c4c26a2 report commands as formal entities, with def/ref positions;
wenzelm
parents: 48646
diff changeset
   153
fun markup_command markup (spec, pos) comment parse =
58893
9e0ecb66d6a7 eliminated unused int_only flag (see also c12484a27367);
wenzelm
parents: 58864
diff changeset
   154
  add_command spec (new_command comment (SOME markup) parse pos);
29311
4c830711e6f1 added type 'a parser, simplified signature;
wenzelm
parents: 28436
diff changeset
   155
43711
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   156
end;
608d1b451f67 less stateful outer_syntax;
wenzelm
parents: 43621
diff changeset
   157
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   158
26990
a91f7741967a added local_theory command wrappers;
wenzelm
parents: 26881
diff changeset
   159
(* local_theory commands *)
a91f7741967a added local_theory command wrappers;
wenzelm
parents: 26881
diff changeset
   160
56895
f058120aaad4 discontinued Toplevel.print flag -- print uniformly according to Keyword.is_printed;
wenzelm
parents: 56334
diff changeset
   161
fun local_theory_command trans command_spec comment parse =
f058120aaad4 discontinued Toplevel.print flag -- print uniformly according to Keyword.is_printed;
wenzelm
parents: 56334
diff changeset
   162
  command command_spec comment (Parse.opt_target -- parse >> (fn (loc, f) => trans loc f));
26990
a91f7741967a added local_theory command wrappers;
wenzelm
parents: 26881
diff changeset
   163
56895
f058120aaad4 discontinued Toplevel.print flag -- print uniformly according to Keyword.is_printed;
wenzelm
parents: 56334
diff changeset
   164
val local_theory' = local_theory_command Toplevel.local_theory';
f058120aaad4 discontinued Toplevel.print flag -- print uniformly according to Keyword.is_printed;
wenzelm
parents: 56334
diff changeset
   165
val local_theory = local_theory_command Toplevel.local_theory;
f058120aaad4 discontinued Toplevel.print flag -- print uniformly according to Keyword.is_printed;
wenzelm
parents: 56334
diff changeset
   166
val local_theory_to_proof' = local_theory_command Toplevel.local_theory_to_proof';
f058120aaad4 discontinued Toplevel.print flag -- print uniformly according to Keyword.is_printed;
wenzelm
parents: 56334
diff changeset
   167
val local_theory_to_proof = local_theory_command Toplevel.local_theory_to_proof;
26990
a91f7741967a added local_theory command wrappers;
wenzelm
parents: 26881
diff changeset
   168
a91f7741967a added local_theory command wrappers;
wenzelm
parents: 26881
diff changeset
   169
24872
7fd1aa6671a4 report on keyword/command declarations;
wenzelm
parents: 24868
diff changeset
   170
(* inspect syntax *)
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   171
58862
63a16e98cc14 tuned signature, in accordance to Scala version;
wenzelm
parents: 58861
diff changeset
   172
fun help_syntax pats =
50213
7b73c0509835 refined outer syntax 'help' command;
wenzelm
parents: 50201
diff changeset
   173
  dest_commands (#2 (get_syntax ()))
7b73c0509835 refined outer syntax 'help' command;
wenzelm
parents: 50201
diff changeset
   174
  |> filter (fn (name, _) => forall (fn pat => match_string pat name) pats)
7b73c0509835 refined outer syntax 'help' command;
wenzelm
parents: 50201
diff changeset
   175
  |> map pretty_command
56334
6b3739fee456 some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents: 56294
diff changeset
   176
  |> Pretty.writeln_chunks;
50213
7b73c0509835 refined outer syntax 'help' command;
wenzelm
parents: 50201
diff changeset
   177
58862
63a16e98cc14 tuned signature, in accordance to Scala version;
wenzelm
parents: 58861
diff changeset
   178
fun print_syntax () =
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   179
  let
58903
38c72f5f6c2e explicit type Keyword.keywords;
wenzelm
parents: 58893
diff changeset
   180
    val (keywords, syntax) = CRITICAL (fn () => (Keyword.get_keywords (), #2 (get_syntax ())));
38c72f5f6c2e explicit type Keyword.keywords;
wenzelm
parents: 58893
diff changeset
   181
    val minor = Scan.dest_lexicon (Keyword.minor_keywords keywords);
58893
9e0ecb66d6a7 eliminated unused int_only flag (see also c12484a27367);
wenzelm
parents: 58864
diff changeset
   182
    val commands = dest_commands syntax;
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   183
  in
58903
38c72f5f6c2e explicit type Keyword.keywords;
wenzelm
parents: 58893
diff changeset
   184
    [Pretty.strs ("keywords:" :: map quote minor),
58893
9e0ecb66d6a7 eliminated unused int_only flag (see also c12484a27367);
wenzelm
parents: 58864
diff changeset
   185
      Pretty.big_list "commands:" (map pretty_command commands)]
56334
6b3739fee456 some shortcuts for chunks, which sometimes avoid bulky string output;
wenzelm
parents: 56294
diff changeset
   186
    |> Pretty.writeln_chunks
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   187
  end;
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   188
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   189
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   190
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   191
(** toplevel parsing **)
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   192
58861
5ff61774df11 command-line terminator ";" is no longer accepted;
wenzelm
parents: 58854
diff changeset
   193
(* parse commands *)
5ff61774df11 command-line terminator ";" is no longer accepted;
wenzelm
parents: 58854
diff changeset
   194
5ff61774df11 command-line terminator ";" is no longer accepted;
wenzelm
parents: 58854
diff changeset
   195
local
6860
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
   196
58862
63a16e98cc14 tuned signature, in accordance to Scala version;
wenzelm
parents: 58861
diff changeset
   197
fun parse_command syntax =
58908
58bedbc18915 tuned signature;
wenzelm
parents: 58904
diff changeset
   198
  Parse.position Parse.command_ :|-- (fn (name, pos) =>
58861
5ff61774df11 command-line terminator ";" is no longer accepted;
wenzelm
parents: 58854
diff changeset
   199
    let
5ff61774df11 command-line terminator ";" is no longer accepted;
wenzelm
parents: 58854
diff changeset
   200
      val tr0 = Toplevel.empty |> Toplevel.name name |> Toplevel.position pos;
5ff61774df11 command-line terminator ";" is no longer accepted;
wenzelm
parents: 58854
diff changeset
   201
    in
58862
63a16e98cc14 tuned signature, in accordance to Scala version;
wenzelm
parents: 58861
diff changeset
   202
      (case lookup_commands syntax name of
58893
9e0ecb66d6a7 eliminated unused int_only flag (see also c12484a27367);
wenzelm
parents: 58864
diff changeset
   203
        SOME (Command {parse, ...}) => Parse.!!! (Parse.tags |-- parse) >> (fn f => f tr0)
58861
5ff61774df11 command-line terminator ";" is no longer accepted;
wenzelm
parents: 58854
diff changeset
   204
      | NONE =>
5ff61774df11 command-line terminator ";" is no longer accepted;
wenzelm
parents: 58854
diff changeset
   205
          Scan.succeed
5ff61774df11 command-line terminator ";" is no longer accepted;
wenzelm
parents: 58854
diff changeset
   206
            (tr0 |> Toplevel.imperative (fn () =>
5ff61774df11 command-line terminator ";" is no longer accepted;
wenzelm
parents: 58854
diff changeset
   207
              error ("Bad parser for outer syntax command " ^ quote name ^ Position.here pos))))
5ff61774df11 command-line terminator ";" is no longer accepted;
wenzelm
parents: 58854
diff changeset
   208
    end);
5ff61774df11 command-line terminator ";" is no longer accepted;
wenzelm
parents: 58854
diff changeset
   209
5ff61774df11 command-line terminator ";" is no longer accepted;
wenzelm
parents: 58854
diff changeset
   210
val parse_cmt = Parse.$$$ "--" -- Parse.!!! Parse.document_source;
5ff61774df11 command-line terminator ";" is no longer accepted;
wenzelm
parents: 58854
diff changeset
   211
5ff61774df11 command-line terminator ";" is no longer accepted;
wenzelm
parents: 58854
diff changeset
   212
in
5ff61774df11 command-line terminator ";" is no longer accepted;
wenzelm
parents: 58854
diff changeset
   213
58862
63a16e98cc14 tuned signature, in accordance to Scala version;
wenzelm
parents: 58861
diff changeset
   214
fun commands_source syntax =
58861
5ff61774df11 command-line terminator ";" is no longer accepted;
wenzelm
parents: 58854
diff changeset
   215
  Token.source_proper #>
58864
505a8150368a recover via scanner;
wenzelm
parents: 58863
diff changeset
   216
  Source.source Token.stopper (Scan.bulk (parse_cmt >> K NONE || Parse.not_eof >> SOME)) #>
58861
5ff61774df11 command-line terminator ";" is no longer accepted;
wenzelm
parents: 58854
diff changeset
   217
  Source.map_filter I #>
58864
505a8150368a recover via scanner;
wenzelm
parents: 58863
diff changeset
   218
  Source.source Token.stopper (Scan.bulk (fn xs => Parse.!!! (parse_command syntax) xs));
58861
5ff61774df11 command-line terminator ";" is no longer accepted;
wenzelm
parents: 58854
diff changeset
   219
5ff61774df11 command-line terminator ";" is no longer accepted;
wenzelm
parents: 58854
diff changeset
   220
end;
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   221
7746
b52c1a632e6a simplified;
wenzelm
parents: 7735
diff changeset
   222
25580
6623674df897 added off-line parse;
wenzelm
parents: 25526
diff changeset
   223
(* off-line scanning/parsing *)
14925
0f86a8a694f8 added read (provides transition names and sources);
wenzelm
parents: 14687
diff changeset
   224
58903
38c72f5f6c2e explicit type Keyword.keywords;
wenzelm
parents: 58893
diff changeset
   225
fun scan keywords pos =
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   226
  Source.of_string #>
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   227
  Symbol.source #>
58904
f49c4f883c58 eliminated pointless dynamic keywords (TTY legacy);
wenzelm
parents: 58903
diff changeset
   228
  Token.source keywords pos #>
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   229
  Source.exhaust;
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   230
58903
38c72f5f6c2e explicit type Keyword.keywords;
wenzelm
parents: 58893
diff changeset
   231
fun parse (keywords, syntax) pos str =
57918
f5d73caba4e5 tuned signature according to Scala version -- prefer explicit argument;
wenzelm
parents: 57905
diff changeset
   232
  Source.of_string str
f5d73caba4e5 tuned signature according to Scala version -- prefer explicit argument;
wenzelm
parents: 57905
diff changeset
   233
  |> Symbol.source
58904
f49c4f883c58 eliminated pointless dynamic keywords (TTY legacy);
wenzelm
parents: 58903
diff changeset
   234
  |> Token.source keywords pos
58861
5ff61774df11 command-line terminator ";" is no longer accepted;
wenzelm
parents: 58854
diff changeset
   235
  |> commands_source syntax
57918
f5d73caba4e5 tuned signature according to Scala version -- prefer explicit argument;
wenzelm
parents: 57905
diff changeset
   236
  |> Source.exhaust;
f5d73caba4e5 tuned signature according to Scala version -- prefer explicit argument;
wenzelm
parents: 57905
diff changeset
   237
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   238
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   239
(* parse spans *)
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   240
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   241
local
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   242
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   243
fun ship span =
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   244
  let
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   245
    val kind =
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   246
      if not (null span) andalso Token.is_command (hd span) andalso not (exists Token.is_error span)
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   247
      then Command_Span.Command_Span (Token.content_of (hd span), Token.pos_of (hd span))
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   248
      else if forall Token.is_improper span then Command_Span.Ignored_Span
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   249
      else Command_Span.Malformed_Span;
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   250
  in cons (Command_Span.Span (kind, span)) end;
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   251
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   252
fun flush (result, content, improper) =
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   253
  result
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   254
  |> not (null content) ? ship (rev content)
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   255
  |> not (null improper) ? ship (rev improper);
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   256
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   257
fun parse tok (result, content, improper) =
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   258
  if Token.is_command tok then (flush (result, content, improper), [tok], [])
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   259
  else if Token.is_improper tok then (result, content, tok :: improper)
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   260
  else (result, tok :: (improper @ content), []);
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   261
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   262
in
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   263
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   264
fun parse_spans toks =
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   265
  fold parse toks ([], [], []) |> flush |> rev;
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   266
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   267
end;
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents: 57623
diff changeset
   268
14091
ad6ba9c55190 A patch by david aspinall
nipkow
parents: 12943
diff changeset
   269
52510
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
   270
(* side-comments *)
48749
c197b3c3e7fa some attempts to keep malformed syntax errors focussed, without too much red spilled onto the document view;
wenzelm
parents: 48647
diff changeset
   271
52510
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
   272
fun cmts (t1 :: t2 :: toks) =
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
   273
      if Token.keyword_with (fn s => s = "--") t1 then t2 :: cmts toks
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
   274
      else cmts (t2 :: toks)
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
   275
  | cmts _ = [];
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
   276
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
   277
val side_comments = filter Token.is_proper #> cmts;
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
   278
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
   279
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
   280
(* read commands *)
48647
a5144c4c26a2 report commands as formal entities, with def/ref positions;
wenzelm
parents: 48646
diff changeset
   281
58862
63a16e98cc14 tuned signature, in accordance to Scala version;
wenzelm
parents: 58861
diff changeset
   282
fun command_reports syntax tok =
52510
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
   283
  if Token.is_command tok then
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
   284
    let val name = Token.content_of tok in
58862
63a16e98cc14 tuned signature, in accordance to Scala version;
wenzelm
parents: 58861
diff changeset
   285
      (case lookup_commands syntax name of
52510
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
   286
        NONE => []
55708
f4b114070675 tuned signature;
wenzelm
parents: 55489
diff changeset
   287
      | SOME cmd => [((Token.pos_of tok, command_markup false (name, cmd)), "")])
52510
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
   288
    end
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
   289
  else [];
48749
c197b3c3e7fa some attempts to keep malformed syntax errors focussed, without too much red spilled onto the document view;
wenzelm
parents: 48647
diff changeset
   290
58862
63a16e98cc14 tuned signature, in accordance to Scala version;
wenzelm
parents: 58861
diff changeset
   291
fun read_spans syntax toks =
52510
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
   292
  Source.of_list toks
58862
63a16e98cc14 tuned signature, in accordance to Scala version;
wenzelm
parents: 58861
diff changeset
   293
  |> commands_source syntax
52510
a4a102237ded tuned signature;
wenzelm
parents: 52509
diff changeset
   294
  |> Source.exhaust;
28432
944cb67f809a load_thy: Toplevel.excursion based on units of command/proof pairs;
wenzelm
parents: 28424
diff changeset
   295
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   296
end;
36953
2af1ad9aa1a3 renamed structure OuterSyntax to Outer_Syntax, keeping the old name as alias for some time;
wenzelm
parents: 36950
diff changeset
   297