src/Pure/Isar/outer_syntax.ML
author wenzelm
Fri Jun 30 21:21:11 2000 +0200 (2000-06-30)
changeset 9213 2651a4db8883
parent 9132 52286129faa5
child 9223 eb752c2fac22
permissions -rw-r--r--
help_antiquotations;
     1 (*  Title:      Pure/Isar/outer_syntax.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     5 
     6 The global Isabelle/Isar outer syntax.
     7 *)
     8 
     9 signature BASIC_OUTER_SYNTAX =
    10 sig
    11   val main: unit -> unit
    12   val loop: unit -> unit
    13   val sync_main: unit -> unit
    14   val sync_loop: unit -> unit
    15   val help: unit -> unit
    16 end;
    17 
    18 signature OUTER_SYNTAX =
    19 sig
    20   include BASIC_OUTER_SYNTAX
    21   structure Keyword:
    22     sig
    23       val control: string
    24       val diag: string
    25       val thy_begin: string
    26       val thy_switch: string
    27       val thy_end: string
    28       val thy_heading: string
    29       val thy_decl: string
    30       val thy_goal: string
    31       val qed: string
    32       val qed_block: string
    33       val qed_global: string
    34       val prf_goal: string
    35       val prf_block: string
    36       val prf_open: string
    37       val prf_close: string
    38       val prf_chain: string
    39       val prf_decl: string
    40       val prf_asm: string
    41       val prf_asm_goal: string
    42       val prf_script: string
    43       val kinds: string list
    44     end
    45   type token
    46   type parser
    47   val command: string -> string -> string ->
    48     (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
    49   val markup_command: IsarOutput.markup -> string -> string -> string ->
    50     (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
    51   val improper_command: string -> string -> string ->
    52     (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
    53   val dest_keywords: unit -> string list
    54   val dest_parsers: unit -> (string * string * string * bool) list
    55   val print_outer_syntax: unit -> unit
    56   val print_help: Toplevel.transition -> Toplevel.transition
    57   val add_keywords: string list -> unit
    58   val add_parsers: parser list -> unit
    59   val deps_thy: string -> bool -> Path.T -> string list * Path.T list
    60   val load_thy: string -> bool -> bool -> Path.T -> unit
    61   val isar: bool -> bool -> Toplevel.isar
    62 end;
    63 
    64 structure OuterSyntax: OUTER_SYNTAX =
    65 struct
    66 
    67 structure T = OuterLex;
    68 structure P = OuterParse;
    69 
    70 
    71 (** outer syntax **)
    72 
    73 (* command keyword classification *)
    74 
    75 structure Keyword =
    76 struct
    77   val control = "control";
    78   val diag = "diag";
    79   val thy_begin = "theory-begin";
    80   val thy_switch = "theory-switch";
    81   val thy_end = "theory-end";
    82   val thy_heading = "theory-heading";
    83   val thy_decl = "theory-decl";
    84   val thy_goal = "theory-goal";
    85   val qed = "qed";
    86   val qed_block = "qed-block";
    87   val qed_global = "qed-global";
    88   val prf_goal = "proof-goal";
    89   val prf_block = "proof-block";
    90   val prf_open = "proof-open";
    91   val prf_close = "proof-close";
    92   val prf_chain = "proof-chain";
    93   val prf_decl = "proof-decl";
    94   val prf_asm = "proof-asm";
    95   val prf_asm_goal = "proof-asm-goal";
    96   val prf_script = "proof-script";
    97 
    98   val kinds = [control, diag, thy_begin, thy_switch, thy_end, thy_heading, thy_decl, thy_goal,
    99     qed, qed_block, qed_global, prf_goal, prf_block, prf_open, prf_close, prf_chain, prf_decl,
   100     prf_asm, prf_asm_goal, prf_script];
   101 end;
   102 
   103 
   104 (* parsers *)
   105 
   106 type token = T.token;
   107 type parser_fn = token list -> (Toplevel.transition -> Toplevel.transition) * token list;
   108 
   109 datatype parser =
   110   Parser of string * (string * string * IsarOutput.markup option) * bool * parser_fn;
   111 
   112 fun parser int_only markup name comment kind parse =
   113   Parser (name, (comment, kind, markup), int_only, parse);
   114 
   115 
   116 (* parse command *)
   117 
   118 local
   119 
   120 fun command_body cmd (name, _) =
   121   (case cmd name of
   122     Some (int_only, parse) => P.!!! (Scan.prompt (name ^ "# ") (parse >> pair int_only))
   123   | None => sys_error ("no parser for outer syntax command " ^ quote name));
   124 
   125 fun terminator false = Scan.succeed ()
   126   | terminator true = P.group "end of input" (Scan.option P.sync -- P.semicolon >> K ());
   127 
   128 in
   129 
   130 fun command term cmd =
   131   P.semicolon >> K None ||
   132   P.sync >> K None ||
   133   (P.position P.command :-- command_body cmd) --| terminator term
   134     >> (fn ((name, pos), (int_only, f)) =>
   135       Some (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |>
   136         Toplevel.interactive int_only |> f));
   137 
   138 end;
   139 
   140 
   141 
   142 (** global outer syntax **)
   143 
   144 local
   145 
   146 val global_lexicons = ref (Scan.empty_lexicon, Scan.empty_lexicon);
   147 val global_parsers =
   148   ref (Symtab.empty: (((string * string) * (bool * parser_fn)) * IsarOutput.markup option)
   149     Symtab.table);
   150 val global_markups = ref ([]: (string * IsarOutput.markup) list);
   151 
   152 fun change_lexicons f =
   153   let val lexs = f (! global_lexicons) in
   154     (case (op inter_string) (pairself Scan.dest_lexicon lexs) of
   155       [] => global_lexicons := lexs
   156     | bads => error ("Clash of outer syntax commands and keywords: " ^ commas_quote bads))
   157   end;
   158 
   159 fun get_markup (ms, (name, (_, Some m))) = (name, m) :: ms
   160   | get_markup (ms, _) = ms;
   161 
   162 fun make_markups () = global_markups := Symtab.foldl get_markup ([], ! global_parsers);
   163 fun change_parsers f = (Library.change global_parsers f; make_markups ());
   164 
   165 in
   166 
   167 
   168 (* access current syntax *)
   169 
   170 (*Note: the syntax for files is statically determined at the very
   171   beginning; for interactive processing it may change dynamically.*)
   172 
   173 fun get_lexicons () = ! global_lexicons;
   174 fun get_parsers () = ! global_parsers;
   175 fun get_parser () = apsome (#2 o #1) o curry Symtab.lookup (! global_parsers);
   176 
   177 fun is_markup kind name =
   178   (case assoc (! global_markups, name) of Some k => k = kind | None => false);
   179 fun markup kind = Scan.one (T.is_kind T.Command andf is_markup kind o T.val_of);
   180 
   181 
   182 (* augment syntax *)
   183 
   184 fun add_keywords keywords = change_lexicons (apfst (fn lex =>
   185   (Scan.extend_lexicon lex (map Symbol.explode keywords))));
   186 
   187 fun add_parser (tab, Parser (name, (comment, kind, markup), int_only, parse)) =
   188  (if is_none (Symtab.lookup (tab, name)) then ()
   189   else warning ("Redefined outer syntax command " ^ quote name);
   190   Symtab.update ((name, (((comment, kind), (int_only, parse)), markup)), tab));
   191 
   192 fun add_parsers parsers =
   193   (change_parsers (fn tab => foldl add_parser (tab, parsers));
   194     change_lexicons (apsnd (fn lex => Scan.extend_lexicon lex
   195       (map (fn Parser (name, _, _, _) => Symbol.explode name) parsers))));
   196 
   197 end;
   198 
   199 
   200 (* print syntax *)
   201 
   202 fun dest_keywords () = Scan.dest_lexicon (#1 (get_lexicons ()));
   203 
   204 fun dest_parsers () =
   205   map (fn (name, (((cmt, kind), (int_only, _)), _)) => (name, cmt, kind, int_only))
   206     (Symtab.dest (get_parsers ()));
   207 
   208 fun help_outer_syntax () =
   209   let
   210     fun pretty_cmd (name, comment, _, _) =
   211       Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
   212     val (int_cmds, cmds) = partition #4 (dest_parsers ());
   213   in
   214     [Pretty.strs ("syntax keywords:" :: map quote (dest_keywords ())),
   215       Pretty.big_list "proper commands:" (map pretty_cmd cmds),
   216       Pretty.big_list "improper commands (interactive-only):" (map pretty_cmd int_cmds)]
   217   end;
   218 
   219 val print_outer_syntax = Pretty.writeln o Pretty.chunks o help_outer_syntax;
   220 
   221 val print_help =
   222   Toplevel.keep (fn state =>
   223     let val opt_thy = try Toplevel.theory_of state in
   224       help_outer_syntax () @
   225       IsarOutput.help_antiquotations () @
   226       Method.help_methods opt_thy @
   227       Attrib.help_attributes opt_thy
   228       |> Pretty.chunks |> Pretty.writeln
   229     end);
   230 
   231 
   232 
   233 (** toplevel parsing **)
   234 
   235 (* basic sources *)
   236 
   237 fun token_source (src, pos) =
   238   src
   239   |> Symbol.source false
   240   |> T.source false (K (get_lexicons ())) pos;
   241 
   242 fun toplevel_source term do_recover cmd src =
   243   let
   244     val no_terminator =
   245       Scan.unless P.semicolon (Scan.one (T.not_sync andf T.not_eof));
   246     val recover = Scan.prompt "recover# " (Scan.repeat no_terminator);
   247   in
   248     src
   249     |> Source.source T.stopper
   250       (Scan.bulk (fn xs => P.!!! (command term (cmd ())) xs))
   251       (if do_recover then Some recover else None)
   252     |> Source.mapfilter I
   253   end;
   254 
   255 
   256 (* interactive source of toplevel transformers *)
   257 
   258 fun isar term no_pos =
   259   Source.tty
   260   |> Symbol.source true
   261   |> T.source true get_lexicons
   262     (if no_pos then Position.none else Position.line_name 1 "stdin")
   263   |> T.source_proper
   264   |> toplevel_source term true get_parser;
   265 
   266 
   267 
   268 (** read theory **)
   269 
   270 (* deps_thy *)
   271 
   272 fun deps_thy name ml path =
   273   let
   274     val src = Source.of_string (File.read path);
   275     val pos = Path.position path;
   276     val (name', parents, files) = ThyHeader.scan (src, pos);
   277     val ml_path = ThyLoad.ml_path name;
   278     val ml_file = if ml andalso is_some (ThyLoad.check_file ml_path) then [ml_path] else [];
   279   in
   280     if name <> name' then
   281       error ("Filename " ^ quote (Path.pack path) ^
   282         " does not agree with theory name " ^ quote name')
   283     else (parents, map (Path.unpack o #1) files @ ml_file)
   284   end;
   285 
   286 
   287 (* load_thy *)
   288 
   289 local
   290 
   291 fun try_ml_file name time =
   292   let
   293     val path = ThyLoad.ml_path name;
   294     val tr = Toplevel.imperative (fn () => ThyInfo.load_file time path);
   295     val tr_name = if time then "time_use" else "use";
   296   in
   297     if is_none (ThyLoad.check_file path) then ()
   298     else Toplevel.excursion [Toplevel.empty |> Toplevel.name tr_name |> tr]
   299   end;
   300 
   301 fun parse_thy src =
   302   src
   303   |> T.source_proper
   304   |> toplevel_source false false (K (get_parser ()))
   305   |> Source.exhaust;
   306 
   307 fun run_thy name path =
   308   let
   309     val pos = Path.position path;
   310     val text = Library.untabify (explode (File.read path));
   311     val text_src = Source.of_list text;
   312     fun present_text () = Source.exhaust (Symbol.source false text_src);
   313   in
   314     Present.init_theory name;
   315     Present.verbatim_source name present_text;
   316     if ThyHeader.is_old (text_src, pos) then (ThySyn.load_thy name text;
   317       Present.old_symbol_source name present_text)   (*note: text presented twice*)
   318     else
   319       let
   320         val tok_src = Source.exhausted (token_source (text_src, pos));
   321         val out = Toplevel.excursion_result
   322           (IsarOutput.parse_thy markup (#1 (get_lexicons ()))
   323             (parse_thy tok_src) tok_src);
   324       in Present.theory_output name (Buffer.content out) end
   325   end;
   326 
   327 in
   328 
   329 fun load_thy name ml time path =
   330  (if time then
   331     timeit (fn () =>
   332      (writeln ("\n**** Starting theory " ^ quote name ^ " ****");
   333       run_thy name path;
   334       writeln ("**** Finished theory " ^ quote name ^ " ****\n")))
   335   else run_thy name path;
   336   Context.context (ThyInfo.get_theory name);
   337   if ml then try_ml_file name time else ());
   338 
   339 end;
   340 
   341 
   342 
   343 (** the read-eval-print loop **)
   344 
   345 (* main loop *)
   346 
   347 fun gen_loop term no_pos =
   348  (Context.reset_context ();
   349   Toplevel.loop (isar term no_pos));
   350 
   351 fun gen_main term no_pos =
   352  (Toplevel.set_state Toplevel.toplevel;
   353   writeln (Session.welcome ());
   354   gen_loop term no_pos);
   355 
   356 fun main () = gen_main false false;
   357 fun loop () = gen_loop false false;
   358 fun sync_main () = gen_main true true;
   359 fun sync_loop () = gen_loop true true;
   360 
   361 
   362 (* help *)
   363 
   364 fun help () =
   365   writeln ("This is Isabelle's underlying ML system (" ^ ml_system ^ ");\n\
   366     \invoke 'loop();' to enter the Isar loop.");
   367 
   368 
   369 (*final declarations of this structure!*)
   370 val command = parser false None;
   371 val markup_command = parser false o Some;
   372 val improper_command = parser true None;
   373 
   374 
   375 end;
   376 
   377 (*setup theory syntax dependent operations*)
   378 ThyLoad.deps_thy_fn := OuterSyntax.deps_thy;
   379 ThyLoad.load_thy_fn := OuterSyntax.load_thy;
   380 structure ThyLoad: THY_LOAD = ThyLoad;
   381 
   382 structure BasicOuterSyntax: BASIC_OUTER_SYNTAX = OuterSyntax;
   383 open BasicOuterSyntax;