Skip to content

Commit

Permalink
Merge pull request #8 from joneshf/master
Browse files Browse the repository at this point in the history
Bunch of combinators and some string stuff.
  • Loading branch information
jdegoes committed Nov 8, 2014
2 parents 4957979 + afdd5f0 commit 8f2e625
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 11 deletions.
14 changes: 9 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
language: node_js
node_js:
- "0.10"
env:
- PATH=$HOME/bin:$PATH purescript_datadir=$HOME/.local/share/purescript
before_install:
- mkdir -p $HOME/bin
- mkdir -p $HOME/.local/share/purescript/prelude
install:
- "sudo apt-get install cabal-install"
- "cabal update"
- "cabal install Cabal cabal-install"
- "export PATH=~/.cabal/bin:$PATH"
- "cabal install purescript --force-reinstalls"
- wget https://github.com/purescript/purescript/releases/download/v0.5.7.1/linux64.tar.gz
- tar zxvf linux64.tar.gz
- cp purescript/{docgen,psc,psci,psc-make} $HOME/bin
- cp purescript/prelude.purs $purescript_datadir/prelude/prelude.purs
- "npm install bower grunt-cli -g"
- "npm install"
- "bower install"
Expand Down
25 changes: 23 additions & 2 deletions docs/Module.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@

## Module Text.Parsing.Parser.Combinators

### Type Class Instances

instance showParseError :: Show ParseError


### Values

(<?>) :: forall m s a. (Monad m) => ParserT s m a -> String -> ParserT s m a
Expand All @@ -81,11 +86,17 @@

endBy1 :: forall m s a sep. (Monad m) => ParserT s m a -> ParserT s m sep -> ParserT s m [a]

lookAhead :: forall s a m. (Monad m) => ParserT s m a -> ParserT s m a

many1Till :: forall s a m e. (Monad m) => ParserT s m a -> ParserT s m e -> ParserT s m [a]

manyTill :: forall s a m e. (Monad m) => ParserT s m a -> ParserT s m e -> ParserT s m [a]

option :: forall m s a. (Monad m) => a -> ParserT s m a -> ParserT s m a

optionMaybe :: forall m s a. (Functor m, Monad m) => ParserT s m a -> ParserT s m (Maybe a)

optional :: forall m s a. (Monad m) => ParserT s m a -> ParserT s m { }
optional :: forall m s a. (Monad m) => ParserT s m a -> ParserT s m Unit

sepBy :: forall m s a sep. (Monad m) => ParserT s m a -> ParserT s m sep -> ParserT s m [a]

Expand All @@ -95,6 +106,10 @@

sepEndBy1 :: forall m s a sep. (Monad m) => ParserT s m a -> ParserT s m sep -> ParserT s m [a]

skipMany :: forall s a m. (Monad m) => ParserT s m a -> ParserT s m Unit

skipMany1 :: forall s a m. (Monad m) => ParserT s m a -> ParserT s m Unit

try :: forall m s a. (Functor m) => ParserT s m a -> ParserT s m a


Expand Down Expand Up @@ -142,10 +157,16 @@

char :: forall m. (Monad m) => ParserT String m String

eof :: forall m. (Monad m) => ParserT String m { }
eof :: forall m. (Monad m) => ParserT String m Unit

noneOf :: forall s m a. (Monad m) => [String] -> ParserT String m String

oneOf :: forall s m a. (Monad m) => [String] -> ParserT String m String

satisfy :: forall m. (Monad m) => (String -> Boolean) -> ParserT String m String

skipSpaces :: forall m. (Monad m) => ParserT String m Unit

string :: forall m. (Monad m) => String -> ParserT String m String

whiteSpace :: forall m. (Monad m) => ParserT String m String
Expand Down
3 changes: 3 additions & 0 deletions src/Text/Parsing/Parser.purs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ instance errorParseError :: Error ParseError where
noMsg = ParseError { message: "" }
strMsg msg = ParseError { message: msg }

instance showParseError :: Show ParseError where
show (ParseError msg) = "ParseError { message: " ++ msg.message ++ " }"

newtype ParserT s m a = ParserT (s -> m { input :: s, result :: Either ParseError a, consumed :: Boolean })

unParserT :: forall m s a. ParserT s m a -> s -> m { input :: s, result :: Either ParseError a, consumed :: Boolean }
Expand Down
35 changes: 33 additions & 2 deletions src/Text/Parsing/Parser/Combinators.purs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ between open close p = do
option :: forall m s a. (Monad m) => a -> ParserT s m a -> ParserT s m a
option a p = p <|> return a

optional :: forall m s a. (Monad m) => ParserT s m a -> ParserT s m {}
optional :: forall m s a. (Monad m) => ParserT s m a -> ParserT s m Unit
optional p = (do p
return {}) <|> return {}
return unit) <|> return unit

optionMaybe :: forall m s a. (Functor m, Monad m) => ParserT s m a -> ParserT s m (Maybe a)
optionMaybe p = option Nothing (Just <$> p)
Expand Down Expand Up @@ -106,3 +106,34 @@ choice [] = fail "Nothing to parse"
choice [x] = x
choice (x:xs) = x <|> choice xs

skipMany :: forall s a m. (Monad m) => ParserT s m a -> ParserT s m Unit
skipMany p = skipMany1 p <|> return unit

skipMany1 :: forall s a m. (Monad m) => ParserT s m a -> ParserT s m Unit
skipMany1 p = do
x <- p
xs <- skipMany p
return unit

lookAhead :: forall s a m. (Monad m) => ParserT s m a -> ParserT s m a
lookAhead (ParserT p) = ParserT \s -> do
state <- p s
return state{input = s, consumed = false}

manyTill :: forall s a m e. (Monad m) => ParserT s m a -> ParserT s m e -> ParserT s m [a]
manyTill p end = scan
where
scan = (do
end
return [])
<|> (do
x <- p
xs <- scan
return (x:xs))

many1Till :: forall s a m e. (Monad m) => ParserT s m a -> ParserT s m e -> ParserT s m [a]
many1Till p end = do
x <- p
xs <- manyTill p end
return (x:xs)

14 changes: 12 additions & 2 deletions src/Text/Parsing/Parser/String.purs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import Control.Monad.State.Class
import Text.Parsing.Parser
import Text.Parsing.Parser.Combinators

eof :: forall m. (Monad m) => ParserT String m {}
eof :: forall m. (Monad m) => ParserT String m Unit
eof = ParserT $ \s ->
return $ case s of
"" -> { consumed: false, input: s, result: Right {} }
"" -> { consumed: false, input: s, result: Right unit }
_ -> { consumed: false, input: s, result: Left (strMsg "Expected EOF") }

string :: forall m. (Monad m) => String -> ParserT String m String
Expand All @@ -45,3 +45,13 @@ whiteSpace = do
list <- many $ string "\n" <|> string "\r" <|> string " " <|> string "\t"
return $ foldMap id list

skipSpaces :: forall m. (Monad m) => ParserT String m Unit
skipSpaces = do
whiteSpace
return unit

oneOf :: forall s m a. (Monad m) => [String] -> ParserT String m String
oneOf ss = satisfy (flip elem ss)

noneOf :: forall s m a. (Monad m) => [String] -> ParserT String m String
noneOf ss = satisfy (flip notElem ss)

0 comments on commit 8f2e625

Please sign in to comment.