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