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