1 (* Title: Pure/General/antiquote.ML
2 Author: Markus Wenzel, TU Muenchen
4 Text with antiquotations of inner items (types, terms, theorems etc.).
10 Text of Symbol_Pos.T list | Antiq of Symbol_Pos.T list * Position.T |
11 Open of Position.T | Close of Position.T
12 val is_antiq: antiquote -> bool
13 val read: Symbol_Pos.T list * Position.T -> antiquote list
16 structure Antiquote: ANTIQUOTE =
19 (* datatype antiquote *)
22 Text of Symbol_Pos.T list |
23 Antiq of Symbol_Pos.T list * Position.T |
27 fun is_antiq (Text _) = false
33 fun err_unbalanced pos =
34 error ("Unbalanced antiquotation block parentheses" ^ Position.str_of pos);
36 fun check_nesting antiqs =
39 | check [] (pos :: _) = err_unbalanced pos
40 | check (Open pos :: ants) ps = check ants (pos :: ps)
41 | check (Close pos :: _) [] = err_unbalanced pos
42 | check (Close _ :: ants) (_ :: ps) = check ants ps
43 | check (_ :: ants) ps = check ants ps;
44 in check antiqs [] end;
49 open Basic_Symbol_Pos;
54 $$$ "@" --| Scan.ahead (~$$$ "{") ||
55 Scan.one (fn (s, _) => s <> "@" andalso s <> "\\<lbrace>" andalso s <> "\\<rbrace>"
56 andalso Symbol.is_regular s) >> single;
59 Symbol_Pos.scan_quoted ||
60 Scan.one (fn (s, _) => s <> "}" andalso Symbol.is_regular s) >> single;
63 Symbol_Pos.scan_pos -- ($$$ "@" |-- $$$ "{" |--
64 Symbol_Pos.!!! "missing closing brace of antiquotation"
65 (Scan.repeat scan_ant -- ($$$ "}" |-- Symbol_Pos.scan_pos)))
66 >> (fn (pos1, (body, pos2)) => Antiq (flat body, Position.encode_range (pos1, pos2)));
71 (Scan.repeat1 scan_txt >> (Text o flat) ||
73 Symbol_Pos.scan_pos --| $$$ "\\<lbrace>" >> Open ||
74 Symbol_Pos.scan_pos --| $$$ "\\<rbrace>" >> Close);
81 fun report_antiq (Antiq (_, pos)) = Position.report Markup.antiq pos
82 | report_antiq _ = ();
86 (case Scan.read Symbol_Pos.stopper (Scan.repeat scan_antiquote) syms of
87 SOME xs => (List.app report_antiq xs; check_nesting xs; xs)
88 | NONE => error ("Malformed quotation/antiquotation source" ^ Position.str_of pos));