author | wenzelm |
Tue, 24 Jun 2008 19:43:18 +0200 | |
changeset 27342 | 3945da15d410 |
parent 26002 | 469ee728a5d7 |
child 27746 | c7aa4c18739d |
permissions | -rw-r--r-- |
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
1 |
(* Title: Pure/Isar/antiquote.ML |
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
2 |
ID: $Id$ |
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
3 |
Author: Markus Wenzel, TU Muenchen |
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
4 |
|
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
5 |
Text with antiquotations of inner items (terms, types etc.). |
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 |
|
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
8 |
signature ANTIQUOTE = |
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
9 |
sig |
27342 | 10 |
datatype antiquote = |
11 |
Text of string | Antiq of string * Position.T | |
|
12 |
Open of Position.T | Close of Position.T |
|
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
13 |
val is_antiq: antiquote -> bool |
22114 | 14 |
val scan_antiquotes: string * Position.T -> antiquote list |
15 |
val scan_arguments: Scan.lexicon -> (OuterLex.token list -> 'a * OuterLex.token list) -> |
|
16 |
string * Position.T -> 'a |
|
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
17 |
end; |
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
18 |
|
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
19 |
structure Antiquote: ANTIQUOTE = |
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
20 |
struct |
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
21 |
|
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
22 |
(* datatype antiquote *) |
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
23 |
|
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
24 |
datatype antiquote = |
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
25 |
Text of string | |
27342 | 26 |
Antiq of string * Position.T | |
27 |
Open of Position.T | |
|
28 |
Close of Position.T; |
|
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
29 |
|
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
30 |
fun is_antiq (Text _) = false |
27342 | 31 |
| is_antiq _ = true; |
32 |
||
33 |
||
34 |
(* check_nesting *) |
|
35 |
||
36 |
fun err_unbalanced pos = |
|
37 |
error ("Unbalanced antiquotation block parentheses" ^ Position.str_of pos); |
|
38 |
||
39 |
fun check_nesting antiqs = |
|
40 |
let |
|
41 |
fun check [] [] = () |
|
42 |
| check [] (pos :: _) = err_unbalanced pos |
|
43 |
| check (Open pos :: ants) ps = check ants (pos :: ps) |
|
44 |
| check (Close pos :: _) [] = err_unbalanced pos |
|
45 |
| check (Close _ :: ants) (_ :: ps) = check ants ps |
|
46 |
| check (_ :: ants) ps = check ants ps; |
|
47 |
in check antiqs [] end; |
|
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
48 |
|
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
49 |
|
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
50 |
(* scan_antiquote *) |
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
51 |
|
22114 | 52 |
structure T = OuterLex; |
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
53 |
|
22114 | 54 |
local |
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
55 |
|
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
56 |
val scan_txt = |
26002
469ee728a5d7
T.count/counted: improved position handling for token syntax;
wenzelm
parents:
23937
diff
changeset
|
57 |
T.count ($$ "@" --| Scan.ahead (~$$ "{")) || |
27342 | 58 |
T.count (Scan.one (fn s => |
59 |
s <> "@" andalso s <> "\\<lbrace>" andalso s <> "\\<rbrace>" andalso Symbol.is_regular s)); |
|
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
60 |
|
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
61 |
fun escape "\\" = "\\\\" |
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
62 |
| escape "\"" = "\\\"" |
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
63 |
| escape s = s; |
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
64 |
|
14598
7009f59711e3
Replaced quote by Library.quote, since quote now refers to Symbol.quote
berghofe
parents:
12881
diff
changeset
|
65 |
val quote_escape = Library.quote o implode o map escape o Symbol.explode; |
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
66 |
|
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
67 |
val scan_ant = |
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
68 |
T.scan_string >> quote_escape || |
26002
469ee728a5d7
T.count/counted: improved position handling for token syntax;
wenzelm
parents:
23937
diff
changeset
|
69 |
T.count (Scan.one (fn s => s <> "}" andalso Symbol.is_regular s)); |
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
70 |
|
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
71 |
val scan_antiq = |
26002
469ee728a5d7
T.count/counted: improved position handling for token syntax;
wenzelm
parents:
23937
diff
changeset
|
72 |
T.count ($$ "@") |-- T.count ($$ "{") |-- |
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
73 |
T.!!! "missing closing brace of antiquotation" |
26002
469ee728a5d7
T.count/counted: improved position handling for token syntax;
wenzelm
parents:
23937
diff
changeset
|
74 |
(Scan.repeat scan_ant >> implode) --| T.count ($$ "}"); |
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
75 |
|
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
76 |
in |
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
77 |
|
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
78 |
fun scan_antiquote (pos, ss) = (pos, ss) |> |
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
79 |
(Scan.repeat1 scan_txt >> (Text o implode) || |
27342 | 80 |
scan_antiq >> (fn s => Antiq (s, pos)) || |
81 |
T.count ($$ "\\<lbrace>") >> K (Open pos) || |
|
82 |
T.count ($$ "\\<rbrace>") >> K (Close pos)); |
|
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
83 |
|
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
84 |
end; |
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
85 |
|
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
86 |
|
22114 | 87 |
(* scan_antiquotes *) |
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
88 |
|
22114 | 89 |
fun scan_antiquotes (s, pos) = |
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
90 |
(case Scan.error (Scan.finite' Symbol.stopper (Scan.repeat scan_antiquote)) |
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
91 |
(pos, Symbol.explode s) of |
27342 | 92 |
(xs, (_, [])) => (check_nesting xs; xs) |
22114 | 93 |
| (_, (pos', ss)) => error ("Malformed quotation/antiquotation source at: " ^ |
14729 | 94 |
quote (Symbol.beginning 10 ss) ^ Position.str_of pos')); |
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
95 |
|
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
96 |
|
22114 | 97 |
(* scan_arguments *) |
98 |
||
99 |
fun scan_arguments lex antiq (s, pos) = |
|
100 |
let |
|
101 |
fun err msg = cat_error msg |
|
102 |
("Malformed antiquotation: " ^ quote ("@{" ^ s ^ "}") ^ Position.str_of pos); |
|
103 |
||
104 |
val res = |
|
105 |
Source.of_string s |
|
106 |
|> Symbol.source false |
|
23677 | 107 |
|> T.source NONE (K (lex, Scan.empty_lexicon)) pos |
22114 | 108 |
|> T.source_proper |
109 |
|> Source.source T.stopper (Scan.error (Scan.bulk antiq)) NONE |
|
110 |
|> Source.exhaust; |
|
111 |
in (case res of [x] => x | _ => err "") handle ERROR msg => err msg end; |
|
112 |
||
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
113 |
end; |