src/Pure/General/comment.ML
author wenzelm
Thu Jan 25 15:21:05 2018 +0100 (18 months ago)
changeset 67506 30233285270a
parent 67429 95877cc6630e
child 67571 f858fe5531ac
permissions -rw-r--r--
more markup: disable spell-checker for raw latex;
     1 (*  Title:      Pure/General/comment.ML
     2     Author:     Makarius
     3 
     4 Formal comments.
     5 *)
     6 
     7 signature COMMENT =
     8 sig
     9   datatype kind = Comment | Cancel | Latex
    10   val markups: kind -> Markup.T list
    11   val scan_comment: (kind * Symbol_Pos.T list) scanner
    12   val scan_cancel: (kind * Symbol_Pos.T list) scanner
    13   val scan_latex: (kind * Symbol_Pos.T list) scanner
    14   val scan: (kind * Symbol_Pos.T list) scanner
    15   val read_body: Symbol_Pos.T list -> (kind option * Symbol_Pos.T list) list option
    16 end;
    17 
    18 structure Comment: COMMENT =
    19 struct
    20 
    21 (* kinds *)
    22 
    23 datatype kind = Comment | Cancel | Latex;
    24 
    25 val kinds =
    26   [(Comment,
    27      {symbol = Symbol.comment, blanks = true,
    28       markups = [Markup.language_document true]}),
    29    (Cancel,
    30      {symbol = Symbol.cancel, blanks = false,
    31       markups = [Markup.language_text true]}),
    32    (Latex,
    33      {symbol = Symbol.latex, blanks = false,
    34       markups = [Markup.language_latex true, Markup.no_words]})];
    35 
    36 val get_kind = the o AList.lookup (op =) kinds;
    37 val print_kind = quote o #symbol o get_kind;
    38 val markups = #markups o get_kind;
    39 
    40 fun is_symbol sym = exists (fn (_, {symbol, ...}) => symbol = sym) kinds;
    41 
    42 
    43 (* scan *)
    44 
    45 local
    46 
    47 open Basic_Symbol_Pos;
    48 
    49 val err_prefix = "Error in formal comment: ";
    50 
    51 val scan_blanks = Scan.many (Symbol.is_blank o Symbol_Pos.symbol);
    52 
    53 fun scan_symbol kind =
    54   let val {blanks, symbol, ...} = get_kind kind
    55   in if blanks then $$$ symbol @@@ scan_blanks else $$$ symbol end;
    56 
    57 fun scan_strict kind =
    58   scan_symbol kind @@@
    59     Symbol_Pos.!!! (fn () => err_prefix ^ "cartouche expected after " ^ print_kind kind)
    60       (Symbol_Pos.scan_cartouche err_prefix) >> pair kind;
    61 
    62 fun scan_permissive kind =
    63   scan_symbol kind -- Scan.option (Symbol_Pos.scan_cartouche err_prefix) >>
    64     (fn (ss, NONE) => (NONE, ss) | (_, SOME ss) => (SOME kind, ss));
    65 
    66 in
    67 
    68 val scan_comment = scan_strict Comment;
    69 val scan_cancel = scan_strict Cancel;
    70 val scan_latex = scan_strict Latex;
    71 val scan = scan_comment || scan_cancel || scan_latex;
    72 
    73 val scan_body =
    74   Scan.many1 (fn (s, _) => not (is_symbol s) andalso Symbol.not_eof s) >> pair NONE ||
    75   scan_permissive Comment || scan_permissive Cancel || scan_permissive Latex;
    76 
    77 fun read_body syms =
    78   if exists (is_symbol o Symbol_Pos.symbol) syms then
    79     Scan.read Symbol_Pos.stopper (Scan.repeat scan_body) syms
    80   else NONE;
    81 
    82 end;
    83 
    84 end;