69222
|
1 |
(* Title: Tools/Haskell/Haskell.thy
|
|
2 |
Author: Makarius
|
69225
|
3 |
|
|
4 |
Support for Isabelle tools in Haskell.
|
69222
|
5 |
*)
|
|
6 |
|
|
7 |
theory Haskell
|
|
8 |
imports Pure
|
|
9 |
keywords "generate_haskell_file" "export_haskell_file" :: thy_decl
|
|
10 |
begin
|
|
11 |
|
69225
|
12 |
ML_file "haskell.ML"
|
|
13 |
|
|
14 |
|
|
15 |
section \<open>Commands\<close>
|
|
16 |
|
69222
|
17 |
ML \<open>
|
|
18 |
Outer_Syntax.command \<^command_keyword>\<open>generate_haskell_file\<close> "generate Haskell file"
|
69225
|
19 |
(Parse.position Parse.path -- (\<^keyword>\<open>=\<close> |-- Parse.input Parse.embedded)
|
|
20 |
>> Haskell.generate_file_cmd);
|
|
21 |
|
|
22 |
Outer_Syntax.command \<^command_keyword>\<open>export_haskell_file\<close> "export Haskell file"
|
|
23 |
(Parse.name -- (\<^keyword>\<open>=\<close> |-- Parse.input Parse.embedded)
|
|
24 |
>> Haskell.export_file_cmd);
|
|
25 |
\<close>
|
|
26 |
|
|
27 |
|
|
28 |
section \<open>Source modules\<close>
|
|
29 |
|
69226
|
30 |
generate_haskell_file Library.hs = \<open>
|
|
31 |
{- Title: Tools/Haskell/Library.hs
|
69225
|
32 |
Author: Makarius
|
|
33 |
LICENSE: BSD 3-clause (Isabelle)
|
|
34 |
|
|
35 |
Basic library of Isabelle idioms.
|
|
36 |
-}
|
|
37 |
|
|
38 |
module Isabelle.Library
|
69234
|
39 |
((|>), (|->), (#>), (#->), the_default, fold, fold_rev, single, quote, trim_line)
|
69225
|
40 |
where
|
|
41 |
|
69234
|
42 |
import Data.Maybe
|
|
43 |
|
|
44 |
|
69225
|
45 |
{- functions -}
|
|
46 |
|
|
47 |
(|>) :: a -> (a -> b) -> b
|
|
48 |
x |> f = f x
|
|
49 |
|
|
50 |
(|->) :: (a, b) -> (a -> b -> c) -> c
|
|
51 |
(x, y) |-> f = f x y
|
|
52 |
|
|
53 |
(#>) :: (a -> b) -> (b -> c) -> a -> c
|
|
54 |
(f #> g) x = x |> f |> g
|
|
55 |
|
|
56 |
(#->) :: (a -> (c, b)) -> (c -> b -> d) -> a -> d
|
|
57 |
(f #-> g) x = x |> f |-> g
|
|
58 |
|
|
59 |
|
69234
|
60 |
{- options -}
|
|
61 |
|
|
62 |
the_default :: a -> Maybe a -> a
|
|
63 |
the_default x Nothing = x
|
|
64 |
the_default _ (Just y) = y
|
|
65 |
|
|
66 |
|
69225
|
67 |
{- lists -}
|
|
68 |
|
|
69 |
fold :: (a -> b -> b) -> [a] -> b -> b
|
|
70 |
fold _ [] y = y
|
|
71 |
fold f (x : xs) y = fold f xs (f x y)
|
|
72 |
|
|
73 |
fold_rev :: (a -> b -> b) -> [a] -> b -> b
|
|
74 |
fold_rev _ [] y = y
|
|
75 |
fold_rev f (x : xs) y = f x (fold_rev f xs y)
|
|
76 |
|
|
77 |
single :: a -> [a]
|
|
78 |
single x = [x]
|
|
79 |
|
|
80 |
|
|
81 |
{- strings -}
|
|
82 |
|
|
83 |
quote :: String -> String
|
|
84 |
quote s = "\"" ++ s ++ "\""
|
|
85 |
|
|
86 |
trim_line :: String -> String
|
|
87 |
trim_line line =
|
|
88 |
case reverse line of
|
|
89 |
'\n' : '\r' : rest -> reverse rest
|
|
90 |
'\n' : rest -> reverse rest
|
|
91 |
_ -> line
|
|
92 |
\<close>
|
|
93 |
|
69233
|
94 |
generate_haskell_file Value.hs = \<open>
|
|
95 |
{- Title: Haskell/Tools/Value.hs
|
|
96 |
Author: Makarius
|
|
97 |
LICENSE: BSD 3-clause (Isabelle)
|
|
98 |
|
|
99 |
Plain values, represented as string.
|
|
100 |
-}
|
|
101 |
|
|
102 |
module Isabelle.Value
|
|
103 |
(print_bool, parse_bool, print_int, parse_int, print_real, parse_real)
|
|
104 |
where
|
|
105 |
|
|
106 |
import Data.Maybe
|
|
107 |
import qualified Data.List as List
|
|
108 |
import qualified Text.Read as Read
|
|
109 |
|
|
110 |
|
|
111 |
{- bool -}
|
|
112 |
|
|
113 |
print_bool :: Bool -> String
|
|
114 |
print_bool True = "true"
|
|
115 |
print_bool False = "false"
|
|
116 |
|
|
117 |
parse_bool :: String -> Maybe Bool
|
|
118 |
parse_bool "true" = Just True
|
|
119 |
parse_bool "false" = Just False
|
|
120 |
parse_bool _ = Nothing
|
|
121 |
|
|
122 |
|
|
123 |
{- int -}
|
|
124 |
|
|
125 |
print_int :: Int -> String
|
|
126 |
print_int = show
|
|
127 |
|
|
128 |
parse_int :: String -> Maybe Int
|
|
129 |
parse_int = Read.readMaybe
|
|
130 |
|
|
131 |
|
|
132 |
{- real -}
|
|
133 |
|
|
134 |
print_real :: Double -> String
|
|
135 |
print_real x =
|
|
136 |
let s = show x in
|
|
137 |
case span (/= '.') s of
|
|
138 |
(a, '.' : b) | List.all (== '0') b -> a
|
|
139 |
_ -> s
|
|
140 |
|
|
141 |
parse_real :: String -> Maybe Double
|
|
142 |
parse_real = Read.readMaybe
|
|
143 |
\<close>
|
|
144 |
|
69226
|
145 |
generate_haskell_file Buffer.hs = \<open>
|
|
146 |
{- Title: Tools/Haskell/Buffer.hs
|
69225
|
147 |
Author: Makarius
|
|
148 |
LICENSE: BSD 3-clause (Isabelle)
|
|
149 |
|
|
150 |
Efficient text buffers.
|
|
151 |
-}
|
69222
|
152 |
|
69225
|
153 |
module Isabelle.Buffer (T, empty, add, content)
|
|
154 |
where
|
|
155 |
|
|
156 |
newtype T = Buffer [String]
|
|
157 |
|
|
158 |
empty :: T
|
|
159 |
empty = Buffer []
|
|
160 |
|
|
161 |
add :: String -> T -> T
|
|
162 |
add "" buf = buf
|
|
163 |
add x (Buffer xs) = Buffer (x : xs)
|
|
164 |
|
|
165 |
content :: T -> String
|
|
166 |
content (Buffer xs) = concat (reverse xs)
|
|
167 |
\<close>
|
|
168 |
|
69226
|
169 |
generate_haskell_file Properties.hs = \<open>
|
|
170 |
{- Title: Tools/Haskell/Properties.hs
|
69225
|
171 |
Author: Makarius
|
|
172 |
LICENSE: BSD 3-clause (Isabelle)
|
|
173 |
|
|
174 |
Property lists.
|
|
175 |
-}
|
|
176 |
|
|
177 |
module Isabelle.Properties (Entry, T, defined, get, put, remove)
|
|
178 |
where
|
|
179 |
|
|
180 |
import qualified Data.List as List
|
|
181 |
|
|
182 |
|
|
183 |
type Entry = (String, String)
|
|
184 |
type T = [Entry]
|
|
185 |
|
|
186 |
defined :: T -> String -> Bool
|
|
187 |
defined props name = any (\(a, _) -> a == name) props
|
|
188 |
|
|
189 |
get :: T -> String -> Maybe String
|
|
190 |
get props name = List.lookup name props
|
|
191 |
|
|
192 |
put :: Entry -> T -> T
|
|
193 |
put entry props = entry : remove (fst entry) props
|
|
194 |
|
|
195 |
remove :: String -> T -> T
|
|
196 |
remove name props =
|
|
197 |
if defined props name then filter (\(a, _) -> a /= name) props
|
|
198 |
else props
|
|
199 |
\<close>
|
|
200 |
|
69226
|
201 |
generate_haskell_file Markup.hs = \<open>
|
|
202 |
{- Title: Haskell/Tools/Markup.hs
|
69225
|
203 |
Author: Makarius
|
|
204 |
LICENSE: BSD 3-clause (Isabelle)
|
|
205 |
|
|
206 |
Quasi-abstract markup elements.
|
|
207 |
-}
|
|
208 |
|
69234
|
209 |
module Isabelle.Markup (
|
|
210 |
T, empty, is_empty, properties,
|
|
211 |
|
|
212 |
nameN, name, xnameN, xname, kindN,
|
|
213 |
|
|
214 |
lineN, end_lineN, offsetN, end_offsetN, fileN, idN, positionN, position,
|
|
215 |
|
|
216 |
wordsN, words, no_wordsN, no_words,
|
|
217 |
|
|
218 |
tfreeN, tfree, tvarN, tvar, freeN, free, skolemN, skolem, boundN, bound, varN, var,
|
|
219 |
numeralN, numeral, literalN, literal, delimiterN, delimiter, inner_stringN, inner_string,
|
|
220 |
inner_cartoucheN, inner_cartouche, inner_commentN, inner_comment,
|
|
221 |
token_rangeN, token_range,
|
|
222 |
sortingN, sorting, typingN, typing, class_parameterN, class_parameter,
|
|
223 |
|
|
224 |
antiquotedN, antiquoted, antiquoteN, antiquote,
|
|
225 |
|
|
226 |
paragraphN, paragraph, text_foldN, text_fold,
|
|
227 |
|
|
228 |
keyword1N, keyword1, keyword2N, keyword2, keyword3N, keyword3, quasi_keywordN, quasi_keyword,
|
|
229 |
improperN, improper, operatorN, operator, stringN, string, alt_stringN, alt_string,
|
|
230 |
verbatimN, verbatim, cartoucheN, cartouche, commentN, comment,
|
|
231 |
|
|
232 |
writelnN, writeln, stateN, state, informationN, information, tracingN, tracing,
|
|
233 |
warningN, warning, legacyN, legacy, errorN, error, reportN, report, no_reportN, no_report,
|
|
234 |
|
|
235 |
intensifyN, intensify,
|
|
236 |
Output, no_output)
|
69225
|
237 |
where
|
|
238 |
|
69234
|
239 |
import Prelude hiding (words, error)
|
|
240 |
|
|
241 |
import Isabelle.Library
|
69225
|
242 |
import qualified Isabelle.Properties as Properties
|
|
243 |
|
|
244 |
|
69234
|
245 |
{- basic markup -}
|
|
246 |
|
69225
|
247 |
type T = (String, Properties.T)
|
|
248 |
|
|
249 |
empty :: T
|
|
250 |
empty = ("", [])
|
|
251 |
|
|
252 |
is_empty :: T -> Bool
|
|
253 |
is_empty ("", _) = True
|
|
254 |
is_empty _ = False
|
|
255 |
|
69234
|
256 |
properties :: Properties.T -> T -> T
|
|
257 |
properties more_props (elem, props) =
|
|
258 |
(elem, fold_rev Properties.put more_props props)
|
|
259 |
|
|
260 |
markup_elem name = (name, (name, []) :: T)
|
|
261 |
|
|
262 |
|
|
263 |
{- misc properties -}
|
|
264 |
|
|
265 |
nameN :: String
|
|
266 |
nameN = \<open>Markup.nameN\<close>
|
|
267 |
|
|
268 |
name :: String -> T -> T
|
|
269 |
name a = properties [(nameN, a)]
|
|
270 |
|
|
271 |
xnameN :: String
|
|
272 |
xnameN = \<open>Markup.xnameN\<close>
|
|
273 |
|
|
274 |
xname :: String -> T -> T
|
|
275 |
xname a = properties [(xnameN, a)]
|
|
276 |
|
|
277 |
kindN :: String
|
|
278 |
kindN = \<open>Markup.kindN\<close>
|
|
279 |
|
|
280 |
|
|
281 |
{- position -}
|
|
282 |
|
|
283 |
lineN, end_lineN :: String
|
|
284 |
lineN = \<open>Markup.lineN\<close>
|
|
285 |
end_lineN = \<open>Markup.end_lineN\<close>
|
|
286 |
|
|
287 |
offsetN, end_offsetN :: String
|
|
288 |
offsetN = \<open>Markup.offsetN\<close>
|
|
289 |
end_offsetN = \<open>Markup.end_offsetN\<close>
|
|
290 |
|
|
291 |
fileN, idN :: String
|
|
292 |
fileN = \<open>Markup.fileN\<close>
|
|
293 |
idN = \<open>Markup.idN\<close>
|
|
294 |
|
|
295 |
positionN :: String; position :: T
|
|
296 |
(positionN, position) = markup_elem \<open>Markup.positionN\<close>
|
|
297 |
|
|
298 |
|
|
299 |
{- text properties -}
|
|
300 |
|
|
301 |
wordsN :: String; words :: T
|
|
302 |
(wordsN, words) = markup_elem \<open>Markup.wordsN\<close>
|
|
303 |
|
|
304 |
no_wordsN :: String; no_words :: T
|
|
305 |
(no_wordsN, no_words) = markup_elem \<open>Markup.no_wordsN\<close>
|
|
306 |
|
|
307 |
|
|
308 |
{- inner syntax -}
|
|
309 |
|
|
310 |
tfreeN :: String; tfree :: T
|
|
311 |
(tfreeN, tfree) = markup_elem \<open>Markup.tfreeN\<close>
|
|
312 |
|
|
313 |
tvarN :: String; tvar :: T
|
|
314 |
(tvarN, tvar) = markup_elem \<open>Markup.tvarN\<close>
|
|
315 |
|
|
316 |
freeN :: String; free :: T
|
|
317 |
(freeN, free) = markup_elem \<open>Markup.freeN\<close>
|
|
318 |
|
|
319 |
skolemN :: String; skolem :: T
|
|
320 |
(skolemN, skolem) = markup_elem \<open>Markup.skolemN\<close>
|
|
321 |
|
|
322 |
boundN :: String; bound :: T
|
|
323 |
(boundN, bound) = markup_elem \<open>Markup.boundN\<close>
|
|
324 |
|
|
325 |
varN :: String; var :: T
|
|
326 |
(varN, var) = markup_elem \<open>Markup.varN\<close>
|
|
327 |
|
|
328 |
numeralN :: String; numeral :: T
|
|
329 |
(numeralN, numeral) = markup_elem \<open>Markup.numeralN\<close>
|
|
330 |
|
|
331 |
literalN :: String; literal :: T
|
|
332 |
(literalN, literal) = markup_elem \<open>Markup.literalN\<close>
|
|
333 |
|
|
334 |
delimiterN :: String; delimiter :: T
|
|
335 |
(delimiterN, delimiter) = markup_elem \<open>Markup.delimiterN\<close>
|
|
336 |
|
|
337 |
inner_stringN :: String; inner_string :: T
|
|
338 |
(inner_stringN, inner_string) = markup_elem \<open>Markup.inner_stringN\<close>
|
|
339 |
|
|
340 |
inner_cartoucheN :: String; inner_cartouche :: T
|
|
341 |
(inner_cartoucheN, inner_cartouche) = markup_elem \<open>Markup.inner_cartoucheN\<close>
|
|
342 |
|
|
343 |
inner_commentN :: String; inner_comment :: T
|
|
344 |
(inner_commentN, inner_comment) = markup_elem \<open>Markup.inner_commentN\<close>
|
|
345 |
|
|
346 |
|
|
347 |
token_rangeN :: String; token_range :: T
|
|
348 |
(token_rangeN, token_range) = markup_elem \<open>Markup.token_rangeN\<close>
|
|
349 |
|
|
350 |
|
|
351 |
sortingN :: String; sorting :: T
|
|
352 |
(sortingN, sorting) = markup_elem \<open>Markup.sortingN\<close>
|
|
353 |
|
|
354 |
typingN :: String; typing :: T
|
|
355 |
(typingN, typing) = markup_elem \<open>Markup.typingN\<close>
|
|
356 |
|
|
357 |
class_parameterN :: String; class_parameter :: T
|
|
358 |
(class_parameterN, class_parameter) = markup_elem \<open>Markup.class_parameterN\<close>
|
|
359 |
|
|
360 |
|
|
361 |
{- antiquotations -}
|
|
362 |
|
|
363 |
antiquotedN :: String; antiquoted :: T
|
|
364 |
(antiquotedN, antiquoted) = markup_elem \<open>Markup.antiquotedN\<close>
|
|
365 |
|
|
366 |
antiquoteN :: String; antiquote :: T
|
|
367 |
(antiquoteN, antiquote) = markup_elem \<open>Markup.antiquoteN\<close>
|
|
368 |
|
|
369 |
|
|
370 |
{- text structure -}
|
|
371 |
|
|
372 |
paragraphN :: String; paragraph :: T
|
|
373 |
(paragraphN, paragraph) = markup_elem \<open>Markup.paragraphN\<close>
|
|
374 |
|
|
375 |
text_foldN :: String; text_fold :: T
|
|
376 |
(text_foldN, text_fold) = markup_elem \<open>Markup.text_foldN\<close>
|
|
377 |
|
|
378 |
|
|
379 |
{- outer syntax -}
|
|
380 |
|
|
381 |
keyword1N :: String; keyword1 :: T
|
|
382 |
(keyword1N, keyword1) = markup_elem \<open>Markup.keyword1N\<close>
|
|
383 |
|
|
384 |
keyword2N :: String; keyword2 :: T
|
|
385 |
(keyword2N, keyword2) = markup_elem \<open>Markup.keyword2N\<close>
|
|
386 |
|
|
387 |
keyword3N :: String; keyword3 :: T
|
|
388 |
(keyword3N, keyword3) = markup_elem \<open>Markup.keyword3N\<close>
|
|
389 |
|
|
390 |
quasi_keywordN :: String; quasi_keyword :: T
|
|
391 |
(quasi_keywordN, quasi_keyword) = markup_elem \<open>Markup.quasi_keywordN\<close>
|
|
392 |
|
|
393 |
improperN :: String; improper :: T
|
|
394 |
(improperN, improper) = markup_elem \<open>Markup.improperN\<close>
|
|
395 |
|
|
396 |
operatorN :: String; operator :: T
|
|
397 |
(operatorN, operator) = markup_elem \<open>Markup.operatorN\<close>
|
|
398 |
|
|
399 |
stringN :: String; string :: T
|
|
400 |
(stringN, string) = markup_elem \<open>Markup.stringN\<close>
|
|
401 |
|
|
402 |
alt_stringN :: String; alt_string :: T
|
|
403 |
(alt_stringN, alt_string) = markup_elem \<open>Markup.alt_stringN\<close>
|
|
404 |
|
|
405 |
verbatimN :: String; verbatim :: T
|
|
406 |
(verbatimN, verbatim) = markup_elem \<open>Markup.verbatimN\<close>
|
|
407 |
|
|
408 |
cartoucheN :: String; cartouche :: T
|
|
409 |
(cartoucheN, cartouche) = markup_elem \<open>Markup.cartoucheN\<close>
|
|
410 |
|
|
411 |
commentN :: String; comment :: T
|
|
412 |
(commentN, comment) = markup_elem \<open>Markup.commentN\<close>
|
|
413 |
|
|
414 |
|
|
415 |
{- messages -}
|
|
416 |
|
|
417 |
writelnN :: String; writeln :: T
|
|
418 |
(writelnN, writeln) = markup_elem \<open>Markup.writelnN\<close>
|
|
419 |
|
|
420 |
stateN :: String; state :: T
|
|
421 |
(stateN, state) = markup_elem \<open>Markup.stateN\<close>
|
|
422 |
|
|
423 |
informationN :: String; information :: T
|
|
424 |
(informationN, information) = markup_elem \<open>Markup.informationN\<close>
|
|
425 |
|
|
426 |
tracingN :: String; tracing :: T
|
|
427 |
(tracingN, tracing) = markup_elem \<open>Markup.tracingN\<close>
|
|
428 |
|
|
429 |
warningN :: String; warning :: T
|
|
430 |
(warningN, warning) = markup_elem \<open>Markup.warningN\<close>
|
|
431 |
|
|
432 |
legacyN :: String; legacy :: T
|
|
433 |
(legacyN, legacy) = markup_elem \<open>Markup.legacyN\<close>
|
|
434 |
|
|
435 |
errorN :: String; error :: T
|
|
436 |
(errorN, error) = markup_elem \<open>Markup.errorN\<close>
|
|
437 |
|
|
438 |
reportN :: String; report :: T
|
|
439 |
(reportN, report) = markup_elem \<open>Markup.reportN\<close>
|
|
440 |
|
|
441 |
no_reportN :: String; no_report :: T
|
|
442 |
(no_reportN, no_report) = markup_elem \<open>Markup.no_reportN\<close>
|
|
443 |
|
|
444 |
intensifyN :: String; intensify :: T
|
|
445 |
(intensifyN, intensify) = markup_elem \<open>Markup.intensifyN\<close>
|
|
446 |
|
|
447 |
|
|
448 |
{- output -}
|
69225
|
449 |
|
|
450 |
type Output = (String, String)
|
|
451 |
|
|
452 |
no_output :: Output
|
|
453 |
no_output = ("", "")
|
69222
|
454 |
\<close>
|
|
455 |
|
69226
|
456 |
generate_haskell_file XML.hs = \<open>
|
|
457 |
{- Title: Tools/Haskell/XML.hs
|
69225
|
458 |
Author: Makarius
|
|
459 |
LICENSE: BSD 3-clause (Isabelle)
|
|
460 |
|
|
461 |
Untyped XML trees and representation of ML values.
|
|
462 |
-}
|
|
463 |
|
|
464 |
module Isabelle.XML (Attributes, Body, Tree(..), wrap_elem, unwrap_elem, content_of)
|
|
465 |
where
|
|
466 |
|
|
467 |
import qualified Data.List as List
|
|
468 |
|
|
469 |
import Isabelle.Library
|
|
470 |
import qualified Isabelle.Properties as Properties
|
|
471 |
import qualified Isabelle.Markup as Markup
|
|
472 |
import qualified Isabelle.Buffer as Buffer
|
|
473 |
|
|
474 |
|
|
475 |
{- types -}
|
|
476 |
|
|
477 |
type Attributes = Properties.T
|
|
478 |
type Body = [Tree]
|
|
479 |
data Tree = Elem Markup.T Body | Text String
|
|
480 |
|
|
481 |
|
|
482 |
{- wrapped elements -}
|
|
483 |
|
69234
|
484 |
xml_elemN = \<open>XML.xml_elemN\<close>
|
|
485 |
xml_nameN = \<open>XML.xml_nameN\<close>
|
|
486 |
xml_bodyN = \<open>XML.xml_bodyN\<close>
|
69225
|
487 |
|
|
488 |
wrap_elem (((a, atts), body1), body2) =
|
|
489 |
Elem (xml_elemN, (xml_nameN, a) : atts) (Elem (xml_bodyN, []) body1 : body2)
|
|
490 |
|
|
491 |
unwrap_elem (Elem (name, (n, a) : atts) (Elem (name', atts') body1 : body2)) =
|
|
492 |
if name == xml_elemN && n == xml_nameN && name' == xml_bodyN && null atts'
|
|
493 |
then Just (((a, atts), body1), body2) else Nothing
|
|
494 |
unwrap_elem _ = Nothing
|
|
495 |
|
|
496 |
|
|
497 |
{- text content -}
|
|
498 |
|
|
499 |
add_content tree =
|
|
500 |
case unwrap_elem tree of
|
|
501 |
Just (_, ts) -> fold add_content ts
|
|
502 |
Nothing ->
|
|
503 |
case tree of
|
|
504 |
Elem _ ts -> fold add_content ts
|
|
505 |
Text s -> Buffer.add s
|
|
506 |
|
|
507 |
content_of body = Buffer.empty |> fold add_content body |> Buffer.content
|
|
508 |
|
|
509 |
|
|
510 |
{- string representation -}
|
|
511 |
|
|
512 |
encode '<' = "<"
|
|
513 |
encode '>' = ">"
|
|
514 |
encode '&' = "&"
|
|
515 |
encode '\'' = "'"
|
|
516 |
encode '\"' = """
|
|
517 |
encode c = [c]
|
|
518 |
|
|
519 |
instance Show Tree where
|
|
520 |
show tree =
|
|
521 |
Buffer.empty |> show_tree tree |> Buffer.content
|
|
522 |
where
|
|
523 |
show_tree (Elem (name, atts) []) =
|
|
524 |
Buffer.add "<" #> Buffer.add (show_elem name atts) #> Buffer.add "/>"
|
|
525 |
show_tree (Elem (name, atts) ts) =
|
|
526 |
Buffer.add "<" #> Buffer.add (show_elem name atts) #> Buffer.add ">" #>
|
|
527 |
fold show_tree ts #>
|
|
528 |
Buffer.add "</" #> Buffer.add name #> Buffer.add ">"
|
|
529 |
show_tree (Text s) = Buffer.add (show_text s)
|
|
530 |
|
|
531 |
show_elem name atts =
|
|
532 |
unwords (name : map (\(a, x) -> a ++ "=\"" ++ show_text x ++ "\"") atts)
|
|
533 |
|
|
534 |
show_text = concatMap encode
|
|
535 |
\<close>
|
|
536 |
|
69226
|
537 |
generate_haskell_file YXML.hs = \<open>
|
|
538 |
{- Title: Tools/Haskell/YXML.hs
|
69225
|
539 |
Author: Makarius
|
|
540 |
LICENSE: BSD 3-clause (Isabelle)
|
|
541 |
|
|
542 |
Efficient text representation of XML trees. Suitable for direct
|
|
543 |
inlining into plain text.
|
|
544 |
-}
|
|
545 |
|
|
546 |
module Isabelle.YXML (charX, charY, strX, strY, detect,
|
|
547 |
buffer_body, buffer, string_of_body, string_of, parse_body, parse)
|
|
548 |
where
|
|
549 |
|
|
550 |
import qualified Data.Char as Char
|
|
551 |
import qualified Data.List as List
|
69222
|
552 |
|
69225
|
553 |
import Isabelle.Library
|
|
554 |
import qualified Isabelle.Markup as Markup
|
|
555 |
import qualified Isabelle.XML as XML
|
|
556 |
import qualified Isabelle.Buffer as Buffer
|
|
557 |
|
|
558 |
|
|
559 |
{- markers -}
|
|
560 |
|
|
561 |
charX, charY :: Char
|
|
562 |
charX = Char.chr 5
|
|
563 |
charY = Char.chr 6
|
|
564 |
|
|
565 |
strX, strY, strXY, strXYX :: String
|
|
566 |
strX = [charX]
|
|
567 |
strY = [charY]
|
|
568 |
strXY = strX ++ strY
|
|
569 |
strXYX = strXY ++ strX
|
|
570 |
|
|
571 |
detect :: String -> Bool
|
|
572 |
detect = any (\c -> c == charX || c == charY)
|
|
573 |
|
|
574 |
|
|
575 |
{- output -}
|
|
576 |
|
|
577 |
buffer_attrib (a, x) =
|
|
578 |
Buffer.add strY #> Buffer.add a #> Buffer.add "=" #> Buffer.add x
|
|
579 |
|
|
580 |
buffer_body :: XML.Body -> Buffer.T -> Buffer.T
|
|
581 |
buffer_body = fold buffer
|
|
582 |
|
|
583 |
buffer :: XML.Tree -> Buffer.T -> Buffer.T
|
|
584 |
buffer (XML.Elem (name, atts) ts) =
|
|
585 |
Buffer.add strXY #> Buffer.add name #> fold buffer_attrib atts #> Buffer.add strX #>
|
|
586 |
buffer_body ts #>
|
|
587 |
Buffer.add strXYX
|
|
588 |
buffer (XML.Text s) = Buffer.add s
|
|
589 |
|
|
590 |
string_of_body :: XML.Body -> String
|
|
591 |
string_of_body body = Buffer.empty |> buffer_body body |> Buffer.content
|
|
592 |
|
|
593 |
string_of :: XML.Tree -> String
|
|
594 |
string_of = string_of_body . single
|
|
595 |
|
|
596 |
|
|
597 |
{- parse -}
|
|
598 |
|
|
599 |
-- split: fields or non-empty tokens
|
|
600 |
|
|
601 |
split :: Bool -> Char -> String -> [String]
|
|
602 |
split _ _ [] = []
|
|
603 |
split fields sep str = splitting str
|
|
604 |
where
|
|
605 |
splitting rest =
|
|
606 |
case span (/= sep) rest of
|
|
607 |
(_, []) -> cons rest []
|
|
608 |
(prfx, _ : rest') -> cons prfx (splitting rest')
|
|
609 |
cons item = if fields || not (null item) then (:) item else id
|
|
610 |
|
|
611 |
|
|
612 |
-- structural errors
|
|
613 |
|
|
614 |
err msg = error ("Malformed YXML: " ++ msg)
|
|
615 |
err_attribute = err "bad attribute"
|
|
616 |
err_element = err "bad element"
|
|
617 |
err_unbalanced "" = err "unbalanced element"
|
|
618 |
err_unbalanced name = err ("unbalanced element " ++ quote name)
|
|
619 |
|
|
620 |
|
|
621 |
-- stack operations
|
|
622 |
|
|
623 |
add x ((elem, body) : pending) = (elem, x : body) : pending
|
|
624 |
|
|
625 |
push "" _ _ = err_element
|
|
626 |
push name atts pending = ((name, atts), []) : pending
|
|
627 |
|
|
628 |
pop ((("", _), _) : _) = err_unbalanced ""
|
|
629 |
pop ((markup, body) : pending) = add (XML.Elem markup (reverse body)) pending
|
|
630 |
|
|
631 |
|
|
632 |
-- parsing
|
|
633 |
|
|
634 |
parse_attrib s =
|
|
635 |
case List.elemIndex '=' s of
|
|
636 |
Just i | i > 0 -> (take i s, drop (i + 1) s)
|
|
637 |
_ -> err_attribute
|
|
638 |
|
|
639 |
parse_chunk ["", ""] = pop
|
|
640 |
parse_chunk ("" : name : atts) = push name (map parse_attrib atts)
|
|
641 |
parse_chunk txts = fold (add . XML.Text) txts
|
|
642 |
|
|
643 |
parse_body :: String -> XML.Body
|
|
644 |
parse_body source =
|
|
645 |
case fold parse_chunk chunks [(("", []), [])] of
|
|
646 |
[(("", _), result)] -> reverse result
|
|
647 |
((name, _), _) : _ -> err_unbalanced name
|
|
648 |
where chunks = split False charX source |> map (split True charY)
|
|
649 |
|
|
650 |
parse :: String -> XML.Tree
|
|
651 |
parse source =
|
|
652 |
case parse_body source of
|
|
653 |
[result] -> result
|
|
654 |
[] -> XML.Text ""
|
|
655 |
_ -> err "multiple results"
|
69222
|
656 |
\<close>
|
|
657 |
|
|
658 |
end
|