src/Pure/Isar/outer_syntax.ML
author wenzelm
Sat, 21 Jan 2006 23:02:14 +0100
changeset 18728 6790126ab5f6
parent 18717 6261fcfaca1d
child 19060 c814a7856121
permissions -rw-r--r--
simplified type attribute;
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
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
     5
The global Isabelle/Isar outer syntax.
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
     6
*)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
     7
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
     8
signature BASIC_OUTER_SYNTAX =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
     9
sig
15830
74d8412b1a27 added structure Isar (from isar.ML);
wenzelm
parents: 15570
diff changeset
    10
  structure Isar:
74d8412b1a27 added structure Isar (from isar.ML);
wenzelm
parents: 15570
diff changeset
    11
    sig
18064
f5727fa16c77 added Isar.state/exn;
wenzelm
parents: 17975
diff changeset
    12
      val state: unit -> Toplevel.state
f5727fa16c77 added Isar.state/exn;
wenzelm
parents: 17975
diff changeset
    13
      val exn: unit -> (exn * string) option
15830
74d8412b1a27 added structure Isar (from isar.ML);
wenzelm
parents: 15570
diff changeset
    14
      val main: unit -> unit
74d8412b1a27 added structure Isar (from isar.ML);
wenzelm
parents: 15570
diff changeset
    15
      val loop: unit -> unit
74d8412b1a27 added structure Isar (from isar.ML);
wenzelm
parents: 15570
diff changeset
    16
      val sync_main: unit -> unit
74d8412b1a27 added structure Isar (from isar.ML);
wenzelm
parents: 15570
diff changeset
    17
      val sync_loop: unit -> unit
