src/Pure/Isar/outer_syntax.ML
author wenzelm
Sat, 12 Apr 2008 17:00:35 +0200
changeset 26626 c6231d64d264
parent 26620 722cf4fdd4dd
child 26700 493db7848904
permissions -rw-r--r--
rep_cterm/rep_thm: no longer dereference theory_ref;
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
    ID:         $Id$
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
     3
    Author:     Markus Wenzel, TU Muenchen
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
     4
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
     5
The global Isabelle/Isar outer syntax. Note: the syntax for files is
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
     6
statically determined at the very beginning; for interactive processing
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
     7
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
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
    12
  type parser_fn = OuterLex.token list ->
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
    13
    (Toplevel.transition -> Toplevel.transition) * OuterLex.token list
23722
a4af559708ab export get_lexicons;
wenzelm
parents: 23682
diff changeset
    14
  val get_lexicons: unit -> Scan.lexicon * Scan.lexicon
23796
f37ff1f39fe9 exported command_keyword;
wenzelm
parents: 23722
diff changeset
    15
  val command_keyword: string -> OuterKeyword.T option
14687
e089757b952a added is_keyword;
wenzelm
parents: 14091
diff changeset
    16
  val is_keyword: string -> bool
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
    17
  val keywords: string list -> unit
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
    18
  val command: string -> string -> OuterKeyword.T -> parser_fn -> unit
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
    19
  val markup_command: ThyOutput.markup -> string -> string -> OuterKeyword.T -> parser_fn -> unit
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
    20
  val improper_command: string -> string -> OuterKeyword.T -> parser_fn -> unit
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
    21
  val dest_keywords: unit -> string list
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
    22
  val dest_parsers: unit -> (string * string * string * bool) list
5883
975c984526b0 tuned names;
wenzelm
parents: 5829
diff changeset
    23
  val print_outer_syntax: unit -> unit
24872
7fd1aa6671a4 report on keyword/command declarations;
wenzelm
parents: 24868
diff changeset
    24
  val report: unit -> unit
19060
c814a7856121 check_text: Toplevel.node option;
wenzelm
parents: 18717
diff changeset
    25
  val check_text: string * Position.T -> Toplevel.node option -> unit
16195
wenzelm
parents: 15989
diff changeset
    26
  val scan: string -> OuterLex.token list
25580
6623674df897 added off-line parse;
wenzelm
parents: 25526
diff changeset
    27
  val parse: Position.T -> string -> Toplevel.transition list
26431
f1c79c00f1e4 added process_file;
wenzelm
parents: 26415
diff changeset
    28
  val process_file: Path.T -> theory -> theory
26600
f11515535c83 moved structure Isar to isar.ML;
wenzelm
parents: 26431
diff changeset
    29
  type isar
f11515535c83 moved structure Isar to isar.ML;
wenzelm
parents: 26431
diff changeset
    30
  val isar: bool -> isar
26611
03455add4801 export load_thy -- no backpatching;
wenzelm
parents: 26600
diff changeset
    31
  val load_thy: Path.T -> string -> Position.T -> string list -> bool -> unit
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    32
end;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    33
26600
f11515535c83 moved structure Isar to isar.ML;
wenzelm
parents: 26431
diff changeset
    34
structure OuterSyntax: OUTER_SYNTAX =
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    35
struct
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    36
7750
e63416829538 added markup_command;
wenzelm
parents: 7746
diff changeset
    37
structure T = OuterLex;
6860
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
    38
structure P = OuterParse;
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
    39
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    40
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    41
(** outer syntax **)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    42
24872
7fd1aa6671a4 report on keyword/command declarations;
wenzelm
parents: 24868
diff changeset
    43
(* diagnostics *)
7fd1aa6671a4 report on keyword/command declarations;
wenzelm
parents: 24868
diff changeset
    44
7fd1aa6671a4 report on keyword/command declarations;
wenzelm
parents: 24868
diff changeset
    45
fun report_keyword name =
7fd1aa6671a4 report on keyword/command declarations;
wenzelm
parents: 24868
diff changeset
    46
  Pretty.markup (Markup.keyword_decl name)
7fd1aa6671a4 report on keyword/command declarations;
wenzelm
parents: 24868
diff changeset
    47
    [Pretty.str ("Outer syntax keyword: " ^ quote name)];
