Skip to content

Commit 5ea6386

Browse files
committed
renaming, examples added
1 parent 2bdba0d commit 5ea6386

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

examples/Test.purs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import Text.Parsing.Parser
1616
import Text.Parsing.Parser.Combinators
1717
import Text.Parsing.Parser.Expr
1818
import Text.Parsing.Parser.String
19+
import Text.Parsing.Parser.Token
1920

2021
parens :: forall m a. (Monad m) => ParserT String m a -> ParserT String m a
2122
parens = between (string "(") (string ")")
@@ -58,6 +59,23 @@ manySatisfyTest = do
5859
string "?"
5960
return r
6061

62+
data TestToken = A | B
63+
64+
instance showTestTokens :: Show TestToken where
65+
show A = "A"
66+
show B = "B"
67+
68+
instance testTokensEq :: Eq TestToken where
69+
(==) A A = true
70+
(==) B B = true
71+
(==) _ _ = false
72+
(/=) a b = not $ a == b
73+
74+
isA :: TestToken -> Boolean
75+
isA A = true
76+
isA _ = false
77+
78+
6179
main = do
6280
parseTest nested "(((a)))"
6381
parseTest (many (string "a")) "aaa"
@@ -72,3 +90,22 @@ main = do
7290
parseTest opTest "a+b+c"
7391
parseTest exprTest "1*2+3/4-5"
7492
parseTest manySatisfyTest "ab?"
93+
94+
print "should be A"
95+
parseTest token [A, B]
96+
print "should be B"
97+
parseTest token [B, A]
98+
99+
print "should be A"
100+
parseTest (when isA) [A, B]
101+
print "should fail"
102+
parseTest (when isA) [B, B]
103+
104+
print "should be A"
105+
parseTest (match A) [A]
106+
print "should be B"
107+
parseTest (match B) [B]
108+
print "should be A"
109+
parseTest (match A) [A, B]
110+
print "should fail"
111+
parseTest (match B) [A, B]

src/Text/Parsing/Parser/Token.purs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,28 @@ import Data.String
44
import Data.Either
55

66
import Control.Monad.State.Class hiding (get)
7+
import Control.Monad.Error
78
import Control.Monad.Error.Class
9+
import Control.MonadPlus
810

911
import Text.Parsing.Parser
1012
import Text.Parsing.Parser.String
1113
import Text.Parsing.Parser.Combinators
1214

13-
takeTok :: forall m a. (Monad m) => ParserT [a] m a
14-
takeTok = ParserT $ \s ->
15+
token :: forall m a. (Monad m) => ParserT [a] m a
16+
token = ParserT $ \s ->
1517
return $ case s of
16-
x:xs -> {consumed: true,
17-
input: xs,
18-
result: Right x}
19-
_ -> {consumed: false,
20-
input: s,
21-
result: Left (ParseError {message: "there is nothing in 'takeTok'"})}
18+
x:xs -> { consumed: true, input: xs, result: Right x }
19+
_ -> { consumed: false, input: s, result: Left (strMsg "expected token, met EOF") }
2220

2321
when :: forall m a. (Monad m) => (a -> Boolean) -> ParserT [a] m a
2422
when f = try $ do
25-
a <- takeTok
26-
if f a then return a
27-
else fail "token doesn't satisfy when test"
23+
a <- token
24+
guard $ f a
25+
return a
2826

29-
get :: forall a m. (Monad m, Eq a) => a -> ParserT [a] m a
30-
get token = when ((==) token)
27+
match :: forall a m. (Monad m, Eq a) => a -> ParserT [a] m a
28+
match token = when ((==) token)
3129

3230

3331
type LanguageDef s m = {

0 commit comments

Comments
 (0)