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