src/Pure/Isar/outer_syntax.ML
author wenzelm
Mon Apr 17 13:57:55 2000 +0200 (2000-04-17)
changeset 8720 840c75ab2a7f
parent 8660 e5048a26e1d8
child 8807 0046be1769f9
permissions -rw-r--r--
Pretty.chunks;
     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 help_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.strs ("syntax keywords:" :: map quote (dest_keywords ())),
   215       Pretty.big_list "proper commands:" (map pretty_cmd cmds),
   216       Pretty.big_list "improper commands (interactive-only):" (map pretty_cmd int_cmds)]
   217   end;
   218 
   219 val print_outer_syntax = Pretty.writeln o Pretty.chunks o help_outer_syntax;
   220 
   221 val print_help =
   222   Toplevel.keep (fn state =>
   223     let val opt_thy = try Toplevel.theory_of state in
   224       help_outer_syntax () @
   225       Method.help_methods opt_thy @
   226       Attrib.help_attributes opt_thy
   227       |> Pretty.chunks |> Pretty.writeln
   228     end);
   229 
   230 
   231 
   232 (** read theory **)
   233 
   234 (* special keywords *)
   235 
   236 val headerN = "header";
   237 val theoryN = "theory";
   238 
   239 val theory_keyword = P.$$$ theoryN;
   240 val header_keyword = P.$$$ headerN;
   241 val semicolon = P.$$$ ";";
   242 
   243 
   244 (* sources *)
   245 
   246 local
   247 
   248 val no_terminator =
   249   Scan.unless semicolon (Scan.one (T.not_sync andf T.not_eof));
   250 
   251 val recover = Scan.prompt "recover# " (Scan.repeat no_terminator);
   252 
   253 in
   254 
   255 fun source term do_recover cmd src =
   256   src
   257   |> Source.source T.stopper
   258     (Scan.bulk (fn xs => P.!!! (command term (cmd ())) xs))
   259     (if do_recover then Some recover else None)
   260   |> Source.mapfilter I;
   261 
   262 end;
   263 
   264 fun token_source (src, pos) =
   265   src
   266   |> Symbol.source false
   267   |> T.source false (K (get_lexicons ())) pos;
   268 
   269 fun filter_proper src =
   270   src
   271   |> Source.filter T.is_proper;
   272 
   273 
   274 (* scan header *)
   275 
   276 fun scan_header get_lex scan (src, pos) =
   277   src
   278   |> Symbol.source false
   279   |> T.source false (fn () => (get_lex (), Scan.empty_lexicon)) pos
   280   |> filter_proper
   281   |> Source.source T.stopper (Scan.single scan) None
   282   |> (fst o the o Source.get_single);
   283 
   284 
   285 (* detect new/old header *)
   286 
   287 local
   288 
   289 val check_header_lexicon = Scan.make_lexicon [Symbol.explode headerN, Symbol.explode theoryN];
   290 val check_header = Scan.option (header_keyword || theory_keyword);
   291 
   292 in
   293 
   294 fun is_old_theory src = is_none (scan_header (K check_header_lexicon) check_header src);
   295 
   296 end;
   297 
   298 
   299 (* deps_thy --- inspect theory header *)
   300 
   301 local
   302 
   303 val header_lexicon =
   304   Scan.make_lexicon (map Symbol.explode ["(", ")", "+", ":", ";", "=", "files", headerN, theoryN]);
   305 
   306 val file_name =
   307   (P.$$$ "(" |-- P.!!! (P.name --| P.$$$ ")")) >> rpair false || P.name >> rpair true;
   308 
   309 in
   310 
   311 val theory_header =
   312   (P.name -- (P.$$$ "=" |-- P.enum1 "+" P.name) --
   313     Scan.optional (P.$$$ "files" |-- P.!!! (Scan.repeat1 file_name)) [] --|  P.$$$ ":")
   314   >> (fn ((A, Bs), files) => (A, Bs, files));
   315 
   316 val new_header =
   317   header_keyword |-- (P.!!! (P.text -- Scan.option semicolon -- theory_keyword |-- theory_header))
   318     || theory_keyword |-- P.!!! theory_header;
   319 
   320 val old_header =
   321   P.!!! (P.group "theory header"
   322     (P.name -- (P.$$$ "=" |-- P.name -- Scan.repeat (P.$$$ "+" |-- P.name))))
   323   >> (fn (A, (B, Bs)) => (A, B :: Bs, []: (string * bool) list));
   324 
   325 fun deps_thy name ml path =
   326   let
   327     val src = Source.of_string (File.read path);
   328     val pos = Path.position path;
   329     val (name', parents, files) =
   330       (*unfortunately, old-style headers dynamically depend on the current lexicon*)
   331       if is_old_theory (src, pos) then
   332         scan_header ThySyn.get_lexicon (Scan.error old_header) (src, pos)
   333       else scan_header (K header_lexicon) (Scan.error new_header) (src, pos);
   334 
   335     val ml_path = ThyLoad.ml_path name;
   336     val ml_file = if ml andalso is_some (ThyLoad.check_file ml_path) then [ml_path] else [];
   337   in
   338     if name <> name' then
   339       error ("Filename " ^ quote (Path.pack path) ^
   340         " does not agree with theory name " ^ quote name')
   341     else (parents, map (Path.unpack o #1) files @ ml_file)
   342   end;
   343 
   344 end;
   345 
   346 
   347 (* present theory source *)
   348 
   349 local
   350 
   351 val is_improper = not o (T.is_proper orf T.is_begin_ignore orf T.is_end_ignore);
   352 val improper = Scan.any is_improper;
   353 
   354 val improper_keep_indent = Scan.repeat
   355   (Scan.unless (Scan.one T.is_indent -- Scan.one T.is_proper) (Scan.one is_improper));
   356 
   357 val improper_end =
   358   (improper -- semicolon) |-- improper_keep_indent ||
   359   improper_keep_indent;
   360 
   361 
   362 val ignore =
   363   Scan.depend (fn d => Scan.one T.is_begin_ignore >> pair (d + 1)) ||
   364   Scan.depend (fn 0 => Scan.fail | d => Scan.one T.is_end_ignore >> pair (d - 1)) ||
   365   Scan.lift (Scan.one (OuterLex.not_eof andf (not o OuterLex.is_end_ignore)));
   366 
   367 val opt_newline = Scan.option (Scan.one T.is_newline);
   368 
   369 val ignore_stuff =
   370   opt_newline -- Scan.one T.is_begin_ignore --
   371     P.!!!! (Scan.pass 0 (Scan.repeat ignore) -- Scan.one T.is_end_ignore -- opt_newline);
   372 
   373 fun markup_kind k = Scan.one (T.is_kind T.Command andf is_markup k o T.val_of);
   374 
   375 val markup = markup_kind Markup >> T.val_of;
   376 val markup_env = markup_kind MarkupEnv >> T.val_of;
   377 val verbatim = markup_kind Verbatim;
   378 
   379 val present_token =
   380   ignore_stuff >> K None ||
   381   (improper |-- markup -- P.!!!! (improper |-- P.text --| improper_end) >> Present.markup_token ||
   382     improper |-- markup_env -- P.!!!! (improper |-- P.text --| improper_end) >> Present.markup_env_token ||
   383     (P.$$$ "--" >> K "cmt") -- P.!!!! (improper |-- P.text) >> Present.markup_token ||
   384     (improper -- verbatim) |-- P.!!!! (improper |-- P.text --| improper_end)
   385       >> Present.verbatim_token ||
   386     Scan.one T.not_eof >> Present.basic_token) >> Some;
   387 
   388 in
   389 
   390 (*note: lazy evaluation ahead*)
   391 
   392 fun present_toks text pos () =
   393   token_source (Source.of_list (Library.untabify text), pos)
   394   |> Source.source T.stopper (Scan.bulk (Scan.error present_token)) None
   395   |> Source.mapfilter I
   396   |> Source.exhaust;
   397 
   398 fun present_text text () =
   399   Source.exhaust (Symbol.source false (Source.of_list (Library.untabify text)));
   400 
   401 end;
   402 
   403 
   404 (* load_thy --- read text (including header) *)
   405 
   406 local
   407 
   408 fun try_ml_file name time =
   409   let
   410     val path = ThyLoad.ml_path name;
   411     val tr = Toplevel.imperative (fn () => ThyInfo.load_file time path);
   412     val tr_name = if time then "time_use" else "use";
   413   in
   414     if is_none (ThyLoad.check_file path) then ()
   415     else Toplevel.excursion_error [Toplevel.empty |> Toplevel.name tr_name |> tr]
   416   end;
   417 
   418 fun parse_thy src_pos =
   419   src_pos
   420   |> token_source
   421   |> filter_proper
   422   |> source false false (K (get_parser ()))
   423   |> Source.exhaust;
   424 
   425 fun run_thy name path =
   426   let
   427     val text = explode (File.read path);
   428     val src = Source.of_list text;
   429     val pos = Path.position path;
   430   in
   431     Present.init_theory name;
   432     Present.verbatim_source name (present_text text);
   433     if is_old_theory (src, pos) then (ThySyn.load_thy name text;
   434       Present.old_symbol_source name (present_text text))   (*note: text presented twice!*)
   435     else (Toplevel.excursion_error (parse_thy (src, pos));
   436       Present.token_source name (present_toks text pos))
   437   end;
   438 
   439 in
   440 
   441 fun load_thy name ml time path =
   442  (if time then
   443     timeit (fn () =>
   444      (writeln ("\n**** Starting theory " ^ quote name ^ " ****");
   445       setmp Goals.proof_timing true (run_thy name) path;
   446       writeln ("**** Finished theory " ^ quote name ^ " ****\n")))
   447   else run_thy name path;
   448   Context.context (ThyInfo.get_theory name);
   449   if ml then try_ml_file name time else ());
   450 
   451 end;
   452 
   453 
   454 (* interactive source of state transformers *)
   455 
   456 fun isar term no_pos =
   457   Source.tty
   458   |> Symbol.source true
   459   |> T.source true get_lexicons
   460     (if no_pos then Position.none else Position.line_name 1 "stdin")
   461   |> filter_proper
   462   |> source term true get_parser;
   463 
   464 
   465 
   466 (** the read-eval-print loop **)
   467 
   468 (* main loop *)
   469 
   470 fun gen_loop term no_pos =
   471  (Context.reset_context ();
   472   Toplevel.loop (isar term no_pos));
   473 
   474 fun gen_main term no_pos =
   475  (Toplevel.set_state Toplevel.toplevel;
   476   writeln (Session.welcome ());
   477   gen_loop term no_pos);
   478 
   479 fun main () = gen_main false false;
   480 fun loop () = gen_loop false false;
   481 fun sync_main () = gen_main true true;
   482 fun sync_loop () = gen_loop true true;
   483 
   484 
   485 (* help *)
   486 
   487 fun help () =
   488   writeln ("This is Isabelle's underlying ML system (" ^ ml_system ^ ");\n\
   489     \invoke 'loop();' to enter the Isar loop.");
   490 
   491 
   492 (*final declarations of this structure!*)
   493 val command = parser false None;
   494 val markup_command = parser false (Some Markup);
   495 val markup_env_command = parser false (Some MarkupEnv);
   496 val verbatim_command = parser false (Some Verbatim);
   497 val improper_command = parser true None;
   498 
   499 
   500 end;
   501 
   502 (*setup theory syntax dependent operations*)
   503 ThyLoad.deps_thy_fn := OuterSyntax.deps_thy;
   504 ThyLoad.load_thy_fn := OuterSyntax.load_thy;
   505 structure ThyLoad: THY_LOAD = ThyLoad;
   506 
   507 structure BasicOuterSyntax: BASIC_OUTER_SYNTAX = OuterSyntax;
   508 open BasicOuterSyntax;