ParseTerm.hs revision f7663514e02f6095198371a64e574c50e6ec857a
4b0a4c7dea0f67a233dcc42ce9bb18d36de109aeChristian MaederModule : $Header$
4b0a4c7dea0f67a233dcc42ce9bb18d36de109aeChristian MaederDescription : parser for HasCASL kinds, types, terms, patterns and equations
75a6279dbae159d018ef812185416cf6df386c10Till MossakowskiCopyright : (c) Christian Maeder and Uni Bremen 2002-2005
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian MaederLicense : similar to LGPL, see HetCATS/LICENSE.txt or LIZENZ.txt
4b0a4c7dea0f67a233dcc42ce9bb18d36de109aeChristian MaederMaintainer : Christian.Maeder@dfki.de
4b0a4c7dea0f67a233dcc42ce9bb18d36de109aeChristian MaederStability : provisional
4b0a4c7dea0f67a233dcc42ce9bb18d36de109aeChristian MaederPortability : portable
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian Maederparser for HasCASL kinds, types, terms, patterns and equations
c18e9c3c6d5039618f1f2c05526ece84c7794ea3Christian Maeder-- * key sign tokens
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder-- | a colon not followed by a question mark
c00adad2e9459b422dee09e3a2bddba66b433bb7Christian MaedercolT :: AParser st Token
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian MaedercolT = asKey colonS
c00adad2e9459b422dee09e3a2bddba66b433bb7Christian Maeder-- * parser for bracketed lists
fd5d3885a092ac0727fa2436cdfc3b248318ebd8Christian Maeder-- | a generic bracket parser
ea03c5d09694b4a966fbd19d46cfa5772648d95fChristian MaederbracketParser :: AParser st a -> AParser st Token -> AParser st Token
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder -> AParser st Token -> ([a] -> Range -> b) -> AParser st b
cf3232cec840a6945667bdb06f5b47b22243bc8fChristian MaederbracketParser parser op cl sep k = do
df638d53c2d5fe5e80b943a58609c8936848ed82Christian Maeder (ts, ps) <- option ([], []) $ separatedBy parser sep
23f8d286586ff38a9e73052b2c7c04c62c5c638fChristian Maeder return $ k ts $ toPos o ps c
df638d53c2d5fe5e80b943a58609c8936848ed82Christian Maeder-- | parser for square brackets
23f8d286586ff38a9e73052b2c7c04c62c5c638fChristian MaedermkBrackets :: AParser st a -> ([a] -> Range -> b) -> AParser st b
23f8d286586ff38a9e73052b2c7c04c62c5c638fChristian MaedermkBrackets p c = bracketParser p oBracketT cBracketT anComma c
836e72a3c413366ba9801726f3b249c7791cb9caChristian Maeder-- | parser for braces
0f894093b1d435fd222074706d7fdadb9725cfdfChristian MaedermkBraces :: AParser st a -> ([a] -> Range -> b) -> AParser st b
df638d53c2d5fe5e80b943a58609c8936848ed82Christian MaedermkBraces p c = bracketParser p oBraceT cBraceT anComma c
cf3232cec840a6945667bdb06f5b47b22243bc8fChristian Maeder-- | parse a simple class name or the type universe as kind
cf3232cec840a6945667bdb06f5b47b22243bc8fChristian MaederparseClassId :: AParser st Kind
cf3232cec840a6945667bdb06f5b47b22243bc8fChristian MaederparseClassId = fmap ClassKind classId
cf3232cec840a6945667bdb06f5b47b22243bc8fChristian Maeder-- | do 'parseClassId' or a kind in parenthessis
e8a2ca3a7b3e9a19ef03b6b1c0b5d03dbad6463cChristian MaederparseSimpleKind :: AParser st Kind
e8a2ca3a7b3e9a19ef03b6b1c0b5d03dbad6463cChristian MaederparseSimpleKind = parseClassId <|> (oParenT >> kind << cParenT)
e8a2ca3a7b3e9a19ef03b6b1c0b5d03dbad6463cChristian Maeder-- | do 'parseSimpleKind' and check for an optional 'Variance'
e8a2ca3a7b3e9a19ef03b6b1c0b5d03dbad6463cChristian MaederparseExtKind :: AParser st (Variance, Kind)
cf3232cec840a6945667bdb06f5b47b22243bc8fChristian MaederparseExtKind = bind (,) variance parseSimpleKind
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian Maeder-- | parse a (right associative) function kind for a given argument kind
f71a8dcf94fd9eb3c9800e16dcdc5e5ff74e5c22Christian MaederarrowKind :: (Variance, Kind) -> AParser st Kind
7c57322afb6342e5cc8b1fdc96050b707407fc61Christian MaederarrowKind (v, k) = do
fd5d3885a092ac0727fa2436cdfc3b248318ebd8Christian Maeder a <- asKey funS
fd5d3885a092ac0727fa2436cdfc3b248318ebd8Christian Maeder return $ FunKind v k k2 $ tokPos a
628310b42327ad76ce471caf0dde6563d6fa6307Christian Maeder-- | parse a function kind but reject an extended kind
628310b42327ad76ce471caf0dde6563d6fa6307Christian Maederkind :: AParser st Kind
628310b42327ad76ce471caf0dde6563d6fa6307Christian Maeder k1@(v, k) <- parseExtKind
628310b42327ad76ce471caf0dde6563d6fa6307Christian Maeder arrowKind k1 <|> case v of
31242f7541fd6ef179e4eb5be7522ddf54ae397bChristian Maeder InVar -> return k
31242f7541fd6ef179e4eb5be7522ddf54ae397bChristian Maeder _ -> unexpected "variance of kind"
31242f7541fd6ef179e4eb5be7522ddf54ae397bChristian Maeder-- | parse a function kind but accept an extended kind
76fa667489c5e0868ac68de9f0253ac10f73d0b5Christian MaederextKind :: AParser st (Variance, Kind)
31242f7541fd6ef179e4eb5be7522ddf54ae397bChristian Maeder k1 <- parseExtKind
76fa667489c5e0868ac68de9f0253ac10f73d0b5Christian Maeder do k <- arrowKind k1
628310b42327ad76ce471caf0dde6563d6fa6307Christian Maeder return (InVar, k)
628310b42327ad76ce471caf0dde6563d6fa6307Christian Maeder <|> return k1
e8a2ca3a7b3e9a19ef03b6b1c0b5d03dbad6463cChristian Maeder-- * type variables
e8a2ca3a7b3e9a19ef03b6b1c0b5d03dbad6463cChristian Maedervariance :: AParser st Variance
628310b42327ad76ce471caf0dde6563d6fa6307Christian Maedervariance = let l = [CoVar, ContraVar] in
ffd01020a4f35f434b912844ad6e0d6918fadffdChristian Maeder choice (map ( \ v -> asKey (show v) >> return v) l) <|> return InVar
ffd01020a4f35f434b912844ad6e0d6918fadffdChristian Maeder-- a (simple) type variable with a 'Variance'
fd5d3885a092ac0727fa2436cdfc3b248318ebd8Christian MaederextVar :: AParser st Id -> AParser st (Id, Variance)
c00adad2e9459b422dee09e3a2bddba66b433bb7Christian MaederextVar vp = bind (,) vp variance
c00adad2e9459b422dee09e3a2bddba66b433bb7Christian Maeder-- several 'extVar' with a 'Kind'
502ed7ed7fecd10b6d0c83cdd48a244ec45e840aChristian MaedertypeVars :: AParser st [TypeArg]
ac07a6558423dae7adc488ed9092cd8e9450a29dChristian Maeder (ts, ps) <- extVar typeVar `separatedBy` anComma
c00adad2e9459b422dee09e3a2bddba66b433bb7Christian Maeder typeKind ts ps
c00adad2e9459b422dee09e3a2bddba66b433bb7Christian MaederallIsInVar :: [(Id, Variance)] -> Bool
dc6b48bb46df8e56da3491c98476e6da0d1d5d1dChristian MaederallIsInVar = all ( \ (_, v) -> case v of
b49276c9f50038e0bd499ad49f7bd6444566a834Christian Maeder InVar -> True
c00adad2e9459b422dee09e3a2bddba66b433bb7Christian Maeder-- 'parseType' a 'Downset' starting with 'lessT'
dc6b48bb46df8e56da3491c98476e6da0d1d5d1dChristian MaedertypeKind :: [(Id, Variance)] -> [Token]
c00adad2e9459b422dee09e3a2bddba66b433bb7Christian Maeder -> AParser st [TypeArg]
53301de22afd7190981b363b57c48df86fcb50f7Christian MaedertypeKind vs ps = do
53301de22afd7190981b363b57c48df86fcb50f7Christian Maeder if allIsInVar vs then do
53301de22afd7190981b363b57c48df86fcb50f7Christian Maeder (v, k) <- extKind
53301de22afd7190981b363b57c48df86fcb50f7Christian Maeder return $ makeTypeArgs vs ps v (VarKind k) $ tokPos c
<|> placeS <?> "id/literal")