src/Pure/Isar/outer_syntax.ML
author wenzelm
Wed Sep 29 13:49:07 1999 +0200 (1999-09-29)
changeset 7632 25a0d2ba3a87
parent 7613 fe818734c387
child 7676 811022c3837e
permissions -rw-r--r--
removed extra shyps error;
     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.keep (fn state =>
   197     let val opt_thy = try Toplevel.theory_of state in
   198       print_outer_syntax ();
   199       Method.help_methods opt_thy;
   200       Attrib.help_attributes opt_thy
   201     end);
   202 
   203 
   204 
   205 (** read theory **)
   206 
   207 (* theory keyword *)
   208 
   209 val theoryN = "theory";
   210 val theory_keyword = OuterParse.$$$ theoryN;
   211 
   212 
   213 (* source *)
   214 
   215 local
   216 
   217 val no_terminator =
   218   Scan.unless (P.$$$ ";") (Scan.one (OuterLex.not_sync andf OuterLex.not_eof));
   219 
   220 val recover = Scan.prompt "recover# " (Scan.repeat no_terminator);
   221 
   222 in
   223 
   224 fun source term do_recover cmd src =
   225   src
   226   |> Source.source OuterLex.stopper
   227     (Scan.bulk (fn xs => OuterParse.!!! (command term (cmd ())) xs))
   228     (if do_recover then Some recover else None)
   229   |> Source.mapfilter I;
   230 
   231 end;
   232 
   233 
   234 (* detect header *)
   235 
   236 fun scan_header get_lex scan (src, pos) =
   237   src
   238   |> Symbol.source false
   239   |> OuterLex.source false (fn () => (get_lex (), Scan.empty_lexicon)) pos
   240   |> Source.source OuterLex.stopper (Scan.single scan) None
   241   |> (fst o the o Source.get_single);
   242 
   243 val check_header_lexicon = Scan.make_lexicon [Symbol.explode theoryN];
   244 
   245 fun is_old_theory src =
   246   is_none (scan_header (K check_header_lexicon) (Scan.option theory_keyword) src);
   247 
   248 
   249 (* deps_thy --- inspect theory header *)
   250 
   251 val header_lexicon =
   252   Scan.make_lexicon (map Symbol.explode ["(", ")", "+", ":", "=", "files", theoryN]);
   253 
   254 local
   255 
   256 val file_name =
   257   (P.$$$ "(" |-- P.!!! (P.name --| P.$$$ ")")) >> rpair false || P.name >> rpair true;
   258 
   259 val theory_head =
   260   (P.name -- (P.$$$ "=" |-- P.enum1 "+" P.name) --
   261     Scan.optional (P.$$$ "files" |-- P.!!! (Scan.repeat1 file_name)) [])
   262   >> (fn ((A, Bs), files) => (A, Bs, files));
   263 
   264 in
   265 
   266 val theory_header = theory_head --| (Scan.ahead P.eof || P.$$$ ":");
   267 val only_header = theory_keyword |-- theory_head --| Scan.ahead P.eof;
   268 val new_header = theory_keyword |-- P.!!! theory_header;
   269 
   270 val old_header =
   271   P.name -- (P.$$$ "=" |-- P.name -- Scan.repeat (P.$$$ "+" |-- P.name))
   272   >> (fn (A, (B, Bs)) => (A, B :: Bs, []: (string * bool) list));
   273 
   274 end;
   275 
   276 fun deps_thy name path =
   277   let
   278     val src = File.source path;
   279     val (name', parents, files) =
   280       (*Note: old style headers dynamically depend on the current lexicon :-( *)
   281       if is_old_theory src then scan_header ThySyn.get_lexicon (Scan.error old_header) src
   282       else scan_header (K header_lexicon) (Scan.error new_header) src;
   283 
   284     val ml_path = ThyLoad.ml_path name;
   285     val ml_file = if is_none (ThyLoad.check_file ml_path) then [] else [ml_path];
   286   in
   287     if name <> name' then
   288       error ("Filename " ^ quote (Path.pack path) ^ " does not match theory name " ^ quote name)
   289     else (parents, map (Path.unpack o #1) files @ ml_file)
   290   end;
   291 
   292 
   293 (* load_thy --- read text (including header) *)
   294 
   295 fun try_ml_file name ml time =
   296   let
   297     val path = ThyLoad.ml_path name;
   298     val tr = Toplevel.imperative (fn () => ThyInfo.may_load_file ml time path);
   299     val tr_name = if time then "time_use" else "use";
   300   in
   301     if is_none (ThyLoad.check_file path) then ()
   302     else Toplevel.excursion_error [Toplevel.empty |> Toplevel.name tr_name |> tr]
   303   end;
   304 
   305 fun parse_thy (src, pos) =
   306   let
   307     val lex_src =
   308       src
   309       |> Symbol.source false
   310       |> OuterLex.source false (K (get_lexicons ())) pos;
   311     val only_head =
   312       lex_src
   313       |> Source.source OuterLex.stopper (Scan.single (Scan.option only_header)) None
   314       |> (fst o the o Source.get_single);
   315   in
   316     (case only_head of
   317       None =>
   318         lex_src
   319         |> source false false (K (get_parser ()))
   320         |> Source.exhaust
   321     | Some spec =>
   322         [Toplevel.empty |> Toplevel.name theoryN |> IsarThy.theory spec,
   323           Toplevel.empty |> Toplevel.name "end" |> Toplevel.exit])
   324   end;
   325 
   326 fun run_thy name path =
   327   let val (src, pos) = File.source path in
   328     Present.theory_source name src;
   329     if is_old_theory (src, pos) then ThySyn.load_thy name (Source.exhaust src)
   330     else Toplevel.excursion_error (parse_thy (src, pos))
   331   end;
   332 
   333 fun load_thy name ml time path =
   334  (if time then
   335     timeit (fn () =>
   336      (writeln ("\n**** Starting theory " ^ quote name ^ " ****");
   337       setmp Goals.proof_timing true (run_thy name) path;
   338       writeln ("**** Finished theory " ^ quote name ^ " ****\n")))
   339   else run_thy name path;
   340   Context.context (ThyInfo.get_theory name);
   341   try_ml_file name ml time);
   342 
   343 
   344 (* interactive source of state transformers *)
   345 
   346 fun isar term no_pos =
   347   Source.tty
   348   |> Symbol.source true
   349   |> OuterLex.source true get_lexicons
   350     (if no_pos then Position.none else Position.line_name 1 "stdin")
   351   |> source term true get_parser;
   352 
   353 
   354 
   355 (** the read-eval-print loop **)
   356 
   357 (* main loop *)
   358 
   359 fun gen_loop term no_pos =
   360  (Context.reset_context ();
   361   Toplevel.loop (isar term no_pos));
   362 
   363 fun gen_main term no_pos =
   364  (Toplevel.set_state Toplevel.toplevel;
   365   ml_prompts "ML> " "ML# ";
   366   writeln (Session.welcome ());
   367   gen_loop term no_pos);
   368 
   369 fun main () = gen_main false false;
   370 fun loop () = gen_loop false false;
   371 fun sync_main () = gen_main true true;
   372 fun sync_loop () = gen_loop true true;
   373 
   374 
   375 (* help *)
   376 
   377 fun help () =
   378   writeln ("This is Isabelle's underlying ML system (" ^ ml_system ^ ");\n\
   379     \invoke 'loop();' to enter the Isar loop.");
   380 
   381 
   382 (*final declarations of this structure!*)
   383 val command = parser false;
   384 val improper_command = parser true;
   385 
   386 
   387 end;
   388 
   389 (*setup theory syntax dependent operations*)
   390 ThyLoad.deps_thy_fn := OuterSyntax.deps_thy;
   391 ThyLoad.load_thy_fn := OuterSyntax.load_thy;
   392 structure ThyLoad: THY_LOAD = ThyLoad;
   393 
   394 structure BasicOuterSyntax: BASIC_OUTER_SYNTAX = OuterSyntax;
   395 open BasicOuterSyntax;