import Anno_Parser(annotationL)
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
-- annotations on one line
lineAnnos :: GenParser Char st [Annotation]
lineAnnos = do { p <- getPosition
; if sourceLine q == sourceLine p then
-- optional semicolon followed by annotations on the same line
optSemi :: GenParser Char st (Maybe Token, [Annotation])
optSemi = bind (,) (option Nothing (fmap Just semiT)) lineAnnos
annoParser :: GenParser Char st b -> GenParser Char st (Annoted b)
annoParser parser = bind (\x y -> Annoted y [] x []) annos parser
itemList :: String -> GenParser Char st b -> ([Annoted b] -> [Pos] -> a)
itemList keyword parser constr =
do { p <- pluralKeyword keyword
; (vs, ts, ans) <- itemAux (annoParser parser)
; let r = zipWith appendAnno vs ans
in return (constr r (map tokPos (p:ts)))
appendAnno :: Annoted a -> [Annotation] -> Annoted a
appendAnno (Annoted x p l r) y = Annoted x p l (r++y)
itemAux :: GenParser Char st a
-> GenParser Char st ([a], [Token], [[Annotation]])
; case m of { Nothing -> return ([a], [], [an])
; Just t -> do { tryItemEnd startKeyword
; return ([a], [t], [an])
do { (as, ts, ans) <- itemAux itemParser
; return (a:as, t:ts, an:ans)
-- ------------------------------------------------------------------------
-- ------------------------------------------------------------------------
nullKind = Kind [] (Intersection [] []) []
commaTypeDecl :: TypePattern -> GenParser Char st TypeItem
commaTypeDecl s = do { c <- commaT
; (is, cs) <- typePattern `separatedBy` commaT
<|> kindedTypeDecl (l, p)
<|> return (TypeDecl l nullKind (map tokPos p))
kindedTypeDecl :: ([TypePattern], [Token]) -> GenParser Char st TypeItem
; let d = TypeDecl l s (map tokPos (p++[t])) in
if length l > 1 then return d
else pseudoTypeDef (head l) s [t]
<|> dataDef (head l) s [t]
isoDecl :: TypePattern -> GenParser Char st TypeItem
isoDecl s = do { e <- equalT
(do { (l, p) <- typePattern `separatedBy` equalT
; return (IsoDecl (s:l) (map tokPos (e:p)))
subTypeDefn :: (TypePattern, Token) -> GenParser Char st TypeItem
subTypeDefn (s, e) = do { a <- annos
; f <- fmap TermFormula term
; return (SubtypeDefn s v t (Annoted f [] a [])
subTypeDecl :: ([TypePattern], [Token]) -> GenParser Char st TypeItem
; return (SubtypeDecl l s (map tokPos (p++[t])))
sortItem :: GenParser Char st TypeItem
sortItem = do { s <- typePattern;
return (TypeDecl [s] nullKind [])
sortItems :: GenParser Char st SigItems
sortItems = itemList sortS sortItem (TypeItems Plain)
typeItem :: GenParser Char st TypeItem
typeItem = do { s <- typePattern;
pseudoTypeDef s nullKind []
return (TypeDecl [s] nullKind [])
typeItemList :: [Token] -> Instance -> GenParser Char st SigItems
do { (vs, ts, ans) <- itemAux (annoParser typeItem)
; let r = zipWith appendAnno vs ans
in return (TypeItems k r (map tokPos (ps++ts)))
typeItems :: GenParser Char st SigItems
typeItems = do p <- pluralKeyword typeS
do q <- pluralKeyword instanceS
typeItemList [p, q] Instance
<|> typeItemList [p] Plain
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
typeArgParen :: GenParser Char st TypeArgs
(ts, ps) <- typeArgs `separatedBy` semiT
return (TypeArgs (concat ts) (map tokPos (o:ps++[c])))
typeArgSeq :: GenParser Char st [TypeArgs]
do (ts, ps) <- typeArgs `separatedBy` semiT
return [TypeArgs (concat ts) (map tokPos ps)]
pseudoType :: GenParser Char st PseudoType
pseudoType = do l <- asKey lamS
ts <- many1 typeArgParen <|> typeArgSeq
return (PseudoType ts t (map tokPos [l,d]))
<|> fmap SimplePseudoType parseType
pseudoTypeDef :: TypePattern -> Kind -> [Token] -> GenParser Char st TypeItem
return (AliasType t k p (map tokPos (l++[c])))
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
component :: GenParser Char st [Components]
component = do { (is, cs) <- uninstOpName `separatedBy` commaT
<|> return [NoSelector (mkType(head is))]
where mkType (Id is cs ps) =
let ts = map TypeToken is
t = if length ts == 1 then head ts
else let qs = map mkType cs
q = BracketType Squares qs ps
tupleComponent :: GenParser Char st Components
do (cs, ps) <- component `separatedBy` semiT
return (NestedComponents (concat cs) (toPos o ps c))
do (cs, ps) <- tupleComponent `separatedBy` commaT
return (NestedComponents cs (toPos o ps c))
compType :: [UninstOpName] -> [Token] -> GenParser Char st [Components]
compType is cs = do { c <- colT
; return (makeComps is (cs++[c]) Total t)
; return (makeComps is (cs++[c]) Partial t)
where makeComps [a] [b] k t = [Selector a k t Other (tokPos b)]
makeComps (a:r) (b:s) k t =
(Selector a k t Comma (tokPos b)):makeComps r s k t
makeComps _ _ _ _ = error "makeComps: empty selector list"
alternative :: GenParser Char st Alternative
alternative = do { s <- pluralKeyword sortS <|> pluralKeyword typeS
; (ts, cs) <- parseType `separatedBy` commaT
; return (Subtype ts (map tokPos (s:cs)))
; cs <- many tupleComponent
; return (Constructor i cs Partial [tokPos q])
<|> return (Constructor i cs Total [])
dataDef :: TypePattern -> Kind -> [Token] -> GenParser Char st TypeItem
(Annoted v _ _ b:as, ps) <- aAlternative `separatedBy` barT
let aa = Annoted v [] a b:as
qs = map tokPos (l++c:ps)
in do d <- asKey derivingS
return (Datatype (DatatypeDecl t k aa (Just cl)
<|> return (Datatype (DatatypeDecl t k aa Nothing qs))
where aAlternative = bind (\ a an -> Annoted a [] [] an)
dataItem :: GenParser Char st DatatypeDecl
dataItem = do t <- typePattern
Datatype d <- dataDef t k [c]
<|> do Datatype d <- dataDef t nullKind []
dataItems :: GenParser Char st BasicItem
dataItems = itemList typeS dataItem FreeDatatype
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
subClassDecl :: ([ClassName], [Token]) -> GenParser Char st ClassDecl
; return (SubclassDecl l s (map tokPos (p++[t])))
isoClassDecl :: ClassName -> GenParser Char st ClassDecl
return (DownsetDefn s i t (map tokPos [e,o,d,j,l,p]))
return (ClassDefn s c [tokPos e])
classDecl :: GenParser Char st ClassDecl
classDecl = do (is, cs) <- className `separatedBy` commaT
return (ClassDecl is (map tokPos cs))
return (ClassDecl is (map tokPos cs))
classItem :: GenParser Char st ClassItem
classItem = do c <- classDecl
; i:is <- many1 basicItems
; return (ClassItem c ((Annoted i [] a [])
: map (\x -> Annoted x [] [] []) is)
return (ClassItem c [] [])
classItemList :: [Token] -> Instance -> GenParser Char st BasicItem
do { (vs, ts, ans) <- itemAux (annoParser classItem)
; let r = zipWith appendAnno vs ans
in return (ClassItems k r (map tokPos (ps++ts)))
classItems :: GenParser Char st BasicItem
classItems = do p <- pluralKeyword classS
do q <- pluralKeyword instanceS
classItemList [p, q] Instance
<|> classItemList [p] Plain
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
typeVarDeclSeq :: GenParser Char st TypeVarDecls
(ts, cs) <- typeVarDecls `separatedBy` semiT
return (TypeVarDecls (concat ts) (toPos o cs c))
opName :: GenParser Char st OpName
opName = do i@(Id is cs ps) <- uninstOpName
if isPlace $ last is then return (OpName i [])
do ts <- many typeVarDeclSeq
return (OpName (Id (is++u) cs ps) ts)
opAttr :: GenParser Char st OpAttr
opAttr = do a <- asKey assocS
return (BinOpAttr Assoc (tokPos a))
return (BinOpAttr Comm (tokPos a))
return (BinOpAttr Idem (tokPos a))
return (UnitOpAttr t (tokPos a))
opDecl :: [OpName] -> [Token] -> GenParser Char st OpItem
opDecl os ps = do c <- colT
(as, cs) <- opAttr `separatedBy` commaT
return (OpDecl os t as (map tokPos (ps++[c,d]++cs)))
<|> return (OpDecl os t [] (map tokPos (ps++[c])))
opDeclOrDefn :: OpName -> GenParser Char st OpItem
(as, cs) <- opAttr `separatedBy` commaT
return (OpDecl [o] t as (map tokPos ([c,d]++cs)))
return (OpDefn o [] t Total f (toPos c [] e))
<|> return (OpDecl [o] t [] (map tokPos [c]))
do ps <- many1 (bracketParser patterns oParenT cParenT semiT
return (OpDefn o ps (SimpleTypeScheme t) p f (toPos c [] e))
return (OpDefn o [] (SimpleTypeScheme t) Partial f (toPos c [] e))
opItem :: GenParser Char st OpItem
opItem = do (os, ps) <- opName `separatedBy` commaT
opItems :: GenParser Char st SigItems
opItems = itemList opS opItem OpItems
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
predDecl :: [OpName] -> [Token] -> GenParser Char st PredItem
predDecl os ps = do c <- colT
return (PredDecl os t (map tokPos (ps++[c])))
predDefn :: OpName -> GenParser Char st PredItem
predDefn o = do ps <- many (bracketParser patterns oParenT cParenT semiT
return (PredDefn o ps (TermFormula f) [tokPos e])
predItem :: GenParser Char st PredItem
predItem = do (os, ps) <- opName `separatedBy` commaT
predItems :: GenParser Char st SigItems
predItems = itemList predS predItem PredItems
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
sigItems :: GenParser Char st SigItems
sigItems = sortItems <|> opItems <|> predItems <|> typeItems
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
generatedItems :: GenParser Char st BasicItem
generatedItems = do { g <- asKey generatedS
; do { FreeDatatype ds ps <- dataItems
; return (GenItems [Annoted (TypeItems Plain
[] (l_annos d) (r_annos d)) ds) ps)
; return (GenItems ((Annoted i [] a [])
: map (\x -> Annoted x [] [] []) is)
-----------------------------------------------------------------------------
-- generic var decls (after "vars")
-----------------------------------------------------------------------------
genVarItems :: GenParser Char st ([GenVarDecl], [Token])
; do { tryItemEnd startKeyword
do { (ws, ts) <- genVarItems
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
freeDatatype, progItems, axiomItems, forallItem, genVarItem, dotFormulae,
basicItems :: GenParser Char st BasicItem
freeDatatype = do { f <- asKey freeS
; FreeDatatype ds ps <- dataItems
; return (FreeDatatype ds (tokPos f : ps))
progItems = itemList programS (patternTermPair True True equalS) ProgItems
axiomItems = do { a <- pluralKeyword axiomS
; (fs, ps, ans) <- itemAux (fmap TermFormula term)
; return (AxiomItems [] (zipWith
(\ x y -> Annoted x [] [] y)
fs ans) (map tokPos (a:ps)))
forallItem = do { f <- forallT
; (vs, ps) <- genVarDecls `separatedBy` semiT
; AxiomItems [] fs ds <- dotFormulae
; return (AxiomItems (concat vs) fs
(map tokPos (f:ps) ++ ds))
genVarItem = do { v <- pluralKeyword varS
; (vs, ps) <- genVarItems
; return (GenVarItems vs (map tokPos (v:ps)))
dotFormulae = do { d <- dotT
; (fs, ds) <- aFormula `separatedBy` dotT
; let ps = map tokPos (d:ds) in
if null (r_annos(last fs)) then
{ Nothing -> return (AxiomItems [] fs ps)
; Just t -> return (AxiomItems []
(init fs ++ [appendAnno (last fs) an])
else return (AxiomItems [] fs ps)
where aFormula = bind appendAnno
(annoParser (fmap TermFormula term)) lineAnnos
basicItems = fmap SigItems sigItems
aBasicItems :: GenParser Char st (Annoted BasicItem)
aBasicItems = bind (\ x y -> Annoted y [] x []) annos basicItems
basicSpec :: GenParser Char st BasicSpec
basicSpec = (oBraceT >> cBraceT >> return (BasicSpec []))
fmap BasicSpec (many1 aBasicItems)