Skip to content

Commit 40475b1

Browse files
Use ParseErrorPos for position reporting
1 parent cd05ce4 commit 40475b1

File tree

23 files changed

+144
-135
lines changed

23 files changed

+144
-135
lines changed

benchmark/Streamly/Benchmark/Data/Parser.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ moduleName = "Data.Parser"
708708

709709
instance NFData ParseError where
710710
{-# INLINE rnf #-}
711-
rnf (ParseError i x) = rnf i `seq` rnf x
711+
rnf (ParseError x) = rnf x
712712

713713
o_1_space_serial :: Int -> [Benchmark]
714714
o_1_space_serial value =

benchmark/Streamly/Benchmark/Data/ParserK.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ moduleName = MODULE_NAME
374374

375375
instance NFData ParseError where
376376
{-# INLINE rnf #-}
377-
rnf (ParseError i x) = rnf i `seq` rnf x
377+
rnf (ParseError x) = rnf x
378378

379379
o_1_space_serial :: Int -> [Benchmark]
380380
o_1_space_serial value =

benchmark/Streamly/Benchmark/Unicode/Parser.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ moduleName = "Unicode.Parser"
7272

7373
instance NFData ParseError where
7474
{-# INLINE rnf #-}
75-
rnf (ParseError i x) = rnf i `seq` rnf x
75+
rnf (ParseError x) = rnf x
7676

7777
o_n_heap_serial :: Int -> [Benchmark]
7878
o_n_heap_serial value =

core/src/DocTestDataParser.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,4 @@ For APIs that have not been released yet.
1717
1818
>>> import qualified Streamly.Internal.Data.Fold as Fold
1919
>>> import qualified Streamly.Internal.Data.Parser as Parser
20-
>>> import qualified Streamly.Internal.Data.Stream as Stream (parsePos)
2120
-}

core/src/Streamly/Data/Parser.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ module Streamly.Data.Parser
188188
-- * Parser Type
189189
Parser
190190
, ParseError(..)
191+
, ParseErrorPos(..)
191192

192193
-- -- * Downgrade to Fold
193194
-- , toFold

core/src/Streamly/Internal/Data/Array.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ import Prelude hiding (length, null, last, map, (!!), read, concat)
130130
import Streamly.Internal.Data.MutByteArray.Type (PinnedState(..), MutByteArray)
131131
import Streamly.Internal.Data.Serialize.Type (Serialize)
132132
import Streamly.Internal.Data.Fold.Type (Fold(..))
133-
import Streamly.Internal.Data.Parser (ParseError(..))
133+
import Streamly.Internal.Data.Parser (ParseError(..), ParseErrorPos(..))
134134
import Streamly.Internal.Data.ParserK.Type
135135
(ParserK, ParseResult(..), Input(..), Step(..))
136136
import Streamly.Internal.Data.Stream (Stream(..))
@@ -1034,7 +1034,7 @@ parseBreakPos
10341034
:: (Monad m, Unbox a)
10351035
=> ParserK (Array a) m b
10361036
-> StreamK m (Array a)
1037-
-> m (Either ParseError b, StreamK m (Array a))
1037+
-> m (Either ParseErrorPos b, StreamK m (Array a))
10381038
parseBreakPos = Drivers.parseBreakChunksPos
10391039

10401040
{-# INLINE parse #-}
@@ -1047,7 +1047,7 @@ parse f = fmap fst . parseBreak f
10471047
--
10481048
{-# INLINE parsePos #-}
10491049
parsePos :: (Monad m, Unbox a) =>
1050-
ParserK (Array a) m b -> StreamK m (Array a) -> m (Either ParseError b)
1050+
ParserK (Array a) m b -> StreamK m (Array a) -> m (Either ParseErrorPos b)
10511051
parsePos f = fmap fst . parseBreakPos f
10521052

10531053
-------------------------------------------------------------------------------

core/src/Streamly/Internal/Data/Array/Generic.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module Streamly.Internal.Data.Array.Generic
1818
)
1919
where
2020

21-
import Streamly.Internal.Data.Parser (ParseError(..))
21+
import Streamly.Internal.Data.Parser (ParseError(..), ParseErrorPos(..))
2222
import Streamly.Internal.Data.StreamK.Type (StreamK)
2323

2424
import qualified Streamly.Internal.Data.ParserDrivers as Drivers
@@ -47,7 +47,7 @@ parseBreakPos
4747
:: forall m a b. Monad m
4848
=> ParserK.ParserK (Array a) m b
4949
-> StreamK m (Array a)
50-
-> m (Either ParseError b, StreamK m (Array a))
50+
-> m (Either ParseErrorPos b, StreamK m (Array a))
5151
parseBreakPos = Drivers.parseBreakChunksGenericPos
5252

5353
{-# INLINE parse #-}
@@ -63,5 +63,5 @@ parsePos ::
6363
(Monad m)
6464
=> ParserK.ParserK (Array a) m b
6565
-> StreamK m (Array a)
66-
-> m (Either ParseError b)
66+
-> m (Either ParseErrorPos b)
6767
parsePos f = fmap fst . parseBreakPos f

core/src/Streamly/Internal/Data/Array/Stream.hs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ runArrayParserDBreak
321321
case res of
322322
PRD.IPartial s -> go SPEC state (List []) s
323323
PRD.IDone b -> return (Right b, stream)
324-
PRD.IError err -> return (Left (ParseError (-1) err), stream)
324+
PRD.IError err -> return (Left (ParseError err), stream)
325325

326326
where
327327

@@ -374,7 +374,7 @@ runArrayParserDBreak
374374
let src0 = x:getList backBuf
375375
src = Prelude.reverse src0 ++ x:xs
376376
strm = D.append (D.fromList src) (D.Stream step s)
377-
return (Left (ParseError (-1) err), strm)
377+
return (Left (ParseError err), strm)
378378

379379
-- This is a simplified gobuf
380380
goExtract _ [] backBuf !pst = goStop backBuf pst
@@ -411,7 +411,7 @@ runArrayParserDBreak
411411
PR.SError err -> do
412412
let src0 = getList backBuf
413413
src = Prelude.reverse src0 ++ x:xs
414-
return (Left (ParseError (-1) err), D.fromList src)
414+
return (Left (ParseError err), D.fromList src)
415415

416416
-- This is a simplified goExtract
417417
{-# INLINE goStop #-}
@@ -438,7 +438,7 @@ runArrayParserDBreak
438438
PR.FError err -> do
439439
let src0 = getList backBuf
440440
src = Prelude.reverse src0
441-
return (Left (ParseError (-1) err), D.fromList src)
441+
return (Left (ParseError err), D.fromList src)
442442

443443
{-
444444
-- | Parse an array stream using the supplied 'Parser'. Returns the parse
@@ -516,7 +516,7 @@ runArrayFoldManyD
516516
let next = ParseChunksInitLeftOver []
517517
return
518518
$ D.Skip
519-
$ ParseChunksYield (Left (ParseError (-1) err)) next
519+
$ ParseChunksYield (Left (ParseError err)) next
520520
D.Skip s -> return $ D.Skip $ ParseChunksInit [] s
521521
D.Stop -> return D.Stop
522522

@@ -533,7 +533,7 @@ runArrayFoldManyD
533533
let next = ParseChunksInitLeftOver []
534534
return
535535
$ D.Skip
536-
$ ParseChunksYield (Left (ParseError (-1) err)) next
536+
$ ParseChunksYield (Left (ParseError err)) next
537537

538538
-- This is a simplified ParseChunksInit
539539
stepOuter _ (ParseChunksInitBuf src) = do
@@ -548,7 +548,7 @@ runArrayFoldManyD
548548
let next = ParseChunksInitLeftOver []
549549
return
550550
$ D.Skip
551-
$ ParseChunksYield (Left (ParseError (-1) err)) next
551+
$ ParseChunksYield (Left (ParseError err)) next
552552

553553
-- XXX we just discard any leftover input at the end
554554
stepOuter _ (ParseChunksInitLeftOver _) = return D.Stop
@@ -595,7 +595,7 @@ runArrayFoldManyD
595595
let next = ParseChunksInitLeftOver []
596596
return
597597
$ D.Skip
598-
$ ParseChunksYield (Left (ParseError (-1) err)) next
598+
$ ParseChunksYield (Left (ParseError err)) next
599599

600600
D.Skip s -> return $ D.Skip $ ParseChunksStream s backBuf pst
601601
D.Stop -> return $ D.Skip $ ParseChunksStop backBuf pst
@@ -637,7 +637,7 @@ runArrayFoldManyD
637637
let next = ParseChunksInitLeftOver []
638638
return
639639
$ D.Skip
640-
$ ParseChunksYield (Left (ParseError (-1) err)) next
640+
$ ParseChunksYield (Left (ParseError err)) next
641641

642642
-- This is a simplified ParseChunksBuf
643643
stepOuter _ (ParseChunksExtract [] buf pst) =
@@ -675,7 +675,7 @@ runArrayFoldManyD
675675
let next = ParseChunksInitLeftOver []
676676
return
677677
$ D.Skip
678-
$ ParseChunksYield (Left (ParseError (-1) err)) next
678+
$ ParseChunksYield (Left (ParseError err)) next
679679

680680

681681
-- This is a simplified ParseChunksExtract
@@ -704,7 +704,7 @@ runArrayFoldManyD
704704
let next = ParseChunksInitLeftOver []
705705
return
706706
$ D.Skip
707-
$ ParseChunksYield (Left (ParseError (-1) err)) next
707+
$ ParseChunksYield (Left (ParseError err)) next
708708

709709
stepOuter _ (ParseChunksYield a next) = return $ D.Yield a next
710710

core/src/Streamly/Internal/Data/Parser.hs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ module Streamly.Internal.Data.Parser
1919
module Streamly.Internal.Data.Parser.Type
2020
--, module Streamly.Internal.Data.Parser.Tee
2121

22-
-- * Types
23-
, Parser (..)
24-
, ParseError (..)
25-
, Step (..)
26-
, Initial (..)
27-
2822
-- * Downgrade to Fold
2923
, toFold
3024

@@ -618,7 +612,7 @@ data Tuple'Fused a b = Tuple'Fused !a !b deriving Show
618612
-- Right [1,2]
619613
--
620614
-- >>> takeBetween' 2 4 [1]
621-
-- Left (ParseError 1 "takeBetween: Expecting alteast 2 elements, got 1")
615+
-- Left (ParseErrorPos 1 "takeBetween: Expecting alteast 2 elements, got 1")
622616
--
623617
-- >>> takeBetween' 0 0 [1, 2]
624618
-- Right []
@@ -724,7 +718,7 @@ takeBetween low high (Fold fstep finitial _ ffinal) =
724718
-- Right [1,0]
725719
--
726720
-- >>> Stream.parsePos (Parser.takeEQ 4 Fold.toList) $ Stream.fromList [1,0,1]
727-
-- Left (ParseError 3 "takeEQ: Expecting exactly 4 elements, input terminated on 3")
721+
-- Left (ParseErrorPos 3 "takeEQ: Expecting exactly 4 elements, input terminated on 3")
728722
--
729723
{-# INLINE takeEQ #-}
730724
takeEQ :: Monad m => Int -> Fold m a b -> Parser a m b
@@ -785,7 +779,7 @@ data TakeGEState s =
785779
-- elements.
786780
--
787781
-- >>> Stream.parsePos (Parser.takeGE 4 Fold.toList) $ Stream.fromList [1,0,1]
788-
-- Left (ParseError 3 "takeGE: Expecting at least 4 elements, input terminated on 3")
782+
-- Left (ParseErrorPos 3 "takeGE: Expecting at least 4 elements, input terminated on 3")
789783
--
790784
-- >>> Stream.parse (Parser.takeGE 4 Fold.toList) $ Stream.fromList [1,0,1,0,1]
791785
-- Right [1,0,1,0,1]
@@ -1325,7 +1319,7 @@ takeEitherSepBy _cond = undefined -- D.toParserK . D.takeEitherSepBy cond
13251319
-- >>> p = Parser.takeBeginBy (== ',') Fold.toList
13261320
-- >>> leadingComma = Stream.parsePos p . Stream.fromList
13271321
-- >>> leadingComma "a,b"
1328-
-- Left (ParseError 1 "takeBeginBy: missing frame start")
1322+
-- Left (ParseErrorPos 1 "takeBeginBy: missing frame start")
13291323
-- ...
13301324
-- >>> leadingComma ",,"
13311325
-- Right ","
@@ -1403,7 +1397,7 @@ RENAME(takeStartBy_,takeBeginBy_)
14031397
-- >>> Stream.parse p $ Stream.fromList "{hello \\{world}"
14041398
-- Right "hello {world"
14051399
-- >>> Stream.parsePos p $ Stream.fromList "{hello {world}"
1406-
-- Left (ParseError 14 "takeFramedByEsc_: missing frame end")
1400+
-- Left (ParseErrorPos 14 "takeFramedByEsc_: missing frame end")
14071401
--
14081402
-- /Pre-release/
14091403
{-# INLINE takeFramedByEsc_ #-}
@@ -2146,7 +2140,7 @@ groupByRollingEither
21462140
-- Right "string"
21472141
--
21482142
-- >>> Stream.parsePos (Parser.listEqBy (==) "mismatch") $ Stream.fromList "match"
2149-
-- Left (ParseError 2 "streamEqBy: mismtach occurred")
2143+
-- Left (ParseErrorPos 2 "streamEqBy: mismtach occurred")
21502144
--
21512145
{-# INLINE listEqBy #-}
21522146
listEqBy :: Monad m => (a -> a -> Bool) -> [a] -> Parser a m [a]
@@ -2437,7 +2431,7 @@ spanByRolling eq f1 f2 =
24372431
-- Right [1,2]
24382432
--
24392433
-- >>> Stream.parsePos (Parser.takeP 4 (Parser.takeEQ 5 Fold.toList)) $ Stream.fromList [1, 2, 3, 4, 5]
2440-
-- Left (ParseError 4 "takeEQ: Expecting exactly 5 elements, input terminated on 4")
2434+
-- Left (ParseErrorPos 4 "takeEQ: Expecting exactly 5 elements, input terminated on 4")
24412435
--
24422436
-- /Internal/
24432437
{-# INLINE takeP #-}
@@ -2591,7 +2585,7 @@ data DeintercalateAllState fs sp ss =
25912585
-- >>> Stream.parse p $ Stream.fromList "1"
25922586
-- Right [Left "1"]
25932587
-- >>> Stream.parsePos p $ Stream.fromList "1+"
2594-
-- Left (ParseError 2 "takeWhile1: end of input")
2588+
-- Left (ParseErrorPos 2 "takeWhile1: end of input")
25952589
-- >>> Stream.parse p $ Stream.fromList "1+2+3"
25962590
-- Right [Left "1",Right '+',Left "2",Right '+',Left "3"]
25972591
--
@@ -2867,7 +2861,7 @@ data Deintercalate1State b fs sp ss =
28672861
-- >>> p2 = Parser.satisfy (== '+')
28682862
-- >>> p = Parser.deintercalate1 p1 p2 Fold.toList
28692863
-- >>> Stream.parsePos p $ Stream.fromList ""
2870-
-- Left (ParseError 0 "takeWhile1: end of input")
2864+
-- Left (ParseErrorPos 0 "takeWhile1: end of input")
28712865
-- >>> Stream.parse p $ Stream.fromList "1"
28722866
-- Right [Left "1"]
28732867
-- >>> Stream.parse p $ Stream.fromList "1+"
@@ -3160,7 +3154,7 @@ sepBy1 p sep sink = do
31603154
-- >>> p2 = Parser.satisfy (== '+')
31613155
-- >>> p = Parser.sepBy1 p1 p2 Fold.toList
31623156
-- >>> Stream.parsePos p $ Stream.fromList ""
3163-
-- Left (ParseError 0 "takeWhile1: end of input")
3157+
-- Left (ParseErrorPos 0 "takeWhile1: end of input")
31643158
-- >>> Stream.parse p $ Stream.fromList "1"
31653159
-- Right ["1"]
31663160
-- >>> Stream.parse p $ Stream.fromList "1+"

core/src/Streamly/Internal/Data/Parser/Type.hs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ module Streamly.Internal.Data.Parser.Type
188188
, bimapMorphOverrideCount
189189
, Parser (..)
190190
, ParseError (..)
191+
, ParseErrorPos (..)
191192
, rmapM
192193

193194
-- * Constructors
@@ -517,16 +518,23 @@ data Parser a m b =
517518
(s -> m (Final s b))
518519

519520
-- | This exception is used when a parser ultimately fails, the user of the
520-
-- parser is intimated via this exception. The @Int@ is the position in the
521-
-- stream where the error ocurred. Note that the position is reported only when
522-
-- a position reporting parser driver is used, otherwise it will be reported as
523-
-- 0.
521+
-- parser is intimated via this exception.
524522
--
525-
data ParseError = ParseError Int String
523+
newtype ParseError = ParseError String
526524
deriving (Eq, Show)
527525

528526
instance Exception ParseError where
529-
displayException (ParseError pos err) = concat ["At ", show pos, ":", err]
527+
displayException (ParseError err) = err
528+
529+
-- | Like 'ParseError' but reports the stream position where the error ocurred.
530+
-- The @Int@ is the position in the stream where the error ocurred. This
531+
-- exception is used by position reporting parser drivers.
532+
data ParseErrorPos = ParseErrorPos Int String
533+
deriving (Eq, Show)
534+
535+
instance Exception ParseErrorPos where
536+
displayException (ParseErrorPos pos err) =
537+
concat ["At ", show pos, ":", err]
530538

531539
-- | Map a function on the result i.e. on @b@ in @Parser a m b@.
532540
instance Functor m => Functor (Parser a m) where

0 commit comments

Comments
 (0)