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