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