Skip to content

Compiler/0.12 #41

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 8 commits into from
May 31, 2018
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
8 changes: 7 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
language: node_js
dist: trusty
sudo: required
node_js: 6
node_js: stable
env:
- PATH=$HOME/purescript:$PATH
install:
- npm install -g bower
- TAG=$(wget -q -O - https://github.com/purescript/purescript/releases/latest --server-response --max-redirect 0 2>&1 | sed -n -e 's/.*Location:.*tag\///p')
- wget -O $HOME/purescript.tar.gz https://github.com/purescript/purescript/releases/download/$TAG/linux64.tar.gz
- tar -xvf $HOME/purescript.tar.gz -C $HOME/
- chmod a+x $HOME/purescript
- npm install
- bower install --production
script:
Expand Down
24 changes: 13 additions & 11 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@
"url": "git://github.com/paf31/purescript-string-parsers.git"
},
"dependencies": {
"purescript-control": "^3.0.0",
"purescript-arrays": "^4.0.0",
"purescript-maybe": "^3.0.0",
"purescript-strings": "^3.0.0",
"purescript-foldable-traversable": "^3.0.0",
"purescript-either": "^3.0.0",
"purescript-lists": "^4.0.0",
"purescript-tailrec": "^3.0.0"
"purescript-arrays": "^5.0.0",
"purescript-bifunctors": "^4.0.0",
"purescript-control": "^4.0.0",
"purescript-either": "^4.0.0",
"purescript-foldable-traversable": "^4.0.0",
"purescript-lists": "^5.0.0",
"purescript-maybe": "^4.0.0",
"purescript-prelude": "^4.0.0",
"purescript-strings": "^4.0.0",
"purescript-tailrec": "^4.0.0"
},
"devDependencies": {
"purescript-math": "^2.0.0",
"purescript-console": "^3.0.0",
"purescript-assert": "^3.0.0"
"purescript-math": "^2.1.1",
"purescript-console": "^4.1.0",
"purescript-assert": "^4.0.0"
}
}
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
"test": "pulp test"
},
"devDependencies": {
"pulp": "^11.0.0",
"purescript": "^0.11.1",
"purescript-psa": "^0.5.0",
"rimraf": "^2.5.0"
"pulp": "^12.2.0",
"purescript-psa": "^0.6.0",
"rimraf": "^2.6.2"
}
}
40 changes: 22 additions & 18 deletions src/Text/Parsing/StringParser/Combinators.purs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ import Prelude
import Control.Alt ((<|>))
import Control.Lazy (fix)
import Control.Monad.Rec.Class (Step(..), tailRecM)

import Data.Either (Either(..))
import Data.Foldable (class Foldable, foldl)
import Data.List (List(..), singleton, manyRec, reverse)
import Data.List (List(..), manyRec)
import Data.List.NonEmpty (NonEmptyList(..))
import Data.List.NonEmpty as NEL
import Data.Maybe (Maybe(..))

import Data.NonEmpty ((:|))
import Text.Parsing.StringParser (Parser(..), fail)

-- | Read ahead without consuming input.
Expand All @@ -52,8 +53,8 @@ many :: forall a. Parser a -> Parser (List a)
many = manyRec

-- | Match one or more times.
many1 :: forall a. Parser a -> Parser (List a)
many1 p = Cons <$> p <*> many p
many1 :: forall a. Parser a -> Parser (NonEmptyList a)
many1 p = cons' <$> p <*> many p

-- | Provide an error message in case of failure.
withError :: forall a. Parser a -> String -> Parser a
Expand All @@ -79,32 +80,32 @@ optionMaybe p = option Nothing (Just <$> p)

-- | Parse zero or more separated values.
sepBy :: forall a sep. Parser a -> Parser sep -> Parser (List a)
sepBy p sep = sepBy1 p sep <|> pure Nil
sepBy p sep = map NEL.toList (sepBy1 p sep) <|> pure Nil

