Skip to content

Commit 4d246b5

Browse files
dwhitneypaf31
authored andcommitted
added Semigroup instance for Parser a where a is a Semigroup (#38)
* added Semigroup instance for Parser a where a is a Semigroup * added @paf31's suggestions
1 parent 2f9dcfb commit 4d246b5

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/Text/Parsing/StringParser.purs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
module Text.Parsing.StringParser where
44

55
import Prelude
6+
7+
import Control.Apply (lift2)
68
import Control.MonadPlus (class MonadPlus, class MonadZero, class Alternative)
79
import Control.Monad.Rec.Class (class MonadRec, tailRecM, Step(..))
810
import Control.Plus (class Plus, class Alt)
@@ -98,3 +100,6 @@ fail msg = Parser \{ pos } -> Left { pos, error: ParseError msg }
98100
-- | `try p` backtracks even if input was consumed.
99101
try :: forall a. Parser a -> Parser a
100102
try (Parser p) = Parser \(s@{ pos }) -> lmap (_ { pos = pos}) (p s)
103+
104+
instance semigroupParser :: Semigroup a => Semigroup (Parser a) where
105+
append = lift2 append

test/Main.purs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ 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 = try ((<>) <$> string "aa" <*> string "bb") <|>
55-
(<>) <$> string "aa" <*> string "cc"
54+
tryTest =
55+
try (string "aa" <> string "bb") <|>
56+
(string "aa" <> string "cc")
5657

5758
canParse :: forall a. Parser a -> String -> Boolean
5859
canParse p input = isRight $ runParser p input

0 commit comments

Comments
 (0)