src/Pure/Isar/outer_syntax.ML
author wenzelm
Thu Mar 27 14:41:19 2008 +0100 (2008-03-27 ago)
changeset 26431 f1c79c00f1e4
parent 26415 1b624d6e9163
child 26600 f11515535c83
permissions -rw-r--r--
added process_file;
     1 (*  Title:      Pure/Isar/outer_syntax.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 The global Isabelle/Isar outer syntax. Note: the syntax for files is
     6 statically determined at the very beginning; for interactive processing
     7 it may change dynamically.
     8 *)
     9 
    10 signature BASIC_OUTER_SYNTAX =
    11 sig
    12   structure Isar:
    13     sig
    14       val state: unit -> Toplevel.state
    15       val exn: unit -> (exn * string) option
    16       val context: unit -> Proof.context
    17       val goal: unit -> thm list * thm
    18       val main: unit -> unit
    19       val loop: unit -> unit
    20       val sync_main: unit -> unit
    21       val sync_loop: unit -> unit
    22       val secure_main: unit -> unit
    23       val toplevel: (unit -> 'a) -> 'a
    24     end;
    25 end;
    26 
    27 signature OUTER_SYNTAX =
    28 sig
    29   include BASIC_OUTER_SYNTAX
    30   type parser_fn = OuterLex.token list ->
    31     (Toplevel.transition -> Toplevel.transition) * OuterLex.token list
    32   val get_lexicons: unit -> Scan.lexicon * Scan.lexicon
    33   val command_keyword: string -> OuterKeyword.T option
    34   val is_keyword: string -> bool
    35   val keywords: string list -> unit
    36   val command: string -> string -> OuterKeyword.T -> parser_fn -> unit
    37   val markup_command: ThyOutput.markup -> string -> string -> OuterKeyword.T -> parser_fn -> unit
    38   val improper_command: string -> string -> OuterKeyword.T -> parser_fn -> unit
    39   val dest_keywords: unit -> string list
    40   val dest_parsers: unit -> (string * string * string * bool) list
    41   val print_outer_syntax: unit -> unit
    42   val report: unit -> unit
    43   val check_text: string * Position.T -> Toplevel.node option -> unit
    44   val scan: string -> OuterLex.token list
    45   val read: OuterLex.token list -> (string * OuterLex.token list * Toplevel.transition) list
    46   val parse: Position.T -> string -> Toplevel.transition list
    47   val process_file: Path.T -> theory -> theory
    48   val isar: bool -> unit Toplevel.isar
    49 end;
    50 
    51 structure OuterSyntax : OUTER_SYNTAX  =
    52 struct
    53 
    54 structure T = OuterLex;
    55 structure P = OuterParse;
    56 
    57 
    58 (** outer syntax **)
    59 
    60 (* diagnostics *)
    61 
    62 fun report_keyword name =
    63   Pretty.markup (Markup.keyword_decl name)
    64     [Pretty.str ("Outer syntax keyword: " ^ quote name)];
    65 
    66 fun report_command name kind =
    67   Pretty.markup (Markup.command_decl name kind)
    68     [Pretty.str ("Outer syntax command: " ^ quote name ^ " (" ^ kind ^ ")")];
    69 
    70 
    71 (* parsers *)
    72 
    73 type parser_fn = T.token list -> (Toplevel.transition -> Toplevel.transition) * T.token list;
    74 
    75 datatype parser = Parser of
    76  {comment: string,
    77   kind: OuterKeyword.T,
    78   markup: ThyOutput.markup option,
    79   int_only: bool,
    80   parse: parser_fn};
    81 
    82 fun make_parser comment kind markup int_only parse =
    83   Parser {comment = comment, kind = kind, markup = markup, int_only = int_only, parse = parse};
    84 
    85 
    86 (* parse command *)
    87 
    88 local
    89 
    90 fun terminate false = Scan.succeed ()
    91   | terminate true = P.group "end of input" (Scan.option P.sync -- P.semicolon >> K ());
    92 
    93 fun trace false parse = parse
    94   | trace true parse = Scan.trace parse >> (fn (f, toks) => f o Toplevel.source toks);
    95 
    96 fun body cmd do_trace (name, _) =
    97   (case cmd name of
    98     SOME (Parser {int_only, parse, ...}) =>
    99       P.!!! (Scan.prompt (name ^ "# ") (trace do_trace (P.tags |-- parse) >> pair int_only))
   100   | NONE => sys_error ("no parser for outer syntax command " ^ quote name));
   101 
   102 in
   103 
   104 fun parse_command do_terminate do_trace cmd =
   105   P.semicolon >> K NONE ||
   106   P.sync >> K NONE ||
   107   (P.position P.command :-- body cmd do_trace) --| terminate do_terminate
   108     >> (fn ((name, pos), (int_only, f)) =>
   109       SOME (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |>
   110         Toplevel.interactive int_only |> f));
   111 
   112 end;
   113 
   114 
   115 
   116 (** global outer syntax **)
   117 
   118 local
   119 
   120 val global_lexicons = ref (Scan.empty_lexicon, Scan.empty_lexicon);
   121 val global_parsers = ref (Symtab.empty: parser Symtab.table);
   122 val global_markups = ref ([]: (string * ThyOutput.markup) list);
   123 
   124 fun change_lexicons f = CRITICAL (fn () =>
   125   let val lexs = f (! global_lexicons) in
   126     (case (op inter_string) (pairself Scan.dest_lexicon lexs) of
   127       [] => global_lexicons := lexs
   128     | bads => error ("Clash of outer syntax commands and keywords: " ^ commas_quote bads))
   129   end);
   130 
   131 fun change_parsers f = CRITICAL (fn () =>
   132  (change global_parsers f;
   133   global_markups :=
   134     Symtab.fold (fn (name, Parser {markup = SOME m, ...}) => cons (name, m) | _ => I)
   135       (! global_parsers) []));
   136 
   137 in
   138 
   139 (* access current syntax *)
   140 
   141 fun get_lexicons () = CRITICAL (fn () => ! global_lexicons);
   142 fun get_parsers () = CRITICAL (fn () => ! global_parsers);
   143 fun get_markups () = CRITICAL (fn () => ! global_markups);
   144 
   145 fun get_parser () = Symtab.lookup (get_parsers ());
   146 
   147 fun command_keyword name =
   148   (case Symtab.lookup (get_parsers ()) name of
   149     SOME (Parser {kind, ...}) => SOME kind
   150   | NONE => NONE);
   151 
   152 fun command_tags name = these ((Option.map OuterKeyword.tags_of) (command_keyword name));
   153 
   154 fun is_markup kind name = AList.lookup (op =) (get_markups ()) name = SOME kind;
   155 
   156 
   157 (* augment syntax *)
   158 
   159 fun keywords names =
   160  (change_lexicons (apfst (Scan.extend_lexicon (map Symbol.explode names)));
   161   List.app (Pretty.writeln o report_keyword) names);
   162 
   163 
   164 fun add_parser (name, parser as Parser {kind, ...}) =
   165  (if not (Symtab.defined (get_parsers ()) name) then ()
   166   else warning ("Redefining outer syntax command " ^ quote name);
   167   change_parsers (Symtab.update (name, parser));
   168   change_lexicons (apsnd (Scan.extend_lexicon [Symbol.explode name]));
   169   Pretty.writeln (report_command name (OuterKeyword.kind_of kind)));
   170 
   171 fun command name comment kind parse =
   172   add_parser (name, make_parser comment kind NONE false parse);
   173 
   174 fun markup_command markup name comment kind parse =
   175   add_parser (name, make_parser comment kind (SOME markup) false parse);
   176 
   177 fun improper_command name comment kind parse =
   178   add_parser (name, make_parser comment kind NONE true parse);
   179 
   180 end;
   181 
   182 
   183 (* inspect syntax *)
   184 
   185 fun is_keyword s = Scan.is_literal (#1 (get_lexicons ())) (Symbol.explode s);
   186 fun dest_keywords () = Scan.dest_lexicon (#1 (get_lexicons ()));
   187 
   188 fun dest_parsers () =
   189   get_parsers () |> Symtab.dest |> sort_wrt #1
   190   |> map (fn (name, Parser {comment, kind, int_only, ...}) =>
   191     (name, comment, OuterKeyword.kind_of kind, int_only));
   192 
   193 fun print_outer_syntax () =
   194   let
   195     fun pretty_cmd (name, comment, _, _) =
   196       Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
   197     val (int_cmds, cmds) = List.partition #4 (dest_parsers ());
   198   in
   199     [Pretty.strs ("syntax keywords:" :: map quote (dest_keywords ())),
   200       Pretty.big_list "commands:" (map pretty_cmd cmds),
   201       Pretty.big_list "interactive-only commands:" (map pretty_cmd int_cmds)]
   202     |> Pretty.chunks |> Pretty.writeln
   203   end;
   204 
   205 fun report () =
   206   (map report_keyword (dest_keywords ()) @
   207     map (fn (name, _, kind, _) => report_command name kind) (dest_parsers ()))
   208   |> Pretty.chunks |> Pretty.writeln;
   209 
   210 
   211 
   212 (** toplevel parsing **)
   213 
   214 (* basic sources *)
   215 
   216 fun toplevel_source term do_trace do_recover cmd src =
   217   let
   218     val no_terminator =
   219       Scan.unless P.semicolon (Scan.one (T.not_sync andf T.not_eof));
   220     fun recover int =
   221       (int, fn _ => Scan.prompt "recover# " (Scan.repeat no_terminator) >> K [NONE]);
   222   in
   223     src
   224     |> T.source_proper
   225     |> Source.source T.stopper
   226       (Scan.bulk (P.$$$ "--" -- P.!!! P.text >> K NONE || P.not_eof >> SOME))
   227         (Option.map recover do_recover)
   228     |> Source.map_filter I
   229     |> Source.source T.stopper
   230         (Scan.bulk (fn xs => P.!!! (parse_command term do_trace (cmd ())) xs))
   231         (Option.map recover do_recover)
   232     |> Source.map_filter I
   233   end;
   234 
   235 
   236 (* off-line scanning/parsing *)
   237 
   238 (*tokens*)
   239 fun scan str =
   240   Source.of_string str
   241   |> Symbol.source false
   242   |> T.source (SOME false) get_lexicons Position.none
   243   |> Source.exhaust;
   244 
   245 (*commands from tokens, with trace*)
   246 fun read toks =
   247   Source.of_list toks
   248   |> toplevel_source false true NONE get_parser
   249   |> Source.exhaust
   250   |> map (fn tr => (Toplevel.name_of tr, the (Toplevel.source_of tr), tr));
   251 
   252 (*commands from string, without trace*)
   253 fun parse pos str =
   254   Source.of_string str
   255   |> Symbol.source false
   256   |> T.source (SOME false) get_lexicons pos
   257   |> toplevel_source false false NONE get_parser
   258   |> Source.exhaust;
   259 
   260 
   261 (* process file *)
   262 
   263 fun process_file path thy =
   264   let
   265     val result = ref thy;
   266     val trs = parse (Position.path path) (File.read path);
   267     val init = Toplevel.init_theory (K thy) (fn thy' => result := thy') (K ());
   268     val _ = Toplevel.excursion (init Toplevel.empty :: trs @ [Toplevel.exit Toplevel.empty]);
   269   in ! result end;
   270 
   271 
   272 (* interactive source of toplevel transformers *)
   273 
   274 fun isar term =
   275   Source.tty
   276   |> Symbol.source true
   277   |> T.source (SOME true) get_lexicons Position.none
   278   |> toplevel_source term false (SOME true) get_parser;
   279 
   280 
   281 
   282 (** read theory **)
   283 
   284 (* check_text *)
   285 
   286 fun check_text s state = (ThyOutput.eval_antiquote (#1 (get_lexicons ())) state s; ());
   287 
   288 
   289 (* load_thy (backpatching) *)
   290 
   291 local
   292 
   293 fun load_thy dir name pos text time =
   294   let
   295     val text_src = Source.of_list (Library.untabify text);
   296 
   297     val _ = Present.init_theory name;
   298     val _ = Present.verbatim_source name (fn () => Source.exhaust (Symbol.source false text_src));
   299     val toks = text_src
   300       |> Symbol.source false
   301       |> T.source NONE (K (get_lexicons ())) pos
   302       |> Source.exhausted;
   303     val trs = toks
   304       |> toplevel_source false false NONE (K (get_parser ()))
   305       |> Source.exhaust;
   306 
   307     val _ = if time then writeln ("\n**** Starting theory " ^ quote name ^ " ****") else ();
   308     val _ = cond_timeit time "" (fn () =>
   309       ThyOutput.process_thy (#1 (get_lexicons ())) command_tags is_markup trs toks
   310       |> Buffer.content
   311       |> Present.theory_output name);
   312     val _ = if time then writeln ("**** Finished theory " ^ quote name ^ " ****\n") else ();
   313   in () end;
   314 
   315 in val _ = ThyLoad.load_thy_fn := load_thy end;
   316 
   317 
   318 
   319 (** the read-eval-print loop **)
   320 
   321 (* main loop *)
   322 
   323 fun gen_loop secure do_terminate =
   324  (CRITICAL (fn () => Context.set_thread_data NONE);
   325   Toplevel.loop secure (isar do_terminate));
   326 
   327 fun gen_main secure do_terminate =
   328  (Toplevel.init_state ();
   329   writeln (Session.welcome ());
   330   gen_loop secure do_terminate);
   331 
   332 structure Isar =
   333 struct
   334   val state = Toplevel.get_state;
   335   val exn = Toplevel.exn;
   336 
   337   fun context () =
   338     Toplevel.context_of (state ())
   339       handle Toplevel.UNDEF => error "Unknown context";
   340 
   341   fun goal () =
   342     #2 (Proof.get_goal (Toplevel.proof_of (state ())))
   343       handle Toplevel.UNDEF => error "No goal present";
   344 
   345   fun main () = gen_main (Secure.is_secure ()) false;
   346   fun loop () = gen_loop (Secure.is_secure ()) false;
   347   fun sync_main () = gen_main (Secure.is_secure ()) true;
   348   fun sync_loop () = gen_loop (Secure.is_secure ()) true;
   349   fun secure_main () = (Toplevel.init_state (); gen_loop true true);
   350   val toplevel = Toplevel.program;
   351 end;
   352 
   353 end;
   354 
   355 structure ThyLoad: THY_LOAD = ThyLoad;
   356 structure BasicOuterSyntax: BASIC_OUTER_SYNTAX = OuterSyntax;
   357 open BasicOuterSyntax;