| author | blanchet | 
| Tue, 22 May 2018 17:15:02 +0200 | |
| changeset 68250 | c45067867860 | 
| parent 67735 | e2e002d4a4de | 
| child 69592 | a80d8ec6c998 | 
| permissions | -rw-r--r-- | 
| 
30587
 
ad19c99529eb
moved Isar/antiquote.ML to General/antiquote.ML, which is loaded early;
 
wenzelm 
parents: 
30573 
diff
changeset
 | 
1  | 
(* Title: Pure/General/antiquote.ML  | 
| 
55511
 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 
wenzelm 
parents: 
55107 
diff
changeset
 | 
2  | 
Author: Makarius  | 
| 
9138
 
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
3  | 
|
| 
55511
 
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
 
wenzelm 
parents: 
55107 
diff
changeset
 | 
4  | 
Antiquotations within plain text.  | 
| 
9138
 
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
5  | 
*)  | 
| 
 
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
6  | 
|
| 
 
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
7  | 
signature ANTIQUOTE =  | 
| 
 
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
8  | 
sig  | 
| 
61473
 
34d1913f0b20
clarified control antiquotations: decode control symbol to get name;
 
wenzelm 
parents: 
61471 
diff
changeset
 | 
9  | 
  type control = {range: Position.range, name: string * Position.T, body: Symbol_Pos.T list}
 | 
| 
 
34d1913f0b20
clarified control antiquotations: decode control symbol to get name;
 
wenzelm 
parents: 
61471 
diff
changeset
 | 
10  | 
  type antiq = {start: Position.T, stop: Position.T, range: Position.range, body: Symbol_Pos.T list}
 | 
| 
 
34d1913f0b20
clarified control antiquotations: decode control symbol to get name;
 
wenzelm 
parents: 
61471 
diff
changeset
 | 
11  | 
datatype 'a antiquote = Text of 'a | Control of control | Antiq of antiq  | 
| 61434 | 12  | 
type text_antiquote = Symbol_Pos.T list antiquote  | 
| 67467 | 13  | 
val text_antiquote_range: text_antiquote -> Position.range  | 
14  | 
val text_range: text_antiquote list -> Position.range  | 
|
| 61434 | 15  | 
val split_lines: text_antiquote list -> text_antiquote list list  | 
| 61457 | 16  | 
val antiq_reports: 'a antiquote list -> Position.report list  | 
| 67426 | 17  | 
val scan_control: control scanner  | 
18  | 
val scan_antiq: antiq scanner  | 
|
19  | 
val scan_antiquote: text_antiquote scanner  | 
|
| 
67571
 
f858fe5531ac
more uniform treatment of formal comments within document source;
 
wenzelm 
parents: 
67522 
diff
changeset
 | 
20  | 
val scan_antiquote_comments: text_antiquote scanner  | 
| 
 
f858fe5531ac
more uniform treatment of formal comments within document source;
 
wenzelm 
parents: 
67522 
diff
changeset
 | 
21  | 
val parse_comments: Position.T -> Symbol_Pos.T list -> text_antiquote list  | 
| 
 
f858fe5531ac
more uniform treatment of formal comments within document source;
 
wenzelm 
parents: 
67522 
diff
changeset
 | 
22  | 
val read_comments: Input.source -> text_antiquote list  | 
| 
9138
 
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
23  | 
end;  | 
| 
 
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
24  | 
|
| 
 
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
25  | 
structure Antiquote: ANTIQUOTE =  | 
| 
 
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
26  | 
struct  | 
| 
 
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
27  | 
|
| 
 
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
28  | 
(* datatype antiquote *)  | 
| 
 
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
29  | 
|
| 
61473
 
34d1913f0b20
clarified control antiquotations: decode control symbol to get name;
 
wenzelm 
parents: 
61471 
diff
changeset
 | 
30  | 
type control = {range: Position.range, name: string * Position.T, body: Symbol_Pos.T list};
 | 
| 
 
34d1913f0b20
clarified control antiquotations: decode control symbol to get name;
 
wenzelm 
parents: 
61471 
diff
changeset
 | 
