Export individual parsers from HaskLike
Getty Ritter
7 years ago
5 | 5 | HaskLikeAtom(..) |
6 | 6 | , haskLikeParser |
7 | 7 | , haskLikePrinter |
8 | -- * Individual Parsers | |
9 | , parseHaskellString | |
10 | , parseHaskellFloat | |
11 | , parseHaskellInt | |
8 | 12 | ) where |
9 | 13 | |
10 | 14 | #if !MIN_VERSION_base(4,8,0) |
51 | 55 | instance IsString HaskLikeAtom where |
52 | 56 | fromString = HSIdent . fromString |
53 | 57 | |
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)) | |
56 | 60 | where val = Just <$> satisfy (\ c -> c /= '"' && c /= '\\' && c > '\026') |
57 | 61 | esc = do _ <- char '\\' |
58 | 62 | Nothing <$ (gap <|> char '&') <|> |
81 | 85 | "\STX\ETX\EOT\ENQ\ACK\BEL\DLE\DC1\DC2\DC3\DC4\NAK" ++ |
82 | 86 | "\SYN\ETB\CAN\SUB\ESC\DEL") |
83 | 87 | |
84 | pFloat :: Parser Double | |
85 | pFloat = do | |
88 | parseHaskellFloat :: Parser Double | |
89 | parseHaskellFloat = do | |
86 | 90 | n <- decNumber |
87 | 91 | withDot n <|> noDot n |
88 | 92 | where withDot n = do |
105 | 109 | power :: Num a => Parser (a -> a) |
106 | 110 | power = negate <$ char '-' <|> id <$ char '+' <|> return id |
107 | 111 | |
108 | pInt :: Parser Integer | |
109 | pInt = do | |
112 | parseHaskellInt :: Parser Integer | |
113 | parseHaskellInt = do | |
110 | 114 | s <- power |
111 | 115 | n <- pZeroNum <|> decNumber |
112 | 116 | return (fromIntegral (s n)) |
121 | 125 | |
122 | 126 | pHaskLikeAtom :: Parser HaskLikeAtom |
123 | 127 | pHaskLikeAtom |
124 | = HSFloat <$> (try pFloat <?> "float") | |
125 | <|> HSInt <$> (try pInt <?> "integer") | |
126 |
|
|
128 | = HSFloat <$> (try parseHaskellFloat <?> "float") | |
129 | <|> HSInt <$> (try parseHaskellInt <?> "integer") | |
130 | <|> HSString <$> (parseHaskellString <?> "string literal") | |
127 | 131 | <|> HSIdent <$> (parseR5RSIdent <?> "token") |
128 | 132 | |
129 | 133 | sHaskLikeAtom :: HaskLikeAtom -> Text |