gdritter repos s-cargot / 07f31ac
Export individual parsers from HaskLike Getty Ritter 7 years ago
1 changed file(s) with 13 addition(s) and 9 deletion(s). Collapse all Expand all
55 HaskLikeAtom(..)
66 , haskLikeParser
77 , haskLikePrinter
8 -- * Individual Parsers
9 , parseHaskellString
10 , parseHaskellFloat
11 , parseHaskellInt
812 ) where
913
1014 #if !MIN_VERSION_base(4,8,0)
5155 instance IsString HaskLikeAtom where
5256 fromString = HSIdent . fromString
5357
54 pString :: Parser Text
55 pString = pack . catMaybes <$> between (char '"') (char '"') (many (val <|> esc))
58 parseHaskellString :: Parser Text
59 parseHaskellString = pack . catMaybes <$> between (char '"') (char '"') (many (val <|> esc))
5660 where val = Just <$> satisfy (\ c -> c /= '"' && c /= '\\' && c > '\026')
5761 esc = do _ <- char '\\'
5862 Nothing <$ (gap <|> char '&') <|>
8185 "\STX\ETX\EOT\ENQ\ACK\BEL\DLE\DC1\DC2\DC3\DC4\NAK" ++
8286 "\SYN\ETB\CAN\SUB\ESC\DEL")
8387
84 pFloat :: Parser Double
85 pFloat = do
88 parseHaskellFloat :: Parser Double
89 parseHaskellFloat = do
8690 n <- decNumber
8791 withDot n <|> noDot n
8892 where withDot n = do
105109 power :: Num a => Parser (a -> a)
106110 power = negate <$ char '-' <|> id <$ char '+' <|> return id
107111
108 pInt :: Parser Integer
109 pInt = do
112 parseHaskellInt :: Parser Integer
113 parseHaskellInt = do
110114 s <- power
111115 n <- pZeroNum <|> decNumber
112116 return (fromIntegral (s n))
121125
122126 pHaskLikeAtom :: Parser HaskLikeAtom
123127 pHaskLikeAtom
124 = HSFloat <$> (try pFloat <?> "float")
125 <|> HSInt <$> (try pInt <?> "integer")
126 <|> HSString <$> (pString <?> "string literal")
128 = HSFloat <$> (try parseHaskellFloat <?> "float")
129 <|> HSInt <$> (try parseHaskellInt <?> "integer")
130 <|> HSString <$> (parseHaskellString <?> "string literal")
127131 <|> HSIdent <$> (parseR5RSIdent <?> "token")
128132
129133 sHaskLikeAtom :: HaskLikeAtom -> Text