src/Pure/Isar/outer_syntax.ML
author wenzelm
Tue Nov 24 12:00:05 1998 +0100 (1998-11-24)
changeset 5952 7d4ec8992b23
parent 5923 5a8c731b1532
child 5992 263051aaf0de
permissions -rw-r--r--
added commands;
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@5829
    88
    Pretty.writeln (Pretty.big_list "general commands:" (map pretty_cmd cmds));
wenzelm@5829
    89
    Pretty.writeln (Pretty.big_list "interactive commands:" (map pretty_cmd int_cmds))
wenzelm@5829
    90
  end;
wenzelm@5829
    91
wenzelm@5829
    92
wenzelm@5829
    93
(* augment syntax *)
wenzelm@5829
    94
wenzelm@5829
    95
fun add_keywords keywords =
wenzelm@5829
    96
  global_lexicon := Scan.extend_lexicon (! global_lexicon) (map Symbol.explode keywords);
wenzelm@5829
    97
wenzelm@5829
    98
fun add_parser (tab, Parser (name, comment, int_only, parse)) =
wenzelm@5829
    99
 (if is_none (Symtab.lookup (tab, name)) then ()
wenzelm@5829
   100
  else warning ("Redefined outer syntax command " ^ quote name);
wenzelm@5829
   101
  Symtab.update ((name, (comment, (int_only, parse))), tab));
wenzelm@5829
   102
wenzelm@5829
   103
fun add_parsers parsers =
wenzelm@5829
   104
  (global_parsers := foldl add_parser (! global_parsers, parsers);
wenzelm@5829
   105
    add_keywords (map (fn Parser (name, _, _, _) => name) parsers));
wenzelm@5829
   106
wenzelm@5829
   107
wenzelm@5829
   108
(* get current lexer / parser *)
wenzelm@5829
   109
wenzelm@5829
   110
(*Note: the syntax for files is statically determined at the very
wenzelm@5829
   111
  beginning; for interactive processing it may change dynamically.*)
wenzelm@5829
   112
wenzelm@5829
   113
fun get_lexicon () = ! global_lexicon;
wenzelm@5829
   114
fun get_parser () = apsome snd o curry Symtab.lookup (! global_parsers);
wenzelm@5829
   115
wenzelm@5829
   116
wenzelm@5829
   117
wenzelm@5829
   118
(** read theory **)
wenzelm@5829
   119
wenzelm@5829
   120
(* source *)
wenzelm@5829
   121
wenzelm@5829
   122
fun no_command cmd =
wenzelm@5829
   123
  Scan.one ((not o OuterLex.keyword_pred ((is_some o cmd) orf equal ";")) andf OuterLex.not_eof);
wenzelm@5829
   124
wenzelm@5829
   125
fun recover cmd =
wenzelm@5829
   126
  Scan.prompt "recover# " (Scan.one OuterLex.not_eof -- Scan.repeat (no_command cmd));
wenzelm@5829
   127
wenzelm@5829
   128
fun source do_recover cmd src =
wenzelm@5829
   129
  src
wenzelm@5829
   130
  |> Source.source OuterLex.stopper (Scan.bulk (fn xs => !!! (command (cmd ())) xs))
wenzelm@5829
   131
    (if do_recover then Some (fn xs => recover (cmd ()) xs) else None)
wenzelm@5829
   132
  |> Source.mapfilter I;
wenzelm@5829
   133
wenzelm@5829
   134
wenzelm@5829
   135
(* detect header *)
wenzelm@5829
   136
wenzelm@5829
   137
val head_lexicon =
wenzelm@5829
   138
  Scan.make_lexicon (map Symbol.explode ["+", ":", "=", "theory"]);
wenzelm@5829
   139
wenzelm@5829
   140
val head_parser =
wenzelm@5829
   141
  $$$ "theory" |-- !!! (name -- ($$$ "=" |-- enum "+" name --| (Scan.ahead eof || $$$ ":")));
wenzelm@5829
   142
wenzelm@5829
   143
fun get_header pos src =
wenzelm@5829
   144
  src
wenzelm@5829
   145
  |> Symbol.source false
wenzelm@5829
   146
  |> OuterLex.source false (K head_lexicon) pos
wenzelm@5829
   147
  |> Source.source OuterLex.stopper (Scan.single (Scan.option head_parser)) None
wenzelm@5829
   148
  |> (fst o the o Source.get_single);
wenzelm@5829
   149
wenzelm@5829
   150
wenzelm@5829
   151
(* read text (including header) *)
wenzelm@5829
   152
wenzelm@5829
   153
fun read_text pos src =
wenzelm@5829
   154
  src
wenzelm@5829
   155
  |> Symbol.source false
wenzelm@5829
   156
  |> OuterLex.source false (K (get_lexicon ())) pos
wenzelm@5829
   157
  |> source false (K (get_parser ()))
wenzelm@5829
   158
  |> Source.exhaust;
wenzelm@5829
   159
wenzelm@5829
   160
fun read_file name = read_text (Position.line_name 1 (quote name)) (Source.of_file name);
wenzelm@5829
   161
wenzelm@5829
   162
wenzelm@5829
   163
(* interactive source of state transformers *)
wenzelm@5829
   164
wenzelm@5829
   165
val isar =
wenzelm@5829
   166
  Source.tty
wenzelm@5829
   167
  |> Symbol.source true
wenzelm@5829
   168
  |> OuterLex.source true get_lexicon (Position.line_name 1 "stdin")
wenzelm@5829
   169
  |> source true get_parser;
wenzelm@5829
   170
wenzelm@5829
   171
wenzelm@5829
   172
wenzelm@5829
   173
(** the read-eval-print loop **)
wenzelm@5829
   174
wenzelm@5923
   175
(* main loop *)
wenzelm@5923
   176
wenzelm@5883
   177
fun loop () = (Context.reset_context (); Toplevel.loop isar);
wenzelm@5829
   178
wenzelm@5829
   179
fun main () =
wenzelm@5829
   180
 (Toplevel.set_state Toplevel.toplevel;
wenzelm@5829
   181
  ml_prompts "ML> " "ML# ";
wenzelm@5829
   182
  writeln ("\n\n" ^ Context.welcome ());
wenzelm@5883
   183
  loop ());
wenzelm@5829
   184
wenzelm@5829
   185
wenzelm@5923
   186
(* load *)
wenzelm@5923
   187
wenzelm@5923
   188
fun load name = Toplevel.excursion (read_file (name ^ ".thy"))
wenzelm@5923
   189
  handle exn => error (Toplevel.exn_message exn);
wenzelm@5923
   190
wenzelm@5923
   191
wenzelm@5829
   192
(* help *)
wenzelm@5829
   193
wenzelm@5829
   194
fun help () =
wenzelm@5829
   195
  writeln ("This is Isabelle's underlying ML system (" ^ ml_system ^ ");\n\
wenzelm@5883
   196
    \invoke 'loop();' to enter the Isar loop.");
wenzelm@5829
   197
wenzelm@5829
   198
wenzelm@5829
   199
end;
wenzelm@5829
   200
wenzelm@5829
   201
structure BasicOuterSyntax: BASIC_OUTER_SYNTAX = OuterSyntax;
wenzelm@5829
   202
open BasicOuterSyntax;