src/Pure/Thy/document_source.ML
author haftmann
Fri, 14 Jun 2019 08:34:28 +0000
changeset 70347 e5cd5471c18a
parent 70134 e69f00f4b897
permissions -rw-r--r--
official fact collection sign_simps
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
69876
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Thy/document_source.ML
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
     3
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
     4
Document source for presentation.
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
     5
*)
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
     6
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
     7
signature DOCUMENT_SOURCE =
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
     8
sig
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
     9
  val is_white: Token.T -> bool
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    10
  val is_black: Token.T -> bool
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    11
  val is_white_comment: Token.T -> bool
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    12
  val is_black_comment: Token.T -> bool
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    13
  val is_improper: Token.T -> bool
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    14
  val improper: Token.T list parser
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    15
  val improper_end: Token.T list parser
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    16
  val blank_end: Token.T list parser
70134
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    17
  datatype scope = Command | Proof
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    18
  type tag = string * scope
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    19
  val eq_tag: tag * tag -> bool
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    20
  val update_tags: tag -> Proof.context -> Proof.context
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    21
  val get_tags: Proof.context -> tag list
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    22
  type tagging = tag list
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    23
  val update_tagging: Proof.context -> tagging -> tag option * tagging
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    24
  val tag_scope: scope parser
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    25
  val tag_name: string parser
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    26
  val old_tags: string list parser
69891
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 69887
diff changeset
    27
  val annotation: unit parser
69876
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    28
end;
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    29
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    30
structure Document_Source: DOCUMENT_SOURCE =
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    31
struct
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    32
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    33
(* white space and comments *)
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    34
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    35
(*NB: arranging white space around command spans is a black art*)
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    36
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    37
val is_white = Token.is_space orf Token.is_informal_comment;
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    38
val is_black = not o is_white;
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    39
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    40
val is_white_comment = Token.is_informal_comment;
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    41
val is_black_comment = Token.is_formal_comment;
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    42
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    43
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    44
val space_proper =
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    45
  Scan.one Token.is_blank -- Scan.many is_white_comment -- Scan.one is_black;
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    46
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    47
val is_improper = not o (is_black orf Token.is_begin_ignore orf Token.is_end_ignore);
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    48
val improper = Scan.many is_improper;
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    49
val improper_end = Scan.repeat (Scan.unless space_proper (Scan.one is_improper));
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    50
val blank_end = Scan.repeat (Scan.unless space_proper (Scan.one Token.is_blank));
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    51
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    52
70134
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    53
(** syntactic tags **)
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    54
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    55
(* scope *)
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    56
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    57
datatype scope = Command | Proof;
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    58
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    59
type tag = string * scope;
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    60
val eq_tag: tag * tag -> bool = eq_fst op =;
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    61
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    62
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    63
(* context data *)
69887
b9985133805d added semantic document markers;
wenzelm
parents: 69876
diff changeset
    64
b9985133805d added semantic document markers;
wenzelm
parents: 69876
diff changeset
    65
structure Tags = Proof_Data
b9985133805d added semantic document markers;
wenzelm
parents: 69876
diff changeset
    66
(
70134
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    67
  type T = tag list;
69887
b9985133805d added semantic document markers;
wenzelm
parents: 69876
diff changeset
    68
  fun init _ = [];
b9985133805d added semantic document markers;
wenzelm
parents: 69876
diff changeset
    69
);
b9985133805d added semantic document markers;
wenzelm
parents: 69876
diff changeset
    70
70134
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    71
val update_tags = Tags.map o update eq_tag;
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    72
69887
b9985133805d added semantic document markers;
wenzelm
parents: 69876
diff changeset
    73
val get_tags = Tags.get;
70134
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    74
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    75
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    76
(* command tagging *)
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    77
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    78
type tagging = tag list;
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    79
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    80
fun update_tagging ctxt tagging =
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    81
  let
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    82
    val tagging' = fold (update eq_tag) (get_tags ctxt) tagging;
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    83
    val nested_tagging' = filter (fn (_, scope) => scope = Proof) tagging';
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    84
  in (try hd tagging', nested_tagging') end;
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    85
69876
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    86
70134
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    87
(* parse scope and name *)
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    88
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    89
val scope = Parse.reserved "command" >> K Command || Parse.reserved "proof" >> K Proof;
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    90
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    91
val tag_scope =
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    92
  Parse.group (fn () => "document tag scope") (Parse.$$$ "(" |-- Parse.!!! (scope --| Parse.$$$ ")"));
69876
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
    93
70134
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    94
val tag_name =
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    95
  Parse.group (fn () => "document tag name") (Parse.short_ident || Parse.string);
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    96
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    97
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    98
(* syntactic tags (old-style) *)
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
    99
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
   100
val old_tag = (improper -- Parse.$$$ "%" -- improper) |-- Parse.!!! (tag_name --| blank_end);
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
   101
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
   102
val old_tags = Scan.repeat old_tag;
69876
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
   103
69887
b9985133805d added semantic document markers;
wenzelm
parents: 69876
diff changeset
   104
b9985133805d added semantic document markers;
wenzelm
parents: 69876
diff changeset
   105
(* semantic markers (operation on presentation context) *)
b9985133805d added semantic document markers;
wenzelm
parents: 69876
diff changeset
   106
69891
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 69887
diff changeset
   107
val marker = improper |-- Parse.document_marker --| blank_end;
69887
b9985133805d added semantic document markers;
wenzelm
parents: 69876
diff changeset
   108
70134
e69f00f4b897 support "tag" marker with scope;
wenzelm
parents: 69891
diff changeset
   109
val annotation = Scan.repeat (old_tag >> K () || marker >> K ()) >> K ();
69887
b9985133805d added semantic document markers;
wenzelm
parents: 69876
diff changeset
   110
69876
b49bd228ac8a clarified modules;
wenzelm
parents:
diff changeset
   111
end;