7fd1aa6671a4 report on keyword/command declarations;
wenzelm
parents: 24868
diff changeset
    48
7fd1aa6671a4 report on keyword/command declarations;
wenzelm
parents: 24868
diff changeset
    49
fun report_command name kind =
7fd1aa6671a4 report on keyword/command declarations;
wenzelm
parents: 24868
diff changeset
    50
  Pretty.markup (Markup.command_decl name kind)
7fd1aa6671a4 report on keyword/command declarations;
wenzelm
parents: 24868
diff changeset
    51
    [Pretty.str ("Outer syntax command: " ^ quote name ^ " (" ^ kind ^ ")")];
7fd1aa6671a4 report on keyword/command declarations;
wenzelm
parents: 24868
diff changeset
    52
7fd1aa6671a4 report on keyword/command declarations;
wenzelm
parents: 24868
diff changeset
    53
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    54
(* parsers *)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    55
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
    56
type parser_fn = T.token list -> (Toplevel.transition -> Toplevel.transition) * T.token list;
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    57
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
    58
datatype parser = Parser of
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
    59
 {comment: string,
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
    60
  kind: OuterKeyword.T,
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
    61
  markup: ThyOutput.markup option,
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
    62
  int_only: bool,
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
    63
  parse: parser_fn};
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    64
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
    65
fun make_parser comment kind markup int_only parse =
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
    66
  Parser {comment = comment, kind = kind, markup = markup, int_only = int_only, parse = parse};
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    67
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    68
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    69
(* parse command *)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    70
6860
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
    71
local
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
    72
14925
0f86a8a694f8 added read (provides transition names and sources);
wenzelm
parents: 14687
diff changeset
    73
fun terminate false = Scan.succeed ()
0f86a8a694f8 added read (provides transition names and sources);
wenzelm
parents: 14687
diff changeset
    74
  | 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
    75
26620
722cf4fdd4dd eliminated unused trace, read;
wenzelm
parents: 26611
diff changeset
    76
fun body cmd (name, _) =
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
    77
  (case cmd name of
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
    78
    SOME (Parser {int_only, parse, ...}) =>
26620
722cf4fdd4dd eliminated unused trace, read;
wenzelm
parents: 26611
diff changeset
    79
      P.!!! (Scan.prompt (name ^ "# ") (P.tags |-- parse >> pair int_only))
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15224
diff changeset
    80
  | NONE => sys_error ("no parser for outer syntax command " ^ quote name));
6860
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
    81
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
    82
in
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    83
26620
722cf4fdd4dd eliminated unused trace, read;
wenzelm
parents: 26611
diff changeset
    84
fun parse_command do_terminate cmd =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15224
diff changeset
    85
  P.semicolon >> K NONE ||
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15224
diff changeset
    86
  P.sync >> K NONE ||
26620
722cf4fdd4dd eliminated unused trace, read;
wenzelm
parents: 26611
diff changeset
    87
  (P.position P.command :-- body cmd) --| terminate do_terminate
6860
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
    88
    >> (fn ((name, pos), (int_only, f)) =>
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15224
diff changeset
    89
      SOME (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |>
6860
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
    90
        Toplevel.interactive int_only |> f));
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    91
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
    92
end;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
    93
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    94
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    95
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
    96
(** global outer syntax **)
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    97
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
    98
local
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
    99
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   100
val global_lexicons = ref (Scan.empty_lexicon, Scan.empty_lexicon);
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   101
val global_parsers = ref (Symtab.empty: parser Symtab.table);
22120
8424ef945cb5 renamed IsarOutput to ThyOutput;
wenzelm
parents: 22086
diff changeset
   102
val global_markups = ref ([]: (string * ThyOutput.markup) list);
5952
7d4ec8992b23 added commands;
wenzelm
parents: 5923
diff changeset
   103
23939
e543359fe8b6 marked some CRITICAL sections;
wenzelm
parents: 23884
diff changeset
   104
fun change_lexicons f = CRITICAL (fn () =>
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   105
  let val lexs = f (! global_lexicons) in
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   106
    (case (op inter_string) (pairself Scan.dest_lexicon lexs) of
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   107
      [] => global_lexicons := lexs
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   108
    | bads => error ("Clash of outer syntax commands and keywords: " ^ commas_quote bads))
23939
e543359fe8b6 marked some CRITICAL sections;
wenzelm
parents: 23884
diff changeset
   109
  end);
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   110
23939
e543359fe8b6 marked some CRITICAL sections;
wenzelm
parents: 23884
diff changeset
   111
