src/Pure/Isar/outer_syntax.ML
author wenzelm
Wed Dec 22 20:29:36 1999 +0100 (1999-12-22)
changeset 8078 c6da7585f9d1
parent 7940 def6db239934
child 8191 6483e7132a70
permissions -rw-r--r--
fixed error msg;
     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_asm_goal: string
    38       val prf_script: string
    39       val kinds: string list
    40     end
    41   type token
    42   type parser
    43   val command: string -> string -> string ->
    44     (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
    45   val markup_command: string -> string -> string ->
    46     (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
    47   val verbatim_command: string -> string -> string ->
    48     (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
    49   val improper_command: string -> string -> string ->
    50     (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
    51   val dest_keywords: unit -> string list
    52   val dest_parsers: unit -> (string * string * string * bool) list
    53   val print_outer_syntax: unit -> unit
    54   val print_help: Toplevel.transition -> Toplevel.transition
    55   val add_keywords: string list -> unit
    56   val add_parsers: parser list -> unit
    57   val theory_header: token list -> (string * string list * (string * bool) list) * token list
    58   val deps_thy: string -> bool -> Path.T -> string list * Path.T list
    59   val load_thy: string -> bool -> bool -> Path.T -> unit
    60   val isar: bool -> bool -> Toplevel.isar
    61 end;
    62 
    63 structure OuterSyntax: OUTER_SYNTAX =
    64 struct
    65 
    66 structure T = OuterLex;
    67 structure P = OuterParse;
    68 
    69 
    70 (** outer syntax **)
    71 
    72 (* command keyword classification *)
    73 
    74 structure Keyword =
    75 struct
    76   val control = "control";
    77   val diag = "diag";
    78   val thy_begin = "theory-begin";
    79   val thy_switch = "theory-switch";
    80   val thy_end = "theory-end";
    81   val thy_heading = "theory-heading";
    82   val thy_decl = "theory-decl";
    83   val thy_goal = "theory-goal";
    84   val qed = "qed";
    85   val qed_block = "qed-block";
    86   val prf_goal = "proof-goal";
    87   val prf_block = "proof-block";
    88   val prf_chain = "proof-chain";
    89   val prf_decl = "proof-decl";
    90   val prf_asm = "proof-asm";
    91   val prf_asm_goal = "proof-asm-goal";
    92   val prf_script = "proof-script";
    93 
    94   val kinds = [control, diag, thy_begin, thy_switch, thy_end, thy_heading, thy_decl, thy_goal,
    95     qed, qed_block, prf_goal, prf_block, prf_chain, prf_decl, prf_asm, prf_asm_goal, prf_script];
    96 end;
    97 
    98 
    99 (* parsers *)
   100 
   101 type token = T.token;
   102 type parser_fn = token list -> (Toplevel.transition -> Toplevel.transition) * token list;
   103 
   104 datatype parser =
   105   Parser of string * (string * string * bool option) * bool * parser_fn;
   106 
   107 fun parser int_only markup name comment kind parse =
   108   Parser (name, (comment, kind, markup), int_only, parse);
   109 
   110 
   111 (* parse command *)
   112 
   113 local
   114 
   115 fun command_body cmd (name, _) =
   116   (case cmd name of
   117     Some (int_only, parse) => P.!!! (Scan.prompt (name ^ "# ") (parse >> pair int_only))
   118   | None => sys_error ("no parser for outer syntax command " ^ quote name));
   119 
   120 fun terminator false = Scan.succeed ()
   121   | terminator true = P.group "terminator" (Scan.option P.sync -- P.$$$ ";" >> K ());
   122 
   123 in
   124 
   125 fun command term cmd =
   126   P.$$$ ";" >> K None ||
   127   P.sync >> K None ||
   128   (P.position P.command :-- command_body cmd) --| terminator term
   129     >> (fn ((name, pos), (int_only, f)) =>
   130       Some (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |>
   131         Toplevel.interactive int_only |> f));
   132 
   133 end;
   134 
   135 
   136 
   137 (** global syntax state **)
   138 
   139 local
   140 
   141 val global_lexicons = ref (Scan.empty_lexicon, Scan.empty_lexicon);
   142 val global_parsers =
   143   ref (Symtab.empty: (((string * string) * (bool * parser_fn)) * bool option) Symtab.table);
   144 val global_markups = ref ([]: (string * bool) list);
   145 
   146 fun change_lexicons f =
   147   let val lexs = f (! global_lexicons) in
   148     (case (op inter_string) (pairself Scan.dest_lexicon lexs) of
   149       [] => global_lexicons := lexs
   150     | bads => error ("Clash of outer syntax commands and keywords: " ^ commas_quote bads))
   151   end;
   152 
   153 fun get_markup (ms, (name, (_, Some m))) = (name, m) :: ms
   154   | get_markup (ms, _) = ms;
   155 
   156 fun make_markups () = global_markups := Symtab.foldl get_markup ([], ! global_parsers);
   157 fun change_parsers f = (global_parsers := f (! global_parsers); make_markups ());
   158 
   159 in
   160 
   161 
   162 (* get current syntax *)
   163 
   164 (*Note: the syntax for files is statically determined at the very
   165   beginning; for interactive processing it may change dynamically.*)
   166 
   167 fun get_lexicons () = ! global_lexicons;
   168 fun get_parsers () = ! global_parsers;
   169 fun get_parser () = apsome (#2 o #1) o curry Symtab.lookup (! global_parsers);
   170 
   171 fun lookup_markup name = assoc (! global_markups, name);
   172 fun is_markup name = if_none (lookup_markup name) false;
   173 fun is_verbatim name = if_none (apsome not (lookup_markup name)) false;
   174 
   175 
   176 (* augment syntax *)
   177 
   178 fun add_keywords keywords = change_lexicons (apfst (fn lex =>
   179   (Scan.extend_lexicon lex (map Symbol.explode keywords))));
   180 
   181 fun add_parser (tab, Parser (name, (comment, kind, markup), int_only, parse)) =
   182  (if is_none (Symtab.lookup (tab, name)) then ()
   183   else warning ("Redefined outer syntax command " ^ quote name);
   184   Symtab.update ((name, (((comment, kind), (int_only, parse)), markup)), tab));
   185 
   186 fun add_parsers parsers =
   187   (change_parsers (fn tab => foldl add_parser (tab, parsers));
   188     change_lexicons (apsnd (fn lex => Scan.extend_lexicon lex
   189       (map (fn Parser (name, _, _, _) => Symbol.explode name) parsers))));
   190 
   191 end;
   192 
   193 
   194 (* print syntax *)
   195 
   196 fun dest_keywords () = Scan.dest_lexicon (#1 (get_lexicons ()));
   197 
   198 fun dest_parsers () =
   199   map (fn (name, (((cmt, kind), (int_only, _)), _)) => (name, cmt, kind, int_only))
   200     (Symtab.dest (get_parsers ()));
   201 
   202 fun print_outer_syntax () =
   203   let
   204     fun pretty_cmd (name, comment, _, _) =
   205       Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
   206     val (int_cmds, cmds) = partition #4 (dest_parsers ());
   207   in
   208     Pretty.writeln (Pretty.strs ("syntax keywords:" :: map quote (dest_keywords ())));
   209     Pretty.writeln (Pretty.big_list "proper commands:" (map pretty_cmd cmds));
   210     Pretty.writeln (Pretty.big_list "improper commands (interactive-only):"
   211       (map pretty_cmd int_cmds))
   212   end;
   213 
   214 val print_help =
   215   Toplevel.keep (fn state =>
   216     let val opt_thy = try Toplevel.theory_of state in
   217       print_outer_syntax ();
   218       Method.help_methods opt_thy;
   219       Attrib.help_attributes opt_thy
   220     end);
   221 
   222 
   223 
   224 (** read theory **)
   225 
   226 (* special keywords *)
   227 
   228 val headerN = "header";
   229 val theoryN = "theory";
   230 
   231 val theory_keyword = P.$$$ theoryN;
   232 val header_keyword = P.$$$ headerN;
   233 val semicolon = P.$$$ ";";
   234 
   235 
   236 (* sources *)
   237 
   238 local
   239 
   240 val no_terminator =
   241   Scan.unless semicolon (Scan.one (T.not_sync andf T.not_eof));
   242 
   243 val recover = Scan.prompt "recover# " (Scan.repeat no_terminator);
   244 
   245 in
   246 
   247 fun source term do_recover cmd src =
   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 
   254 end;
   255 
   256 fun token_source (src, pos) =
   257   src
   258   |> Symbol.source false
   259   |> T.source false (K (get_lexicons ())) pos;
   260 
   261 fun filter_proper src =
   262   src
   263   |> Source.filter T.is_proper;
   264 
   265 
   266 (* scan header *)
   267 
   268 fun scan_header get_lex scan (src, pos) =
   269   src
   270   |> Symbol.source false
   271   |> T.source false (fn () => (get_lex (), Scan.empty_lexicon)) pos
   272   |> filter_proper
   273   |> Source.source T.stopper (Scan.single scan) None
   274   |> (fst o the o Source.get_single);
   275 
   276 
   277 (* detect new/old header *)
   278 
   279 local
   280 
   281 val check_header_lexicon = Scan.make_lexicon [Symbol.explode headerN, Symbol.explode theoryN];
   282 val check_header = Scan.option (header_keyword || theory_keyword);
   283 
   284 in
   285 
   286 fun is_old_theory src = is_none (scan_header (K check_header_lexicon) check_header src);
   287 
   288 end;
   289 
   290 
   291 (* deps_thy --- inspect theory header *)
   292 
   293 local
   294 
   295 val header_lexicon =
   296   Scan.make_lexicon (map Symbol.explode ["(", ")", "+", ":", ";", "=", "files", headerN, theoryN]);
   297 
   298 val file_name =
   299   (P.$$$ "(" |-- P.!!! (P.name --| P.$$$ ")")) >> rpair false || P.name >> rpair true;
   300 
   301 in
   302 
   303 val theory_header =
   304   (P.name -- (P.$$$ "=" |-- P.enum1 "+" P.name) --
   305     Scan.optional (P.$$$ "files" |-- P.!!! (Scan.repeat1 file_name)) [] --|  P.$$$ ":")
   306   >> (fn ((A, Bs), files) => (A, Bs, files));
   307 
   308 val new_header =
   309   header_keyword |-- (P.!!! (P.text -- Scan.option semicolon -- theory_keyword |-- theory_header))
   310     || theory_keyword |-- P.!!! theory_header;
   311 
   312 val old_header =
   313   P.!!! (P.group "theory header"
   314     (P.name -- (P.$$$ "=" |-- P.name -- Scan.repeat (P.$$$ "+" |-- P.name))))
   315   >> (fn (A, (B, Bs)) => (A, B :: Bs, []: (string * bool) list));
   316 
   317 fun deps_thy name ml path =
   318   let
   319     val src = Source.of_string (File.read path);
   320     val pos = Path.position path;
   321     val (name', parents, files) =
   322       (*unfortunately, old-style headers dynamically depend on the current lexicon*)
   323       if is_old_theory (src, pos) then
   324         scan_header ThySyn.get_lexicon (Scan.error old_header) (src, pos)
   325       else scan_header (K header_lexicon) (Scan.error new_header) (src, pos);
   326 
   327     val ml_path = ThyLoad.ml_path name;
   328     val ml_file = if ml andalso is_some (ThyLoad.check_file ml_path) then [ml_path] else [];
   329   in
   330     if name <> name' then
   331       error ("Filename " ^ quote (Path.pack path) ^
   332         " does not agree with theory name " ^ quote name')
   333     else (parents, map (Path.unpack o #1) files @ ml_file)
   334   end;
   335 
   336 end;
   337 
   338 
   339 (* present theory source *)
   340 
   341 local
   342 
   343 val indent_prop = Scan.one T.is_indent -- Scan.one T.is_proper;
   344 val improp = Scan.unless indent_prop (Scan.one (not o T.is_proper));
   345 val improper_keep_indent = Scan.repeat improp;
   346 
   347 val improper = Scan.any (not o T.is_proper);
   348 
   349 val improper_end =
   350   (improper -- semicolon) |-- improper_keep_indent ||
   351   improper_keep_indent;
   352 
   353 val markup = Scan.one (T.is_kind T.Command andf is_markup o T.val_of) >> T.val_of;
   354 val verbatim = Scan.one (T.is_kind T.Command andf is_verbatim o T.val_of);
   355 
   356 val present_token =
   357   improper |-- markup -- (improper |-- P.text --| improper_end) >> Present.markup_token ||
   358   (P.$$$ "--" >> K "cmt") -- (improper |-- P.text) >> Present.markup_token ||
   359   (improper -- verbatim -- improper) |-- P.text --| improper_end >> Present.verbatim_token ||
   360   Scan.one T.not_eof >> Present.basic_token;
   361 
   362 in
   363 
   364 (*note: lazy evaluation ahead*)
   365 
   366 fun present_toks text pos () =
   367   token_source (Source.of_list (Library.untabify text), pos)
   368   |> Source.source T.stopper (Scan.bulk present_token) None
   369   |> Source.exhaust;
   370 
   371 fun present_text text () =
   372   Source.exhaust (Symbol.source false (Source.of_list (Library.untabify text)));
   373 
   374 end;
   375 
   376 
   377 (* load_thy --- read text (including header) *)
   378 
   379 local
   380 
   381 fun try_ml_file name time =
   382   let
   383     val path = ThyLoad.ml_path name;
   384     val tr = Toplevel.imperative (fn () => ThyInfo.load_file time path);
   385     val tr_name = if time then "time_use" else "use";
   386   in
   387     if is_none (ThyLoad.check_file path) then ()
   388     else Toplevel.excursion_error [Toplevel.empty |> Toplevel.name tr_name |> tr]
   389   end;
   390 
   391 fun parse_thy src_pos =
   392   src_pos
   393   |> token_source
   394   |> filter_proper
   395   |> source false false (K (get_parser ()))
   396   |> Source.exhaust;
   397 
   398 fun run_thy name path =
   399   let
   400     val text = explode (File.read path);
   401     val src = Source.of_list text;
   402     val pos = Path.position path;
   403   in
   404     Present.init_theory name;
   405     Present.verbatim_source name (present_text text);
   406     if is_old_theory (src, pos) then ThySyn.load_thy name text
   407     else (Toplevel.excursion_error (parse_thy (src, pos));
   408       Present.token_source name (present_toks text pos))
   409   end;
   410 
   411 in
   412 
   413 fun load_thy name ml time path =
   414  (if time then
   415     timeit (fn () =>
   416      (writeln ("\n**** Starting theory " ^ quote name ^ " ****");
   417       setmp Goals.proof_timing true (run_thy name) path;
   418       writeln ("**** Finished theory " ^ quote name ^ " ****\n")))
   419   else run_thy name path;
   420   Context.context (ThyInfo.get_theory name);
   421   if ml then try_ml_file name time else ());
   422 
   423 end;
   424 
   425 
   426 (* interactive source of state transformers *)
   427 
   428 fun isar term no_pos =
   429   Source.tty
   430   |> Symbol.source true
   431   |> T.source true get_lexicons
   432     (if no_pos then Position.none else Position.line_name 1 "stdin")
   433   |> filter_proper
   434   |> source term true get_parser;
   435 
   436 
   437 
   438 (** the read-eval-print loop **)
   439 
   440 (* main loop *)
   441 
   442 fun gen_loop term no_pos =
   443  (Context.reset_context ();
   444   Toplevel.loop (isar term no_pos));
   445 
   446 fun gen_main term no_pos =
   447  (Toplevel.set_state Toplevel.toplevel;
   448   writeln (Session.welcome ());
   449   gen_loop term no_pos);
   450 
   451 fun main () = gen_main false false;
   452 fun loop () = gen_loop false false;
   453 fun sync_main () = gen_main true true;
   454 fun sync_loop () = gen_loop true true;
   455 
   456 
   457 (* help *)
   458 
   459 fun help () =
   460   writeln ("This is Isabelle's underlying ML system (" ^ ml_system ^ ");\n\
   461     \invoke 'loop();' to enter the Isar loop.");
   462 
   463 
   464 (*final declarations of this structure!*)
   465 val command = parser false None;
   466 val markup_command = parser false (Some true);
   467 val verbatim_command = parser false (Some false);
   468 val improper_command = parser true None;
   469 
   470 
   471 end;
   472 
   473 (*setup theory syntax dependent operations*)
   474 ThyLoad.deps_thy_fn := OuterSyntax.deps_thy;
   475 ThyLoad.load_thy_fn := OuterSyntax.load_thy;
   476 structure ThyLoad: THY_LOAD = ThyLoad;
   477 
   478 structure BasicOuterSyntax: BASIC_OUTER_SYNTAX = OuterSyntax;
   479 open BasicOuterSyntax;