Skip to content

added takeTok, when and get to Token module #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions docs/Module.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Module Text.Parsing.Parser



#### `ParseError`

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

## Module Text.Parsing.Parser.Combinators



#### `(<?>)`

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

## Module Text.Parsing.Parser.Expr



#### `Assoc`

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

## Module Text.Parsing.Parser.String



#### `eof`

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

## Module Text.Parsing.Parser.Token



#### `token`

``` purescript
token :: forall m a. (Monad m) => ParserT [a] m a
```


#### `when`

``` purescript
when :: forall m a. (Monad m) => (a -> Boolean) -> ParserT [a] m a
```


#### `match`

``` purescript
match :: forall a m. (Monad m, Eq a) => a -> ParserT [a] m a
```


#### `LanguageDef`

``` purescript
Expand Down
37 changes: 37 additions & 0 deletions examples/Test.purs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Text.Parsing.Parser
import Text.Parsing.Parser.Combinators
import Text.Parsing.Parser.Expr
import Text.Parsing.Parser.String
import Text.Parsing.Parser.Token

parens :: forall m a. (Monad m) => ParserT String m a -> ParserT String m a
parens = between (string "(") (string ")")
Expand Down Expand Up @@ -58,6 +59,23 @@ manySatisfyTest = do
string "?"
return r

data TestToken = A | B

instance showTestTokens :: Show TestToken where
show A = "A"
show B = "B"

instance testTokensEq :: Eq TestToken where
(==) A A = true
(==) B B = true
(==) _ _ = false
(/=) a b = not $ a == b

isA :: TestToken -> Boolean
isA A = true
isA _ = false


main = do
parseTest nested "(((a)))"
parseTest (many (string "a")) "aaa"
Expand All @@ -72,3 +90,22 @@ main = do
parseTest opTest "a+b+c"
parseTest exprTest "1*2+3/4-5"
parseTest manySatisfyTest "ab?"

print "should be A"
parseTest token [A, B]
print "should be B"
parseTest token [B, A]

print "should be A"
parseTest (when isA) [A, B]
print "should fail"
parseTest (when isA) [B, B]

print "should be A"
parseTest (match A) [A]
print "should be B"
parseTest (match B) [B]
print "should be A"
parseTest (match A) [A, B]
print "should fail"
parseTest (match B) [A, B]
21 changes: 20 additions & 1 deletion src/Text/Parsing/Parser/Token.purs
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
module Text.Parsing.Parser.Token where

import Data.String
import Data.Either

import Control.Monad.State.Class
import Control.Monad.State.Class hiding (get)
import Control.Monad.Error
import Control.Monad.Error.Class
import Control.MonadPlus

import Text.Parsing.Parser
import Text.Parsing.Parser.String
import Text.Parsing.Parser.Combinators

token :: forall m a. (Monad m) => ParserT [a] m a
token = ParserT $ \s ->
return $ case s of
x:xs -> { consumed: true, input: xs, result: Right x }
_ -> { consumed: false, input: s, result: Left (strMsg "expected token, met EOF") }

when :: forall m a. (Monad m) => (a -> Boolean) -> ParserT [a] m a
when f = try $ do
a <- token
guard $ f a
return a

match :: forall a m. (Monad m, Eq a) => a -> ParserT [a] m a
match token = when ((==) token)


type LanguageDef s m = {
commentStart :: String,
commentEnd :: String,
Expand Down