fun change_parsers f = CRITICAL (fn () =>
e543359fe8b6 marked some CRITICAL sections;
wenzelm
parents: 23884
diff changeset
   112
 (change global_parsers f;
e543359fe8b6 marked some CRITICAL sections;
wenzelm
parents: 23884
diff changeset
   113
  global_markups :=
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   114
    Symtab.fold (fn (name, Parser {markup = SOME m, ...}) => cons (name, m) | _ => I)
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   115
      (! global_parsers) []));
6722
5e82c7196789 added keyword classification;
wenzelm
parents: 6685
diff changeset
   116
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   117
in
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   118
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   119
(* access current syntax *)
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   120
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   121
fun get_lexicons () = CRITICAL (fn () => ! global_lexicons);
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   122
fun get_parsers () = CRITICAL (fn () => ! global_parsers);
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   123
fun get_markups () = CRITICAL (fn () => ! global_markups);
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   124
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   125
fun get_parser () = Symtab.lookup (get_parsers ());
7789
57d20133224e verbatim markup tokens;
wenzelm
parents: 7774
diff changeset
   126
23796
f37ff1f39fe9 exported command_keyword;
wenzelm
parents: 23722
diff changeset
   127
fun command_keyword name =
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   128
  (case Symtab.lookup (get_parsers ()) name of
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   129
    SOME (Parser {kind, ...}) => SOME kind
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   130
  | NONE => NONE);
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   131
26600
f11515535c83 moved structure Isar to isar.ML;
wenzelm
parents: 26431
diff changeset
   132
fun command_tags name = these (Option.map OuterKeyword.tags_of (command_keyword name));
17071
f753d6dd9bd0 moved structure Keyword to OuterKeyword (Isar/outer_keyword.ML);
wenzelm
parents: 16894
diff changeset
   133
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   134
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
   135
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   136
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   137
(* augment syntax *)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   138
24872
7fd1aa6671a4 report on keyword/command declarations;
wenzelm
parents: 24868
diff changeset
   139
fun keywords names =
7fd1aa6671a4 report on keyword/command declarations;
wenzelm
parents: 24868
diff changeset
   140
 (change_lexicons (apfst (Scan.extend_lexicon (map Symbol.explode names)));
7fd1aa6671a4 report on keyword/command declarations;
wenzelm
parents: 24868
diff changeset
   141
  List.app (Pretty.writeln o report_keyword) names);
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   142
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   143
24872
7fd1aa6671a4 report on keyword/command declarations;
wenzelm
parents: 24868
diff changeset
   144
fun add_parser (name, parser as Parser {kind, ...}) =
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   145
 (if not (Symtab.defined (get_parsers ()) name) then ()
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   146
  else warning ("Redefining outer syntax command " ^ quote name);
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   147
  change_parsers (Symtab.update (name, parser));
24872
7fd1aa6671a4 report on keyword/command declarations;
wenzelm
parents: 24868
diff changeset
   148
  change_lexicons (apsnd (Scan.extend_lexicon [Symbol.explode name]));
7fd1aa6671a4 report on keyword/command declarations;
wenzelm
parents: 24868
diff changeset
   149
  Pretty.writeln (report_command name (OuterKeyword.kind_of kind)));
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   150
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   151
fun command name comment kind parse =
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   152
  add_parser (name, make_parser comment kind NONE false parse);
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   153
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   154
fun markup_command markup name comment kind parse =
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   155
  add_parser (name, make_parser comment kind (SOME markup) false parse);
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   156
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   157
fun improper_command name comment kind parse =
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   158
  add_parser (name, make_parser comment kind NONE true parse);
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   159
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   160
end;
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   161
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   162
24872
7fd1aa6671a4 report on keyword/command declarations;
wenzelm
parents: 24868
diff changeset
   163
(* inspect syntax *)
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   164
14687
e089757b952a added is_keyword;
wenzelm
parents: 14091
diff changeset
   165
