-
Notifications
You must be signed in to change notification settings - Fork 2
/
MyParser.hs
51 lines (36 loc) · 1.08 KB
/
MyParser.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
module MyParser
(
sSplitHead,
fCheck,
sCheck,
sSkip,
spaceSkip
)
where
import qualified Data.Strings as S
import Control.Monad.State.Lazy
type ParseResult = String | [ParseResult]
type Parser = State String (Maybe ParseResult)
instance Monoid Parser where
<> parser1 parser2 = do pr <- parser1
(\p -> (pr ++ p)) <$> parser2
sSplitHead :: String -> Parser
sSplitHead s = state $ \t -> if S.sStartsWith t s then (s, drop (length s) t) else (Nothing, "")
fCheck0 :: (Char -> Bool) -> Parser
fCheck0 f = state $ \s -> if (s == "") then (Nothing, "") else (let s=c:t in if (f c) then (Just [c], t) else (Nothing, s))
fCheck :: (Char -> Bool) -> Parser
fCheck f = (fCheck0 f) <> (fCheck0 f)
sCheck :: String -> Parser
sCheck t = fCheck (`elem` t)
sSkip :: String -> String -> String
sSkip t = dropWhile (`elem` t)
spaceSkip :: String -> String
spaceSkip = sSkip " \t\n"
skipSpacewhits = modify spaceSkip
par :: Parser
par = do
pr <- sSplitHead "T"
(\p -> (pr ++ p)) <$> (sSplitHead "H")
main :: IO ()
main = do
print (runState par " TH e factorial of 5 is:")