wenzelm@9138
|
1 |
(* Title: Pure/Isar/antiquote.ML
|
wenzelm@9138
|
2 |
ID: $Id$
|
wenzelm@9138
|
3 |
Author: Markus Wenzel, TU Muenchen
|
wenzelm@9138
|
4 |
|
wenzelm@9138
|
5 |
Text with antiquotations of inner items (terms, types etc.).
|
wenzelm@9138
|
6 |
*)
|
wenzelm@9138
|
7 |
|
wenzelm@9138
|
8 |
signature ANTIQUOTE =
|
wenzelm@9138
|
9 |
sig
|
wenzelm@9138
|
10 |
datatype antiquote = Text of string | Antiq of string * Position.T
|
wenzelm@9138
|
11 |
val is_antiq: antiquote -> bool
|
wenzelm@22114
|
12 |
val scan_antiquotes: string * Position.T -> antiquote list
|
wenzelm@22114
|
13 |
val scan_arguments: Scan.lexicon -> (OuterLex.token list -> 'a * OuterLex.token list) ->
|
wenzelm@22114
|
14 |
string * Position.T -> 'a
|
wenzelm@9138
|
15 |
end;
|
wenzelm@9138
|
16 |
|
wenzelm@9138
|
17 |
structure Antiquote: ANTIQUOTE =
|
wenzelm@9138
|
18 |
struct
|
wenzelm@9138
|
19 |
|
wenzelm@9138
|
20 |
(* datatype antiquote *)
|
wenzelm@9138
|
21 |
|
wenzelm@9138
|
22 |
datatype antiquote =
|
wenzelm@9138
|
23 |
Text of string |
|
wenzelm@9138
|
24 |
Antiq of string * Position.T;
|
wenzelm@9138
|
25 |
|
wenzelm@9138
|
26 |
fun is_antiq (Text _) = false
|
wenzelm@9138
|
27 |
| is_antiq (Antiq _) = true;
|
wenzelm@9138
|
28 |
|
wenzelm@9138
|
29 |
|
wenzelm@9138
|
30 |
(* scan_antiquote *)
|
wenzelm@9138
|
31 |
|
wenzelm@22114
|
32 |
structure T = OuterLex;
|
wenzelm@22114
|
33 |
|
wenzelm@9138
|
34 |
local
|
wenzelm@9138
|
35 |
|
wenzelm@9138
|
36 |
val scan_txt =
|
wenzelm@26002
|
37 |
T.count ($$ "@" --| Scan.ahead (~$$ "{")) ||
|
wenzelm@26002
|
38 |
T.count (Scan.one (fn s => s <> "@" andalso Symbol.is_regular s));
|
wenzelm@9138
|
39 |
|
wenzelm@9138
|
40 |
fun escape "\\" = "\\\\"
|
wenzelm@9138
|
41 |
| escape "\"" = "\\\""
|
wenzelm@9138
|
42 |
| escape s = s;
|
wenzelm@9138
|
43 |
|
berghofe@14598
|
44 |
val quote_escape = Library.quote o implode o map escape o Symbol.explode;
|
wenzelm@9138
|
45 |
|
wenzelm@9138
|
46 |
val scan_ant =
|
wenzelm@9138
|
47 |
T.scan_string >> quote_escape ||
|
wenzelm@26002
|
48 |
T.count (Scan.one (fn s => s <> "}" andalso Symbol.is_regular s));
|
wenzelm@9138
|
49 |
|
wenzelm@9138
|
50 |
val scan_antiq =
|
wenzelm@26002
|
51 |
T.count ($$ "@") |-- T.count ($$ "{") |--
|
wenzelm@9138
|
52 |
T.!!! "missing closing brace of antiquotation"
|
wenzelm@26002
|
53 |
(Scan.repeat scan_ant >> implode) --| T.count ($$ "}");
|
wenzelm@9138
|
54 |
|
wenzelm@9138
|
55 |
in
|
wenzelm@9138
|
56 |
|
wenzelm@9138
|
57 |
fun scan_antiquote (pos, ss) = (pos, ss) |>
|
wenzelm@9138
|
58 |
(Scan.repeat1 scan_txt >> (Text o implode) ||
|
wenzelm@9138
|
59 |
scan_antiq >> (fn s => Antiq (s, pos)));
|
wenzelm@9138
|
60 |
|
wenzelm@9138
|
61 |
end;
|
wenzelm@9138
|
62 |
|
wenzelm@9138
|
63 |
|
wenzelm@22114
|
64 |
(* scan_antiquotes *)
|
wenzelm@9138
|
65 |
|
wenzelm@22114
|
66 |
fun scan_antiquotes (s, pos) =
|
wenzelm@9138
|
67 |
(case Scan.error (Scan.finite' Symbol.stopper (Scan.repeat scan_antiquote))
|
wenzelm@9138
|
68 |
(pos, Symbol.explode s) of
|
wenzelm@9138
|
69 |
(xs, (_, [])) => xs
|
wenzelm@22114
|
70 |
| (_, (pos', ss)) => error ("Malformed quotation/antiquotation source at: " ^
|
wenzelm@14729
|
71 |
quote (Symbol.beginning 10 ss) ^ Position.str_of pos'));
|
wenzelm@9138
|
72 |
|
wenzelm@9138
|
73 |
|
wenzelm@22114
|
74 |
(* scan_arguments *)
|
wenzelm@22114
|
75 |
|
wenzelm@22114
|
76 |
fun scan_arguments lex antiq (s, pos) =
|
wenzelm@22114
|
77 |
let
|
wenzelm@22114
|
78 |
fun err msg = cat_error msg
|
wenzelm@22114
|
79 |
("Malformed antiquotation: " ^ quote ("@{" ^ s ^ "}") ^ Position.str_of pos);
|
wenzelm@22114
|
80 |
|
wenzelm@22114
|
81 |
val res =
|
wenzelm@22114
|
82 |
Source.of_string s
|
wenzelm@22114
|
83 |
|> Symbol.source false
|
wenzelm@23677
|
84 |
|> T.source NONE (K (lex, Scan.empty_lexicon)) pos
|
wenzelm@22114
|
85 |
|> T.source_proper
|
wenzelm@22114
|
86 |
|> Source.source T.stopper (Scan.error (Scan.bulk antiq)) NONE
|
wenzelm@22114
|
87 |
|> Source.exhaust;
|
wenzelm@22114
|
88 |
in (case res of [x] => x | _ => err "") handle ERROR msg => err msg end;
|
wenzelm@22114
|
89 |
|
wenzelm@9138
|
90 |
end;
|