fun is_keyword s = Scan.is_literal (#1 (get_lexicons ())) (Symbol.explode s);
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   166
fun dest_keywords () = Scan.dest_lexicon (#1 (get_lexicons ()));
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   167
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   168
fun dest_parsers () =
16727
e264077b68a7 dest_parsers: sort result;
wenzelm
parents: 16195
diff changeset
   169
  get_parsers () |> Symtab.dest |> sort_wrt #1
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   170
  |> map (fn (name, Parser {comment, kind, int_only, ...}) =>
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   171
    (name, comment, OuterKeyword.kind_of kind, int_only));
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   172
9223
eb752c2fac22 removed help;
wenzelm
parents: 9213
diff changeset
   173
fun print_outer_syntax () =
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   174
  let
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   175
    fun pretty_cmd (name, comment, _, _) =
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   176
      Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   177
    val (int_cmds, cmds) = List.partition #4 (dest_parsers ());
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   178
  in
8720
840c75ab2a7f Pretty.chunks;
wenzelm
parents: 8660
diff changeset
   179
    [Pretty.strs ("syntax keywords:" :: map quote (dest_keywords ())),
18326
2f57579e618f tuned msg;
wenzelm
parents: 18064
diff changeset
   180
      Pretty.big_list "commands:" (map pretty_cmd cmds),
2f57579e618f tuned msg;
wenzelm
parents: 18064
diff changeset
   181
      Pretty.big_list "interactive-only commands:" (map pretty_cmd int_cmds)]
9223
eb752c2fac22 removed help;
wenzelm
parents: 9213
diff changeset
   182
    |> Pretty.chunks |> Pretty.writeln
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   183
  end;
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   184
24872
7fd1aa6671a4 report on keyword/command declarations;
wenzelm
parents: 24868
diff changeset
   185
fun report () =
7fd1aa6671a4 report on keyword/command declarations;
wenzelm
parents: 24868
diff changeset
   186
  (map report_keyword (dest_keywords ()) @
7fd1aa6671a4 report on keyword/command declarations;
wenzelm
parents: 24868
diff changeset
   187
    map (fn (name, _, kind, _) => report_command name kind) (dest_parsers ()))
7fd1aa6671a4 report on keyword/command declarations;
wenzelm
parents: 24868
diff changeset
   188
  |> Pretty.chunks |> Pretty.writeln;
7367
a79d4683fadf print_help;
wenzelm
parents: 7333
diff changeset
   189
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   190
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   191
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   192
(** toplevel parsing **)
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   193
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   194
(* basic sources *)
6860
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
   195
26620
722cf4fdd4dd eliminated unused trace, read;
wenzelm
parents: 26611
diff changeset
   196
fun toplevel_source term do_recover cmd src =
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   197
  let
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   198
    val no_terminator =
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   199
      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
   200
    fun recover int =
cf4773532006 nested source: explicit interactive flag for recover avoids duplicate errors;
wenzelm
parents: 23679
diff changeset
   201
      (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
   202
  in
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   203
    src
12876
a70df1e5bf10 got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents: 10749
diff changeset
   204
    |> T.source_proper
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   205
    |> Source.source T.stopper
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15224
diff changeset
   206
      (Scan.bulk (P.$$$ "--" -- P.!!! P.text >> K NONE || P.not_eof >> SOME))
23682
cf4773532006 nested source: explicit interactive flag for recover avoids duplicate errors;
wenzelm
parents: 23679
diff changeset
   207
        (Option.map recover do_recover)
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19060
diff changeset
   208
    |> Source.map_filter I
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   209
    |> Source.source T.stopper
26620
722cf4fdd4dd eliminated unused trace, read;
wenzelm
parents: 26611
diff changeset
   210
        (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
   211
        (Option.map recover do_recover)
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19060
diff changeset
   212
    |> Source.map_filter I
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   213
  end;
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   214
7746
b52c1a632e6a simplified;
wenzelm
parents: 7735
diff changeset
   215
25580
6623674df897 added off-line parse;
wenzelm
parents: 25526
diff changeset
   216
(* off-line scanning/parsing *)
14925
0f86a8a694f8 added read (provides transition names and sources);
wenzelm
parents: 14687
diff changeset
   217
15144
85929e1b307d Remove isar_readstring. Split read into scanner and parser.
aspinall
parents: 14981
diff changeset
   218
fun scan str =
16195
wenzelm
parents: 15989
diff changeset
   219
  Source.of_string str
wenzelm
parents: 15989
diff changeset
   220
  |> Symbol.source false
23679
57dceb84d1a0 toplevel_source: interactive flag indicates intermittent error_msg;
wenzelm
parents: 22826
diff changeset
   221
  |> T.source (SOME false) get_lexicons Position.none
16195
wenzelm
parents: 15989
diff changeset
   222
  |> Source.exhaust;
wenzelm
parents: 15989
diff changeset
   223
25580
6623674df897 added off-line parse;
wenzelm
parents: 25526
diff changeset
   224
fun parse pos str =
6623674df897 added off-line parse;
wenzelm
parents: 25526
diff changeset
   225
  Source.of_string str
6623674df897 added off-line parse;
wenzelm
parents: 25526
diff changeset
   226
  |> Symbol.source false
6623674df897 added off-line parse;
wenzelm
parents: 25526
diff changeset
   227
  |> T.source (SOME false) get_lexicons pos
26620
722cf4fdd4dd eliminated unused trace, read;
wenzelm
parents: 26611
diff changeset
   228
  |> toplevel_source false NONE get_parser
25580
6623674df897 added off-line parse;
wenzelm
parents: 25526
diff changeset
   229
  |> Source.exhaust;
6623674df897 added off-line parse;
wenzelm
parents: 25526
diff changeset
   230
14091
ad6ba9c55190 A patch by david aspinall
nipkow
parents: 12943
diff changeset
   231
26431
f1c79c00f1e4 added process_file;
wenzelm
parents: 26415
diff changeset
   232
(* process file *)
f1c79c00f1e4 added process_file;
wenzelm
parents: 26415
diff changeset
   233
f1c79c00f1e4 added process_file;
wenzelm
parents: 26415
diff changeset
   234
fun process_file path thy =
f1c79c00f1e4 added process_file;
wenzelm
parents: 26415
diff changeset
   235
  let
f1c79c00f1e4 added process_file;
wenzelm
parents: 26415
diff changeset
   236
    val result = ref thy;
f1c79c00f1e4 added process_file;
wenzelm
parents: 26415
diff changeset
   237
    val trs = parse (Position.path path) (File.read path);
f1c79c00f1e4 added process_file;
wenzelm
parents: 26415
diff changeset
   238
    val init = Toplevel.init_theory (K thy) (fn thy' => result := thy') (K ());
f1c79c00f1e4 added process_file;
wenzelm
parents: 26415
diff changeset
   239
    val _ = Toplevel.excursion (init Toplevel.empty :: trs @ [Toplevel.exit Toplevel.empty]);
f1c79c00f1e4 added process_file;
wenzelm
parents: 26415
diff changeset
   240
  in ! result end;
f1c79c00f1e4 added process_file;
wenzelm
parents: 26415
diff changeset
   241
f1c79c00f1e4 added process_file;
wenzelm
parents: 26415
diff changeset
   242
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   243
(* interactive source of toplevel transformers *)
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   244
26600
f11515535c83 moved structure Isar to isar.ML;
wenzelm
parents: 26431
diff changeset
   245
type isar =
f11515535c83 moved structure Isar to isar.ML;
wenzelm
parents: 26431
diff changeset
   246
  (Toplevel.transition, (Toplevel.transition option,
f11515535c83 moved structure Isar to isar.ML;
wenzelm
parents: 26431
diff changeset
   247
    (OuterLex.token, (OuterLex.token option, (OuterLex.token, (OuterLex.token,
f11515535c83 moved structure Isar to isar.ML;
wenzelm
parents: 26431
diff changeset
   248
      Position.T * (Symbol.symbol, (string, unit) Source.source) Source.source)
f11515535c83 moved structure Isar to isar.ML;
wenzelm
parents: 26431
diff changeset
   249
          Source.source) Source.source) Source.source) Source.source) Source.source) Source.source;
f11515535c83 moved structure Isar to isar.ML;
wenzelm
parents: 26431
diff changeset
   250
f11515535c83 moved structure Isar to isar.ML;
wenzelm
parents: 26431
diff changeset
   251
fun isar term : isar =
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   252
  Source.tty
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   253
  |> Symbol.source true
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   254
  |> T.source (SOME true) get_lexicons Position.none
26620
722cf4fdd4dd eliminated unused trace, read;
wenzelm
parents: 26611
diff changeset
   255
  |> toplevel_source term (SOME true) get_parser;
24868
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   256
2990c327d8c6 simplified interfaces for outer syntax;
wenzelm
parents: 24188
diff changeset
   257
16195
wenzelm
parents: 15989
diff changeset
   258
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   259
(** read theory **)
6247
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   260
12943
1db24da0537b added check_text;
wenzelm
parents: 12876
diff changeset
   261
(* check_text *)
1db24da0537b added check_text;
wenzelm
parents: 12876
diff changeset
   262
22120
8424ef945cb5 renamed IsarOutput to ThyOutput;
wenzelm
parents: 22086
diff changeset
   263
fun check_text s state = (ThyOutput.eval_antiquote (#1 (get_lexicons ())) state s; ());
12943
1db24da0537b added check_text;
wenzelm
parents: 12876
diff changeset
   264
1db24da0537b added check_text;
wenzelm
parents: 12876
diff changeset
   265
26611
03455add4801 export load_thy -- no backpatching;
wenzelm
parents: 26600
diff changeset
   266
(* load_thy *)
7746
b52c1a632e6a simplified;
wenzelm
parents: 7735
diff changeset
   267
26323
73efc70edeef theory loader: discontinued *attached* ML scripts;
wenzelm
parents: 26291
diff changeset
   268
fun load_thy dir name pos text time =
7683
e74f43f1d8a3 export token_source;
wenzelm
parents: 7676
diff changeset
   269
  let
24065
21483400c2ca load_thy: avoid reloading of text;
wenzelm
parents: 23939
diff changeset
   270
    val text_src = Source.of_list (Library.untabify text);
23866
5295671034f8 moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents: 23796
diff changeset
   271
17932
677f7bec354e removed old-style theory format;
wenzelm
parents: 17412
diff changeset
   272
    val _ = Present.init_theory name;
24065
21483400c2ca load_thy: avoid reloading of text;
wenzelm
parents: 23939
diff changeset
   273
    val _ = Present.verbatim_source name (fn () => Source.exhaust (Symbol.source false text_src));
21483400c2ca load_thy: avoid reloading of text;
wenzelm
parents: 23939
diff changeset
   274
    val toks = text_src
17932
677f7bec354e removed old-style theory format;
wenzelm
parents: 17412
diff changeset
   275
      |> Symbol.source false
24065
21483400c2ca load_thy: avoid reloading of text;
wenzelm
parents: 23939
diff changeset
   276
      |> T.source NONE (K (get_lexicons ())) pos
17932
677f7bec354e removed old-style theory format;
wenzelm
parents: 17412
diff changeset
   277
      |> Source.exhausted;
677f7bec354e removed old-style theory format;
wenzelm
parents: 17412
diff changeset
   278
    val trs = toks
26620
722cf4fdd4dd eliminated unused trace, read;
wenzelm
parents: 26611
diff changeset
   279
      |> toplevel_source false NONE (K (get_parser ()))
17932
677f7bec354e removed old-style theory format;
wenzelm
parents: 17412
diff changeset
   280
      |> Source.exhaust;
23866
5295671034f8 moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents: 23796
diff changeset
   281
5295671034f8 moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents: 23796
diff changeset
   282
    val _ = if time then writeln ("\n**** Starting theory " ^ quote name ^ " ****") else ();
25685
c36e10812ae4 cond_timeit: added message argument;
wenzelm
parents: 25583
diff changeset
   283
    val _ = cond_timeit time "" (fn () =>
23866
5295671034f8 moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents: 23796
diff changeset
   284
      ThyOutput.process_thy (#1 (get_lexicons ())) command_tags is_markup trs toks
5295671034f8 moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents: 23796
diff changeset
   285
      |> Buffer.content
5295671034f8 moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents: 23796
diff changeset
   286
      |> Present.theory_output name);
5295671034f8 moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents: 23796
diff changeset
   287
    val _ = if time then writeln ("**** Finished theory " ^ quote name ^ " ****\n") else ();
24065
21483400c2ca load_thy: avoid reloading of text;
wenzelm
parents: 23939
diff changeset
   288
  in () end;
23866
5295671034f8 moved deps_thy to ThyLoad (independent of outer syntax);
wenzelm
parents: 23796
diff changeset
   289
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   290
end;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   291