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