author | wenzelm |
Fri, 16 Oct 2015 10:11:20 +0200 | |
changeset 61457 | 3e21699bb83b |
parent 61456 | b521b8b400f7 |
child 61471 | 9d4c08af61b8 |
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 |
55526 | 9 |
type antiq = Symbol_Pos.T list * {start: Position.T, stop: Position.T, range: Position.range} |
10 |
datatype 'a antiquote = Text of 'a | Antiq of antiq |
|
61434 | 11 |
type text_antiquote = Symbol_Pos.T list antiquote |
61450 | 12 |
val range: text_antiquote list -> Position.range |
61434 | 13 |
val split_lines: text_antiquote list -> text_antiquote list list |
61457 | 14 |
val antiq_reports: 'a antiquote list -> Position.report list |
55526 | 15 |
val scan_antiq: Symbol_Pos.T list -> antiq * Symbol_Pos.T list |
61434 | 16 |
val scan_antiquote: Symbol_Pos.T list -> text_antiquote * Symbol_Pos.T list |
61456 | 17 |
val read': Position.T -> Symbol_Pos.T list -> text_antiquote list |
61434 | 18 |
val read: Input.source -> text_antiquote list |
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
19 |
end; |
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
20 |
|
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
21 |
structure Antiquote: ANTIQUOTE = |
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
22 |
struct |
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 |
|
55526 | 26 |
type antiq = Symbol_Pos.T list * {start: Position.T, stop: Position.T, range: Position.range}; |
27 |
datatype 'a antiquote = Text of 'a | Antiq of antiq; |
|
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
28 |
|
61434 | 29 |
type text_antiquote = Symbol_Pos.T list antiquote; |
30 |
||
61450 | 31 |
fun antiquote_range (Text ss) = Symbol_Pos.range ss |
32 |
| antiquote_range (Antiq (_, {range, ...})) = range; |
|
33 |
||
34 |
fun range ants = |
|
35 |
if null ants then Position.no_range |
|
36 |
else Position.range (#1 (antiquote_range (hd ants))) (#2 (antiquote_range (List.last ants))); |
|
37 |
||
61434 | 38 |
|
39 |
(* split lines *) |
|
40 |
||
41 |
fun split_lines input = |
|
42 |
let |
|
43 |
fun add a (line, lines) = (a :: line, lines); |
|
44 |
fun flush (line, lines) = ([], rev line :: lines); |
|
45 |
fun split (a as Text ss) = |
|
46 |
(case take_prefix (fn ("\n", _) => false | _ => true) ss of |
|
47 |
([], []) => I |
|
48 |
| (_, []) => add a |
|
49 |
| ([], _ :: rest) => flush #> split (Text rest) |
|
50 |
| (prefix, _ :: rest) => add (Text prefix) #> flush #> split (Text rest)) |
|
51 |
| split a = add a; |
|
61440 | 52 |
in if null input then [] else rev (#2 (flush (fold split input ([], [])))) end; |
61434 | 53 |
|
27342 | 54 |
|
44736 | 55 |
(* reports *) |
30641
72980f8d7ee8
export report -- version that actually covers all cases;
wenzelm
parents:
30635
diff
changeset
|
56 |
|
61457 | 57 |
fun antiq_reports ants = ants |> maps |
58 |
(fn Antiq (_, {start, stop, range = (pos, _)}) => |
|
59 |
[(start, Markup.antiquote), |
|
60 |
(stop, Markup.antiquote), |
|
61 |
(pos, Markup.antiquoted), |
|
62 |
(pos, Markup.language_antiquotation)] |
|
63 |
| _ => []); |
|
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
64 |
|
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
65 |
|
30590
1d9c9fcf8513
parameterized datatype antiquote and read operation;
wenzelm
parents:
30589
diff
changeset
|
66 |
(* scan *) |
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
67 |
|
30573 | 68 |
open Basic_Symbol_Pos; |
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
69 |
|
22114 | 70 |
local |
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
71 |
|
48764 | 72 |
val err_prefix = "Antiquotation lexical error: "; |
73 |
||
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
74 |
val scan_txt = |
55511
984e210d412e
antiquotations within plain text: Scala version in accordance to ML;
wenzelm
parents:
55107
diff
changeset
|
75 |
Scan.repeat1 ($$$ "@" --| Scan.ahead (~$$ "{") || |
58854 | 76 |
Scan.many1 (fn (s, _) => s <> "@" andalso Symbol.not_eof s)) >> flat; |
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
77 |
|
55512 | 78 |
val scan_antiq_body = |
48764 | 79 |
Scan.trace (Symbol_Pos.scan_string_qq err_prefix || Symbol_Pos.scan_string_bq err_prefix) >> #2 || |
55105 | 80 |
Scan.trace (Symbol_Pos.scan_cartouche err_prefix) >> #2 || |
58854 | 81 |
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
|
82 |
|
42508
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42503
diff
changeset
|
83 |
in |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42503
diff
changeset
|
84 |
|
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
85 |
val scan_antiq = |
55526 | 86 |
Symbol_Pos.scan_pos -- ($$ "@" |-- $$ "{" |-- Symbol_Pos.scan_pos -- |
48764 | 87 |
Symbol_Pos.!!! (fn () => err_prefix ^ "missing closing brace") |
55526 | 88 |
(Scan.repeat scan_antiq_body -- Symbol_Pos.scan_pos -- ($$ "}" |-- Symbol_Pos.scan_pos))) |
89 |
>> (fn (pos1, (pos2, ((body, pos3), pos4))) => |
|
90 |
(flat body, |
|
91 |
{start = Position.set_range (pos1, pos2), |
|
92 |
stop = Position.set_range (pos3, pos4), |
|
93 |
range = Position.range pos1 pos4})); |
|
30590
1d9c9fcf8513
parameterized datatype antiquote and read operation;
wenzelm
parents:
30589
diff
changeset
|
94 |
|
55512 | 95 |
val scan_antiquote = scan_antiq >> Antiq || scan_txt >> Text; |
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
96 |
|
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
97 |
end; |
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
98 |
|
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
99 |
|
27767 | 100 |
(* read *) |
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
101 |
|
61456 | 102 |
fun read' pos syms = |
103 |
(case Scan.read Symbol_Pos.stopper (Scan.repeat scan_antiquote) syms of |
|
61457 | 104 |
SOME ants => (Position.reports (antiq_reports ants); ants) |
61456 | 105 |
| NONE => error ("Malformed quotation/antiquotation source" ^ Position.here pos)); |
106 |
||
107 |
fun read source = read' (Input.pos_of source) (Input.source_explode source); |
|
9138
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
108 |
|
6a4fae41a75f
Text with antiquotations of inner items (terms, types etc.).
wenzelm
parents:
diff
changeset
|
109 |
end; |