-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ch24_AltParsing.hs
51 lines (41 loc) · 1.08 KB
/
Ch24_AltParsing.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
{-# LANGUAGE QuasiQuotes #-}
module Ch24_AltParsing where
import Text.Trifecta
import Text.RawString.QQ
import Control.Applicative
type NumberOrString = Either Integer String
a = "blah"
b = "123"
c = "123blah789"
(<<) :: Monad m => m a -> m b -> m a
ma << mb = do
a <- ma
_ <- mb
return a
-- Quasiquotation string (aka. template literal)
eitherOr :: String
eitherOr = [r|
123
abc
456
def
|]
parseNos :: Parser NumberOrString
parseNos = (Left <$> integer) <|> (Right <$> some letter)
parseNos' :: Parser [NumberOrString]
parseNos' = many $
skipMany (oneOf "\n ") >>
(Left <$> integer) <|>
(Right <$> some letter) <<
skipMany (oneOf "\n ")
main :: IO ()
main = do
print $ parseString (some letter) mempty a
print $ parseString integer mempty b
print $ parseString parseNos mempty a
print $ parseString parseNos mempty b
print $ parseString (many parseNos) mempty c
print $ parseString (some parseNos) mempty c
-- with quasi string
print $ parseString parseNos mempty eitherOr
print $ parseString parseNos' mempty eitherOr