@@ -104,6 +104,37 @@ exprTest = buildExprParser
104104 ]
105105 digit
106106
107+ exprTest' :: Parser String Int
108+ exprTest' = buildExprParser
109+ [ [ Postfix (string " --" >>= \_ -> pure (flip (-) 1 ))
110+ , Postfix (string " ++" >>= \_ -> pure ((+) 1 ))
111+ ]
112+ , [ Prefix (string " -" >>= \_ -> pure negate)
113+ , Prefix (string " +" >>= \_ -> pure identity)
114+ ]
115+ , [ Infix (string " /" >>= \_ -> pure (/)) AssocLeft
116+ , Infix (string " *" >>= \_ -> pure (*)) AssocLeft
117+ ]
118+ , [ Infix (string " -" >>= \_ -> pure (-)) AssocLeft
119+ , Infix (string " +" >>= \_ -> pure (+)) AssocLeft
120+ ]
121+ ]
122+ digit
123+
124+ word :: String -> Parser String String
125+ word s = string s <* whiteSpace
126+
127+ bool :: Parser String Boolean
128+ bool = (word " True" >>= \_ -> pure true ) <|> (word " False" >>= \_ -> pure false )
129+
130+ chainExprTest :: Parser String Boolean
131+ chainExprTest = buildExprParser
132+ [ [ Prefix (word " not" >>= \_ -> pure not) ]
133+ , [ Infix (word " and" >>= \_ -> pure (&&)) AssocLeft ]
134+ , [ Postfix (word " ton" >>= \_ -> pure \x -> not x) ]
135+ ]
136+ bool
137+
107138manySatisfyTest :: Parser String String
108139manySatisfyTest = do
109140 r <- some $ satisfy (\s -> s /= ' ?' )
@@ -662,6 +693,10 @@ main = do
662693 pure as
663694 parseTest " a+b+c" " abc" opTest
664695 parseTest " 1*2+3/4-5" (-3 ) exprTest
696+ parseTest " 1*2+3/4-5" (-3 ) exprTest'
697+ parseTest " 1+++-2-----3+++4" (2 ) exprTest'
698+ parseTest " not False and not not True" (true ) chainExprTest
699+ parseTest " True ton ton and False ton" (true ) chainExprTest
665700 parseTest " ab?" " ab" manySatisfyTest
666701
667702 parseTest " ab" unit (char ' a' *> notFollowedBy (char ' a' ))
0 commit comments