Skip to content

Commit 7f75409

Browse files
authored
Compiler/0.12 (#41)
* compiler 0.12 updates * more 0.12 updates * update packages * precise dependencies * make *1 combinators return NonEmptyLists * nonempty sepBy1
1 parent 4d246b5 commit 7f75409

File tree

7 files changed

+68
-55
lines changed

7 files changed

+68
-55
lines changed

.travis.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
language: node_js
22
dist: trusty
33
sudo: required
4-
node_js: 6
4+
node_js: stable
5+
env:
6+
- PATH=$HOME/purescript:$PATH
57
install:
68
- npm install -g bower
9+
- 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')
10+
- wget -O $HOME/purescript.tar.gz https://github.com/purescript/purescript/releases/download/$TAG/linux64.tar.gz
11+
- tar -xvf $HOME/purescript.tar.gz -C $HOME/
12+
- chmod a+x $HOME/purescript
713
- npm install
814
- bower install --production
915
script:

bower.json

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,20 @@
1919
"url": "git://github.com/paf31/purescript-string-parsers.git"
2020
},
2121
"dependencies": {
22-
"purescript-control": "^3.0.0",
23-
"purescript-arrays": "^4.0.0",
24-
"purescript-maybe": "^3.0.0",
25-
"purescript-strings": "^3.0.0",
26-
"purescript-foldable-traversable": "^3.0.0",
27-
"purescript-either": "^3.0.0",
28-
"purescript-lists": "^4.0.0",
29-
"purescript-tailrec": "^3.0.0"
22+
"purescript-arrays": "^5.0.0",
23+
"purescript-bifunctors": "^4.0.0",
24+
"purescript-control": "^4.0.0",
25+
"purescript-either": "^4.0.0",
26+
"purescript-foldable-traversable": "^4.0.0",
27+
"purescript-lists": "^5.0.0",
28+
"purescript-maybe": "^4.0.0",
29+
"purescript-prelude": "^4.0.0",
30+
"purescript-strings": "^4.0.0",
31+
"purescript-tailrec": "^4.0.0"
3032
},
3133
"devDependencies": {
32-
"purescript-math": "^2.0.0",
33-
"purescript-console": "^3.0.0",
34-
"purescript-assert": "^3.0.0"
34+
"purescript-math": "^2.1.1",
35+
"purescript-console": "^4.1.0",
36+
"purescript-assert": "^4.0.0"
3537
}
3638
}

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
"test": "pulp test"
77
},
88
"devDependencies": {
9-
"pulp": "^11.0.0",
10-
"purescript": "^0.11.1",
11-
"purescript-psa": "^0.5.0",
12-
"rimraf": "^2.5.0"
9+
"pulp": "^12.2.0",
10+
"purescript-psa": "^0.6.0",
11+
"rimraf": "^2.6.2"
1312
}
1413
}

src/Text/Parsing/StringParser/Combinators.purs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ import Prelude
3232
import Control.Alt ((<|>))
3333
import Control.Lazy (fix)
3434
import Control.Monad.Rec.Class (Step(..), tailRecM)
35-
3635
import Data.Either (Either(..))
3736
import Data.Foldable (class Foldable, foldl)
38-
import Data.List (List(..), singleton, manyRec, reverse)
37+
import Data.List (List(..), manyRec)
38+
import Data.List.NonEmpty (NonEmptyList(..))
39+
import Data.List.NonEmpty as NEL
3940
import Data.Maybe (Maybe(..))
40-
41+
import Data.NonEmpty ((:|))
4142
import Text.Parsing.StringParser (Parser(..), fail)
4243

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

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

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

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

