src/Pure/Thy/thy_output.ML
changeset 71507 39fa41148890
parent 70308 7f568724d67e
child 73687 54fe8cc0e1c6
equal deleted inserted replaced
71506:4197e30040f3 71507:39fa41148890
   173 (** present theory source **)
   173 (** present theory source **)
   174 
   174 
   175 (* presentation tokens *)
   175 (* presentation tokens *)
   176 
   176 
   177 datatype token =
   177 datatype token =
   178     Ignore_Token
   178     Ignore
   179   | Basic_Token of Token.T
   179   | Token of Token.T
   180   | Markup_Token of string * Input.source
   180   | Heading of string * Input.source
   181   | Markup_Env_Token of string * Input.source
   181   | Body of string * Input.source
   182   | Raw_Token of Input.source;
   182   | Raw of Input.source;
   183 
   183 
   184 fun basic_token pred (Basic_Token tok) = pred tok
   184 fun token_with pred (Token tok) = pred tok
   185   | basic_token _ _ = false;
   185   | token_with _ _ = false;
   186 
   186 
   187 val white_token = basic_token Document_Source.is_white;
   187 val white_token = token_with Document_Source.is_white;
   188 val white_comment_token = basic_token Document_Source.is_white_comment;
   188 val white_comment_token = token_with Document_Source.is_white_comment;
   189 val blank_token = basic_token Token.is_blank;
   189 val blank_token = token_with Token.is_blank;
   190 val newline_token = basic_token Token.is_newline;
   190 val newline_token = token_with Token.is_newline;
   191 
   191 
   192 fun present_token ctxt tok =
   192 fun present_token ctxt tok =
   193   (case tok of
   193   (case tok of
   194     Ignore_Token => []
   194     Ignore => []
   195   | Basic_Token tok => output_token ctxt tok
   195   | Token tok => output_token ctxt tok
   196   | Markup_Token (cmd, source) =>
   196   | Heading (cmd, source) =>
   197       Latex.enclose_body ("%\n\\isamarkup" ^ cmd ^ "{") "%\n}\n"
   197       Latex.enclose_body ("%\n\\isamarkup" ^ cmd ^ "{") "%\n}\n"
   198         (output_document ctxt {markdown = false} source)
   198         (output_document ctxt {markdown = false} source)
   199   | Markup_Env_Token (cmd, source) =>
   199   | Body (cmd, source) =>
   200       [Latex.environment_block ("isamarkup" ^ cmd) (output_document ctxt {markdown = true} source)]
   200       [Latex.environment_block ("isamarkup" ^ cmd) (output_document ctxt {markdown = true} source)]
   201   | Raw_Token source =>
   201   | Raw source =>
   202       Latex.string "%\n" :: output_document ctxt {markdown = true} source @ [Latex.string "\n"]);
   202       Latex.string "%\n" :: output_document ctxt {markdown = true} source @ [Latex.string "\n"]);
   203 
   203 
   204 
   204 
   205 (* command spans *)
   205 (* command spans *)
   206 
   206 
   365 
   365 
   366 
   366 
   367     (* tokens *)
   367     (* tokens *)
   368 
   368 
   369     val ignored = Scan.state --| ignore
   369     val ignored = Scan.state --| ignore
   370       >> (fn d => (NONE, (Ignore_Token, ("", d))));
   370       >> (fn d => (NONE, (Ignore, ("", d))));
   371 
   371 
   372     fun markup pred mk flag = Scan.peek (fn d =>
   372     fun markup pred mk flag = Scan.peek (fn d =>
   373       Document_Source.improper |--
   373       Document_Source.improper |--
   374         Parse.position (Scan.one (fn tok =>
   374         Parse.position (Scan.one (fn tok =>
   375           Token.is_command tok andalso pred keywords (Token.content_of tok))) --
   375           Token.is_command tok andalso pred keywords (Token.content_of tok))) --
   382 
   382 
   383     val command = Scan.peek (fn d =>
   383     val command = Scan.peek (fn d =>
   384       Scan.optional (Scan.one Token.is_command_modifier ::: Document_Source.improper) [] --
   384       Scan.optional (Scan.one Token.is_command_modifier ::: Document_Source.improper) [] --
   385       Scan.one Token.is_command --| Document_Source.annotation
   385       Scan.one Token.is_command --| Document_Source.annotation
   386       >> (fn (cmd_mod, cmd) =>
   386       >> (fn (cmd_mod, cmd) =>
   387         map (fn tok => (NONE, (Basic_Token tok, ("", d)))) cmd_mod @
   387         map (fn tok => (NONE, (Token tok, ("", d)))) cmd_mod @
   388           [(SOME (Token.content_of cmd, Token.pos_of cmd),
   388           [(SOME (Token.content_of cmd, Token.pos_of cmd),
   389             (Basic_Token cmd, (markup_false, d)))]));
   389             (Token cmd, (markup_false, d)))]));
   390 
   390 
   391     val cmt = Scan.peek (fn d =>
   391     val cmt = Scan.peek (fn d =>
   392       Scan.one Document_Source.is_black_comment >> (fn tok => (NONE, (Basic_Token tok, ("", d)))));
   392       Scan.one Document_Source.is_black_comment >> (fn tok => (NONE, (Token tok, ("", d)))));
   393 
   393 
   394     val other = Scan.peek (fn d =>
   394     val other = Scan.peek (fn d =>
   395        Parse.not_eof >> (fn tok => (NONE, (Basic_Token tok, ("", d)))));
   395        Parse.not_eof >> (fn tok => (NONE, (Token tok, ("", d)))));
   396 
   396 
   397     val tokens =
   397     val tokens =
   398       (ignored ||
   398       (ignored ||
   399         markup Keyword.is_document_heading Markup_Token markup_true ||
   399         markup Keyword.is_document_heading Heading markup_true ||
   400         markup Keyword.is_document_body Markup_Env_Token markup_true ||
   400         markup Keyword.is_document_body Body markup_true ||
   401         markup Keyword.is_document_raw (Raw_Token o #2) "") >> single ||
   401         markup Keyword.is_document_raw (Raw o #2) "") >> single ||
   402       command ||
   402       command ||
   403       (cmt || other) >> single;
   403       (cmt || other) >> single;
   404 
   404 
   405 
   405 
   406     (* spans *)
   406     (* spans *)
   407 
   407 
   408     val is_eof = fn (_, (Basic_Token x, _)) => Token.is_eof x | _ => false;
   408     val is_eof = fn (_, (Token x, _)) => Token.is_eof x | _ => false;
   409     val stopper = Scan.stopper (K (NONE, (Basic_Token Token.eof, ("", 0)))) is_eof;
   409     val stopper = Scan.stopper (K (NONE, (Token Token.eof, ("", 0)))) is_eof;
   410 
   410 
   411     val cmd = Scan.one (is_some o fst);
   411     val cmd = Scan.one (is_some o fst);
   412     val non_cmd = Scan.one (is_none o fst andf not o is_eof) >> #2;
   412     val non_cmd = Scan.one (is_none o fst andf not o is_eof) >> #2;
   413 
   413 
   414     val white_comments = Scan.many (white_comment_token o fst o snd);
   414     val white_comments = Scan.many (white_comment_token o fst o snd);