0 | module Language.XML.Prolog.XMLDecl
 1 |
 2 | import Data.String.Parser
 3 |
 4 | import Language.XML.Attribute
 5 |
 6 | public export
 7 | record XMLDecl where
 8 |     constructor MkXMLDecl
 9 |     version : String
10 |     encoding : Maybe String
11 |     standalone : Maybe Bool
12 |
13 | %name XMLDecl decl
14 |
15 | export
16 | Show XMLDecl where
17 |     show decl = """
18 |                 <?xml version=\{show decl.version}\
19 |                 \{maybe "" (\e => " encoding=" ++ show e) decl.encoding}\
20 |                 \{maybe "" (\s => " standalone=" ++ if s then "\"yes\"" else "\"no\"") decl.standalone}\
21 |                 ?>
22 |                 """
23 |
24 | export
25 | xmlDecl : Parser XMLDecl
26 | xmlDecl = (do
27 |     ignore $ string "<?xml"
28 |     spaces1
29 |     version <- exactAttribute $ MkQName Nothing $ MkName "version"
30 |     encoding <- optional (spaces *> exactAttribute (MkQName Nothing $ MkName "encoding"))
31 |     standalone <- case !(optional (spaces *> exactAttribute (MkQName Nothing $ MkName "standalone"))) of
32 |           Just "yes" => pure $ Just True
33 |           Just "no" => pure $ Just False
34 |           Nothing => pure Nothing
35 |           Just value => fail "Expect \"yes\"/\"no\" for \"standalone\" attribute, got: \{show value}"
36 |     spaces
37 |     ignore $ string "?>"
38 |     pure $ MkXMLDecl version encoding standalone
39 |   ) <?> "XML declaration"
40 |