src/Pure/General/comment.ML
author wenzelm
Sun, 10 Mar 2019 21:12:29 +0100
changeset 69891 def3ec9cdb7e
parent 67572 a93cf1d6ba87
child 69965 da5e7278286b
permissions -rw-r--r--
document markers are formal comments, and may thus occur anywhere in the command-span; clarified Outer_Syntax.parse_span, Outer_Syntax.parse_text wrt. span structure; tuned signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
67425
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/General/comment.ML
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
     3
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
     4
Formal comments.
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
     5
*)
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
     6
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
     7
signature COMMENT =
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
     8
sig
69891
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 67572
diff changeset
     9
  datatype kind = Comment | Cancel | Latex | Marker
67506
30233285270a more markup: disable spell-checker for raw latex;
wenzelm
parents: 67429
diff changeset
    10
  val markups: kind -> Markup.T list
67571
f858fe5531ac more uniform treatment of formal comments within document source;
wenzelm
parents: 67506
diff changeset
    11
  val is_symbol: Symbol.symbol -> bool
67426
6311cf9dc943 clarified signature;
wenzelm
parents: 67425
diff changeset
    12
  val scan_comment: (kind * Symbol_Pos.T list) scanner
6311cf9dc943 clarified signature;
wenzelm
parents: 67425
diff changeset
    13
  val scan_cancel: (kind * Symbol_Pos.T list) scanner
67429
95877cc6630e allow LaTeX source as formal comment;
wenzelm
parents: 67426
diff changeset
    14
  val scan_latex: (kind * Symbol_Pos.T list) scanner
69891
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 67572
diff changeset
    15
  val scan_marker: (kind * Symbol_Pos.T list) scanner
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 67572
diff changeset
    16
  val scan_inner: (kind * Symbol_Pos.T list) scanner
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 67572
diff changeset
    17
  val scan_outer: (kind * Symbol_Pos.T list) scanner
67572
a93cf1d6ba87 clarified signature;
wenzelm
parents: 67571
diff changeset
    18
  val read_body: Symbol_Pos.T list -> (kind option * Symbol_Pos.T list) list
67425
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    19
end;
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    20
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    21
structure Comment: COMMENT =
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    22
struct
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    23
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    24
(* kinds *)
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    25
69891
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 67572
diff changeset
    26
datatype kind = Comment | Cancel | Latex | Marker;
67425
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    27
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    28
val kinds =
67506
30233285270a more markup: disable spell-checker for raw latex;
wenzelm
parents: 67429
diff changeset
    29
  [(Comment,
30233285270a more markup: disable spell-checker for raw latex;
wenzelm
parents: 67429
diff changeset
    30
     {symbol = Symbol.comment, blanks = true,
30233285270a more markup: disable spell-checker for raw latex;
wenzelm
parents: 67429
diff changeset
    31
      markups = [Markup.language_document true]}),
30233285270a more markup: disable spell-checker for raw latex;
wenzelm
parents: 67429
diff changeset
    32
   (Cancel,
30233285270a more markup: disable spell-checker for raw latex;
wenzelm
parents: 67429
diff changeset
    33
     {symbol = Symbol.cancel, blanks = false,
30233285270a more markup: disable spell-checker for raw latex;
wenzelm
parents: 67429
diff changeset
    34
      markups = [Markup.language_text true]}),
30233285270a more markup: disable spell-checker for raw latex;
wenzelm
parents: 67429
diff changeset
    35
   (Latex,
30233285270a more markup: disable spell-checker for raw latex;
wenzelm
parents: 67429
diff changeset
    36
     {symbol = Symbol.latex, blanks = false,
69891
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 67572
diff changeset
    37
      markups = [Markup.language_latex true, Markup.no_words]}),
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 67572
diff changeset
    38
   (Marker,
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 67572
diff changeset
    39
     {symbol = Symbol.marker, blanks = false,
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 67572
diff changeset
    40
      markups = [Markup.language_document_marker]})];
67425
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    41
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    42
val get_kind = the o AList.lookup (op =) kinds;
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    43
val print_kind = quote o #symbol o get_kind;
67506
30233285270a more markup: disable spell-checker for raw latex;
wenzelm
parents: 67429
diff changeset
    44
val markups = #markups o get_kind;
67425
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    45
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    46
fun is_symbol sym = exists (fn (_, {symbol, ...}) => symbol = sym) kinds;
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    47
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    48
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    49
(* scan *)
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    50
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    51
local
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    52
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    53
open Basic_Symbol_Pos;
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    54
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    55
val err_prefix = "Error in formal comment: ";
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    56
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    57
val scan_blanks = Scan.many (Symbol.is_blank o Symbol_Pos.symbol);
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    58
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    59
fun scan_symbol kind =
67429
95877cc6630e allow LaTeX source as formal comment;
wenzelm
parents: 67426
diff changeset
    60
  let val {blanks, symbol, ...} = get_kind kind
67425
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    61
  in if blanks then $$$ symbol @@@ scan_blanks else $$$ symbol end;
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    62
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    63
fun scan_strict kind =
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    64
  scan_symbol kind @@@
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    65
    Symbol_Pos.!!! (fn () => err_prefix ^ "cartouche expected after " ^ print_kind kind)
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    66
      (Symbol_Pos.scan_cartouche err_prefix) >> pair kind;
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    67
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    68
fun scan_permissive kind =
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    69
  scan_symbol kind -- Scan.option (Symbol_Pos.scan_cartouche err_prefix) >>
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    70
    (fn (ss, NONE) => (NONE, ss) | (_, SOME ss) => (SOME kind, ss));
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    71
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    72
in
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    73
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    74
val scan_comment = scan_strict Comment;
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    75
val scan_cancel = scan_strict Cancel;
67429
95877cc6630e allow LaTeX source as formal comment;
wenzelm
parents: 67426
diff changeset
    76
val scan_latex = scan_strict Latex;
69891
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 67572
diff changeset
    77
val scan_marker = scan_strict Marker;
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 67572
diff changeset
    78
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 67572
diff changeset
    79
val scan_inner = scan_comment || scan_cancel || scan_latex;
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 67572
diff changeset
    80
val scan_outer = scan_inner || scan_marker;
67425
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    81
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    82
val scan_body =
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    83
  Scan.many1 (fn (s, _) => not (is_symbol s) andalso Symbol.not_eof s) >> pair NONE ||
67429
95877cc6630e allow LaTeX source as formal comment;
wenzelm
parents: 67426
diff changeset
    84
  scan_permissive Comment || scan_permissive Cancel || scan_permissive Latex;
67425
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    85
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    86
fun read_body syms =
67572
a93cf1d6ba87 clarified signature;
wenzelm
parents: 67571
diff changeset
    87
  (if exists (is_symbol o Symbol_Pos.symbol) syms then
67425
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    88
    Scan.read Symbol_Pos.stopper (Scan.repeat scan_body) syms
67572
a93cf1d6ba87 clarified signature;
wenzelm
parents: 67571
diff changeset
    89
  else NONE) |> the_default [(NONE, syms)];
67425
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    90
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    91
end;
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    92
7d4a088dbc0e clarified modules: uniform notion of formal comments;
wenzelm
parents:
diff changeset
    93
end;