Created
July 7, 2011 22:19
-
-
Save MedeaMelana/1070676 to your computer and use it in GitHub Desktop.
101companies parser using Parsec
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- By Martijn van Steenbergen | |
| -- 7 July 3022, 23h00 | |
| -- GTTSE 2011, Braga, Portugal | |
| module Parser where | |
| import Company | |
| import Control.Applicative hiding (many, (<|>)) | |
| import Text.Parsec | |
| parseCompany :: String -> Either ParseError Company | |
| parseCompany = runP (spaces *> pCompany <* eof) () "input" | |
| type P = Parsec String () | |
| -- Primitives | |
| pString :: String -> P String | |
| pString s = string s <* spaces | |
| pLit :: P String | |
| pLit = string "\"" *> many (noneOf "\"") <* string "\"" <* spaces | |
| pFloat :: P Float | |
| pFloat = read <$> some (digit <|> char '.') <* spaces | |
| -- Company-related parsers | |
| pCompany :: P Company | |
| pCompany = (,) | |
| <$ pString "company" <*> pLit | |
| <* pString "{" <*> many pDepartment <* pString "}" | |
| pDepartment :: P Department | |
| pDepartment = Department | |
| <$ pString "department" <*> pLit | |
| <* pString "{" <*> pEmployee "manager" | |
| <*> many pSubUnit <* pString "}" | |
| pEmployee :: String -> P Manager | |
| pEmployee ty = Employee | |
| <$ pString ty <*> pLit | |
| <* pString "{" <* pString "address" <*> pLit | |
| <* pString "salary" <*> pFloat | |
| <*> optionMaybe (pString "mentor" *> pLit) <* pString "}" | |
| pSubUnit :: P SubUnit | |
| pSubUnit = EUnit <$> pEmployee "employee" | |
| <|> DUnit <$> pDepartment |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment