src/Pure/Isar/outer_syntax.ML
author wenzelm
Tue Jan 12 15:19:09 1999 +0100 (1999-01-12 ago)
changeset 6095 9f75a45384dd
parent 5992 263051aaf0de
child 6107 1418bc571f23
permissions -rw-r--r--
tuned msg;
wenzelm@5829
     1
(*  Title:      Pure/Isar/outer_syntax.ML
wenzelm@5829
     2
    ID:         $Id$
wenzelm@5829
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5829
     4
wenzelm@5829
     5
The global Isabelle/Isar outer syntax.
wenzelm@5829
     6
*)
wenzelm@5829
     7
wenzelm@5829
     8
signature BASIC_OUTER_SYNTAX =
wenzelm@5829
     9
sig
wenzelm@5829
    10
  val main: unit -> unit
wenzelm@5883
    11
  val loop: unit -> unit
wenzelm@5829
    12
  val help: unit -> unit
wenzelm@5923
    13
  val load: string -> unit
wenzelm@5829
    14
end;
wenzelm@5829
    15
wenzelm@5829
    16
signature OUTER_SYNTAX =
wenzelm@5829
    17
sig
wenzelm@5829
    18
  include BASIC_OUTER_SYNTAX
wenzelm@5829
    19
  type token
wenzelm@5829
    20
  type parser
wenzelm@5829
    21
  val parser: bool -> string -> string ->
wenzelm@5829
    22
    (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
wenzelm@5883
    23
  val print_outer_syntax: unit -> unit
wenzelm@5952
    24
  val commands: unit -> string list
wenzelm@5829
    25
  val add_keywords: string list -> unit
wenzelm@5829
    26
  val add_parsers: parser list -> unit
wenzelm@5829
    27
  val get_header: Position.T -> (string, 'a) Source.source -> (bstring * bstring list) option
wenzelm@5829
    28
  val read_text: Position.T -> (string, 'a) Source.source -> Toplevel.transition list
wenzelm@5829
    29
  val read_file: string -> Toplevel.transition list
wenzelm@5829
    30
  val isar: Toplevel.isar
wenzelm@5829
    31
end;
wenzelm@5829
    32
wenzelm@5829
    33
structure OuterSyntax: OUTER_SYNTAX =
wenzelm@5829
    34
struct
wenzelm@5829
    35
wenzelm@5829
    36
open OuterParse;
wenzelm@5829
    37
wenzelm@5829
    38
wenzelm@5829
    39
(** outer syntax **)
wenzelm@5829
    40
wenzelm@5829
    41
(* parsers *)
wenzelm@5829
    42
wenzelm@5829
    43
type token = OuterLex.token;
wenzelm@5829
    44
type parser_fn = token list -> (Toplevel.transition -> Toplevel.transition) * token list;
wenzelm@5829
    45
wenzelm@5829
    46
datatype parser =
wenzelm@5829
    47
  Parser of string * string * bool * parser_fn;
wenzelm@5829
    48
wenzelm@5829
    49
fun parser int_only name comment parse = Parser (name, comment, int_only, parse);
wenzelm@5829
    50
wenzelm@5829
    51
wenzelm@5829
    52
(* parse command *)
wenzelm@5829
    53
wenzelm@5829
    54
fun command_name cmd =
wenzelm@5829
    55
  group "command"
wenzelm@5829
    56
    (position (Scan.one (OuterLex.keyword_pred (is_some o cmd)) >> OuterLex.val_of));
wenzelm@5829
    57
wenzelm@5829
    58
fun command_body cmd (name, _) =
wenzelm@5829
    59
  let val (int_only, parse) = the (cmd name)
wenzelm@5829
    60
  in !!! (Scan.prompt (name ^ "# ") (parse >> pair int_only)) end;
wenzelm@5829
    61
wenzelm@5829
    62
fun command cmd =
wenzelm@5829
    63
  $$$ ";" >> K None ||
wenzelm@5829
    64
  command_name cmd :-- command_body cmd >> (fn ((name, pos), (int_only, f)) =>
wenzelm@5829
    65
    Some (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |>
wenzelm@5829
    66
      Toplevel.interactive int_only |> f));
wenzelm@5829
    67
wenzelm@5829
    68
wenzelm@5829
    69
wenzelm@5829
    70
(** global syntax state **)
wenzelm@5829
    71
wenzelm@5829
    72
val global_lexicon = ref Scan.empty_lexicon;
wenzelm@5829
    73
val global_parsers = ref (Symtab.empty: (string * (bool * parser_fn)) Symtab.table);
wenzelm@5829
    74
wenzelm@5952
    75
fun commands () = Symtab.keys (! global_parsers);
wenzelm@5952
    76
wenzelm@5829
    77
wenzelm@5829
    78
(* print syntax *)
wenzelm@5829
    79
wenzelm@5829
    80
fun print_outer_syntax () =
wenzelm@5829
    81
  let
wenzelm@5829
    82
    val keywords = map implode (Scan.dest_lexicon (! global_lexicon));
wenzelm@5829
    83
    fun pretty_cmd (name, (comment, _)) =
wenzelm@5829
    84
      Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
wenzelm@5829
    85
    val (int_cmds, cmds) = partition (#1 o #2 o #2) (Symtab.dest (! global_parsers));
wenzelm@5829
    86
  in
wenzelm@5829
    87
    Pretty.writeln (Pretty.strs ("keywords:" :: map quote keywords));
wenzelm@6095
    88
    Pretty.writeln (Pretty.big_list "proper commands:" (map pretty_cmd cmds));
wenzelm@6095
    89
    Pretty.writeln (Pretty.big_list "improper commands (interactive-only):"
wenzelm@6095
    90
      (map pretty_cmd int_cmds))
wenzelm@5829
    91
  end;
wenzelm@5829
    92
wenzelm@5829
    93
wenzelm@5829
    94
(* augment syntax *)
wenzelm@5829
    95
wenzelm@5829
    96
fun add_keywords keywords =
wenzelm@5829
    97
  global_lexicon := Scan.extend_lexicon (! global_lexicon) (map Symbol.explode keywords);
wenzelm@5829
    98
wenzelm@5829
    99
fun add_parser (tab, Parser (name, comment, int_only, parse)) =
wenzelm@5829
   100
 (if is_none (Symtab.lookup (tab, name)) then ()
wenzelm@5829
   101
  else warning ("Redefined outer syntax command " ^ quote name);
wenzelm@5829
   102
  Symtab.update ((name, (comment, (int_only, parse))), tab));
wenzelm@5829
   103
wenzelm@5829
   104
fun add_parsers parsers =
wenzelm@5829
   105
  (global_parsers := foldl add_parser (! global_parsers, parsers);
wenzelm@5829
   106
    add_keywords (map (fn Parser (name, _, _, _) => name) parsers));
wenzelm@5829
   107
wenzelm@5829
   108
wenzelm@5829
   109
(* get current lexer / parser *)
wenzelm@5829
   110
wenzelm@5829
   111
(*Note: the syntax for files is statically determined at the very
wenzelm@5829
   112
  beginning; for interactive processing it may change dynamically.*)
wenzelm@5829
   113
wenzelm@5829
   114
fun get_lexicon () = ! global_lexicon;
wenzelm@5829
   115
fun get_parser () = apsome snd o curry Symtab.lookup (! global_parsers);
wenzelm@5829
   116
wenzelm@5829
   117
wenzelm@5829
   118
wenzelm@5829
   119
(** read theory **)
wenzelm@5829
   120
wenzelm@5829
   121
(* source *)
wenzelm@5829
   122
wenzelm@5829
   123
fun no_command cmd =
wenzelm@5829
   124
  Scan.one ((not o OuterLex.keyword_pred ((is_some o cmd) orf equal ";")) andf OuterLex.not_eof);
wenzelm@5829
   125
wenzelm@5829
   126
fun recover cmd =
wenzelm@5829
   127
  Scan.prompt "recover# " (Scan.one OuterLex.not_eof -- Scan.repeat (no_command cmd));
wenzelm@5829
   128
wenzelm@5829
   129
fun source do_recover cmd src =
wenzelm@5829
   130
  src
wenzelm@5829
   131
  |> Source.source OuterLex.stopper (Scan.bulk (fn xs => !!! (command (cmd ())) xs))
wenzelm@5829
   132
    (if do_recover then Some (fn xs => recover (cmd ()) xs) else None)
wenzelm@5829
   133
  |> Source.mapfilter I;
wenzelm@5829
   134
wenzelm@5829
   135
wenzelm@5829
   136
(* detect header *)
wenzelm@5829
   137
wenzelm@5829
   138
val head_lexicon =
wenzelm@5829
   139
  Scan.make_lexicon (map Symbol.explode ["+", ":", "=", "theory"]);
wenzelm@5829
   140
wenzelm@5829
   141
val head_parser =
wenzelm@5829
   142
  $$$ "theory" |-- !!! (name -- ($$$ "=" |-- enum "+" name --| (Scan.ahead eof || $$$ ":")));
wenzelm@5829
   143
wenzelm@5829
   144
fun get_header pos src =
wenzelm@5829
   145
  src
wenzelm@5829
   146
  |> Symbol.source false
wenzelm@5829
   147
  |> OuterLex.source false (K head_lexicon) pos
wenzelm@5829
   148
  |> Source.source OuterLex.stopper (Scan.single (Scan.option head_parser)) None
wenzelm@5829
   149
  |> (fst o the o Source.get_single);
wenzelm@5829
   150
wenzelm@5829
   151
wenzelm@5829
   152
(* read text (including header) *)
wenzelm@5829
   153
wenzelm@5829
   154
fun read_text pos src =
wenzelm@5829
   155
  src
wenzelm@5829
   156
  |> Symbol.source false
wenzelm@5829
   157
  |> OuterLex.source false (K (get_lexicon ())) pos
wenzelm@5829
   158
  |> source false (K (get_parser ()))
wenzelm@5829
   159
  |> Source.exhaust;
wenzelm@5829
   160
wenzelm@5829
   161
fun read_file name = read_text (Position.line_name 1 (quote name)) (Source.of_file name);
wenzelm@5829
   162
wenzelm@5829
   163
wenzelm@5829
   164
(* interactive source of state transformers *)
wenzelm@5829
   165
wenzelm@5829
   166
val isar =
wenzelm@5829
   167
  Source.tty
wenzelm@5829
   168
  |> Symbol.source true
wenzelm@5829
   169
  |> OuterLex.source true get_lexicon (Position.line_name 1 "stdin")
wenzelm@5829
   170
  |> source true get_parser;
wenzelm@5829
   171
wenzelm@5829
   172
wenzelm@5829
   173
wenzelm@5829
   174
(** the read-eval-print loop **)
wenzelm@5829
   175
wenzelm@5923
   176
(* main loop *)
wenzelm@5923
   177
wenzelm@5883
   178
fun loop () = (Context.reset_context (); Toplevel.loop isar);
wenzelm@5829
   179
wenzelm@5829
   180
fun main () =
wenzelm@5829
   181
 (Toplevel.set_state Toplevel.toplevel;
wenzelm@5829
   182
  ml_prompts "ML> " "ML# ";
wenzelm@5992
   183
  writeln (Context.welcome ());
wenzelm@5883
   184
  loop ());
wenzelm@5829
   185
wenzelm@5829
   186
wenzelm@5923
   187
(* load *)
wenzelm@5923
   188
wenzelm@5923
   189
fun load name = Toplevel.excursion (read_file (name ^ ".thy"))
wenzelm@5923
   190
  handle exn => error (Toplevel.exn_message exn);
wenzelm@5923
   191
wenzelm@5923
   192
wenzelm@5829
   193
(* help *)
wenzelm@5829
   194
wenzelm@5829
   195
fun help () =
wenzelm@5829
   196
  writeln ("This is Isabelle's underlying ML system (" ^ ml_system ^ ");\n\
wenzelm@5883
   197
    \invoke 'loop();' to enter the Isar loop.");
wenzelm@5829
   198
wenzelm@5829
   199
wenzelm@5829
   200
end;
wenzelm@5829
   201
wenzelm@5829
   202
structure BasicOuterSyntax: BASIC_OUTER_SYNTAX = OuterSyntax;
wenzelm@5829
   203
open BasicOuterSyntax;