@@ -48,7 +48,7 @@ parseChild :: Cursor -> P (Maybe Xsd.Child)
48parseChild c = do
48parseChild c = do
49 name <- getElementName c
49 name <- getElementName c
50 case name of
50 case name of
51− "element" -> Just . Xsd.ChildElement <$> parseElementOrRef c
51+ "element" -> Just . Xsd.ChildElement <$> parseElement c
52 "simpleType" -> Just <$> parseTopSimpleType c
52 "simpleType" -> Just <$> parseTopSimpleType c
53 "complexType" -> Just <$> parseTopComplexType c
53 "complexType" -> Just <$> parseTopComplexType c
54 "include" -> Just . Xsd.ChildInclude <$> parseInclude c
54 "include" -> Just . Xsd.ChildInclude <$> parseInclude c
@@ -81,19 +81,14 @@ parseImport c = do
81 , Xsd.importNamespace = namespace
81 , Xsd.importNamespace = namespace
84−parseElementOrRef :: Cursor -> P (Xsd.RefOr Xsd.Element)
85−parseElementOrRef c = do
86− case anAttribute "ref" c of
87− Nothing -> Xsd.Inline <$> parseElement c
88− Just ref -> Xsd.Ref <$> makeQName c ref
90parseElement :: Cursor -> P Xsd.Element
84parseElement :: Cursor -> P Xsd.Element
91parseElement c = handleNamespaces c $ do
85parseElement c = handleNamespaces c $ do
92 name <- theAttribute "name" c
86 name <- theAttribute "name" c
88+ -- XXX: generalaze RefOr parsing
94 tp <- case anAttribute "type" c of
89 tp <- case anAttribute "type" c of
95 Just t -> Xsd.Ref <$> makeQName c t
90 Just t -> Xsd.Ref <$> makeQName c t
96− _ -> maybe (Xsd.Ref anyType) Xsd.Inline <$> parseType c
91+ _ -> Xsd.Inline <$> parseType c
98 minOccurs <- case anAttribute "minOccurs" c of
93 minOccurs <- case anAttribute "minOccurs" c of
99 Nothing -> return 1
94 Nothing -> return 1
@@ -112,31 +107,22 @@ parseElement c = handleNamespaces c $ do
113 qname <- makeTargetQName name
108 qname <- makeTargetQName name
115− nillable <- case anAttribute "nillable" c of
116− Nothing -> return False
117− Just "true" -> return True
118− Just "false" -> return False
119− _ -> parseError c "Unexpected value of nillable attribute"
121 return Xsd.Element
110 return Xsd.Element
122 { Xsd.elementName = qname
111 { Xsd.elementName = qname
123 , Xsd.elementType = tp
112 , Xsd.elementType = tp
124 , Xsd.elementAnnotations = annotations
113 , Xsd.elementAnnotations = annotations
125 , Xsd.elementOccurs = (minOccurs, maxOccurs)
114 , Xsd.elementOccurs = (minOccurs, maxOccurs)
126− , Xsd.elementNillable = nillable
129− anyType = Xsd.QName (Just (Xsd.Namespace Xsd.schemaNamespace)) "anyType"
131-- | Parse inline type, simple or complex
117-- | Parse inline type, simple or complex
132−parseType :: Cursor -> P (Maybe Xsd.Type)
118+parseType :: Cursor -> P Xsd.Type
133parseType c = do
119parseType c = do
134 simpleTypeAxis <- makeElemAxis "simpleType"
120 simpleTypeAxis <- makeElemAxis "simpleType"
135 complexTypeAxis <- makeElemAxis "complexType"
121 complexTypeAxis <- makeElemAxis "complexType"
136 case (c $/ simpleTypeAxis, c $/ complexTypeAxis) of
122 case (c $/ simpleTypeAxis, c $/ complexTypeAxis) of
137− ([], []) -> return Nothing
123+ ([], []) -> parseError c "No type"
138− ([t], []) -> Just . Xsd.TypeSimple <$> parseSimpleType t
124+ ([t], []) -> Xsd.TypeSimple <$> parseSimpleType t
139− ([], [t]) -> Just . Xsd.TypeComplex <$> parseComplexType t
125+ ([], [t]) -> Xsd.TypeComplex <$> parseComplexType t
140 _ -> parseError c "Multiple types"
126 _ -> parseError c "Multiple types"
142-- | Top level simple type
128-- | Top level simple type
@@ -160,19 +146,18 @@ parseSimpleType c = do
160 unionAxis <- makeElemAxis "union"
146 unionAxis <- makeElemAxis "union"
161 case (c $/ restrictionAxis, c $/ listAxis, c $/ unionAxis) of
147 case (c $/ restrictionAxis, c $/ listAxis, c $/ unionAxis) of
162 ([e], [], []) -> Xsd.AtomicType
148 ([e], [], []) -> Xsd.AtomicType
163− <$> parseSimpleRestriction e
149+ <$> parseRestriction e
164 <*> parseAnnotations c
150 <*> parseAnnotations c
165 ([], [l], []) -> Xsd.ListType
151 ([], [l], []) -> Xsd.ListType
166 <$> parseList l
152 <$> parseList l
167 <*> parseAnnotations c
153 <*> parseAnnotations c
168− ([], [], [u]) -> Xsd.UnionType
154+ -- TODO implement me
169− <$> parseUnion u
155+ ([], [], [_]) -> parseError c "Not implemented: union"
170− <*> parseAnnotations c
171 _ -> parseError c "Expected exactly of of restriction, list or union"
156 _ -> parseError c "Expected exactly of of restriction, list or union"
173-- | Restriction for simple type
158-- | Restriction for simple type
174−parseSimpleRestriction :: Cursor -> P Xsd.SimpleRestriction
159+parseRestriction :: Cursor -> P Xsd.Restriction
175−parseSimpleRestriction c = do
160+parseRestriction c = do
176 -- XXX: generalaze RefOr parsing
161 -- XXX: generalaze RefOr parsing
177 tp <- case anAttribute "base" c of
162 tp <- case anAttribute "base" c of
178 Nothing -> do
163 Nothing -> do
@@ -183,19 +168,11 @@ parseSimpleRestriction c = do
183 _ -> parseError c "Multiple types"
168 _ -> parseError c "Multiple types"
184 Just t -> Xsd.Ref <$> makeQName c t
169 Just t -> Xsd.Ref <$> makeQName c t
186− constraints <- parseConstrains c
171+ return Xsd.Restriction
187−
172+ { Xsd.restrictionBase = tp
188− return Xsd.SimpleRestriction
173+ , Xsd.restrictionConstraints = [] -- TODO: implement me
189− { Xsd.simpleRestrictionBase = tp
190− , Xsd.simpleRestrictionConstraints = constraints
193−parseConstrains :: Cursor -> P [Xsd.Constraint]
194−parseConstrains c = do
195− enumerationAxis <- makeElemAxis "enumeration"
196− forM (c $/ enumerationAxis) $ \e -> do
197− Xsd.Enumeration <$> theAttribute "value" e
199parseList :: Cursor -> P (Xsd.RefOr Xsd.SimpleType)
176parseList :: Cursor -> P (Xsd.RefOr Xsd.SimpleType)
200parseList c =
177parseList c =
201 -- XXX: generalaze RefOr parsing
178 -- XXX: generalaze RefOr parsing
@@ -208,105 +185,30 @@ parseList c =
208 _ -> parseError c "Multiple types"
185 _ -> parseError c "Multiple types"
209 Just t -> Xsd.Ref <$> makeQName c t
186 Just t -> Xsd.Ref <$> makeQName c t
211−parseUnion :: Cursor -> P [Xsd.RefOr Xsd.SimpleType]
213− case anAttribute "memberTypes" c of
215− mapM (fmap Xsd.Ref . makeQName c) (Text.splitOn " " list)
217− simpleTypeAxis <- makeElemAxis "simpleType"
218− mapM (fmap Xsd.Inline . parseSimpleType) (c $/ simpleTypeAxis)
220parseComplexType :: Cursor -> P Xsd.ComplexType
188parseComplexType :: Cursor -> P Xsd.ComplexType
221parseComplexType c = do
189parseComplexType c = do
222− annotations <- parseAnnotations c
190+ attrAxis <- makeElemAxis "attribute"
223− cont <- parseContent c
191+ attributes <- mapM parseAttribute (c $/ attrAxis)
224− return Xsd.ComplexType
192+
225− { Xsd.complexAnnotations = annotations
193+ group <- do
226− , Xsd.complexContent = cont
194+ sequenceAxis <- makeElemAxis "sequence"
227− }
195+ choiceAxis <- makeElemAxis "choice"
229−parseContent :: Cursor -> P Xsd.Content
231− simpleContentAxis <- makeElemAxis "simpleContent"
232− complexContentAxis <- makeElemAxis "complexContent"
233− case (c $/ simpleContentAxis, c $/ complexContentAxis) of
234− ([], []) -> Xsd.ContentPlain <$> parsePlainContent c
235− ([s], []) -> Xsd.ContentSimple <$> parseSimpleContent s
236− ([], [s]) -> Xsd.ContentComplex <$> parseComplexContent s
238− "Expected one of simpleContent, complextContent or model group"
240−parsePlainContent :: Cursor -> P Xsd.PlainContent
241−parsePlainContent c = do
242− attributes <- parseAttributes c
243− model <- parseModelGroup c
244− return Xsd.PlainContent
245− { Xsd.plainContentModel = model
246− , Xsd.plainContentAttributes = attributes
249−parseSimpleContent :: Cursor -> P Xsd.SimpleContent
250−parseSimpleContent c = do
251− restrictionAxis <- makeElemAxis "restriction"
252− extenstionAxis <- makeElemAxis "extension"
196+ allAxis <- makeElemAxis "all"
253− case (c $/ restrictionAxis, c $/ extenstionAxis) of
197+ case (c $/ sequenceAxis, c $/ choiceAxis, c $/ allAxis) of
254− ([r], []) -> Xsd.SimpleContentRestriction <$> parseSimpleRestriction r
198+ ([], [], []) -> return Nothing
255− ([], [e]) -> Xsd.SimpleContentExtension <$> parseSimpleExtension e
199+ ([g], [], []) -> Just <$> parseSequence g
256− _ -> parseError c "Expected one of restriction or extension"
200+ ([], [g], []) -> Just <$> parseChoice g
257−
201+ ([], [], [g]) -> Just <$> parseAll g
258−parseSimpleExtension :: Cursor -> P Xsd.SimpleExtension
202+ _ -> parseError c "Multiple groups"
259−parseSimpleExtension c = handleNamespaces c $ do
260− base <- theAttribute "base" c >>= makeQName c
261− attributes <- parseAttributes c
262− return Xsd.SimpleExtension
263− { Xsd.simpleExtensionBase = base
264− , Xsd.simpleExtensionAttributes = attributes
267−parseComplexContent :: Cursor -> P Xsd.ComplexContent
204+ annotations <- parseAnnotations c
268−parseComplexContent c = do
269− restrictionAxis <- makeElemAxis "restriction"
270− extenstionAxis <- makeElemAxis "extension"
271− case (c $/ restrictionAxis, c $/ extenstionAxis) of
272− ([r], []) -> Xsd.ComplexContentRestriction <$> parseComplexRestriction r
273− ([], [e]) -> Xsd.ComplexContentExtension <$> parseComplexExtension e
274− _ -> parseError c "Expected one of restriction or extension"
276−parseComplexExtension :: Cursor -> P Xsd.ComplexExtension
277−parseComplexExtension c = handleNamespaces c $ do
278− base <- theAttribute "base" c >>= makeQName c
279− attributes <- parseAttributes c
280− model <- parseModelGroup c
281− return Xsd.ComplexExtension
282− { Xsd.complexExtensionBase = base
283− , Xsd.complexExtensionModel = model
284− , Xsd.complexExtensionAttributes = attributes
287−parseComplexRestriction :: Cursor -> P Xsd.ComplexRestriction
206+ return Xsd.ComplexType
288−parseComplexRestriction c = handleNamespaces c $ do
207+ { Xsd.complexAttributes = attributes
289− base <- theAttribute "base" c >>= makeQName c
208+ , Xsd.complexModelGroup = group
290− attributes <- parseAttributes c
209+ , Xsd.complexAnnotations = annotations
291− model <- parseModelGroup c
292− return Xsd.ComplexRestriction
293− { Xsd.complexRestrictionBase = base
294− , Xsd.complexRestrictionModel = model
295− , Xsd.complexRestrictionAttributes = attributes
298−parseModelGroup :: Cursor -> P (Maybe Xsd.ModelGroup)
299−parseModelGroup c = do
300− sequenceAxis <- makeElemAxis "sequence"
301− choiceAxis <- makeElemAxis "choice"
302− allAxis <- makeElemAxis "all"
303− case (c $/ sequenceAxis, c $/ choiceAxis, c $/ allAxis) of
304− ([], [], []) -> return Nothing
305− ([g], [], []) -> Just <$> parseSequence g
306− ([], [g], []) -> Just <$> parseChoice g
307− ([], [], [g]) -> Just <$> parseAll g
308− _ -> parseError c "Multiple model groups"
310parseSequence :: Cursor -> P Xsd.ModelGroup
212parseSequence :: Cursor -> P Xsd.ModelGroup
311parseSequence c = Xsd.Sequence <$> parseElements c
213parseSequence c = Xsd.Sequence <$> parseElements c
@@ -317,45 +219,31 @@ parseAll :: Cursor -> P Xsd.ModelGroup
317parseAll c = Xsd.All <$> parseElements c
219parseAll c = Xsd.All <$> parseElements c
319-- | Get all child elements
221-- | Get all child elements
320−parseElements :: Cursor -> P [Xsd.RefOr Xsd.Element]
222+parseElements :: Cursor -> P [Xsd.Element]
321parseElements c = do
223parseElements c = do
322 elementAxis <- makeElemAxis "element"
224 elementAxis <- makeElemAxis "element"
323− mapM parseElementOrRef (c $/ elementAxis)
225+ mapM parseElement (c $/ elementAxis)
325−parseAttributes :: Cursor -> P [Xsd.Attribute]
326−parseAttributes c = do
327− attrAxis <- makeElemAxis "attribute"
328− mapM parseAttribute (c $/ attrAxis)
330parseAttribute :: Cursor -> P Xsd.Attribute
227parseAttribute :: Cursor -> P Xsd.Attribute
331parseAttribute c = do
228parseAttribute c = do
229+ name <- theAttribute "name" c >>= makeTargetQName
332 use <- parseUseAttribute c
230 use <- parseUseAttribute c
333− case (anAttribute "name" c, anAttribute "ref" c) of
231+
334− (Just n, Nothing) -> do
232+ -- XXX: generalaze RefOr parsing
335− name <- makeTargetQName n
336− tp <- case anAttribute "type" c of
233+ tp <- case anAttribute "type" c of
337− Nothing -> do
234+ Nothing -> do
338− simpleTypeAxis <- makeElemAxis "simpleType"
235+ simpleTypeAxis <- makeElemAxis "simpleType"
339− case c $/ simpleTypeAxis of
236+ case c $/ simpleTypeAxis of
340− [s] -> Xsd.Inline <$> parseSimpleType s
237+ [s] -> Xsd.Inline <$> parseSimpleType s
341− [] -> return (Xsd.Ref anySimpleType)
238+ [] -> parseError c "Attribute should have type"
342− _ -> parseError c "Multiple simple types"
239+ _ -> parseError c "Multiple simple types"
343− Just t -> Xsd.Ref <$> makeQName c t
240+ Just t -> Xsd.Ref <$> makeQName c t
344− return $ Xsd.InlineAttribute $ Xsd.AttributeInline
241+
345− { Xsd.attributeInlineName = name
242+ return Xsd.Attribute
346− , Xsd.attributeInlineUse = use
243+ { Xsd.attrName = name
347− , Xsd.attributeInlineType = tp
244+ , Xsd.attrUse = use
348− }
245+ , Xsd.attrType = tp
349− (Nothing, Just r) -> do
350− ref <- makeQName c r
351− return $ Xsd.RefAttribute $ Xsd.AttributeRef
352− { Xsd.attributeRefRef = ref
353− , Xsd.attributeRefUse = use
355− _ -> parseError c "Should be either name or ref"
358− Xsd.QName (Just (Xsd.Namespace Xsd.schemaNamespace)) "anySimpleType"
360parseUseAttribute :: Cursor -> P Xsd.Use
248parseUseAttribute :: Cursor -> P Xsd.Use
361parseUseAttribute c = case anAttribute "use" c of
249parseUseAttribute c = case anAttribute "use" c of
@@ -511,9 +399,8 @@ makeElemAxis name = do
511makeQName :: Cursor -> Text -> P Xsd.QName
399makeQName :: Cursor -> Text -> P Xsd.QName
512makeQName c t = case Text.splitOn ":" t of
400makeQName c t = case Text.splitOn ":" t of
513 [n] -> do
401 [n] -> do
514− ns <- asks (lookup Nothing . envPrefixes)
515 return Xsd.QName
402 return Xsd.QName
516− { Xsd.qnNamespace = ns
403+ { Xsd.qnNamespace = Nothing
517 , Xsd.qnName = n
404 , Xsd.qnName = n
519 [p, n] -> do
406 [p, n] -> do