src/Tools/Haskell/Haskell.thy
changeset 69290 fb77612d11eb
parent 69289 bf6937af7fe8
child 69291 36d711008292
equal deleted inserted replaced
69289:bf6937af7fe8 69290:fb77612d11eb
   592   else (Markup.empty, [])
   592   else (Markup.empty, [])
   593 
   593 
   594 markup_report :: [T] -> String
   594 markup_report :: [T] -> String
   595 markup_report [] = ""
   595 markup_report [] = ""
   596 markup_report elems =
   596 markup_report elems =
   597   elems
   597   YXML.string_of $ XML.Elem (Markup.report, map (XML.Elem . markup_element) elems)
   598   |> map (markup_element #> uncurry XML.Elem)
       
   599   |> XML.Elem Markup.report
       
   600   |> YXML.string_of
       
   601 
   598 
   602 make_report :: Int -> (String, Properties.T) -> ((String -> Bool) -> [Name]) -> String
   599 make_report :: Int -> (String, Properties.T) -> ((String -> Bool) -> [Name]) -> String
   603 make_report limit name_props make_names =
   600 make_report limit name_props make_names =
   604   markup_report [make limit name_props make_names]
   601   markup_report [make limit name_props make_names]
   605 \<close>
   602 \<close>
   662 
   659 
   663 {- types -}
   660 {- types -}
   664 
   661 
   665 type Attributes = Properties.T
   662 type Attributes = Properties.T
   666 type Body = [Tree]
   663 type Body = [Tree]
   667 data Tree = Elem Markup.T Body | Text String
   664 data Tree = Elem (Markup.T, Body) | Text String
   668 
   665 
   669 
   666 
   670 {- wrapped elements -}
   667 {- wrapped elements -}
   671 
   668 
   672 wrap_elem (((a, atts), body1), body2) =
   669 wrap_elem (((a, atts), body1), body2) =
   673   Elem (\<open>XML.xml_elemN\<close>, (\<open>XML.xml_nameN\<close>, a) : atts) (Elem (\<open>XML.xml_bodyN\<close>, []) body1 : body2)
   670   Elem ((\<open>XML.xml_elemN\<close>, (\<open>XML.xml_nameN\<close>, a) : atts), Elem ((\<open>XML.xml_bodyN\<close>, []), body1) : body2)
   674 
   671 
   675 unwrap_elem
   672 unwrap_elem
   676   (Elem (\<open>XML.xml_elemN\<close>, (\<open>XML.xml_nameN\<close>, a) : atts) (Elem (\<open>XML.xml_bodyN\<close>, []) body1 : body2)) =
   673   (Elem ((\<open>XML.xml_elemN\<close>, (\<open>XML.xml_nameN\<close>, a) : atts), Elem ((\<open>XML.xml_bodyN\<close>, []), body1) : body2)) =
   677   Just (((a, atts), body1), body2)
   674   Just (((a, atts), body1), body2)
   678 unwrap_elem _ = Nothing
   675 unwrap_elem _ = Nothing
   679 
   676 
   680 
   677 
   681 {- text content -}
   678 {- text content -}
   683 add_content tree =
   680 add_content tree =
   684   case unwrap_elem tree of
   681   case unwrap_elem tree of
   685     Just (_, ts) -> fold add_content ts
   682     Just (_, ts) -> fold add_content ts
   686     Nothing ->
   683     Nothing ->
   687       case tree of
   684       case tree of
   688         Elem _ ts -> fold add_content ts
   685         Elem (_, ts) -> fold add_content ts
   689         Text s -> Buffer.add s
   686         Text s -> Buffer.add s
   690 
   687 
   691 content_of body = Buffer.empty |> fold add_content body |> Buffer.content
   688 content_of body = Buffer.empty |> fold add_content body |> Buffer.content
   692 
   689 
   693 
   690 
   702 
   699 
   703 instance Show Tree where
   700 instance Show Tree where
   704   show tree =
   701   show tree =
   705     Buffer.empty |> show_tree tree |> Buffer.content
   702     Buffer.empty |> show_tree tree |> Buffer.content
   706     where
   703     where
   707       show_tree (Elem (name, atts) []) =
   704       show_tree (Elem ((name, atts), [])) =
   708         Buffer.add "<" #> Buffer.add (show_elem name atts) #> Buffer.add "/>"
   705         Buffer.add "<" #> Buffer.add (show_elem name atts) #> Buffer.add "/>"
   709       show_tree (Elem (name, atts) ts) =
   706       show_tree (Elem ((name, atts), ts)) =
   710         Buffer.add "<" #> Buffer.add (show_elem name atts) #> Buffer.add ">" #>
   707         Buffer.add "<" #> Buffer.add (show_elem name atts) #> Buffer.add ">" #>
   711         fold show_tree ts #>
   708         fold show_tree ts #>
   712         Buffer.add "</" #> Buffer.add name #> Buffer.add ">"
   709         Buffer.add "</" #> Buffer.add name #> Buffer.add ">"
   713       show_tree (Text s) = Buffer.add (show_text s)
   710       show_tree (Text s) = Buffer.add (show_text s)
   714 
   711 
   761 unit_atom () = ""
   758 unit_atom () = ""
   762 
   759 
   763 
   760 
   764 -- structural nodes
   761 -- structural nodes
   765 
   762 
   766 node = XML.Elem (":", [])
   763 node ts = XML.Elem ((":", []), ts)
   767 
   764 
   768 vector = map_index (\(i, x) -> (int_atom i, x))
   765 vector = map_index (\(i, x) -> (int_atom i, x))
   769 
   766 
   770 tagged (tag, (xs, ts)) = XML.Elem (int_atom tag, vector xs) ts
   767 tagged (tag, (xs, ts)) = XML.Elem ((int_atom tag, vector xs), ts)
   771 
   768 
   772 
   769 
   773 -- representation of standard types
   770 -- representation of standard types
   774 
   771 
   775 tree :: T XML.Tree
   772 tree :: T XML.Tree
   776 tree t = [t]
   773 tree t = [t]
   777 
   774 
   778 properties :: T Properties.T
   775 properties :: T Properties.T
   779 properties props = [XML.Elem (":", props) []]
   776 properties props = [XML.Elem ((":", props), [])]
   780 
   777 
   781 string :: T String
   778 string :: T String
   782 string "" = []
   779 string "" = []
   783 string s = [XML.Text s]
   780 string s = [XML.Text s]
   784 
   781 
   857 unit_atom _ = err_atom
   854 unit_atom _ = err_atom
   858 
   855 
   859 
   856 
   860 {- structural nodes -}
   857 {- structural nodes -}
   861 
   858 
   862 node (XML.Elem (":", []) ts) = ts
   859 node (XML.Elem ((":", []), ts)) = ts
   863 node _ = err_body
   860 node _ = err_body
   864 
   861 
   865 vector atts =
   862 vector atts =
   866   map_index (\(i, (a, x)) -> if int_atom a == i then x else err_atom) atts
   863   map_index (\(i, (a, x)) -> if int_atom a == i then x else err_atom) atts
   867 
   864 
   868 tagged (XML.Elem (name, atts) ts) = (int_atom name, (vector atts, ts))
   865 tagged (XML.Elem ((name, atts), ts)) = (int_atom name, (vector atts, ts))
   869 tagged _ = err_body
   866 tagged _ = err_body
   870 
   867 
   871 
   868 
   872 {- representation of standard types -}
   869 {- representation of standard types -}
   873 
   870 
   874 tree :: T XML.Tree
   871 tree :: T XML.Tree
   875 tree [t] = t
   872 tree [t] = t
   876 tree _ = err_body
   873 tree _ = err_body
   877 
   874 
   878 properties :: T Properties.T
   875 properties :: T Properties.T
   879 properties [XML.Elem (":", props) []] = props
   876 properties [XML.Elem ((":", props), [])] = props
   880 properties _ = err_body
   877 properties _ = err_body
   881 
   878 
   882 string :: T String
   879 string :: T String
   883 string [] = ""
   880 string [] = ""
   884 string [XML.Text s] = s
   881 string [XML.Text s] = s
   967 
   964 
   968 buffer_body :: XML.Body -> Buffer.T -> Buffer.T
   965 buffer_body :: XML.Body -> Buffer.T -> Buffer.T
   969 buffer_body = fold buffer
   966 buffer_body = fold buffer
   970 
   967 
   971 buffer :: XML.Tree -> Buffer.T -> Buffer.T
   968 buffer :: XML.Tree -> Buffer.T -> Buffer.T
   972 buffer (XML.Elem (name, atts) ts) =
   969 buffer (XML.Elem ((name, atts), ts)) =
   973   Buffer.add strXY #> Buffer.add name #> fold buffer_attrib atts #> Buffer.add strX #>
   970   Buffer.add strXY #> Buffer.add name #> fold buffer_attrib atts #> Buffer.add strX #>
   974   buffer_body ts #>
   971   buffer_body ts #>
   975   Buffer.add strXYX
   972   Buffer.add strXYX
   976 buffer (XML.Text s) = Buffer.add s
   973 buffer (XML.Text s) = Buffer.add s
   977 
   974 
  1012 
  1009 
  1013 push "" _ _ = err_element
  1010 push "" _ _ = err_element
  1014 push name atts pending = ((name, atts), []) : pending
  1011 push name atts pending = ((name, atts), []) : pending
  1015 
  1012 
  1016 pop ((("", _), _) : _) = err_unbalanced ""
  1013 pop ((("", _), _) : _) = err_unbalanced ""
  1017 pop ((markup, body) : pending) = add (XML.Elem markup (reverse body)) pending
  1014 pop ((markup, body) : pending) = add (XML.Elem (markup, reverse body)) pending
  1018 
  1015 
  1019 
  1016 
  1020 -- parsing
  1017 -- parsing
  1021 
  1018 
  1022 parse_attrib s =
  1019 parse_attrib s =
  1082 symbolic_text "" = []
  1079 symbolic_text "" = []
  1083 symbolic_text s = [XML.Text s]
  1080 symbolic_text s = [XML.Text s]
  1084 
  1081 
  1085 symbolic_markup markup body =
  1082 symbolic_markup markup body =
  1086   if Markup.is_empty markup then body
  1083   if Markup.is_empty markup then body
  1087   else [XML.Elem markup body]
  1084   else [XML.Elem (markup, body)]
  1088 
  1085 
  1089 symbolic :: T -> XML.Body
  1086 symbolic :: T -> XML.Body
  1090 symbolic (Block markup consistent indent prts) =
  1087 symbolic (Block markup consistent indent prts) =
  1091   concatMap symbolic prts
  1088   concatMap symbolic prts
  1092   |> symbolic_markup block_markup
  1089   |> symbolic_markup block_markup
  1093   |> symbolic_markup markup
  1090   |> symbolic_markup markup
  1094   where block_markup = if null prts then Markup.empty else Markup.block consistent indent
  1091   where block_markup = if null prts then Markup.empty else Markup.block consistent indent
  1095 symbolic (Break wd ind) = [XML.Elem (Markup.break wd ind) (symbolic_text (output_spaces wd))]
  1092 symbolic (Break wd ind) = [XML.Elem (Markup.break wd ind, symbolic_text (output_spaces wd))]
  1096 symbolic (Str s) = symbolic_text s
  1093 symbolic (Str s) = symbolic_text s
  1097 
  1094 
  1098 formatted :: T -> String
  1095 formatted :: T -> String
  1099 formatted = YXML.string_of_body . symbolic
  1096 formatted = YXML.string_of_body . symbolic
  1100 
  1097