ParseRS.hs revision 3d3889e0cefcdce9b3f43c53aaa201943ac2e895
0901148a508a9c433851a650cd8eb52899d1222bLaszlo HordosModule : $Header$
0901148a508a9c433851a650cd8eb52899d1222bLaszlo HordosDescription : abstract syntax for Relational Schemes
0901148a508a9c433851a650cd8eb52899d1222bLaszlo HordosCopyright : Dominik Luecke, Uni Bremen 2008
0901148a508a9c433851a650cd8eb52899d1222bLaszlo HordosLicense : GPLv2 or higher, see LICENSE.txt or LIZENZ.txt
0901148a508a9c433851a650cd8eb52899d1222bLaszlo HordosMaintainer : luecke@informatik.uni-bremen.de
0901148a508a9c433851a650cd8eb52899d1222bLaszlo HordosStability : provisional
0901148a508a9c433851a650cd8eb52899d1222bLaszlo HordosPortability : portable
0901148a508a9c433851a650cd8eb52899d1222bLaszlo HordosParser for Relational Schemes
0901148a508a9c433851a650cd8eb52899d1222bLaszlo Hordos parseRSScheme
0901148a508a9c433851a650cd8eb52899d1222bLaszlo Hordosimport qualified Data.Set as Set
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo Hordosimport qualified Data.Map as Map
0901148a508a9c433851a650cd8eb52899d1222bLaszlo Hordos-- ^ parse a simple word not in 'rskeywords'
f5e7b140a6b831b5e1752ffabad257914de758e1Travis HallrsVarId :: [String] -> AParser st Token
9c4d05edc8e0887d3aad788027d46d5afedb3ee0Travis Hall tk <- pToken $ reserved (ks ++ rsKeyWords) scanAnyWords
f5e7b140a6b831b5e1752ffabad257914de758e1Travis HallparseRSScheme :: PrefixMap -> AParser st RSScheme
f5e7b140a6b831b5e1752ffabad257914de758e1Travis HallparseRSScheme _ =
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo Hordos pos1 <- getPos
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo Hordos tb <- parseRSTables
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo Hordos rl <- parseRSRelationships
9c4d05edc8e0887d3aad788027d46d5afedb3ee0Travis Hall pos2 <- getPos
f5e7b140a6b831b5e1752ffabad257914de758e1Travis Hall return $ RSScheme tb rl $ Range [pos1, pos2]
9c4d05edc8e0887d3aad788027d46d5afedb3ee0Travis Hall-- ^ Parser for set of relationships
f5e7b140a6b831b5e1752ffabad257914de758e1Travis HallparseRSRelationships :: AParser st RSRelationships
0901148a508a9c433851a650cd8eb52899d1222bLaszlo HordosparseRSRelationships =
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo Hordos k <- try $ asKey rsRelationships
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo Hordos r <- many parseRSRel
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo Hordos return $ RSRelationships r $ catRange [k]
9c4d05edc8e0887d3aad788027d46d5afedb3ee0Travis Hall return (RSRelationships [] nullRange)
9c4d05edc8e0887d3aad788027d46d5afedb3ee0Travis Hall-- ^ Parser for a single relationship
9c4d05edc8e0887d3aad788027d46d5afedb3ee0Travis HallparseRSRel :: AParser st (Annoted RSRel)
f5e7b140a6b831b5e1752ffabad257914de758e1Travis Hall la <- getAnnos
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo Hordos l <- parseRSQualId
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo Hordos k <- asKey rsArrow
f5e7b140a6b831b5e1752ffabad257914de758e1Travis Hall r <- parseRSQualId
9c4d05edc8e0887d3aad788027d46d5afedb3ee0Travis Hall c <- parseRSRelTypes
9c4d05edc8e0887d3aad788027d46d5afedb3ee0Travis Hall ra <- getAnnos
9c4d05edc8e0887d3aad788027d46d5afedb3ee0Travis Hall return $ makeAnnoted la ra (RSRel l r c $ tokPos k)
9c4d05edc8e0887d3aad788027d46d5afedb3ee0Travis Hall-- ^ Parser for qualified Ids...
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo HordosparseRSQualId :: AParser st [RSQualId]
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo HordosparseRSQualId =
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo Hordos tn <- rsVarId []
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo Hordos cn <- sepBy1 (rsVarId []) commaT
9c4d05edc8e0887d3aad788027d46d5afedb3ee0Travis Hall let out = map
9c4d05edc8e0887d3aad788027d46d5afedb3ee0Travis Hall (\ x -> RSQualId (simpleIdToId tn) (simpleIdToId x) $ tokPos x)
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo Hordos-- ^ parser for collection of tables
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo HordosparseRSTables :: AParser st RSTables
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo HordosparseRSTables =
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo Hordos try $ asKey rsTables
f5e7b140a6b831b5e1752ffabad257914de758e1Travis Hall t <- many parseRSTable
9c4d05edc8e0887d3aad788027d46d5afedb3ee0Travis Hall ot <- setConv t
9c4d05edc8e0887d3aad788027d46d5afedb3ee0Travis Hall return RSTables
f5e7b140a6b831b5e1752ffabad257914de758e1Travis Hall return RSTables
9c4d05edc8e0887d3aad788027d46d5afedb3ee0Travis HallsetCol :: (Monad m) => [RSColumn] -> m (Set.Set RSColumn)
9c4d05edc8e0887d3aad788027d46d5afedb3ee0Travis Hall names = map c_name t
9c4d05edc8e0887d3aad788027d46d5afedb3ee0Travis Hall foldM_ (flip insertUnique) Set.empty names
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo HordossetConv :: (Monad m) => [RSTable] -> m (Set.Set RSTable)
9c4d05edc8e0887d3aad788027d46d5afedb3ee0Travis Hall names = map t_name t
f5e7b140a6b831b5e1752ffabad257914de758e1Travis Hall foldM_ (flip insertUnique) Set.empty names
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo HordosinsertUnique :: (Monad m) => Id -> Set.Set Id -> m (Set.Set Id)
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo HordosinsertUnique t s =
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo Hordos if t `Set.notMember` s then return $ Set.insert t s
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo Hordos else fail $ "Duplicate definition of " ++ show t
f5e7b140a6b831b5e1752ffabad257914de758e1Travis Hall-- ^ parser for table
f5e7b140a6b831b5e1752ffabad257914de758e1Travis HallparseRSTable :: AParser st RSTable
9c4d05edc8e0887d3aad788027d46d5afedb3ee0Travis HallparseRSTable =
9c4d05edc8e0887d3aad788027d46d5afedb3ee0Travis Hall la <- getAnnos
9c4d05edc8e0887d3aad788027d46d5afedb3ee0Travis Hall tid <- rsVarId []
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo Hordos cl <- sepBy1 parseRSColumn commaT
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo Hordos let ccl = concat cl
9c4d05edc8e0887d3aad788027d46d5afedb3ee0Travis Hall ra <- getAnnos
9c4d05edc8e0887d3aad788027d46d5afedb3ee0Travis Hall return RSTable
9c4d05edc8e0887d3aad788027d46d5afedb3ee0Travis Hall t_name = simpleIdToId tid
9c4d05edc8e0887d3aad788027d46d5afedb3ee0Travis Hall , columns = ccl
0901148a508a9c433851a650cd8eb52899d1222bLaszlo Hordos , rsannos = la ++ ra
c423abe89cd6f03ff5859228bd76976ba7279e27omebold $ map (\ x -> (c_name x, c_data x))
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo Hordos $ filter c_key ccl
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo HordosparseEntry :: AParser st (Token, Bool)
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo Hordos iK <- look4Key
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo Hordos iid <- rsVarId []
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo Hordos return (iid, iK)
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo HordosparseRSColumn :: AParser st [RSColumn]
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo HordosparseRSColumn =
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo Hordos iid <- sepBy1 parseEntry commaT
c423abe89cd6f03ff5859228bd76976ba7279e27omebold dt <- parseRSDatatypes
c423abe89cd6f03ff5859228bd76976ba7279e27omebold return $ map (\ (x, y) -> RSColumn (simpleIdToId x) dt y) iid
c423abe89cd6f03ff5859228bd76976ba7279e27omeboldlook4Key :: AParser st Bool
c423abe89cd6f03ff5859228bd76976ba7279e27omebold return True
c423abe89cd6f03ff5859228bd76976ba7279e27omebold return False
0901148a508a9c433851a650cd8eb52899d1222bLaszlo HordostestParse :: GenParser tok (AnnoState ()) a
0901148a508a9c433851a650cd8eb52899d1222bLaszlo Hordos -> Either Text.ParserCombinators.Parsec.Error.ParseError a
0901148a508a9c433851a650cd8eb52899d1222bLaszlo HordostestParse par = runParser par (emptyAnnos ()) ""
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo HordoslongTest :: IO (Either ParseError RSScheme)
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo Hordos return $ testParse (parseRSScheme Map.empty) x
0901148a508a9c433851a650cd8eb52899d1222bLaszlo Hordos-- boring parser for rel types
0901148a508a9c433851a650cd8eb52899d1222bLaszlo HordosparseRSRelTypes :: AParser st RSRelType
0901148a508a9c433851a650cd8eb52899d1222bLaszlo HordosparseRSRelTypes =
47196c962e3caf7cdf7ea5d00ccdefc9f208bdceLaszlo Hordos return RSone_to_one
0901148a508a9c433851a650cd8eb52899d1222bLaszlo Hordos return RSone_to_many
0901148a508a9c433851a650cd8eb52899d1222bLaszlo Hordos return RSmany_to_one
9c4d05edc8e0887d3aad788027d46d5afedb3ee0Travis Hall return RSmany_to_many