Skip to content

Commit c06eab2

Browse files
committed
Merge pull request #12 from cryogenian/master
added takeTok, when and get to Token module
2 parents 4ff8e97 + ae38a8b commit c06eab2

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

docs/Module.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Module Text.Parsing.Parser
44

5+
6+
57
#### `ParseError`
68

79
``` purescript
@@ -161,6 +163,8 @@ fail :: forall m s a. (Monad m) => String -> ParserT s m a
161163

162164
## Module Text.Parsing.Parser.Combinators
163165

166+
167+
164168
#### `(<?>)`
165169

166170
``` purescript
@@ -339,6 +343,8 @@ many1Till :: forall s a m e. (Monad m) => ParserT s m a -> ParserT s m e -> Pars
339343

340344
## Module Text.Parsing.Parser.Expr
341345

346+
347+
342348
#### `Assoc`
343349

344350
``` purescript
@@ -432,6 +438,8 @@ buildExprParser :: forall m s a. (Monad m) => OperatorTable m s a -> ParserT s m
432438

433439
## Module Text.Parsing.Parser.String
434440

441+
442+
435443
#### `eof`
436444

437445
``` purescript
@@ -491,6 +499,29 @@ noneOf :: forall s m a. (Monad m) => [String] -> ParserT String m String
491499

492500
## Module Text.Parsing.Parser.Token
493501

502+
503+
504+
#### `token`
505+
506+
``` purescript
507+
token :: forall m a. (Monad m) => ParserT [a] m a
508+
```
509+
510+
511+
#### `when`
512+
513+
``` purescript
514+
when :: forall m a. (Monad m) => (a -> Boolean) -> ParserT [a] m a
515+
```
516+
517+
518+
#### `match`
519+
520+
``` purescript
521+
match :: forall a m. (Monad m, Eq a) => a -> ParserT [a] m a
522+
```
523+
524+
494525
#### `LanguageDef`
495526

496527
``` purescript

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: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
module Text.Parsing.Parser.Token where
22

33
import Data.String
4+
import Data.Either
45

5-
import Control.Monad.State.Class
6+
import Control.Monad.State.Class hiding (get)
7+
import Control.Monad.Error
68
import Control.Monad.Error.Class
9+
import Control.MonadPlus
710

811
import Text.Parsing.Parser
912
import Text.Parsing.Parser.String
1013
import Text.Parsing.Parser.Combinators
1114

15+
token :: forall m a. (Monad m) => ParserT [a] m a
16+
token = ParserT $ \s ->
17+
return $ case s of
18+
x:xs -> { consumed: true, input: xs, result: Right x }
19+
_ -> { consumed: false, input: s, result: Left (strMsg "expected token, met EOF") }
20+
21+
when :: forall m a. (Monad m) => (a -> Boolean) -> ParserT [a] m a
22+
when f = try $ do
23+
a <- token
24+
guard $ f a
25+
return a
26+
27+
match :: forall a m. (Monad m, Eq a) => a -> ParserT [a] m a
28+
match token = when ((==) token)
29+
30+
1231
type LanguageDef s m = {
1332
commentStart :: String,
1433
commentEnd :: String,

0 commit comments

Comments
 (0)