31  | 
type antiq = {start: Position.T, stop: Position.T, range: Position.range, body: Symbol_Pos.T list};
 | 
| 
 
34d1913f0b20
clarified control antiquotations: decode control symbol to get name;
 
wenzelm 
parents: 
61471 
diff
changeset
 | 
32  | 
datatype 'a antiquote = Text of 'a | Control of control | Antiq of antiq;  | 
| 
9138
 
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
33  | 
|
| 61434 | 34  | 
type text_antiquote = Symbol_Pos.T list antiquote;  | 
35  | 
||
| 67467 | 36  | 
fun text_antiquote_range (Text ss) = Symbol_Pos.range ss  | 
37  | 
  | text_antiquote_range (Control {range, ...}) = range
 | 
|
38  | 
  | text_antiquote_range (Antiq {range, ...}) = range;
 | 
|
| 61450 | 39  | 
|
| 67467 | 40  | 
fun text_range ants =  | 
| 61450 | 41  | 
if null ants then Position.no_range  | 
| 67467 | 42  | 
else  | 
43  | 
Position.range (#1 (text_antiquote_range (hd ants)), #2 (text_antiquote_range (List.last ants)));  | 
|
| 61450 | 44  | 
|
| 61434 | 45  | 
|
46  | 
(* split lines *)  | 
|
47  | 
||
48  | 
fun split_lines input =  | 
|
49  | 
let  | 
|
50  | 
fun add a (line, lines) = (a :: line, lines);  | 
|
51  | 
fun flush (line, lines) = ([], rev line :: lines);  | 
|
52  | 
fun split (a as Text ss) =  | 
|
| 67522 | 53  | 
          (case chop_prefix (fn ("\n", _) => false | _ => true) ss of
 | 
| 61434 | 54  | 
([], []) => I  | 
55  | 
| (_, []) => add a  | 
|
56  | 
| ([], _ :: rest) => flush #> split (Text rest)  | 
|
57  | 
| (prefix, _ :: rest) => add (Text prefix) #> flush #> split (Text rest))  | 
|
58  | 
| split a = add a;  | 
|
| 61440 | 59  | 
in if null input then [] else rev (#2 (flush (fold split input ([], [])))) end;  | 
| 61434 | 60  | 
|
| 27342 | 61  | 
|
| 44736 | 62  | 
(* reports *)  | 
| 
30641
 
72980f8d7ee8
export report -- version that actually covers all cases;
 
wenzelm 
parents: 
30635 
diff
changeset
 | 
63  | 
|
| 61457 | 64  | 
fun antiq_reports ants = ants |> maps  | 
| 61471 | 65  | 
(fn Text _ => []  | 
| 
61473
 
34d1913f0b20
clarified control antiquotations: decode control symbol to get name;
 
wenzelm 
parents: 
61471 
diff
changeset
 | 
66  | 
    | Control {range = (pos, _), ...} => [(pos, Markup.antiquoted)]
 | 
| 
 
34d1913f0b20
clarified control antiquotations: decode control symbol to get name;
 
wenzelm 
parents: 
61471 
diff
changeset
 | 
67  | 
    | Antiq {start, stop, range = (pos, _), ...} =>
 | 
| 61471 | 68  | 
[(start, Markup.antiquote),  | 
69  | 
(stop, Markup.antiquote),  | 
|
70  | 
(pos, Markup.antiquoted),  | 
|
71  | 
(pos, Markup.language_antiquotation)]);  | 
|
| 
9138
 
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
72  | 
|
| 
 
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
73  | 
|
| 
30590
 
1d9c9fcf8513
parameterized datatype antiquote and read operation;
 
wenzelm 
parents: 
30589 
diff
changeset
 | 
74  | 
(* scan *)  | 
| 
9138
 
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
75  | 
|
| 30573 | 76  | 
open Basic_Symbol_Pos;  | 
| 
9138
 
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
77  | 
|
| 22114 | 78  | 
local  | 
| 
9138
 
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
79  | 
|
| 48764 | 80  | 
val err_prefix = "Antiquotation lexical error: ";  | 
81  | 
||
| 67193 | 82  | 
val scan_nl = Scan.one (fn (s, _) => s = "\n") >> single;  | 
| 
67571
 
f858fe5531ac
more uniform treatment of formal comments within document source;
 
wenzelm 
parents: 
67522 
diff
changeset
 | 
83  | 
val scan_nl_opt = Scan.optional scan_nl [];  | 
| 67193 | 84  | 
|
| 
67571
 
f858fe5531ac
more uniform treatment of formal comments within document source;
 
wenzelm 
parents: 
67522 
diff
changeset
 | 
85  | 
val scan_plain_txt =  | 
| 
 
f858fe5531ac
more uniform treatment of formal comments within document source;
 
wenzelm 
parents: 
67522 
diff
changeset
 | 
86  | 
Scan.many1 (fn (s, _) =>  | 
| 
 
f858fe5531ac
more uniform treatment of formal comments within document source;
 
wenzelm 
parents: 
67522 
diff
changeset
 | 
87  | 
not (Comment.is_symbol s) andalso  | 
| 
 
f858fe5531ac
more uniform treatment of formal comments within document source;
 
wenzelm 
parents: 
67522 
diff
changeset
 | 
88  | 
not (Symbol.is_control s) andalso  | 
| 
 
f858fe5531ac
more uniform treatment of formal comments within document source;
 
wenzelm 
parents: 
67522 
diff
changeset
 | 
89  | 
s <> Symbol.open_ andalso  | 
| 
 
f858fe5531ac
more uniform treatment of formal comments within document source;
 
wenzelm 
parents: 
67522 
diff
changeset
 | 
90  | 
s <> "@" andalso  | 
| 
 
f858fe5531ac
more uniform treatment of formal comments within document source;
 
wenzelm 
parents: 
67522 
diff
changeset
 | 
91  | 
s <> "\n" andalso  | 
| 
 
f858fe5531ac
more uniform treatment of formal comments within document source;
 
wenzelm 
parents: 
67522 
diff
changeset
 | 
92  | 
Symbol.not_eof s) ||  | 
| 
 
f858fe5531ac
more uniform treatment of formal comments within document source;
 
wenzelm 
parents: 
67522 
diff
changeset
 | 
93  | 
Scan.one (Comment.is_symbol o Symbol_Pos.symbol) >> single ||  | 
| 
 
f858fe5531ac
more uniform treatment of formal comments within document source;
 
wenzelm 
parents: 
67522 
diff
changeset
 | 
94  | 
  $$$ "@" --| Scan.ahead (~$$ "{");
 | 
| 
 
f858fe5531ac
more uniform treatment of formal comments within document source;
 
wenzelm 
parents: 
67522 
diff
changeset
 | 
95  | 
|
| 
 
f858fe5531ac
more uniform treatment of formal comments within document source;
 
wenzelm 
parents: 
67522 
diff
changeset
 | 
96  | 
val scan_text =  | 
| 
 
f858fe5531ac
more uniform treatment of formal comments within document source;
 
wenzelm 
parents: 
67522 
diff
changeset
 | 
97  | 
scan_nl || Scan.repeats1 scan_plain_txt @@@ scan_nl_opt;  | 
| 
 
f858fe5531ac
more uniform treatment of formal comments within document source;
 
wenzelm 
parents: 
67522 
diff
changeset
 | 
98  | 
|
| 
 
f858fe5531ac
more uniform treatment of formal comments within document source;
 
wenzelm 
parents: 
67522 
diff
changeset
 | 
99  | 
val scan_text_comments =  | 
| 
 
f858fe5531ac
more uniform treatment of formal comments within document source;
 
wenzelm 
parents: 
67522 
diff
changeset
 | 
100  | 
scan_nl || Scan.repeats1 (Comment.scan >> #2 || scan_plain_txt) @@@ scan_nl_opt;  | 
| 
9138
 
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
101  | 
|
| 55512 | 102  | 
val scan_antiq_body =  | 
| 48764 | 103  | 
Scan.trace (Symbol_Pos.scan_string_qq err_prefix || Symbol_Pos.scan_string_bq err_prefix) >> #2 ||  | 
| 61481 | 104  | 
Symbol_Pos.scan_cartouche err_prefix ||  | 
| 
67735
 
e2e002d4a4de
clarified syntax: reject formal comments explicitly, instead of ignoring them silently;
 
wenzelm 
parents: 
67571 
diff
changeset
 | 
105  | 
Comment.scan --  | 
| 
 
e2e002d4a4de
clarified syntax: reject formal comments explicitly, instead of ignoring them silently;
 
wenzelm 
parents: 
67571 
diff
changeset
 | 
106  | 
Symbol_Pos.!!! (fn () => err_prefix ^ "bad formal comment in antiquote body") Scan.fail  | 
| 
 
e2e002d4a4de
clarified syntax: reject formal comments explicitly, instead of ignoring them silently;
 
wenzelm 
parents: 
67571 
diff
changeset
 | 
107  | 
>> K [] ||  | 
| 58854 | 108  | 
Scan.one (fn (s, _) => s <> "}" andalso Symbol.not_eof s) >> single;  | 
| 
9138
 
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
109  | 
|
| 
61491
 
97261e6c1d42
another antiquotation short form: undecorated cartouche as alias for @{text};
 
wenzelm 
parents: 
61481 
diff
changeset
 | 
110  | 
fun control_name sym = (case Symbol.decode sym of Symbol.Control name => name);  | 
| 
 
97261e6c1d42
another antiquotation short form: undecorated cartouche as alias for @{text};
 
wenzelm 
parents: 
61481 
diff
changeset
 | 
111  | 
|
| 
42508
 
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
 
wenzelm 
parents: 
42503 
diff
changeset
 | 
112  | 
in  | 
| 
 
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
 
wenzelm 
parents: 
42503 
diff
changeset
 | 
113  | 
|
| 61471 | 114  | 
val scan_control =  | 
| 
61491
 
97261e6c1d42
another antiquotation short form: undecorated cartouche as alias for @{text};
 
wenzelm 
parents: 
61481 
diff
changeset
 | 
115  | 
Scan.option (Scan.one (Symbol.is_control o Symbol_Pos.symbol)) --  | 
| 61481 | 116  | 
Symbol_Pos.scan_cartouche err_prefix >>  | 
| 
61491
 
97261e6c1d42
another antiquotation short form: undecorated cartouche as alias for @{text};
 
wenzelm 
parents: 
61481 
diff
changeset
 | 
117  | 
(fn (opt_control, body) =>  | 
| 
61473
 
34d1913f0b20
clarified control antiquotations: decode control symbol to get name;
 
wenzelm 
parents: 
61471 
diff
changeset
 | 
118  | 
let  | 
| 
61491
 
97261e6c1d42
another antiquotation short form: undecorated cartouche as alias for @{text};
 
wenzelm 
parents: 
61481 
diff
changeset
 | 
119  | 
val (name, range) =  | 
| 
 
97261e6c1d42
another antiquotation short form: undecorated cartouche as alias for @{text};
 
wenzelm 
parents: 
61481 
diff
changeset
 | 
120  | 
(case opt_control of  | 
| 
 
97261e6c1d42
another antiquotation short form: undecorated cartouche as alias for @{text};
 
wenzelm 
parents: 
61481 
diff
changeset
 | 
121  | 
SOME (sym, pos) => ((control_name sym, pos), Symbol_Pos.range ((sym, pos) :: body))  | 
| 
 
97261e6c1d42
another antiquotation short form: undecorated cartouche as alias for @{text};
 
wenzelm 
parents: 
61481 
diff
changeset
 | 
122  | 
          | NONE => (("cartouche", #2 (hd body)), Symbol_Pos.range body));
 | 
| 61595 | 123  | 
      in {name = name, range = range, body = body} end) ||
 | 
124  | 
Scan.one (Symbol.is_control o Symbol_Pos.symbol) >>  | 
|
125  | 
(fn (sym, pos) =>  | 
|
126  | 
      {name = (control_name sym, pos), range = Symbol_Pos.range [(sym, pos)], body = []});
 | 
|
| 61471 | 127  | 
|
| 
9138
 
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
128  | 
val scan_antiq =  | 
| 55526 | 129  | 
  Symbol_Pos.scan_pos -- ($$ "@" |-- $$ "{" |-- Symbol_Pos.scan_pos --
 | 
| 48764 | 130  | 
Symbol_Pos.!!! (fn () => err_prefix ^ "missing closing brace")  | 
| 61476 | 131  | 
(Scan.repeats scan_antiq_body -- Symbol_Pos.scan_pos -- ($$ "}" |-- Symbol_Pos.scan_pos))) >>  | 
| 
61473
 
34d1913f0b20
clarified control antiquotations: decode control symbol to get name;
 
wenzelm 
parents: 
61471 
diff
changeset
 | 
132  | 
(fn (pos1, (pos2, ((body, pos3), pos4))) =>  | 
| 62797 | 133  | 
      {start = Position.range_position (pos1, pos2),
 | 
134  | 
stop = Position.range_position (pos3, pos4),  | 
|
135  | 
range = Position.range (pos1, pos4),  | 
|
| 61476 | 136  | 
body = body});  | 
| 
30590
 
1d9c9fcf8513
parameterized datatype antiquote and read operation;
 
wenzelm 
parents: 
30589 
diff
changeset
 | 
137  | 
|
| 61471 | 138  | 
val scan_antiquote =  | 
| 
67571
 
f858fe5531ac
more uniform treatment of formal comments within document source;
 
wenzelm 
parents: 
67522 
diff
changeset
 | 
139  | 
scan_text >> Text || scan_control >> Control || scan_antiq >> Antiq;  | 
| 
 
f858fe5531ac
more uniform treatment of formal comments within document source;
 
wenzelm 
parents: 
67522 
diff
changeset
 | 
140  | 
|
| 
 
f858fe5531ac
more uniform treatment of formal comments within document source;
 
wenzelm 
parents: 
67522 
diff
changeset
 | 
141  | 
val scan_antiquote_comments =  | 
| 
 
f858fe5531ac
more uniform treatment of formal comments within document source;
 
wenzelm 
parents: 
67522 
diff
changeset
 | 
142  | 
scan_text_comments >> Text || scan_control >> Control || scan_antiq >> Antiq;  | 
| 
9138
 
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
143  | 
|
| 
 
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
144  | 
end;  | 
| 
 
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
145  | 
|
| 
 
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
146  | 
|
| 
67571
 
f858fe5531ac
more uniform treatment of formal comments within document source;
 
wenzelm 
parents: 
67522 
diff
changeset
 | 
147  | 
(* parse and read (with formal comments) *)  | 
| 
9138
 
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
148  | 
|
| 
67571
 
f858fe5531ac
more uniform treatment of formal comments within document source;
 
wenzelm 
parents: 
67522 
diff
changeset
 | 
149  | 
fun parse_comments pos syms =  | 
| 
 
f858fe5531ac
more uniform treatment of formal comments within document source;
 
wenzelm 
parents: 
67522 
diff
changeset
 | 
150  | 
(case Scan.read Symbol_Pos.stopper (Scan.repeat scan_antiquote_comments) syms of  | 
| 62749 | 151  | 
SOME ants => ants  | 
| 61456 | 152  | 
  | NONE => error ("Malformed quotation/antiquotation source" ^ Position.here pos));
 | 
153  | 
||
| 
67571
 
f858fe5531ac
more uniform treatment of formal comments within document source;
 
wenzelm 
parents: 
67522 
diff
changeset
 | 
154  | 
fun read_comments source =  | 
| 62749 | 155  | 
let  | 
| 
67571
 
f858fe5531ac
more uniform treatment of formal comments within document source;
 
wenzelm 
parents: 
67522 
diff
changeset
 | 
156  | 
val ants = parse_comments (Input.pos_of source) (Input.source_explode source);  | 
| 62749 | 157  | 
val _ = Position.reports (antiq_reports ants);  | 
158  | 
in ants end;  | 
|
| 
9138
 
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
159  | 
|
| 
 
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
 
wenzelm 
parents:  
diff
changeset
 | 
160  | 
end;  |