@@ -24,8 +24,27 @@ import Text.XML.ParentAttributes
24import qualified Text.XML.Writer as XW
24import qualified Text.XML.Writer as XW
27+{- | Marks the type of code generation:
28+@Parser@ - generate only instances for xml parsing (@FromDom@)
29+@Generator@ - generate only @ToXML@ instances
30+@Both@ - generate @FromDom@ and @ToXML@ instances
27data GenType = Parser | Generator | Both
32data GenType = Parser | Generator | Both
34+{- | Marks the style of code generation regarding records fields
35+@LensRenaming@ - fields get renamed by prefixing the field name with
36+'_' and lowercased capital letter used in the type name.
39+> "MyNewDataType" =:= record genType LensRenaming
42+field will be renamed to `_mndtField1` and lens `mndtField1` will be created.
44+@NoLens@ - no field renaming and no lens generation.
46+data GenerateLens = LensRenaming | NoLens
29data XmlFieldPlural
48data XmlFieldPlural
30 = XmlFieldPluralMandatory -- Occurs exactly 1 time (Identity)
49 = XmlFieldPluralMandatory -- Occurs exactly 1 time (Identity)
31 | XmlFieldPluralOptional -- Occurs 0 or 1 times (Maybe)
50 | XmlFieldPluralOptional -- Occurs 0 or 1 times (Maybe)
@@ -55,7 +74,7 @@ data IsoXmlDescRecordPart
55 | IsoXmlDescRecordAttribute IsoXmlDescAttribute
74 | IsoXmlDescRecordAttribute IsoXmlDescAttribute
56 | IsoXmlDescRecordContent IsoXmlDescContent
75 | IsoXmlDescRecordContent IsoXmlDescContent
58−data IsoXmlDescRecord = IsoXmlDescRecord GenType [IsoXmlDescRecordPart]
77+data IsoXmlDescRecord = IsoXmlDescRecord GenType GenerateLens [IsoXmlDescRecordPart]
60makePrisms ''IsoXmlDescRecord
79makePrisms ''IsoXmlDescRecord
@@ -76,26 +95,34 @@ appendField
76 -> IsoXmlDescRecord
95 -> IsoXmlDescRecord
77 -> IsoXmlDescPreField
96 -> IsoXmlDescPreField
78 -> IsoXmlDescRecord
97 -> IsoXmlDescRecord
79−appendField plural (IsoXmlDescRecord genType fields) (IsoXmlDescPreField name ty) =
100+ (IsoXmlDescRecord genType generateLens fields)
101+ (IsoXmlDescPreField name ty) =
80− let xfield = IsoXmlDescRecordField $ IsoXmlDescField plural name ty
102+ let xfield = IsoXmlDescRecordField $ IsoXmlDescField plural name ty
81− in IsoXmlDescRecord genType (xfield:fields)
103+ in IsoXmlDescRecord genType generateLens (xfield:fields)
83appendAttribute
105appendAttribute
84 :: XmlAttributePlural
106 :: XmlAttributePlural
85 -> IsoXmlDescRecord
107 -> IsoXmlDescRecord
86 -> IsoXmlDescPreAttribute
108 -> IsoXmlDescPreAttribute
87 -> IsoXmlDescRecord
109 -> IsoXmlDescRecord
112+ (IsoXmlDescRecord genType generateLens fields)
88−appendAttribute plural (IsoXmlDescRecord genType fields) (IsoXmlDescPreAttribute name ty) =
113+ (IsoXmlDescPreAttribute name ty) =
89− let xattribute = IsoXmlDescRecordAttribute $ IsoXmlDescAttribute plural name ty
114+ let xattribute = IsoXmlDescRecordAttribute $ IsoXmlDescAttribute plural name ty
90− in IsoXmlDescRecord genType (xattribute:fields)
115+ in IsoXmlDescRecord genType generateLens (xattribute:fields)
92appendContent
117appendContent
93 :: IsoXmlDescRecord
118 :: IsoXmlDescRecord
94 -> IsoXmlDescPreContent
119 -> IsoXmlDescPreContent
95 -> IsoXmlDescRecord
120 -> IsoXmlDescRecord
122+ (IsoXmlDescRecord genType generateLens fields)
96−appendContent (IsoXmlDescRecord genType fields) (IsoXmlDescPreContent name ty) =
123+ (IsoXmlDescPreContent name ty) =
97− let xcontent = IsoXmlDescRecordContent $ IsoXmlDescContent name ty
124+ let xcontent = IsoXmlDescRecordContent $ IsoXmlDescContent name ty
98− in IsoXmlDescRecord genType (xcontent:fields)
125+ in IsoXmlDescRecord genType generateLens (xcontent:fields)
100(!), (?), (*), (+) :: IsoXmlDescRecord -> IsoXmlDescPreField -> IsoXmlDescRecord
127(!), (?), (*), (+) :: IsoXmlDescRecord -> IsoXmlDescPreField -> IsoXmlDescRecord
101(!) = appendField XmlFieldPluralMandatory
128(!) = appendField XmlFieldPluralMandatory
@@ -133,11 +160,11 @@ class Description name desc | desc -> name where
133infix 0 =:=
160infix 0 =:=
135instance Description PrefixName IsoXmlDescRecord where
162instance Description PrefixName IsoXmlDescRecord where
136− prefixName =:= (IsoXmlDescRecord genType descRecordParts) =
163+ prefixName =:= (IsoXmlDescRecord genType generateLens descRecordParts) =
137− isoXmlGenerateDatatype genType prefixName (reverse descRecordParts)
164+ isoXmlGenerateDatatype genType generateLens prefixName (reverse descRecordParts)
139−record :: GenType -> IsoXmlDescRecord
166+record :: GenType -> GenerateLens -> IsoXmlDescRecord
140−record gt = IsoXmlDescRecord gt []
167+record gt gl = IsoXmlDescRecord gt gl []
142enum :: GenType -> IsoXmlDescEnum
169enum :: GenType -> IsoXmlDescEnum
143enum gt = IsoXmlDescEnum gt []
170enum gt = IsoXmlDescEnum gt []
@@ -238,14 +265,25 @@ isoXmlGenerateEnum genType (ExhaustivenessName strName' exh) enumCons = do
238 return $ enumDecls ++ [toXmlInst, toXmlAttributeInst,
265 return $ enumDecls ++ [toXmlInst, toXmlAttributeInst,
239 fromDomInst, fromAttributeInst]
266 fromDomInst, fromAttributeInst]
241−isoXmlGenerateDatatype :: GenType -> PrefixName -> [IsoXmlDescRecordPart] -> TH.DecsQ
268+isoXmlGenerateDatatype
272+ -> [IsoXmlDescRecordPart]
274+isoXmlGenerateDatatype
242−isoXmlGenerateDatatype genType (PrefixName strName' strPrefix') descRecordParts = do
277+ (PrefixName strName' strPrefix')
278+ descRecordParts = do
244 isNewtype = length descRecordParts == 1
280 isNewtype = length descRecordParts == 1
245 strName = "Xml" ++ strName'
281 strName = "Xml" ++ strName'
246 strPrefix = "x" ++ strPrefix'
282 strPrefix = "x" ++ strPrefix'
247 name = TH.mkName strName
283 name = TH.mkName strName
284+ fieldName str = case generateLens of
248− fieldName str = "_" ++ strPrefix ++ over _head C.toUpper str
285+ LensRenaming -> "_" ++ strPrefix ++ over _head C.toUpper str
249 termDecl <- do
287 termDecl <- do
251 fields = do
289 fields = do
@@ -287,7 +325,9 @@ isoXmlGenerateDatatype genType (PrefixName strName' strPrefix') descRecordParts
287 -- generate a newtype instead to do less allocations later
325 -- generate a newtype instead to do less allocations later
288 then THC.newtypeD name (TH.recC name fields) [''Eq, ''Show, ''Generic]
326 then THC.newtypeD name (TH.recC name fields) [''Eq, ''Show, ''Generic]
289 else THC.dataD name [TH.recC name fields] [''Eq, ''Show, ''Generic]
327 else THC.dataD name [TH.recC name fields] [''Eq, ''Show, ''Generic]
328+ lensDecls <- case generateLens of
290− lensDecls <- makeFieldOpticsForDec lensRules termDecl
330+ LensRenaming -> makeFieldOpticsForDec lensRules termDecl
291 nfDataInst <- do
331 nfDataInst <- do
292 TH.instanceD
332 TH.instanceD
293 (return [])
333 (return [])