src/Pure/Isar/outer_syntax.ML
author wenzelm
Mon Feb 25 20:51:48 2002 +0100 (2002-02-25)
changeset 12943 1db24da0537b
parent 12876 a70df1e5bf10
child 14091 ad6ba9c55190
permissions -rw-r--r--
added check_text;
     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 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_script: string
    30       val thy_goal: string
    31       val qed: string
    32       val qed_block: string
    33       val qed_global: string
    34       val prf_heading: string
    35       val prf_goal: string
    36       val prf_block: string
    37       val prf_open: string
    38       val prf_close: string
    39       val prf_chain: string
    40       val prf_decl: string
    41       val prf_asm: string
    42       val prf_asm_goal: string
    43       val prf_script: string
    44       val kinds: string list
    45     end
    46   type token
    47   type parser
    48   val command: string -> string -> string ->
    49     (token list -> (Toplevel.transition -> Toplevel.transition) * token list) -> parser
    50   val markup_command: IsarOutput.markup -> 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_commands: Toplevel.transition -> Toplevel.transition
    58   val add_keywords: string list -> unit
    59   val add_parsers: parser list -> unit
    60   val check_text: string * Position.T -> bool -> Toplevel.state -> unit
    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_script = "theory-script";
    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_heading = "proof-heading";
    92   val prf_goal = "proof-goal";
    93   val prf_block = "proof-block";
    94   val prf_open = "proof-open";
    95   val prf_close = "proof-close";
    96   val prf_chain = "proof-chain";
    97   val prf_decl = "proof-decl";
    98   val prf_asm = "proof-asm";
    99   val prf_asm_goal = "proof-asm-goal";
   100   val prf_script = "proof-script";
   101 
   102   val kinds = [control, diag, thy_begin, thy_switch, thy_end, thy_heading, thy_decl, thy_script,
   103     thy_goal, qed, qed_block, qed_global, prf_heading, prf_goal, prf_block, prf_open, prf_close,
   104     prf_chain, prf_decl, prf_asm, prf_asm_goal, prf_script];
   105 end;
   106 
   107 
   108 (* parsers *)
   109 
   110 type token = T.token;
   111 type parser_fn = token list -> (Toplevel.transition -> Toplevel.transition) * token list;
   112 
   113 datatype parser =
   114   Parser of string * (string * string * IsarOutput.markup option) * bool * parser_fn;
   115 
   116 fun parser int_only markup name comment kind parse =
   117   Parser (name, (comment, kind, markup), int_only, parse);
   118 
   119 
   120 (* parse command *)
   121 
   122 local
   123 
   124 fun command_body cmd (name, _) =
   125   (case cmd name of
   126     Some (int_only, parse) => P.!!! (Scan.prompt (name ^ "# ") (parse >> pair int_only))
   127   | None => sys_error ("no parser for outer syntax command " ^ quote name));
   128 
   129 fun terminator false = Scan.succeed ()
   130   | terminator true = P.group "end of input" (Scan.option P.sync -- P.semicolon >> K ());
   131 
   132 in
   133 
   134 fun command term cmd =
   135   P.semicolon >> K None ||
   136   P.sync >> K None ||
   137   (P.position P.command :-- command_body cmd) --| terminator term
   138     >> (fn ((name, pos), (int_only, f)) =>
   139       Some (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |>
   140         Toplevel.interactive int_only |> f));
   141 
   142 end;
   143 
   144 
   145 
   146 (** global outer syntax **)
   147 
   148 local
   149 
   150 val global_lexicons = ref (Scan.empty_lexicon, Scan.empty_lexicon);
   151 val global_parsers =
   152   ref (Symtab.empty: (((string * string) * (bool * parser_fn)) * IsarOutput.markup option)
   153     Symtab.table);
   154 val global_markups = ref ([]: (string * IsarOutput.markup) list);
   155 
   156 fun change_lexicons f =
   157   let val lexs = f (! global_lexicons) in
   158     (case (op inter_string) (pairself Scan.dest_lexicon lexs) of
   159       [] => global_lexicons := lexs
   160     | bads => error ("Clash of outer syntax commands and keywords: " ^ commas_quote bads))
   161   end;
   162 
   163 fun get_markup (ms, (name, (_, Some m))) = (name, m) :: ms
   164   | get_markup (ms, _) = ms;
   165 
   166 fun make_markups () = global_markups := Symtab.foldl get_markup ([], ! global_parsers);
   167 fun change_parsers f = (Library.change global_parsers f; make_markups ());
   168 
   169 in
   170 
   171 
   172 (* access current syntax *)
   173 
   174 (*Note: the syntax for files is statically determined at the very
   175   beginning; for interactive processing it may change dynamically.*)
   176 
   177 fun get_lexicons () = ! global_lexicons;
   178 fun get_parsers () = ! global_parsers;
   179 fun get_parser () = apsome (#2 o #1) o curry Symtab.lookup (! global_parsers);
   180 
   181 fun is_markup kind name =
   182   (case assoc (! global_markups, name) of Some k => k = kind | None => false);
   183 fun markup kind = Scan.one (T.is_kind T.Command andf is_markup kind o T.val_of);
   184 
   185 
   186 (* augment syntax *)
   187 
   188 fun add_keywords keywords = change_lexicons (apfst (fn lex =>
   189   (Scan.extend_lexicon lex (map Symbol.explode keywords))));
   190 
   191 fun add_parser (tab, Parser (name, (comment, kind, markup), int_only, parse)) =
   192  (if is_none (Symtab.lookup (tab, name)) then ()
   193   else warning ("Redefined outer syntax command " ^ quote name);
   194   Symtab.update ((name, (((comment, kind), (int_only, parse)), markup)), tab));
   195 
   196 fun add_parsers parsers =
   197   (change_parsers (fn tab => foldl add_parser (tab, parsers));
   198     change_lexicons (apsnd (fn lex => Scan.extend_lexicon lex
   199       (map (fn Parser (name, _, _, _) => Symbol.explode name) parsers))));
   200 
   201 end;
   202 
   203 
   204 (* print syntax *)
   205 
   206 fun dest_keywords () = Scan.dest_lexicon (#1 (get_lexicons ()));
   207 
   208 fun dest_parsers () =
   209   map (fn (name, (((cmt, kind), (int_only, _)), _)) => (name, cmt, kind, int_only))
   210     (Symtab.dest (get_parsers ()));
   211 
   212 fun print_outer_syntax () =
   213   let
   214     fun pretty_cmd (name, comment, _, _) =
   215       Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
   216     val (int_cmds, cmds) = partition #4 (dest_parsers ());
   217   in
   218     [Pretty.strs ("syntax keywords:" :: map quote (dest_keywords ())),
   219       Pretty.big_list "proper commands:" (map pretty_cmd cmds),
   220       Pretty.big_list "improper commands (interactive-only):" (map pretty_cmd int_cmds)]
   221     |> Pretty.chunks |> Pretty.writeln
   222   end;
   223 
   224 val print_commands = Toplevel.imperative print_outer_syntax;
   225 
   226 
   227 
   228 (** toplevel parsing **)
   229 
   230 (* basic sources *)
   231 
   232 fun toplevel_source term do_recover cmd src =
   233   let
   234     val no_terminator =
   235       Scan.unless P.semicolon (Scan.one (T.not_sync andf T.not_eof));
   236     fun recover x = (Scan.prompt "recover# " (Scan.repeat no_terminator) >> K [None]) x;
   237   in
   238     src
   239     |> T.source_proper
   240     |> Source.source T.stopper
   241       (Scan.bulk (P.$$$ "--" -- P.!!! P.text >> K None || P.not_eof >> Some))
   242       (if do_recover then Some recover else None)
   243     |> Source.mapfilter I
   244     |> Source.source T.stopper (Scan.bulk (fn xs => P.!!! (command term (cmd ())) xs))
   245       (if do_recover then Some recover else None)
   246     |> Source.mapfilter I
   247   end;
   248 
   249 
   250 (* interactive source of toplevel transformers *)
   251 
   252 fun isar term no_pos =
   253   Source.tty
   254   |> Symbol.source true
   255   |> T.source true get_lexicons
   256     (if no_pos then Position.none else Position.line_name 1 "stdin")
   257   |> toplevel_source term true get_parser;
   258 
   259 
   260 
   261 (** read theory **)
   262 
   263 (* check_text *)
   264 
   265 fun check_text s true state = (IsarOutput.eval_antiquote (#1 (get_lexicons ())) state s; ())
   266   | check_text _ false _ = ();
   267 
   268 
   269 (* deps_thy *)
   270 
   271 fun deps_thy name ml path =
   272   let
   273     val src = Source.of_string (File.read path);
   274     val pos = Path.position path;
   275     val (name', parents, files) = ThyHeader.scan (src, pos);
   276     val ml_path = ThyLoad.ml_path name;
   277     val ml_file = if ml andalso is_some (ThyLoad.check_file ml_path) then [ml_path] else [];
   278   in
   279     if name <> name' then
   280       error ("Filename " ^ quote (Path.pack path) ^
   281         " does not agree with theory name " ^ quote name')
   282     else (parents, map (Path.unpack o #1) files @ ml_file)
   283   end;
   284 
   285 
   286 (* load_thy *)
   287 
   288 local
   289 
   290 fun try_ml_file name time =
   291   let
   292     val path = ThyLoad.ml_path name;
   293     val tr = Toplevel.imperative (fn () => ThyInfo.load_file time path);
   294     val tr_name = if time then "time_use" else "use";
   295   in
   296     if is_none (ThyLoad.check_file path) then ()
   297     else Toplevel.excursion [Toplevel.empty |> Toplevel.name tr_name |> tr]
   298   end;
   299 
   300 fun parse_thy src =
   301   src
   302   |> toplevel_source false false (K (get_parser ()))
   303   |> Source.exhaust;
   304 
   305 fun run_thy name path =
   306   let
   307     val pos = Path.position path;
   308     val text = Library.untabify (explode (File.read path));
   309     val text_src = Source.of_list text;
   310     fun present_text () = Source.exhaust (Symbol.source false text_src);
   311   in
   312     Present.init_theory name;
   313     Present.verbatim_source name present_text;
   314     if ThyHeader.is_old (text_src, pos) then (ThySyn.load_thy name text;
   315       Present.old_symbol_source name present_text)   (*note: text presented twice*)
   316     else
   317       let
   318         val tok_src = text_src
   319           |> Symbol.source false
   320           |> T.source false (K (get_lexicons ())) pos
   321           |> Source.exhausted;
   322         val out = Toplevel.excursion_result
   323           (IsarOutput.parse_thy markup (#1 (get_lexicons ()))
   324             (parse_thy tok_src) tok_src);
   325       in Present.theory_output name (Buffer.content out) end
   326   end;
   327 
   328 in
   329 
   330 fun load_thy name ml time path =
   331  (if time then
   332     timeit (fn () =>
   333      (writeln ("\n**** Starting theory " ^ quote name ^ " ****");
   334       run_thy name path;
   335       writeln ("**** Finished theory " ^ quote name ^ " ****\n")))
   336   else run_thy name path;
   337   Context.context (ThyInfo.get_theory name);
   338   if ml then try_ml_file name time else ());
   339 
   340 end;
   341 
   342 
   343 
   344 (** the read-eval-print loop **)
   345 
   346 (* main loop *)
   347 
   348 fun gen_loop term no_pos =
   349  (Context.reset_context ();
   350   Toplevel.loop (isar term no_pos));
   351 
   352 fun gen_main term no_pos =
   353  (Toplevel.set_state Toplevel.toplevel;
   354   writeln (Session.welcome ());
   355   gen_loop term no_pos);
   356 
   357 fun main () = gen_main false false;
   358 fun loop () = gen_loop false false;
   359 fun sync_main () = gen_main true true;
   360 fun sync_loop () = gen_loop true true;
   361 
   362 
   363 (* help *)
   364 
   365 fun help () =
   366   writeln ("This is Isabelle's underlying ML system (" ^ ml_system ^ ");\n\
   367     \invoke 'Isar.loop();' to get back to the Isar read-eval-print loop.");
   368 
   369 
   370 (*final declarations of this structure!*)
   371 val command = parser false None;
   372 val markup_command = parser false o Some;
   373 val improper_command = parser true None;
   374 
   375 
   376 end;
   377 
   378 (*setup theory syntax dependent operations*)
   379 ThyLoad.deps_thy_fn := OuterSyntax.deps_thy;
   380 ThyLoad.load_thy_fn := OuterSyntax.load_thy;
   381 structure ThyLoad: THY_LOAD = ThyLoad;
   382 
   383 structure BasicOuterSyntax: BASIC_OUTER_SYNTAX = OuterSyntax;
   384 open BasicOuterSyntax;