author | wenzelm |
Thu, 09 Oct 2008 20:53:12 +0200 | |
changeset 28545 | 2fb2d48de366 |
parent 27880 | 4ab10bfe8a7f |
child 29606 | fedb8be05f24 |
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 = |
27870
643673ebe065
use SymbolPos.T list directly, instead of encoded SymbolPos.text;
wenzelm
parents:
27835
diff
changeset
|
11 |
Text of string | Antiq of SymbolPos.T list * Position.T | |
27342 | 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 |
27870
643673ebe065
use SymbolPos.T list directly, instead of encoded SymbolPos.text;
wenzelm
parents:
27835
diff
changeset
|
14 |
val read: SymbolPos.T list * Position.T -> antiquote list |
27779
4569003b8813
simplified Antiq: regular SymbolPos.text with position;
wenzelm
parents:
27767
diff
changeset
|
15 |
val read_antiq: Scan.lexicon -> (OuterLex.token list -> 'a * OuterLex.token list) -> |
27870
643673ebe065
use SymbolPos.T list directly, instead of encoded SymbolPos.text;
wenzelm
parents:
27835
diff
changeset
|
16 |
SymbolPos.T list * 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 | |
27870
643673ebe065
use SymbolPos.T list directly, instead of encoded SymbolPos.text;
wenzelm
parents:
27835
diff
changeset
|
26 |
Antiq of SymbolPos.T list * Position.T | |
27342 | 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 |
|
27767 | 52 |
open BasicSymbolPos; |
22114 | 53 |
structure T = OuterLex; |
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
54 |
|
22114 | 55 |
local |
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
56 |
|
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
57 |
val scan_txt = |
27767 | 58 |
$$$ "@" --| Scan.ahead (~$$$ "{") || |
59 |
Scan.one (fn (s, _) => s <> "@" andalso s <> "\\<lbrace>" andalso s <> "\\<rbrace>" |
|
60 |
andalso Symbol.is_regular s) >> single; |
|
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
61 |
|
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
62 |
val scan_ant = |
27767 | 63 |
T.scan_quoted || |
64 |
Scan.one (fn (s, _) => s <> "}" andalso Symbol.is_regular s) >> single; |
|
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
65 |
|
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
66 |
val scan_antiq = |
27779
4569003b8813
simplified Antiq: regular SymbolPos.text with position;
wenzelm
parents:
27767
diff
changeset
|
67 |
SymbolPos.scan_pos -- ($$$ "@" |-- $$$ "{" |-- |
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
68 |
T.!!! "missing closing brace of antiquotation" |
27779
4569003b8813
simplified Antiq: regular SymbolPos.text with position;
wenzelm
parents:
27767
diff
changeset
|
69 |
(Scan.repeat scan_ant -- ($$$ "}" |-- SymbolPos.scan_pos))) |
27870
643673ebe065
use SymbolPos.T list directly, instead of encoded SymbolPos.text;
wenzelm
parents:
27835
diff
changeset
|
70 |
>> (fn (pos1, (body, pos2)) => Antiq (flat body, Position.encode_range (pos1, pos2))); |
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
71 |
|
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
72 |
in |
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
73 |
|
27779
4569003b8813
simplified Antiq: regular SymbolPos.text with position;
wenzelm
parents:
27767
diff
changeset
|
74 |
val scan_antiquote = |
27799 | 75 |
(Scan.repeat1 scan_txt >> (Text o SymbolPos.content o flat) || |
27750 | 76 |
scan_antiq || |
27779
4569003b8813
simplified Antiq: regular SymbolPos.text with position;
wenzelm
parents:
27767
diff
changeset
|
77 |
SymbolPos.scan_pos --| $$$ "\\<lbrace>" >> Open || |
4569003b8813
simplified Antiq: regular SymbolPos.text with position;
wenzelm
parents:
27767
diff
changeset
|
78 |
SymbolPos.scan_pos --| $$$ "\\<rbrace>" >> Close); |
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
79 |
|
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
80 |
end; |
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
81 |
|
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
82 |
|
27767 | 83 |
(* read *) |
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
84 |
|
27880 | 85 |
fun report_antiq (Antiq (_, pos)) = Position.report Markup.antiq pos |
86 |
| report_antiq _ = (); |
|
87 |
||
27870
643673ebe065
use SymbolPos.T list directly, instead of encoded SymbolPos.text;
wenzelm
parents:
27835
diff
changeset
|
88 |
fun read ([], _) = [] |
643673ebe065
use SymbolPos.T list directly, instead of encoded SymbolPos.text;
wenzelm
parents:
27835
diff
changeset
|
89 |
| read (syms, pos) = |
643673ebe065
use SymbolPos.T list directly, instead of encoded SymbolPos.text;
wenzelm
parents:
27835
diff
changeset
|
90 |
(case Scan.read SymbolPos.stopper (Scan.repeat scan_antiquote) syms of |
27880 | 91 |
SOME xs => (List.app report_antiq xs; check_nesting xs; xs) |
27767 | 92 |
| NONE => error ("Malformed quotation/antiquotation source" ^ Position.str_of pos)); |
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
93 |
|
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
94 |
|
27779
4569003b8813
simplified Antiq: regular SymbolPos.text with position;
wenzelm
parents:
27767
diff
changeset
|
95 |
(* read_antiq *) |
22114 | 96 |
|
27870
643673ebe065
use SymbolPos.T list directly, instead of encoded SymbolPos.text;
wenzelm
parents:
27835
diff
changeset
|
97 |
fun read_antiq lex scan (syms, pos) = |
22114 | 98 |
let |
27870
643673ebe065
use SymbolPos.T list directly, instead of encoded SymbolPos.text;
wenzelm
parents:
27835
diff
changeset
|
99 |
fun err msg = cat_error msg ("Malformed antiquotation" ^ Position.str_of pos ^ ":\n" ^ |
643673ebe065
use SymbolPos.T list directly, instead of encoded SymbolPos.text;
wenzelm
parents:
27835
diff
changeset
|
100 |
"@{" ^ SymbolPos.content syms ^ "}"); |
22114 | 101 |
|
102 |
val res = |
|
27870
643673ebe065
use SymbolPos.T list directly, instead of encoded SymbolPos.text;
wenzelm
parents:
27835
diff
changeset
|
103 |
Source.of_list syms |
27835
ff8b8513965a
Symbol.source/OuterLex.source: more explicit do_recover argument;
wenzelm
parents:
27799
diff
changeset
|
104 |
|> T.source' {do_recover = NONE} (K (lex, Scan.empty_lexicon)) |
22114 | 105 |
|> T.source_proper |
27767 | 106 |
|> Source.source T.stopper (Scan.error (Scan.bulk scan)) NONE |
22114 | 107 |
|> Source.exhaust; |
108 |
in (case res of [x] => x | _ => err "") handle ERROR msg => err msg end; |
|
109 |
||
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
110 |
end; |