author | wenzelm |
Wed, 06 Aug 2008 00:12:26 +0200 | |
changeset 27756 | 24d0e890b432 |
parent 27665 | b9e54ba563b3 |
child 27770 | 10d84e124a2f |
permissions | -rw-r--r-- |
23726 | 1 |
(* Title: Pure/Thy/thy_edit.ML |
2 |
ID: $Id$ |
|
3 |
Author: Makarius |
|
4 |
||
5 |
Basic editing of theory sources. |
|
6 |
*) |
|
7 |
||
8 |
signature THY_EDIT = |
|
9 |
sig |
|
23803 | 10 |
val token_source: Position.T -> (string, 'a) Source.source -> |
11 |
(OuterLex.token, Position.T * (Symbol.symbol, (string, 'a) Source.source) Source.source) |
|
12 |
Source.source |
|
13 |
val parse_tokens: Position.T -> (string, 'a) Source.source -> OuterLex.token list |
|
14 |
val present_token: OuterLex.token -> output |
|
27665 | 15 |
datatype span_kind = Command of string | Ignored | Unknown |
16 |
type span = span_kind * OuterLex.token list |
|
17 |
val span_range: span -> Position.range |
|
18 |
val span_source: Position.T -> (string, 'a) Source.source -> |
|
19 |
(span, (OuterLex.token, Position.T * (Symbol.symbol, (string, 'a) Source.source) Source.source) |
|
23803 | 20 |
Source.source) Source.source |
27665 | 21 |
val parse_spans: Position.T -> (string, 'a) Source.source -> span list |
22 |
val present_span: span -> output |
|
23726 | 23 |
val present_html: Path.T -> Path.T -> unit |
24 |
end; |
|
25 |
||
26 |
structure ThyEdit: THY_EDIT = |
|
27 |
struct |
|
28 |
||
29 |
structure T = OuterLex; |
|
30 |
structure P = OuterParse; |
|
31 |
||
32 |
||
23803 | 33 |
(** tokens **) |
34 |
||
35 |
(* parse *) |
|
23726 | 36 |
|
37 |
fun token_source pos src = |
|
38 |
Symbol.source true src |
|
27353
71c4dd53d4cb
moved global keywords from OuterSyntax to OuterKeyword, tuned interfaces;
wenzelm
parents:
26881
diff
changeset
|
39 |
|> T.source (SOME false) OuterKeyword.get_lexicons pos; |
23726 | 40 |
|
23803 | 41 |
fun parse_tokens pos src = Source.exhaust (token_source pos src); |
23726 | 42 |
|
43 |
||
23803 | 44 |
(* present *) |
23726 | 45 |
|
46 |
local |
|
47 |
||
48 |
val token_kind_markup = |
|
23730 | 49 |
fn T.Command => (Markup.commandN, []) |
50 |
| T.Keyword => (Markup.keywordN, []) |
|
51 |
| T.String => Markup.string |
|
52 |
| T.AltString => Markup.altstring |
|
53 |
| T.Verbatim => Markup.verbatim |
|
54 |
| T.Comment => Markup.comment |
|
55 |
| T.Sync => Markup.control |
|
56 |
| T.Malformed => Markup.malformed |
|
57 |
| T.Error _ => Markup.malformed |
|
23790 | 58 |
| T.EOF => Markup.control |
59 |
| _ => Markup.none; |
|
23726 | 60 |
|
23803 | 61 |
in |
62 |
||
23726 | 63 |
fun present_token tok = |
64 |
Markup.enclose (token_kind_markup (T.kind_of tok)) (Output.output (T.unparse tok)); |
|
65 |
||
23803 | 66 |
end; |
67 |
||
68 |
||
69 |
||
27665 | 70 |
(** spans **) |
71 |
||
72 |
datatype span_kind = Command of string | Ignored | Unknown; |
|
73 |
type span = span_kind * OuterLex.token list; |
|
23803 | 74 |
|
27665 | 75 |
fun span_range ((_, []): span) = raise Fail "span_range: empty span" |
76 |
| span_range (_, toks) = |
|
77 |
let |
|
78 |
val start_pos = T.position_of (hd toks); |
|
27756 | 79 |
val end_pos = T.end_position_of (List.last toks); |
27665 | 80 |
in (start_pos, end_pos) end; |
23803 | 81 |
|
82 |
||
83 |
(* parse *) |
|
23726 | 84 |
|
23803 | 85 |
local |
86 |
||
27665 | 87 |
val is_whitespace = T.is_kind T.Space orf T.is_kind T.Comment; |
88 |
||
89 |
val body = Scan.unless (Scan.many is_whitespace -- Scan.ahead (P.command || P.eof)) P.not_eof; |
|
23726 | 90 |
|
27665 | 91 |
val span = |
92 |
Scan.ahead P.command -- P.not_eof -- Scan.repeat body |
|
93 |
>> (fn ((name, c), bs) => (Command name, c :: bs)) || |
|
94 |
Scan.many1 is_whitespace >> pair Ignored || |
|
95 |
Scan.repeat1 body >> pair Unknown; |
|
23726 | 96 |
|
97 |
in |
|
98 |
||
27665 | 99 |
fun span_source pos src = |
23803 | 100 |
token_source pos src |
27665 | 101 |
|> Source.source T.stopper (Scan.bulk span) NONE; |
23803 | 102 |
|
103 |
end; |
|
104 |
||
27665 | 105 |
fun parse_spans pos src = Source.exhaust (span_source pos src); |
23803 | 106 |
|
107 |
||
108 |
(* present *) |
|
109 |
||
110 |
local |
|
111 |
||
27665 | 112 |
fun kind_markup (Command name) = Markup.command_span name |
113 |
| kind_markup Ignored = Markup.ignored_span |
|
114 |
| kind_markup Unknown = Markup.unknown_span; |
|
23803 | 115 |
|
116 |
in |
|
117 |
||
27665 | 118 |
fun present_span (kind, toks) = |
119 |
Markup.enclose (kind_markup kind) (implode (map present_token toks)); |
|
23803 | 120 |
|
121 |
end; |
|
122 |
||
123 |
||
124 |
(* HTML presentation -- example *) |
|
125 |
||
23726 | 126 |
fun present_html inpath outpath = |
27756 | 127 |
parse_spans (Path.position inpath) (Source.of_string (File.read inpath)) |
27665 | 128 |
|> HTML.html_mode (implode o map present_span) |
23726 | 129 |
|> enclose |
130 |
(HTML.begin_document (Path.implode (Path.base inpath)) ^ "<div class=\"source\"><pre>") |
|
131 |
("</pre></div>" ^ HTML.end_document) |
|
132 |
|> File.write outpath; |
|
133 |
||
134 |
end; |