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