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