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