never executed always true always false
1 module HelVM.HelMA.Automaton.ReadPExtra where
2
3 import HelVM.HelMA.Automaton.API.IOTypes
4
5 import HelVM.HelIO.Control.Safe
6
7 import Control.Type.Operator
8
9 import Data.Char
10
11 import Text.ParserCombinators.ReadP hiding (many)
12
13 runParser :: MonadSafe m => ReadP a -> Source -> m a
14 runParser parser source = fst . last <$> nonEmptyRunParser parser source
15
16 nonEmptyRunParser :: MonadSafe m => ReadP a -> Source -> m $ NonEmpty (a , String)
17 nonEmptyRunParser parser source = nonEmptyFromList ("Cannot parse source\n" <> source) $ listRunParser parser source
18
19 listRunParser :: ReadP a -> Source -> [(a , String)]
20 listRunParser parser = readP_to_S parser . toString
21
22 -- | Parsers
23
24 oneOf :: String -> ReadP Char
25 oneOf cs = satisfy (`elem` cs)
26
27 notChar :: Char -> ReadP Char
28 notChar c = satisfy (/= c)
29
30 anyChar :: ReadP Char
31 anyChar = satisfy $ const True
32
33 digit :: ReadP Char
34 digit = satisfy isDigit
35
36 letterAscii :: ReadP Char
37 letterAscii = satisfy isAlphaAscii
38
39 -- | Extra
40
41 isAlphaAscii :: Char -> Bool
42 isAlphaAscii c = isAsciiLower c || isAsciiUpper c
43
44 manyNonEmpty :: Alternative f => f a -> f $ NonEmpty a
45 manyNonEmpty p = liftA2 (:|) p (many p)