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