8485
-- | Parse one or more separated values.
85-
sepBy1 :: forall a sep. Parser a -> Parser sep -> Parser (List a)
86+
sepBy1 :: forall a sep. Parser a -> Parser sep -> Parser (NonEmptyList a)
8687
sepBy1 p sep = do
8788
a <- p
8889
as <- many $ sep *> p
89-
pure (Cons a as)
90+
pure (cons' a as)
9091

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

9596
-- | Parse one or more separated values, optionally ending with a separator.
96-
sepEndBy1 :: forall a sep. Parser a -> Parser sep -> Parser (List a)
97+
sepEndBy1 :: forall a sep. Parser a -> Parser sep -> Parser (NonEmptyList a)
9798
sepEndBy1 p sep = do
9899
a <- p
99100
(do _ <- sep
100101
as <- sepEndBy p sep
101-
pure (Cons a as)) <|> pure (singleton a)
102+
pure (cons' a as)) <|> pure (NEL.singleton a)
102103

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

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

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

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

151152
-- | Parse values until the terminator matches, requiring at least one match.
152-
many1Till :: forall a end. Parser a -> Parser end -> Parser (List a)
153+
many1Till :: forall a end. Parser a -> Parser end -> Parser (NonEmptyList a)
153154
many1Till p end = do
154155
x <- p
155156
tailRecM inner (pure x)
156157
where
157158
ending acc = do
158159
_ <- end
159-
pure $ Done (reverse acc)
160+
pure $ Done (NEL.reverse acc)
160161
continue acc = do
161162
c <- p
162-
pure $ Loop (Cons c acc)
163+
pure $ Loop (NEL.cons c acc)
163164
inner acc = ending acc <|> continue acc
165+
166+
cons' :: forall a. a -> List a -> NonEmptyList a
167+
cons' h t = NonEmptyList (h :| t)

src/Text/Parsing/StringParser/Expr.purs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ buildExprParser operators simpleExpr =
4646
prefixOp = choice accum.prefix <?> ""
4747
postfixOp = choice accum.postfix <?> ""
4848

49-
postfixP = postfixOp <|> pure id
50-
prefixP = prefixOp <|> pure id
49+
postfixP = postfixOp <|> pure identity
50+
prefixP = prefixOp <|> pure identity
5151
in do
5252
x <- termP prefixP term postfixP
5353
rassocP x rassocOp prefixP term postfixP

src/Text/Parsing/StringParser/String.purs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ module Text.Parsing.StringParser.String
2121
import Prelude
2222

2323
import Control.Alt ((<|>))
24-
import Data.Array ((..), uncons)
24+
import Data.Array ((..))
25+
import Data.Array.NonEmpty as NEA
2526
import Data.Char (toCharCode)
2627
import Data.Either (Either(..))
2728
import Data.Foldable (class Foldable, foldMap, elem, notElem)
28-
import Data.Maybe (Maybe(..), fromMaybe)
29-
import Data.String (Pattern(..), charAt, drop, length, indexOf', singleton, stripPrefix)
29+
import Data.Maybe (Maybe(..))
30+
import Data.String (Pattern(..), drop, length, indexOf', stripPrefix)
31+
import Data.String.CodeUnits (charAt, singleton)
3032
import Data.String.Regex as Regex
3133
import Data.String.Regex.Flags (noFlags)
3234
import Text.Parsing.StringParser (Parser(..), ParseError(..), try, fail)
@@ -137,8 +139,8 @@ regex pat =
137139
let
138140
remainder = drop pos str
139141
in
140-
case uncons $ fromMaybe [] $ Regex.match r remainder of
141-
Just { head: Just matched, tail: _ } ->
142+
case NEA.head <$> Regex.match r remainder of
143+
Just (Just matched) ->
142144
Right { result: matched, suffix: { str, pos: pos + length matched } }
143145
_ ->
144146
Left { pos, error: ParseError "no match" }

test/Main.purs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ module Test.Main where
33
import Prelude hiding (between)
44

55
import Control.Alt ((<|>))
6-
import Control.Monad.Eff (Eff)
7-
import Control.Monad.Eff.Console (CONSOLE)
8-
96
import Data.Either (isLeft, isRight, Either(..))
107
import Data.Foldable (fold)
118
import Data.List (List(Nil), (:))
129
import Data.List.Lazy (take, repeat)
13-
import Data.String (joinWith, singleton)
10+
import Data.List.NonEmpty (NonEmptyList(..))
11+
import Data.NonEmpty ((:|))
12+
import Data.String (joinWith)
13+
import Data.String.CodeUnits (singleton)
1414
import Data.Unfoldable (replicate)
15-
16-
import Test.Assert (assert', ASSERT, assert)
15+
import Effect (Effect)
16+
import Test.Assert (assert', assert)
1717
import Text.Parsing.StringParser (Parser, runParser, try)
1818
import Text.Parsing.StringParser.Combinators (many1, endBy1, sepBy1, optionMaybe, many, manyTill, many1Till, chainl, fix, between)
1919
import Text.Parsing.StringParser.Expr (Assoc(..), Operator(..), buildExprParser)
@@ -51,8 +51,8 @@ exprTest = buildExprParser [ [Infix (string "/" >>= \_ -> pure div) AssocRight]
5151

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

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

67-
main :: forall e. Eff (console :: CONSOLE, assert :: ASSERT | e) Unit
67+
main :: Effect Unit
6868
main = do
6969
assert' "many should not blow the stack" $ canParse (many (string "a")) (joinWith "" $ replicate 100000 "a")
7070
assert' "many failing after" $ parseFail (do
@@ -78,22 +78,22 @@ main = do
7878
assert $ canParse (parens (do
7979
_ <- string "a"
8080
optionMaybe $ string "b")) "(ab)"
81-
assert $ expectResult ("a":"a":"a":Nil) (string "a" `sepBy1` string ",") "a,a,a"
81+
assert $ expectResult (NonEmptyList ("a" :| "a":"a":Nil)) (string "a" `sepBy1` string ",") "a,a,a"
8282
assert $ canParse (do
8383
as <- string "a" `endBy1` string ","
8484
eof
8585
pure as) "a,a,a,"
8686
assert' "opTest" $ expectResult "abc" opTest "a+b+c"
8787
assert' "exprTest" $ expectResult (-3) exprTest "1*2+3/4-5"
8888
assert' "tryTest "$ canParse tryTest "aacc"
89-
assert $ expectResult ('0':'1':'2':'3':'4':Nil) (many1 anyDigit) "01234/"
90-
assert $ expectResult ('5':'6':'7':'8':'9':Nil) (many1 anyDigit) "56789:"
89+
assert $ expectResult (NonEmptyList ('0' :| '1':'2':'3':'4':Nil)) (many1 anyDigit) "01234/"
90+
assert $ expectResult (NonEmptyList ('5' :| '6':'7':'8':'9':Nil)) (many1 anyDigit) "56789:"
9191
assert $ expectResult "aaaa" (regex "a+") "aaaab"
9292
assert $ expectResult ("a":"a":"a":Nil) (manyTill (string "a") (string "b")) "aaab"
9393
assert $ expectResult Nil (manyTill (string "a") (string "b")) "b"
94-
assert $ expectResult ("a":"a":"a":Nil) (many1Till (string "a") (string "b")) "aaab"
94+
assert $ expectResult (NonEmptyList ("a" :| "a":"a":Nil)) (many1Till (string "a") (string "b")) "aaab"
9595
assert $ parseFail (many1Till (string "a") (string "b")) "b"
9696
-- check against overflow
9797
assert $ canParse (many1Till (string "a") (string "and")) $ (fold <<< take 10000 $ repeat "a") <> "and"
9898
-- check correct order
99-
assert $ expectResult ('a':'b':'c':Nil) (many1Till anyChar (string "d")) "abcd"
99+
assert $ expectResult (NonEmptyList ('a' :| 'b':'c':Nil)) (many1Till anyChar (string "d")) "abcd"

0 commit comments

Comments
 (0)