18684
38d72231b41d added Isar.toplevel;
wenzelm
parents: 18326
diff changeset
    18
      val toplevel: (unit -> 'a) -> 'a
15830
74d8412b1a27 added structure Isar (from isar.ML);
wenzelm
parents: 15570
diff changeset
    19
    end;
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    20
end;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    21
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    22
signature OUTER_SYNTAX =
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    23
sig
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    24
  include BASIC_OUTER_SYNTAX
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    25
  type token
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    26
  type parser
17071
f753d6dd9bd0 moved structure Keyword to OuterKeyword (Isar/outer_keyword.ML);
wenzelm
parents: 16894
diff changeset
    27
  val command: string -> string -> OuterKeyword.T ->
6373
47b357194f32 added (improper_)command;
wenzelm
parents: 6332
diff changeset
    28
    (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
17071
f753d6dd9bd0 moved structure Keyword to OuterKeyword (Isar/outer_keyword.ML);
wenzelm
parents: 16894
diff changeset
    29
  val markup_command: IsarOutput.markup -> string -> string -> OuterKeyword.T ->
7789
57d20133224e verbatim markup tokens;
wenzelm
parents: 7774
diff changeset
    30
    (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
17071
f753d6dd9bd0 moved structure Keyword to OuterKeyword (Isar/outer_keyword.ML);
wenzelm
parents: 16894
diff changeset
    31
  val improper_command: string -> string -> OuterKeyword.T ->
6373
47b357194f32 added (improper_)command;
wenzelm
parents: 6332
diff changeset
    32
    (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
14687
e089757b952a added is_keyword;
wenzelm
parents: 14091
diff changeset
    33
  val is_keyword: string -> bool
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
    34
  val dest_keywords: unit -> string list
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
    35
  val dest_parsers: unit -> (string * string * string * bool) list
5883
975c984526b0 tuned names;
wenzelm
parents: 5829
diff changeset
    36
  val print_outer_syntax: unit -> unit
9223
eb752c2fac22 removed help;
wenzelm
parents: 9213
diff changeset
    37
  val print_commands: Toplevel.transition -> Toplevel.transition
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    38
  val add_keywords: string list -> unit
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    39
  val add_parsers: parser list -> unit
17265
12d99bb4304b tuned check_text;
wenzelm
parents: 17250
diff changeset
    40
  val check_text: string * Position.T -> Toplevel.state -> unit
7940
def6db239934 improved ml handling;
wenzelm
parents: 7903
diff changeset
    41
  val deps_thy: string -> bool -> Path.T -> string list * Path.T list
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
    42
  val load_thy: string -> bool -> bool -> Path.T -> unit
14091
ad6ba9c55190 A patch by david aspinall
nipkow
parents: 12943
diff changeset
    43
  val isar: bool -> bool -> unit Toplevel.isar
16195
wenzelm
parents: 15989
diff changeset
    44
  val scan: string -> OuterLex.token list
wenzelm
parents: 15989
diff changeset
    45
  val read: OuterLex.token list -> (string * OuterLex.token list * Toplevel.transition) list
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    46
end;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    47
15224
1bd35fd87963 Allow scanning to recover and reconstruct bad input
aspinall
parents: 15156
diff changeset
    48
structure OuterSyntax : OUTER_SYNTAX  =
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    49
struct
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    50
7750
e63416829538 added markup_command;
wenzelm
parents: 7746
diff changeset
    51
structure T = OuterLex;
6860
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
    52
structure P = OuterParse;
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
    53
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    54
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    55
(** outer syntax **)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    56
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    57
(* parsers *)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    58
7750
e63416829538 added markup_command;
wenzelm
parents: 7746
diff changeset
    59
type token = T.token;
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    60
type parser_fn = token list -> (Toplevel.transition -> Toplevel.transition) * token list;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    61
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    62
datatype parser =
17071
f753d6dd9bd0 moved structure Keyword to OuterKeyword (Isar/outer_keyword.ML);
wenzelm
parents: 16894
diff changeset
    63
  Parser of string * (string * OuterKeyword.T * IsarOutput.markup option) * bool * parser_fn;
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    64
7750
e63416829538 added markup_command;
wenzelm
parents: 7746
diff changeset
    65
fun parser int_only markup name comment kind parse =
e63416829538 added markup_command;
wenzelm
parents: 7746
diff changeset
    66
  Parser (name, (comment, kind, markup), int_only, 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
0f86a8a694f8 added read (provides transition names and sources);
wenzelm
parents: 14687
diff changeset
    76
fun trace false parse = parse
0f86a8a694f8 added read (provides transition names and sources);
wenzelm
parents: 14687
diff changeset
    77
  | trace true parse = Scan.trace parse >> (fn (f, toks) => f o Toplevel.source toks);
0f86a8a694f8 added read (provides transition names and sources);
wenzelm
parents: 14687
diff changeset
    78
17071
f753d6dd9bd0 moved structure Keyword to OuterKeyword (Isar/outer_keyword.ML);
wenzelm
parents: 16894
diff changeset
    79
fun body cmd do_trace (name, _) =
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
    80
  (case cmd name of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15224
diff changeset
    81
    SOME (int_only, parse) =>
17118
1ff59b7b35b7 fixed command prompt (was broken due to P.tags);
wenzelm
parents: 17071
diff changeset
    82
      P.!!! (Scan.prompt (name ^ "# ") (trace do_trace (P.tags |-- parse) >> pair int_only))
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15224
diff changeset
    83
  | NONE => sys_error ("no parser for outer syntax command " ^ quote name));
6860
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
    84
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
    85
in
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    86
14925
0f86a8a694f8 added read (provides transition names and sources);
wenzelm
parents: 14687
diff changeset
    87
fun command do_terminate do_trace cmd =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15224
diff changeset
    88
  P.semicolon >> K NONE ||
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15224
diff changeset
    89
  P.sync >> K NONE ||
17118
1ff59b7b35b7 fixed command prompt (was broken due to P.tags);
wenzelm
parents: 17071
diff changeset
    90
  (P.position P.command :-- body cmd do_trace) --| terminate do_terminate
6860
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
    91
    >> (fn ((name, pos), (int_only, f)) =>
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15224
diff changeset
    92
      SOME (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |>
6860
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
    93
        Toplevel.interactive int_only |> f));
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    94
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
    95
end;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
    96
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    97
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
    98
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
    99
(** global outer syntax **)
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   100
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   101
local
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   102
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   103
val global_lexicons = ref (Scan.empty_lexicon, Scan.empty_lexicon);
7750
e63416829538 added markup_command;
wenzelm
parents: 7746
diff changeset
   104
val global_parsers =
17071
f753d6dd9bd0 moved structure Keyword to OuterKeyword (Isar/outer_keyword.ML);
wenzelm
parents: 16894
diff changeset
   105
  ref (Symtab.empty: (((string * OuterKeyword.T) * (bool * parser_fn)) * IsarOutput.markup option)
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   106
    Symtab.table);
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   107
val global_markups = ref ([]: (string * IsarOutput.markup) list);
5952
7d4ec8992b23 added commands;
wenzelm
parents: 5923
diff changeset
   108
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   109
fun change_lexicons f =
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   110
  let val lexs = f (! global_lexicons) in
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   111
    (case (op inter_string) (pairself Scan.dest_lexicon lexs) of
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   112
      [] => global_lexicons := lexs
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   113
    | bads => error ("Clash of outer syntax commands and keywords: " ^ commas_quote bads))
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   114
  end;
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   115
17071
f753d6dd9bd0 moved structure Keyword to OuterKeyword (Isar/outer_keyword.ML);
wenzelm
parents: 16894
diff changeset
   116
fun make_markups () = global_markups :=
f753d6dd9bd0 moved structure Keyword to OuterKeyword (Isar/outer_keyword.ML);
wenzelm
parents: 16894
diff changeset
   117
  Symtab.fold (fn (name, (_, SOME m)) => cons (name, m) | _ => I) (! global_parsers) [];
7750
e63416829538 added markup_command;
wenzelm
parents: 7746
diff changeset
   118
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   119
fun change_parsers f = (Library.change global_parsers f; make_markups ());
6722
5e82c7196789 added keyword classification;
wenzelm
parents: 6685
diff changeset
   120
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   121
in
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   122
7750
e63416829538 added markup_command;
wenzelm
parents: 7746
diff changeset
   123
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   124
(* access current syntax *)
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   125
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   126
(*Note: the syntax for files is statically determined at the very
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   127
  beginning; for interactive processing it may change dynamically.*)
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   128
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   129
fun get_lexicons () = ! global_lexicons;
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   130
fun get_parsers () = ! global_parsers;
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17283
diff changeset
   131
fun get_parser () = Option.map (#2 o #1) o Symtab.lookup (get_parsers ());
7789
57d20133224e verbatim markup tokens;
wenzelm
parents: 7774
diff changeset
   132
17071
f753d6dd9bd0 moved structure Keyword to OuterKeyword (Isar/outer_keyword.ML);
wenzelm
parents: 16894
diff changeset
   133
fun command_tags name =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17283
diff changeset
   134
  (case Symtab.lookup (get_parsers ()) name of
17071
f753d6dd9bd0 moved structure Keyword to OuterKeyword (Isar/outer_keyword.ML);
wenzelm
parents: 16894
diff changeset
   135
    SOME (((_, k), _), _) => OuterKeyword.tags_of k
f753d6dd9bd0 moved structure Keyword to OuterKeyword (Isar/outer_keyword.ML);
wenzelm
parents: 16894
diff changeset
   136
  | NONE => []);
f753d6dd9bd0 moved structure Keyword to OuterKeyword (Isar/outer_keyword.ML);
wenzelm
parents: 16894
diff changeset
   137
17184
3d80209e9a53 use AList operations;
wenzelm
parents: 17118
diff changeset
   138
fun is_markup kind name = (AList.lookup (op =) (! global_markups) name = SOME kind);
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   139
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   140
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   141
(* augment syntax *)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   142
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   143
fun add_keywords keywords = change_lexicons (apfst (fn lex =>
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   144
  (Scan.extend_lexicon lex (map Symbol.explode keywords))));
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   145
17071
f753d6dd9bd0 moved structure Keyword to OuterKeyword (Isar/outer_keyword.ML);
wenzelm
parents: 16894
diff changeset
   146
fun add_parser (Parser (name, (comment, kind, markup), int_only, parse)) tab =
16894
40f80823b451 Inttab.defined;
wenzelm
parents: 16727
diff changeset
   147
 (if not (Symtab.defined tab name) then ()
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   148
  else warning ("Redefined outer syntax command " ^ quote name);
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17283
diff changeset
   149
  Symtab.update (name, (((comment, kind), (int_only, parse)), markup)) tab);
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   150
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   151
fun add_parsers parsers =
17071
f753d6dd9bd0 moved structure Keyword to OuterKeyword (Isar/outer_keyword.ML);
wenzelm
parents: 16894
diff changeset
   152
  (change_parsers (fold add_parser parsers);
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   153
    change_lexicons (apsnd (fn lex => Scan.extend_lexicon lex
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   154
      (map (fn Parser (name, _, _, _) => Symbol.explode name) parsers))));
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   155
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   156
end;
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   157
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   158
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   159
(* print syntax *)
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   160
14687
e089757b952a added is_keyword;
wenzelm
parents: 14091
diff changeset
   161
fun is_keyword s = Scan.is_literal (#1 (get_lexicons ())) (Symbol.explode s);
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   162
fun dest_keywords () = Scan.dest_lexicon (#1 (get_lexicons ()));
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   163
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   164
fun dest_parsers () =
16727
e264077b68a7 dest_parsers: sort result;
wenzelm
parents: 16195
diff changeset
   165
  get_parsers () |> Symtab.dest |> sort_wrt #1
17071
f753d6dd9bd0 moved structure Keyword to OuterKeyword (Isar/outer_keyword.ML);
wenzelm
parents: 16894
diff changeset
   166
  |> map (fn (name, (((cmt, kind), (int_only, _)), _)) =>
f753d6dd9bd0 moved structure Keyword to OuterKeyword (Isar/outer_keyword.ML);
wenzelm
parents: 16894
diff changeset
   167
    (name, cmt, OuterKeyword.kind_of kind, int_only));
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   168
9223
eb752c2fac22 removed help;
wenzelm
parents: 9213
diff changeset
   169
fun print_outer_syntax () =
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   170
  let
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   171
    fun pretty_cmd (name, comment, _, _) =
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   172
      Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   173
    val (int_cmds, cmds) = List.partition #4 (dest_parsers ());
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   174
  in
8720
840c75ab2a7f Pretty.chunks;
wenzelm
parents: 8660
diff changeset
   175
    [Pretty.strs ("syntax keywords:" :: map quote (dest_keywords ())),
18326
2f57579e618f tuned msg;
wenzelm
parents: 18064
diff changeset
   176
      Pretty.big_list "commands:" (map pretty_cmd cmds),
2f57579e618f tuned msg;
wenzelm
parents: 18064
diff changeset
   177
      Pretty.big_list "interactive-only commands:" (map pretty_cmd int_cmds)]
9223
eb752c2fac22 removed help;
wenzelm
parents: 9213
diff changeset
   178
    |> Pretty.chunks |> Pretty.writeln
7026
69724548fad1 separate command tokens;
wenzelm
parents: 6868
diff changeset
   179
  end;
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   180
9223
eb752c2fac22 removed help;
wenzelm
parents: 9213
diff changeset
   181
val print_commands = Toplevel.imperative print_outer_syntax;
7367
a79d4683fadf print_help;
wenzelm
parents: 7333
diff changeset
   182
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   183
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   184
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   185
(** toplevel parsing **)
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   186
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   187
(* basic sources *)
6860
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
   188
17071
f753d6dd9bd0 moved structure Keyword to OuterKeyword (Isar/outer_keyword.ML);
wenzelm
parents: 16894
diff changeset
   189
fun toplevel_source term do_trace do_recover cmd src =
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   190
  let
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   191
    val no_terminator =
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   192
      Scan.unless P.semicolon (Scan.one (T.not_sync andf T.not_eof));
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15224
diff changeset
   193
    fun recover x = (Scan.prompt "recover# " (Scan.repeat no_terminator) >> K [NONE]) x;
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   194
  in
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   195
    src
12876
a70df1e5bf10 got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents: 10749
diff changeset
   196
    |> T.source_proper
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   197
    |> Source.source T.stopper
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15224
diff changeset
   198
      (Scan.bulk (P.$$$ "--" -- P.!!! P.text >> K NONE || P.not_eof >> SOME))
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15224
diff changeset
   199
      (if do_recover then SOME recover else NONE)
12876
a70df1e5bf10 got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents: 10749
diff changeset
   200
    |> Source.mapfilter I
17071
f753d6dd9bd0 moved structure Keyword to OuterKeyword (Isar/outer_keyword.ML);
wenzelm
parents: 16894
diff changeset
   201
    |> Source.source T.stopper (Scan.bulk (fn xs => P.!!! (command term do_trace (cmd ())) xs))
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15224
diff changeset
   202
      (if do_recover then SOME recover else NONE)
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   203
    |> Source.mapfilter I
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   204
  end;
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   205
7746
b52c1a632e6a simplified;
wenzelm
parents: 7735
diff changeset
   206
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   207
(* interactive source of toplevel transformers *)
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   208
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   209
fun isar term no_pos =
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   210
  Source.tty
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   211
  |> Symbol.source true
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   212
  |> T.source true get_lexicons
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   213
    (if no_pos then Position.none else Position.line_name 1 "stdin")
14925
0f86a8a694f8 added read (provides transition names and sources);
wenzelm
parents: 14687
diff changeset
   214
  |> toplevel_source term false true get_parser;
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   215
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   216
16195
wenzelm
parents: 15989
diff changeset
   217
(* scan text *)
14925
0f86a8a694f8 added read (provides transition names and sources);
wenzelm
parents: 14687
diff changeset
   218
15144
85929e1b307d Remove isar_readstring. Split read into scanner and parser.
aspinall
parents: 14981
diff changeset
   219
fun scan str =
16195
wenzelm
parents: 15989
diff changeset
   220
  Source.of_string str
wenzelm
parents: 15989
diff changeset
   221
  |> Symbol.source false
wenzelm
parents: 15989
diff changeset
   222
  |> T.source true get_lexicons Position.none
wenzelm
parents: 15989
diff changeset
   223
  |> Source.exhaust;
wenzelm
parents: 15989
diff changeset
   224
wenzelm
parents: 15989
diff changeset
   225
wenzelm
parents: 15989
diff changeset
   226
(* read tokens with trace *)
14925
0f86a8a694f8 added read (provides transition names and sources);
wenzelm
parents: 14687
diff changeset
   227
15144
85929e1b307d Remove isar_readstring. Split read into scanner and parser.
aspinall
parents: 14981
diff changeset
   228
fun read toks =
85929e1b307d Remove isar_readstring. Split read into scanner and parser.
aspinall
parents: 14981
diff changeset
   229
  Source.of_list toks
14925
0f86a8a694f8 added read (provides transition names and sources);
wenzelm
parents: 14687
diff changeset
   230
  |> toplevel_source false true true get_parser
0f86a8a694f8 added read (provides transition names and sources);
wenzelm
parents: 14687
diff changeset
   231
  |> Source.exhaust
15973
wenzelm
parents: 15830
diff changeset
   232
  |> map (fn tr => (Toplevel.name_of tr, the (Toplevel.source_of tr), tr));
14091
ad6ba9c55190 A patch by david aspinall
nipkow
parents: 12943
diff changeset
   233
ad6ba9c55190 A patch by david aspinall
nipkow
parents: 12943
diff changeset
   234
16195
wenzelm
parents: 15989
diff changeset
   235
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   236
(** read theory **)
6247
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   237
12943
1db24da0537b added check_text;
wenzelm
parents: 12876
diff changeset
   238
(* check_text *)
1db24da0537b added check_text;
wenzelm
parents: 12876
diff changeset
   239
17265
12d99bb4304b tuned check_text;
wenzelm
parents: 17250
diff changeset
   240
fun check_text s state = (IsarOutput.eval_antiquote (#1 (get_lexicons ())) state s; ());
12943
1db24da0537b added check_text;
wenzelm
parents: 12876
diff changeset
   241
1db24da0537b added check_text;
wenzelm
parents: 12876
diff changeset
   242
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   243
(* deps_thy *)
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   244
7940
def6db239934 improved ml handling;
wenzelm
parents: 7903
diff changeset
   245
fun deps_thy name ml path =
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   246
  let
7735
3e5ff3806831 present token source;
wenzelm
parents: 7683
diff changeset
   247
    val src = Source.of_string (File.read path);
3e5ff3806831 present token source;
wenzelm
parents: 7683
diff changeset
   248
    val pos = Path.position path;
17932
677f7bec354e removed old-style theory format;
wenzelm
parents: 17412
diff changeset
   249
    val (name', parents, files) = ThyHeader.read (src, pos);
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   250
    val ml_path = ThyLoad.ml_path name;
15973
wenzelm
parents: 15830
diff changeset
   251
    val ml_file = if ml andalso is_some (ThyLoad.check_file NONE ml_path) then [ml_path] else [];
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   252
  in
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   253
    if name <> name' then
7940
def6db239934 improved ml handling;
wenzelm
parents: 7903
diff changeset
   254
      error ("Filename " ^ quote (Path.pack path) ^
8078
c6da7585f9d1 fixed error msg;
wenzelm
parents: 7940
diff changeset
   255
        " does not agree with theory name " ^ quote name')
6247
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   256
    else (parents, map (Path.unpack o #1) files @ ml_file)
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   257
  end;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   258
7746
b52c1a632e6a simplified;
wenzelm
parents: 7735
diff changeset
   259
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   260
(* load_thy *)
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   261
7746
b52c1a632e6a simplified;
wenzelm
parents: 7735
diff changeset
   262
local
b52c1a632e6a simplified;
wenzelm
parents: 7735
diff changeset
   263
7940
def6db239934 improved ml handling;
wenzelm
parents: 7903
diff changeset
   264
fun try_ml_file name time =
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   265
  let
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   266
    val path = ThyLoad.ml_path name;
17975
a77862a93f02 load_file: setmp OldGoals.legacy true;
wenzelm
parents: 17932
diff changeset
   267
    val tr = Toplevel.imperative (fn () =>
a77862a93f02 load_file: setmp OldGoals.legacy true;
wenzelm
parents: 17932
diff changeset
   268
      setmp OldGoals.legacy true (fn () => ThyInfo.load_file time path) ());
6247
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   269
    val tr_name = if time then "time_use" else "use";
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   270
  in
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15224
diff changeset
   271
    if is_none (ThyLoad.check_file NONE path) then ()
9132
52286129faa5 moved header stuff to thy_header.ML;
wenzelm
parents: 9056
diff changeset
   272
    else Toplevel.excursion [Toplevel.empty |> Toplevel.name tr_name |> tr]
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   273
  end;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   274
18717
6261fcfaca1d run_thy: removed Output.toplevel_errors;
wenzelm
parents: 18684
diff changeset
   275
fun run_thy name path =
7683
e74f43f1d8a3 export token_source;
wenzelm
parents: 7676
diff changeset
   276
  let
17932
677f7bec354e removed old-style theory format;
wenzelm
parents: 17412
diff changeset
   277
    val text = Source.of_list (Library.untabify (explode (File.read path)));
677f7bec354e removed old-style theory format;
wenzelm
parents: 17412
diff changeset
   278
    val _ = Present.init_theory name;
677f7bec354e removed old-style theory format;
wenzelm
parents: 17412
diff changeset
   279
    val _ = Present.verbatim_source name (fn () => Source.exhaust (Symbol.source false text));
677f7bec354e removed old-style theory format;
wenzelm
parents: 17412
diff changeset
   280
    val toks = text
677f7bec354e removed old-style theory format;
wenzelm
parents: 17412
diff changeset
   281
      |> Symbol.source false
677f7bec354e removed old-style theory format;
wenzelm
parents: 17412
diff changeset
   282
      |> T.source false (K (get_lexicons ())) (Path.position path)
677f7bec354e removed old-style theory format;
wenzelm
parents: 17412
diff changeset
   283
      |> Source.exhausted;
677f7bec354e removed old-style theory format;
wenzelm
parents: 17412
diff changeset
   284
    val trs = toks
677f7bec354e removed old-style theory format;
wenzelm
parents: 17412
diff changeset
   285
      |> toplevel_source false false false (K (get_parser ()))
677f7bec354e removed old-style theory format;
wenzelm
parents: 17412
diff changeset
   286
      |> Source.exhaust;
7683
e74f43f1d8a3 export token_source;
wenzelm
parents: 7676
diff changeset
   287
  in
17932
677f7bec354e removed old-style theory format;
wenzelm
parents: 17412
diff changeset
   288
    IsarOutput.present_thy (#1 (get_lexicons ())) command_tags is_markup trs toks
677f7bec354e removed old-style theory format;
wenzelm
parents: 17412
diff changeset
   289
    |> Buffer.content
677f7bec354e removed old-style theory format;
wenzelm
parents: 17412
diff changeset
   290
    |> Present.theory_output name
18717
6261fcfaca1d run_thy: removed Output.toplevel_errors;
wenzelm
parents: 18684
diff changeset
   291
  end;
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   292
7746
b52c1a632e6a simplified;
wenzelm
parents: 7735
diff changeset
   293
in
b52c1a632e6a simplified;
wenzelm
parents: 7735
diff changeset
   294
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   295
fun load_thy name ml time path =
6247
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   296
 (if time then
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   297
    timeit (fn () =>
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   298
     (writeln ("\n**** Starting theory " ^ quote name ^ " ****");
9036
d9e09ef531dd do not setmp Library.timing;
wenzelm
parents: 9033
diff changeset
   299
      run_thy name path;
6247
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   300
      writeln ("**** Finished theory " ^ quote name ^ " ****\n")))
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   301
  else run_thy name path;
e8bbe64861b8 more robust handling of theory context;
wenzelm
parents: 6229
diff changeset
   302
  Context.context (ThyInfo.get_theory name);
7940
def6db239934 improved ml handling;
wenzelm
parents: 7903
diff changeset
   303
  if ml then try_ml_file name time else ());
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   304
7746
b52c1a632e6a simplified;
wenzelm
parents: 7735
diff changeset
   305
end;
b52c1a632e6a simplified;
wenzelm
parents: 7735
diff changeset
   306
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   307
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   308
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   309
(** the read-eval-print loop **)
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   310
5923
wenzelm
parents: 5883
diff changeset
   311
(* main loop *)
wenzelm
parents: 5883
diff changeset
   312
7333
6cb15c6f1d9f isar: no_pos flag;
wenzelm
parents: 7243
diff changeset
   313
fun gen_loop term no_pos =
6cb15c6f1d9f isar: no_pos flag;
wenzelm
parents: 7243
diff changeset
   314
 (Context.reset_context ();
15989
80dd2f5780df re-init ml_prompts after loop termination;
wenzelm
parents: 15973
diff changeset
   315
  Toplevel.loop (isar term no_pos);
80dd2f5780df re-init ml_prompts after loop termination;
wenzelm
parents: 15973
diff changeset
   316
  ml_prompts "ML> " "ML# ");
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   317
7333
6cb15c6f1d9f isar: no_pos flag;
wenzelm
parents: 7243
diff changeset
   318
fun gen_main term no_pos =
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   319
 (Toplevel.set_state Toplevel.toplevel;
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   320
  writeln (Session.welcome ());
7333
6cb15c6f1d9f isar: no_pos flag;
wenzelm
parents: 7243
diff changeset
   321
  gen_loop term no_pos);
6860
8dc6a1e6fa13 added sync;
wenzelm
parents: 6733
diff changeset
   322
15830
74d8412b1a27 added structure Isar (from isar.ML);
wenzelm
parents: 15570
diff changeset
   323
structure Isar =
74d8412b1a27 added structure Isar (from isar.ML);
wenzelm
parents: 15570
diff changeset
   324
struct
18064
f5727fa16c77 added Isar.state/exn;
wenzelm
parents: 17975
diff changeset
   325
  val state = Toplevel.get_state;
f5727fa16c77 added Isar.state/exn;
wenzelm
parents: 17975
diff changeset
   326
  val exn = Toplevel.exn;
15830
74d8412b1a27 added structure Isar (from isar.ML);
wenzelm
parents: 15570
diff changeset
   327
  fun main () = gen_main false false;
74d8412b1a27 added structure Isar (from isar.ML);
wenzelm
parents: 15570
diff changeset
   328
  fun loop () = gen_loop false false;
74d8412b1a27 added structure Isar (from isar.ML);
wenzelm
parents: 15570
diff changeset
   329
  fun sync_main () = gen_main true true;
74d8412b1a27 added structure Isar (from isar.ML);
wenzelm
parents: 15570
diff changeset
   330
  fun sync_loop () = gen_loop true true;
18684
38d72231b41d added Isar.toplevel;
wenzelm
parents: 18326
diff changeset
   331
  val toplevel = Toplevel.program;
15830
74d8412b1a27 added structure Isar (from isar.ML);
wenzelm
parents: 15570
diff changeset
   332
end;
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   333
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   334
6373
47b357194f32 added (improper_)command;
wenzelm
parents: 6332
diff changeset
   335
(*final declarations of this structure!*)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15224
diff changeset
   336
val command = parser false NONE;
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15224
diff changeset
   337
val markup_command = parser false o SOME;
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15224
diff changeset
   338
val improper_command = parser true NONE;
6685
wenzelm
parents: 6641
diff changeset
   339
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   340
end;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   341
6199
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   342
(*setup theory syntax dependent operations*)
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   343
ThyLoad.deps_thy_fn := OuterSyntax.deps_thy;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   344
ThyLoad.load_thy_fn := OuterSyntax.load_thy;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   345
structure ThyLoad: THY_LOAD = ThyLoad;
9b1be867e21a removed load;
wenzelm
parents: 6107
diff changeset
   346
5829
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   347
structure BasicOuterSyntax: BASIC_OUTER_SYNTAX = OuterSyntax;
0acb30dd92bc The global Isabelle/Isar outer syntax.
wenzelm
parents:
diff changeset
   348
open BasicOuterSyntax;