src/Pure/Isar/outer_syntax.ML
changeset 26600 f11515535c83
parent 26431 f1c79c00f1e4
child 26611 03455add4801
equal deleted inserted replaced
26599:791e4b436f8a 26600:f11515535c83
     5 The global Isabelle/Isar outer syntax. Note: the syntax for files is
     5 The global Isabelle/Isar outer syntax. Note: the syntax for files is
     6 statically determined at the very beginning; for interactive processing
     6 statically determined at the very beginning; for interactive processing
     7 it may change dynamically.
     7 it may change dynamically.
     8 *)
     8 *)
     9 
     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 =
    10 signature OUTER_SYNTAX =
    28 sig
    11 sig
    29   include BASIC_OUTER_SYNTAX
       
    30   type parser_fn = OuterLex.token list ->
    12   type parser_fn = OuterLex.token list ->
    31     (Toplevel.transition -> Toplevel.transition) * OuterLex.token list
    13     (Toplevel.transition -> Toplevel.transition) * OuterLex.token list
    32   val get_lexicons: unit -> Scan.lexicon * Scan.lexicon
    14   val get_lexicons: unit -> Scan.lexicon * Scan.lexicon
    33   val command_keyword: string -> OuterKeyword.T option
    15   val command_keyword: string -> OuterKeyword.T option
    34   val is_keyword: string -> bool
    16   val is_keyword: string -> bool
    43   val check_text: string * Position.T -> Toplevel.node option -> unit
    25   val check_text: string * Position.T -> Toplevel.node option -> unit
    44   val scan: string -> OuterLex.token list
    26   val scan: string -> OuterLex.token list
    45   val read: OuterLex.token list -> (string * OuterLex.token list * Toplevel.transition) list
    27   val read: OuterLex.token list -> (string * OuterLex.token list * Toplevel.transition) list
    46   val parse: Position.T -> string -> Toplevel.transition list
    28   val parse: Position.T -> string -> Toplevel.transition list
    47   val process_file: Path.T -> theory -> theory
    29   val process_file: Path.T -> theory -> theory
    48   val isar: bool -> unit Toplevel.isar
    30   type isar
       
    31   val isar: bool -> isar
    49 end;
    32 end;
    50 
    33 
    51 structure OuterSyntax : OUTER_SYNTAX  =
    34 structure OuterSyntax: OUTER_SYNTAX =
    52 struct
    35 struct
    53 
    36 
    54 structure T = OuterLex;
    37 structure T = OuterLex;
    55 structure P = OuterParse;
    38 structure P = OuterParse;
    56 
    39 
   147 fun command_keyword name =
   130 fun command_keyword name =
   148   (case Symtab.lookup (get_parsers ()) name of
   131   (case Symtab.lookup (get_parsers ()) name of
   149     SOME (Parser {kind, ...}) => SOME kind
   132     SOME (Parser {kind, ...}) => SOME kind
   150   | NONE => NONE);
   133   | NONE => NONE);
   151 
   134 
   152 fun command_tags name = these ((Option.map OuterKeyword.tags_of) (command_keyword name));
   135 fun command_tags name = these (Option.map OuterKeyword.tags_of (command_keyword name));
   153 
   136 
   154 fun is_markup kind name = AList.lookup (op =) (get_markups ()) name = SOME kind;
   137 fun is_markup kind name = AList.lookup (op =) (get_markups ()) name = SOME kind;
   155 
   138 
   156 
   139 
   157 (* augment syntax *)
   140 (* augment syntax *)
   269   in ! result end;
   252   in ! result end;
   270 
   253 
   271 
   254 
   272 (* interactive source of toplevel transformers *)
   255 (* interactive source of toplevel transformers *)
   273 
   256 
   274 fun isar term =
   257 type isar =
       
   258   (Toplevel.transition, (Toplevel.transition option,
       
   259     (OuterLex.token, (OuterLex.token option, (OuterLex.token, (OuterLex.token,
       
   260       Position.T * (Symbol.symbol, (string, unit) Source.source) Source.source)
       
   261           Source.source) Source.source) Source.source) Source.source) Source.source) Source.source;
       
   262 
       
   263 fun isar term : isar =
   275   Source.tty
   264   Source.tty
   276   |> Symbol.source true
   265   |> Symbol.source true
   277   |> T.source (SOME true) get_lexicons Position.none
   266   |> T.source (SOME true) get_lexicons Position.none
   278   |> toplevel_source term false (SOME true) get_parser;
   267   |> toplevel_source term false (SOME true) get_parser;
   279 
   268 
   312     val _ = if time then writeln ("**** Finished theory " ^ quote name ^ " ****\n") else ();
   301     val _ = if time then writeln ("**** Finished theory " ^ quote name ^ " ****\n") else ();
   313   in () end;
   302   in () end;
   314 
   303 
   315 in val _ = ThyLoad.load_thy_fn := load_thy end;
   304 in val _ = ThyLoad.load_thy_fn := load_thy end;
   316 
   305 
   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;
   306 end;
   352 
   307 
   353 end;
       
   354 
       
   355 structure ThyLoad: THY_LOAD = ThyLoad;
   308 structure ThyLoad: THY_LOAD = ThyLoad;
   356 structure BasicOuterSyntax: BASIC_OUTER_SYNTAX = OuterSyntax;
   309 
   357 open BasicOuterSyntax;