-- | Parse one or more separated values.
sepBy1 :: forall a sep. Parser a -> Parser sep -> Parser (List a)
sepBy1 :: forall a sep. Parser a -> Parser sep -> Parser (NonEmptyList a)
sepBy1 p sep = do
a <- p
as <- many $ sep *> p
pure (Cons a as)
pure (cons' a as)

-- | Parse zero or more separated values, optionally ending with a separator.
sepEndBy :: forall a sep. Parser a -> Parser sep -> Parser (List a)
sepEndBy p sep = sepEndBy1 p sep <|> pure Nil
sepEndBy p sep = map NEL.toList (sepEndBy1 p sep) <|> pure Nil

-- | Parse one or more separated values, optionally ending with a separator.
sepEndBy1 :: forall a sep. Parser a -> Parser sep -> Parser (List a)
sepEndBy1 :: forall a sep. Parser a -> Parser sep -> Parser (NonEmptyList a)
sepEndBy1 p sep = do
a <- p
(do _ <- sep
as <- sepEndBy p sep
pure (Cons a as)) <|> pure (singleton a)
pure (cons' a as)) <|> pure (NEL.singleton a)

-- | Parse zero or more separated values, ending with a separator.
endBy1 :: forall a sep. Parser a -> Parser sep -> Parser (List a)
-- | Parse one or more separated values, ending with a separator.
endBy1 :: forall a sep. Parser a -> Parser sep -> Parser (NonEmptyList a)
endBy1 p sep = many1 $ p <* sep

-- | Parse one or more separated values, ending with a separator.
-- | Parse zero or more separated values, ending with a separator.
endBy :: forall a sep. Parser a -> Parser sep -> Parser (List a)
endBy p sep = many $ p <* sep

Expand Down Expand Up @@ -146,18 +147,21 @@ choice = foldl (<|>) (fail "Nothing to parse")

-- | Parse values until a terminator.
manyTill :: forall a end. Parser a -> Parser end -> Parser (List a)
manyTill p end = (end *> pure Nil) <|> many1Till p end
manyTill p end = (end *> pure Nil) <|> map NEL.toList (many1Till p end)

-- | Parse values until the terminator matches, requiring at least one match.
many1Till :: forall a end. Parser a -> Parser end -> Parser (List a)
many1Till :: forall a end. Parser a -> Parser end -> Parser (NonEmptyList a)
many1Till p end = do
x <- p
tailRecM inner (pure x)
where
ending acc = do
_ <- end
pure $ Done (reverse acc)
pure $ Done (NEL.reverse acc)
continue acc = do
c <- p
pure $ Loop (Cons c acc)
pure $ Loop (NEL.cons c acc)
inner acc = ending acc <|> continue acc

cons' :: forall a. a -> List a -> NonEmptyList a
cons' h t = NonEmptyList (h :| t)
4 changes: 2 additions & 2 deletions src/Text/Parsing/StringParser/Expr.purs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ buildExprParser operators simpleExpr =
prefixOp = choice accum.prefix <?> ""
postfixOp = choice accum.postfix <?> ""

postfixP = postfixOp <|> pure id
prefixP = prefixOp <|> pure id
postfixP = postfixOp <|> pure identity
prefixP = prefixOp <|> pure identity
in do
x <- termP prefixP term postfixP
rassocP x rassocOp prefixP term postfixP
Expand Down
12 changes: 7 additions & 5 deletions src/Text/Parsing/StringParser/String.purs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ module Text.Parsing.StringParser.String
import Prelude

import Control.Alt ((<|>))
import Data.Array ((..), uncons)
import Data.Array ((..))
import Data.Array.NonEmpty as NEA
import Data.Char (toCharCode)
import Data.Either (Either(..))
import Data.Foldable (class Foldable, foldMap, elem, notElem)
import Data.Maybe (Maybe(..), fromMaybe)
import Data.String (Pattern(..), charAt, drop, length, indexOf', singleton, stripPrefix)
import Data.Maybe (Maybe(..))
import Data.String (Pattern(..), drop, length, indexOf', stripPrefix)
import Data.String.CodeUnits (charAt, singleton)
import Data.String.Regex as Regex
import Data.String.Regex.Flags (noFlags)
import Text.Parsing.StringParser (Parser(..), ParseError(..), try, fail)
Expand Down Expand Up @@ -137,8 +139,8 @@ regex pat =
let
remainder = drop pos str
in
case uncons $ fromMaybe [] $ Regex.match r remainder of
Just { head: Just matched, tail: _ } ->
case NEA.head <$> Regex.match r remainder of
Just (Just matched) ->
Right { result: matched, suffix: { str, pos: pos + length matched } }
_ ->
Left { pos, error: ParseError "no match" }
28 changes: 14 additions & 14 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ module Test.Main where
import Prelude hiding (between)

import Control.Alt ((<|>))
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE)

import Data.Either (isLeft, isRight, Either(..))
import Data.Foldable (fold)
import Data.List (List(Nil), (:))
import Data.List.Lazy (take, repeat)
import Data.String (joinWith, singleton)
import Data.List.NonEmpty (NonEmptyList(..))
import Data.NonEmpty ((:|))
import Data.String (joinWith)
import Data.String.CodeUnits (singleton)
import Data.Unfoldable (replicate)

import Test.Assert (assert', ASSERT, assert)
import Effect (Effect)
import Test.Assert (assert', assert)
import Text.Parsing.StringParser (Parser, runParser, try)
import Text.Parsing.StringParser.Combinators (many1, endBy1, sepBy1, optionMaybe, many, manyTill, many1Till, chainl, fix, between)
import Text.Parsing.StringParser.Expr (Assoc(..), Operator(..), buildExprParser)
Expand Down Expand Up @@ -51,8 +51,8 @@ exprTest = buildExprParser [ [Infix (string "/" >>= \_ -> pure div) AssocRight]

tryTest :: Parser String
-- reduce the possible array of matches to 0 or 1 elements to aid Array pattern matching
tryTest =
try (string "aa" <> string "bb") <|>
tryTest =
try (string "aa" <> string "bb") <|>
(string "aa" <> string "cc")

canParse :: forall a. Parser a -> String -> Boolean
Expand All @@ -64,7 +64,7 @@ parseFail p input = isLeft $ runParser p input
expectResult :: forall a. (Eq a) => a -> Parser a -> String -> Boolean
expectResult res p input = runParser p input == Right res

main :: forall e. Eff (console :: CONSOLE, assert :: ASSERT | e) Unit
main :: Effect Unit
main = do
assert' "many should not blow the stack" $ canParse (many (string "a")) (joinWith "" $ replicate 100000 "a")
assert' "many failing after" $ parseFail (do
Expand All @@ -78,22 +78,22 @@ main = do
assert $ canParse (parens (do
_ <- string "a"
optionMaybe $ string "b")) "(ab)"
assert $ expectResult ("a":"a":"a":Nil) (string "a" `sepBy1` string ",") "a,a,a"
assert $ expectResult (NonEmptyList ("a" :| "a":"a":Nil)) (string "a" `sepBy1` string ",") "a,a,a"
assert $ canParse (do
as <- string "a" `endBy1` string ","
eof
pure as) "a,a,a,"
assert' "opTest" $ expectResult "abc" opTest "a+b+c"
assert' "exprTest" $ expectResult (-3) exprTest "1*2+3/4-5"
assert' "tryTest "$ canParse tryTest "aacc"
assert $ expectResult ('0':'1':'2':'3':'4':Nil) (many1 anyDigit) "01234/"
assert $ expectResult ('5':'6':'7':'8':'9':Nil) (many1 anyDigit) "56789:"
assert $ expectResult (NonEmptyList ('0' :| '1':'2':'3':'4':Nil)) (many1 anyDigit) "01234/"
assert $ expectResult (NonEmptyList ('5' :| '6':'7':'8':'9':Nil)) (many1 anyDigit) "56789:"
assert $ expectResult "aaaa" (regex "a+") "aaaab"
assert $ expectResult ("a":"a":"a":Nil) (manyTill (string "a") (string "b")) "aaab"
assert $ expectResult Nil (manyTill (string "a") (string "b")) "b"
assert $ expectResult ("a":"a":"a":Nil) (many1Till (string "a") (string "b")) "aaab"
assert $ expectResult (NonEmptyList ("a" :| "a":"a":Nil)) (many1Till (string "a") (string "b")) "aaab"
assert $ parseFail (many1Till (string "a") (string "b")) "b"
-- check against overflow
assert $ canParse (many1Till (string "a") (string "and")) $ (fold <<< take 10000 $ repeat "a") <> "and"
-- check correct order
assert $ expectResult ('a':'b':'c':Nil) (many1Till anyChar (string "d")) "abcd"
assert $ expectResult (NonEmptyList ('a' :| 'b':'c':Nil)) (many1Till anyChar (string "d")) "abcd"