src/Pure/Isar/outer_syntax.ML
author wenzelm
Sun, 25 Nov 2012 19:49:24 +0100
changeset 51216 c26369c9eda6
parent 50579 03381c41235b
child 51228 7b73c0509835
permissions -rw-r--r--
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
     1 (*  Title:      Pure/Isar/outer_syntax.ML
     2     Author:     Markus Wenzel, TU Muenchen
     3 
     4 The global Isabelle/Isar outer syntax.
     5 
     6 Note: the syntax for files is statically determined at the very
     7 beginning; for interactive processing it may change dynamically.
     8 *)
     9 
    10 signature OUTER_SYNTAX =
    11 sig
    12   type outer_syntax
    13   val is_markup: outer_syntax -> Thy_Output.markup -> string -> bool
    14   val get_syntax: unit -> (Scan.lexicon * Scan.lexicon) * outer_syntax
    15   val check_syntax: unit -> unit
    16   type command_spec = (string * Keyword.T) * Position.T
    17   val command: command_spec -> string ->
    18     (Toplevel.transition -> Toplevel.transition) parser -> unit
    19   val markup_command: Thy_Output.markup -> command_spec -> string ->
    20     (Toplevel.transition -> Toplevel.transition) parser -> unit
    21   val improper_command: command_spec -> string ->
    22     (Toplevel.transition -> Toplevel.transition) parser -> unit
    23   val local_theory': command_spec -> string ->
    24     (bool -> local_theory -> local_theory) parser -> unit
    25   val local_theory: command_spec -> string ->
    26     (local_theory -> local_theory) parser -> unit
    27   val local_theory_to_proof': command_spec -> string ->
    28     (bool -> local_theory -> Proof.state) parser -> unit
    29   val local_theory_to_proof: command_spec -> string ->
    30     (local_theory -> Proof.state) parser -> unit
    31   val print_outer_syntax: unit -> unit
    32   val scan: Position.T -> string -> Token.T list
    33   val parse: Position.T -> string -> Toplevel.transition list
    34   type isar
    35   val isar: TextIO.instream -> bool -> isar
    36   val span_cmts: Token.T list -> Token.T list
    37   val read_span: outer_syntax -> Token.T list -> Toplevel.transition * bool
    38   val read_element: outer_syntax -> (unit -> theory) -> Thy_Syntax.element ->
    39     (Toplevel.transition * Toplevel.transition list) list
    40 end;
    41 
    42 structure Outer_Syntax: OUTER_SYNTAX =
    43 struct
    44 
    45 (** outer syntax **)
    46 
    47 (* command parsers *)
    48 
    49 datatype command = Command of
    50  {comment: string,
    51   markup: Thy_Output.markup option,
    52   int_only: bool,
    53   parse: (Toplevel.transition -> Toplevel.transition) parser,
    54   pos: Position.T,
    55   id: serial};
    56 
    57 fun new_command comment markup int_only parse pos =
    58   Command {comment = comment, markup = markup, int_only = int_only, parse = parse,
    59     pos = pos, id = serial ()};
    60 
    61 fun command_markup def (name, Command {pos, id, ...}) =
    62   Markup.properties (Position.entity_properties_of def id pos)
    63     (Markup.entity Markup.commandN name);
    64 
    65 
    66 (* parse command *)
    67 
    68 local
    69 
    70 fun terminate false = Scan.succeed ()
    71   | terminate true =
    72       Parse.group (fn () => "end of input")
    73         (Scan.option Parse.sync -- Parse.semicolon >> K ());
    74 
    75 fun body cmd (name, _) =
    76   (case cmd name of
    77     SOME (Command {int_only, parse, ...}) =>
    78       Parse.!!! (Scan.prompt (name ^ "# ") (Parse.tags |-- parse >> pair int_only))
    79   | NONE =>
    80       Scan.succeed (false, Toplevel.imperative (fn () =>
    81         error ("Bad parser for outer syntax command " ^ quote name))));
    82 
    83 in
    84 
    85 fun parse_command do_terminate cmd =
    86   Parse.semicolon >> K NONE ||
    87   Parse.sync >> K NONE ||
    88   (Parse.position Parse.command :-- body cmd) --| terminate do_terminate
    89     >> (fn ((name, pos), (int_only, f)) =>
    90       SOME (Toplevel.empty |> Toplevel.name name |> Toplevel.position pos |>
    91         Toplevel.interactive int_only |> f));
    92 
    93 end;
    94 
    95 
    96 (* type outer_syntax *)
    97 
    98 datatype outer_syntax = Outer_Syntax of
    99  {commands: command Symtab.table,
   100   markups: (string * Thy_Output.markup) list};
   101 
   102 fun make_outer_syntax commands markups =
   103   Outer_Syntax {commands = commands, markups = markups};
   104 
   105 val empty_outer_syntax = make_outer_syntax Symtab.empty [];
   106 
   107 
   108 fun map_commands f (Outer_Syntax {commands, ...}) =
   109   let
   110     val commands' = f commands;
   111     val markups' =
   112       Symtab.fold (fn (name, Command {markup = SOME m, ...}) => cons (name, m) | _ => I)
   113         commands' [];
   114   in make_outer_syntax commands' markups' end;
   115 
   116 fun dest_commands (Outer_Syntax {commands, ...}) =
   117   commands |> Symtab.dest |> sort_wrt #1
   118   |> map (fn (name, Command {comment, int_only, ...}) => (name, comment, int_only));
   119 
   120 fun lookup_commands (Outer_Syntax {commands, ...}) = Symtab.lookup commands;
   121 
   122 fun is_markup (Outer_Syntax {markups, ...}) kind name =
   123   AList.lookup (op =) markups name = SOME kind;
   124 
   125 
   126 
   127 (** global outer syntax **)
   128 
   129 type command_spec = (string * Keyword.T) * Position.T;
   130 
   131 local
   132 
   133 (*synchronized wrt. Keywords*)
   134 val global_outer_syntax = Unsynchronized.ref empty_outer_syntax;
   135 
   136 fun add_command (name, kind) cmd = CRITICAL (fn () =>
   137   let
   138     val thy = ML_Context.the_global_context ();
   139     val Command {pos, ...} = cmd;
   140     val _ =
   141       (case try (Thy_Header.the_keyword thy) name of
   142         SOME spec =>
   143           if Option.map #1 spec = SOME (Keyword.kind_files_of kind) then ()
   144           else error ("Inconsistent outer syntax keyword declaration " ^
   145             quote name ^ Position.here pos)
   146       | NONE =>
   147           if Context.theory_name thy = Context.PureN
   148           then Keyword.define (name, SOME kind)
   149           else error ("Undeclared outer syntax command " ^ quote name ^ Position.here pos));
   150     val _ = Position.report pos (command_markup true (name, cmd));
   151   in
   152     Unsynchronized.change global_outer_syntax (map_commands (fn commands =>
   153      (if not (Symtab.defined commands name) then ()
   154       else warning ("Redefining outer syntax command " ^ quote name);
   155       Symtab.update (name, cmd) commands)))
   156   end);
   157 
   158 in
   159 
   160 fun get_syntax () = CRITICAL (fn () => (Keyword.get_lexicons (), ! global_outer_syntax));
   161 
   162 fun check_syntax () =
   163   let
   164     val ((_, major), syntax) = CRITICAL (fn () => (Keyword.dest (), ! global_outer_syntax));
   165   in
   166     (case subtract (op =) (map #1 (dest_commands syntax)) major of
   167       [] => ()
   168     | missing => error ("Missing outer syntax command(s) " ^ commas_quote missing))
   169   end;
   170 
   171 fun lookup_commands_dynamic () = lookup_commands (! global_outer_syntax);
   172 
   173 fun command (spec, pos) comment parse =
   174   add_command spec (new_command comment NONE false parse pos);
   175 
   176 fun markup_command markup (spec, pos) comment parse =
   177   add_command spec (new_command comment (SOME markup) false parse pos);
   178 
   179 fun improper_command (spec, pos) comment parse =
   180   add_command spec (new_command comment NONE true parse pos);
   181 
   182 end;
   183 
   184 
   185 (* local_theory commands *)
   186 
   187 fun local_theory_command do_print trans command_spec comment parse =
   188   command command_spec comment (Parse.opt_target -- parse
   189     >> (fn (loc, f) => (if do_print then Toplevel.print else I) o trans loc f));
   190 
   191 val local_theory' = local_theory_command false Toplevel.local_theory';
   192 val local_theory = local_theory_command false Toplevel.local_theory;
   193 val local_theory_to_proof' = local_theory_command true Toplevel.local_theory_to_proof';
   194 val local_theory_to_proof = local_theory_command true Toplevel.local_theory_to_proof;
   195 
   196 
   197 (* inspect syntax *)
   198 
   199 fun print_outer_syntax () =
   200   let
   201     val ((keywords, _), outer_syntax) =
   202       CRITICAL (fn () => (Keyword.dest (), #2 (get_syntax ())));
   203     fun pretty_cmd (name, comment, _) =
   204       Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
   205     val (int_cmds, cmds) = List.partition #3 (dest_commands outer_syntax);
   206   in
   207     [Pretty.strs ("syntax keywords:" :: map quote keywords),
   208       Pretty.big_list "commands:" (map pretty_cmd cmds),
   209       Pretty.big_list "interactive-only commands:" (map pretty_cmd int_cmds)]
   210     |> Pretty.chunks |> Pretty.writeln
   211   end;
   212 
   213 
   214 
   215 (** toplevel parsing **)
   216 
   217 (* basic sources *)
   218 
   219 fun toplevel_source term do_recover cmd src =
   220   let
   221     val no_terminator =
   222       Scan.unless Parse.semicolon (Scan.one (Token.not_sync andf Token.not_eof));
   223     fun recover int =
   224       (int, fn _ => Scan.prompt "recover# " (Scan.repeat no_terminator) >> K [NONE]);
   225   in
   226     src
   227     |> Token.source_proper
   228     |> Source.source Token.stopper
   229       (Scan.bulk (Parse.$$$ "--" -- Parse.!!! Parse.doc_source >> K NONE || Parse.not_eof >> SOME))
   230         (Option.map recover do_recover)
   231     |> Source.map_filter I
   232     |> Source.source Token.stopper
   233         (Scan.bulk (fn xs => Parse.!!! (parse_command term (cmd ())) xs))
   234         (Option.map recover do_recover)
   235     |> Source.map_filter I
   236   end;
   237 
   238 
   239 (* off-line scanning/parsing *)
   240 
   241 fun scan pos str =
   242   Source.of_string str
   243   |> Symbol.source
   244   |> Token.source {do_recover = SOME false} Keyword.get_lexicons pos
   245   |> Source.exhaust;
   246 
   247 fun parse pos str =
   248   Source.of_string str
   249   |> Symbol.source
   250   |> Token.source {do_recover = SOME false} Keyword.get_lexicons pos
   251   |> toplevel_source false NONE lookup_commands_dynamic
   252   |> Source.exhaust;
   253 
   254 
   255 (* interactive source of toplevel transformers *)
   256 
   257 type isar =
   258   (Toplevel.transition, (Toplevel.transition option,
   259     (Token.T, (Token.T option, (Token.T, (Token.T,
   260       (Symbol_Pos.T, Position.T * (Symbol.symbol, (string, unit) Source.source)
   261   Source.source) Source.source) Source.source) Source.source)
   262   Source.source) Source.source) Source.source) Source.source;
   263 
   264 fun isar in_stream term : isar =
   265   Source.tty in_stream
   266   |> Symbol.source
   267   |> Token.source {do_recover = SOME true} Keyword.get_lexicons Position.none
   268   |> toplevel_source term (SOME true) lookup_commands_dynamic;
   269 
   270 
   271 (* side-comments *)
   272 
   273 local
   274 
   275 fun cmts (t1 :: t2 :: toks) =
   276       if Token.keyword_with (fn s => s = "--") t1 then t2 :: cmts toks
   277       else cmts (t2 :: toks)
   278   | cmts _ = [];
   279 
   280 in
   281 
   282 val span_cmts = filter Token.is_proper #> cmts;
   283 
   284 end;
   285 
   286 
   287 (* read toplevel commands -- fail-safe *)
   288 
   289 fun read_span outer_syntax toks =
   290   let
   291     val commands = lookup_commands outer_syntax;
   292 
   293     val proper_range = Position.set_range (Command.proper_range toks);
   294     val pos =
   295       (case find_first Token.is_command toks of
   296         SOME tok => Token.position_of tok
   297       | NONE => proper_range);
   298 
   299     fun command_reports tok =
   300       if Token.is_command tok then
   301         let val name = Token.content_of tok in
   302           (case commands name of
   303             NONE => []
   304           | SOME cmd => [((Token.position_of tok, command_markup false (name, cmd)), "")])
   305         end
   306       else [];
   307 
   308     val (is_malformed, token_reports) = Thy_Syntax.reports_of_tokens toks;
   309     val _ = Position.reports_text (token_reports @ maps command_reports toks);
   310   in
   311     if is_malformed then (Toplevel.malformed pos "Malformed command syntax", true)
   312     else
   313       (case Source.exhaust (toplevel_source false NONE (K commands) (Source.of_list toks)) of
   314         [tr] =>
   315           if Keyword.is_control (Toplevel.name_of tr) then
   316             (Toplevel.malformed pos "Illegal control command", true)
   317           else (tr, true)
   318       | [] => (Toplevel.ignored (Position.set_range (Command.range toks)), false)
   319       | _ => (Toplevel.malformed proper_range "Exactly one command expected", true))
   320       handle ERROR msg => (Toplevel.malformed proper_range msg, true)
   321   end;
   322 
   323 fun read_element outer_syntax init {head, proof, proper_proof} =
   324   let
   325     val read = read_span outer_syntax o Thy_Syntax.span_content;
   326     val (tr, proper_head) = read head |>> Toplevel.modify_init init;
   327     val proof_trs = map read proof |> filter #2 |> map #1;
   328   in
   329     if proper_head andalso proper_proof andalso
   330       not (Keyword.is_schematic_goal (Toplevel.name_of tr)) then [(tr, proof_trs)]
   331     else map (rpair []) (if proper_head then tr :: proof_trs else proof_trs)
   332   end;
   333 
   334 end;
   335