src/Pure/Isar/outer_syntax.ML
author haftmann
Fri Nov 10 07:44:47 2006 +0100 (2006-11-10)
changeset 21286 b5e7b80caa6a
parent 21207 cef082634be9
child 21401 faddc6504177
permissions -rw-r--r--
introduces canonical AList functions for loop_tacs
     1 (*  Title:      Pure/Isar/outer_syntax.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 The global Isabelle/Isar outer syntax.
     6 *)
     7 
     8 signature BASIC_OUTER_SYNTAX =
     9 sig
    10   structure Isar:
    11     sig
    12       val state: unit -> Toplevel.state
    13       val exn: unit -> (exn * string) option
    14       val context: unit -> Proof.context
    15       val main: unit -> unit
    16       val loop: unit -> unit
    17       val sync_main: unit -> unit
    18       val sync_loop: unit -> unit
    19       val toplevel: (unit -> 'a) -> 'a
    20     end;
    21 end;
    22 
    23 signature OUTER_SYNTAX =
    24 sig
    25   include BASIC_OUTER_SYNTAX
    26   type token
    27   type parser
    28   val command: string -> string -> OuterKeyword.T ->
    29     (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
    30   val markup_command: IsarOutput.markup -> string -> string -> OuterKeyword.T ->
    31     (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
    32   val improper_command: string -> string -> OuterKeyword.T ->
    33     (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
    34   val is_keyword: string -> bool
    35   val dest_keywords: unit -> string list
    36   val dest_parsers: unit -> (string * string * string * bool) list
    37   val print_outer_syntax: unit -> unit
    38   val print_commands: Toplevel.transition -> Toplevel.transition
    39   val add_keywords: string list -> unit
    40   val add_parsers: parser list -> unit
    41   val check_text: string * Position.T -> Toplevel.node option -> unit
    42   val deps_thy: string -> bool -> Path.T -> string list * Path.T list
    43   val load_thy: string -> bool -> bool -> Path.T -> unit
    44   val isar: bool -> bool -> unit Toplevel.isar
    45   val scan: string -> OuterLex.token list
    46   val read: OuterLex.token list -> (string * OuterLex.token list * Toplevel.transition) list
    47 end;
    48 
    49 structure OuterSyntax : OUTER_SYNTAX  =
    50 struct
    51 
    52 structure T = OuterLex;
    53 structure P = OuterParse;
    54 
    55 
    56 (** outer syntax **)
    57 
    58 (* parsers *)
    59 
    60 type token = T.token;
    61 type parser_fn = token list -> (Toplevel.transition -> Toplevel.transition) * token list;
    62 
    63 datatype parser =
    64   Parser of string * (string * OuterKeyword.T * IsarOutput.markup option) * bool * parser_fn;
    65 
    66 fun parser int_only markup name comment kind parse =
    67   Parser (name, (comment, kind, markup), int_only, parse);
    68 
    69 
    70 (* parse command *)
    71 
    72 local
    73 
    74 fun terminate false = Scan.succeed ()
    75   | terminate true = P.group "end of input" (Scan.option P.sync -- P.semicolon >> K ());
    76 
    77 fun trace false parse = parse
    78   | trace true parse = Scan.trace parse >> (fn (f, toks) => f o Toplevel.source toks);
    79 
    80 fun body cmd do_trace (name, _) =
    81   (case cmd name of
    82     SOME (int_only, parse) =>
    83       P.!!! (Scan.prompt (name ^ "# ") (trace do_trace (P.tags |-- parse) >> pair int_only))
    84   | NONE => sys_error ("no parser for outer syntax command " ^ quote name));
    85 
    86 in
    87 
    88 fun command do_terminate do_trace cmd =
    89   P.semicolon >> K NONE ||
    90   P.sync >> K NONE ||
    91   (P.position P.command :-- body cmd do_trace) --| terminate do_terminate
    92     >> (fn ((name, pos), (int_only, f)) =>
    93       SOME (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |>
    94         Toplevel.interactive int_only |> f));
    95 
    96 end;
    97 
    98 
    99 
   100 (** global outer syntax **)
   101 
   102 local
   103 
   104 val global_lexicons = ref (Scan.empty_lexicon, Scan.empty_lexicon);
   105 val global_parsers =
   106   ref (Symtab.empty: (((string * OuterKeyword.T) * (bool * parser_fn)) * IsarOutput.markup option)
   107     Symtab.table);
   108 val global_markups = ref ([]: (string * IsarOutput.markup) list);
   109 
   110 fun change_lexicons f =
   111   let val lexs = f (! global_lexicons) in
   112     (case (op inter_string) (pairself Scan.dest_lexicon lexs) of
   113       [] => global_lexicons := lexs
   114     | bads => error ("Clash of outer syntax commands and keywords: " ^ commas_quote bads))
   115   end;
   116 
   117 fun make_markups () = global_markups :=
   118   Symtab.fold (fn (name, (_, SOME m)) => cons (name, m) | _ => I) (! global_parsers) [];
   119 
   120 fun change_parsers f = (Library.change global_parsers f; make_markups ());
   121 
   122 in
   123 
   124 
   125 (* access current syntax *)
   126 
   127 (*Note: the syntax for files is statically determined at the very
   128   beginning; for interactive processing it may change dynamically.*)
   129 
   130 fun get_lexicons () = ! global_lexicons;
   131 fun get_parsers () = ! global_parsers;
   132 fun get_parser () = Option.map (#2 o #1) o Symtab.lookup (get_parsers ());
   133 
   134 fun command_tags name =
   135   (case Symtab.lookup (get_parsers ()) name of
   136     SOME (((_, k), _), _) => OuterKeyword.tags_of k
   137   | NONE => []);
   138 
   139 fun is_markup kind name = (AList.lookup (op =) (! global_markups) name = SOME kind);
   140 
   141 
   142 (* augment syntax *)
   143 
   144 fun add_keywords keywords = change_lexicons (apfst (fn lex =>
   145   (Scan.extend_lexicon lex (map Symbol.explode keywords))));
   146 
   147 fun add_parser (Parser (name, (comment, kind, markup), int_only, parse)) tab =
   148  (if not (Symtab.defined tab name) then ()
   149   else warning ("Redefined outer syntax command " ^ quote name);
   150   Symtab.update (name, (((comment, kind), (int_only, parse)), markup)) tab);
   151 
   152 fun add_parsers parsers =
   153   (change_parsers (fold add_parser parsers);
   154     change_lexicons (apsnd (fn lex => Scan.extend_lexicon lex
   155       (map (fn Parser (name, _, _, _) => Symbol.explode name) parsers))));
   156 
   157 end;
   158 
   159 
   160 (* print syntax *)
   161 
   162 fun is_keyword s = Scan.is_literal (#1 (get_lexicons ())) (Symbol.explode s);
   163 fun dest_keywords () = Scan.dest_lexicon (#1 (get_lexicons ()));
   164 
   165 fun dest_parsers () =
   166   get_parsers () |> Symtab.dest |> sort_wrt #1
   167   |> map (fn (name, (((cmt, kind), (int_only, _)), _)) =>
   168     (name, cmt, OuterKeyword.kind_of kind, int_only));
   169 
   170 fun print_outer_syntax () =
   171   let
   172     fun pretty_cmd (name, comment, _, _) =
   173       Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
   174     val (int_cmds, cmds) = List.partition #4 (dest_parsers ());
   175   in
   176     [Pretty.strs ("syntax keywords:" :: map quote (dest_keywords ())),
   177       Pretty.big_list "commands:" (map pretty_cmd cmds),
   178       Pretty.big_list "interactive-only commands:" (map pretty_cmd int_cmds)]
   179     |> Pretty.chunks |> Pretty.writeln
   180   end;
   181 
   182 val print_commands = Toplevel.imperative print_outer_syntax;
   183 
   184 
   185 
   186 (** toplevel parsing **)
   187 
   188 (* basic sources *)
   189 
   190 fun toplevel_source term do_trace do_recover cmd src =
   191   let
   192     val no_terminator =
   193       Scan.unless P.semicolon (Scan.one (T.not_sync andf T.not_eof));
   194     fun recover x = (Scan.prompt "recover# " (Scan.repeat no_terminator) >> K [NONE]) x;
   195   in
   196     src
   197     |> T.source_proper
   198     |> Source.source T.stopper
   199       (Scan.bulk (P.$$$ "--" -- P.!!! P.text >> K NONE || P.not_eof >> SOME))
   200       (if do_recover then SOME recover else NONE)
   201     |> Source.map_filter I
   202     |> Source.source T.stopper (Scan.bulk (fn xs => P.!!! (command term do_trace (cmd ())) xs))
   203       (if do_recover then SOME recover else NONE)
   204     |> Source.map_filter I
   205   end;
   206 
   207 
   208 (* interactive source of toplevel transformers *)
   209 
   210 fun isar term no_pos =
   211   Source.tty
   212   |> Symbol.source true
   213   |> T.source true get_lexicons
   214     (if no_pos then Position.none else Position.line_name 1 "stdin")
   215   |> toplevel_source term false true get_parser;
   216 
   217 
   218 (* scan text *)
   219 
   220 fun scan str =
   221   Source.of_string str
   222   |> Symbol.source false
   223   |> T.source true get_lexicons Position.none
   224   |> Source.exhaust;
   225 
   226 
   227 (* read tokens with trace *)
   228 
   229 fun read toks =
   230   Source.of_list toks
   231   |> toplevel_source false true true get_parser
   232   |> Source.exhaust
   233   |> map (fn tr => (Toplevel.name_of tr, the (Toplevel.source_of tr), tr));
   234 
   235 
   236 
   237 (** read theory **)
   238 
   239 (* check_text *)
   240 
   241 fun check_text s state = (IsarOutput.eval_antiquote (#1 (get_lexicons ())) state s; ());
   242 
   243 
   244 (* deps_thy *)
   245 
   246 fun deps_thy name ml path =
   247   let
   248     val src = Source.of_string (File.read path);
   249     val pos = Path.position path;
   250     val (name', parents, files) = ThyHeader.read (src, pos);
   251     val ml_path = ThyLoad.ml_path name;
   252     val ml_file = if ml andalso is_some (ThyLoad.check_file NONE ml_path) then [ml_path] else [];
   253   in
   254     if name <> name' then
   255       error ("Filename " ^ quote (Path.pack path) ^
   256         " does not agree with theory name " ^ quote name')
   257     else (parents, map (Path.unpack o #1) files @ ml_file)
   258   end;
   259 
   260 
   261 (* load_thy *)
   262 
   263 local
   264 
   265 fun try_ml_file name time =
   266   let val path = ThyLoad.ml_path name in
   267     if is_none (ThyLoad.check_file NONE path) then ()
   268     else
   269       let
   270         val _ = warning ("Loading legacy ML script " ^ quote (Path.pack path));
   271         val tr = Toplevel.imperative (fn () => ThyInfo.load_file time path);
   272         val tr_name = if time then "time_use" else "use";
   273       in Toplevel.excursion [Toplevel.empty |> Toplevel.name tr_name |> tr] end
   274   end;
   275 
   276 fun run_thy name path =
   277   let
   278     val text = Source.of_list (Library.untabify (explode (File.read path)));
   279     val _ = Present.init_theory name;
   280     val _ = Present.verbatim_source name (fn () => Source.exhaust (Symbol.source false text));
   281     val toks = text
   282       |> Symbol.source false
   283       |> T.source false (K (get_lexicons ())) (Path.position path)
   284       |> Source.exhausted;
   285     val trs = toks
   286       |> toplevel_source false false false (K (get_parser ()))
   287       |> Source.exhaust;
   288   in
   289     IsarOutput.present_thy (#1 (get_lexicons ())) command_tags is_markup trs toks
   290     |> Buffer.content
   291     |> Present.theory_output name
   292   end;
   293 
   294 in
   295 
   296 fun load_thy name ml time path =
   297  (if time then
   298     timeit (fn () =>
   299      (writeln ("\n**** Starting theory " ^ quote name ^ " ****");
   300       run_thy name path;
   301       writeln ("**** Finished theory " ^ quote name ^ " ****\n")))
   302   else run_thy name path;
   303   Context.context (ThyInfo.get_theory name);
   304   if ml then try_ml_file name time else ());
   305 
   306 end;
   307 
   308 
   309 
   310 (** the read-eval-print loop **)
   311 
   312 (* main loop *)
   313 
   314 fun gen_loop term no_pos =
   315  (Context.reset_context ();
   316   Toplevel.loop (isar term no_pos);
   317   ml_prompts "ML> " "ML# ");
   318 
   319 fun gen_main term no_pos =
   320  (Toplevel.set_state Toplevel.toplevel;
   321   writeln (Session.welcome ());
   322   gen_loop term no_pos);
   323 
   324 structure Isar =
   325 struct
   326   val state = Toplevel.get_state;
   327   val exn = Toplevel.exn;
   328   fun context () =
   329     Context.proof_of (Toplevel.context_of (state ()))
   330       handle Toplevel.UNDEF => error "Unknown context";
   331   fun main () = gen_main false false;
   332   fun loop () = gen_loop false false;
   333   fun sync_main () = gen_main true true;
   334   fun sync_loop () = gen_loop true true;
   335   val toplevel = Toplevel.program;
   336 end;
   337 
   338 
   339 (*final declarations of this structure!*)
   340 val command = parser false NONE;
   341 val markup_command = parser false o SOME;
   342 val improper_command = parser true NONE;
   343 
   344 end;
   345 
   346 (*setup theory syntax dependent operations*)
   347 ThyLoad.deps_thy_fn := OuterSyntax.deps_thy;
   348 ThyLoad.load_thy_fn := OuterSyntax.load_thy;
   349 structure ThyLoad: THY_LOAD = ThyLoad;
   350 
   351 structure BasicOuterSyntax: BASIC_OUTER_SYNTAX = OuterSyntax;
   352 open BasicOuterSyntax;