Skip to content

Commit fc6cd08

Browse files
authoredOct 1, 2020
parsing failure context label (#96)
1 parent f8dae05 commit fc6cd08

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed
 

‎README.md

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ bower install purescript-parsing
1919
- [See the tests](test/Main.purs) for some example usages.
2020
- Module documentation is [published on Pursuit](http://pursuit.purescript.org/packages/purescript-parsing).
2121

22+
## Related Packages
23+
24+
- [__purescript-parsing-dataview__](https://pursuit.purescript.org/packages/purescript-parsing-dataview)
25+
Provides the module __Text.Parsing.Parser.DataView__ for binary parsing of
26+
`ArrayBuffer`.
27+
2228
## Contributing
2329

2430
Read the [contribution guidelines](https://github.com/purescript-contrib/purescript-parsing/blob/master/.github/contributing.md) to get started and see helpful related resources.

‎src/Text/Parsing/Parser.purs

+8-1
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ module Text.Parsing.Parser
1313
, position
1414
, fail
1515
, failWithPosition
16+
, label
1617
) where
1718

1819
import Prelude
1920

2021
import Control.Alt (class Alt)
2122
import Control.Apply (lift2)
2223
import Control.Lazy (defer, class Lazy)
23-
import Control.Monad.Error.Class (class MonadThrow, throwError)
24+
import Control.Monad.Error.Class (class MonadThrow, throwError, catchError)
2425
import Control.Monad.Except (class MonadError, ExceptT(..), runExceptT, mapExceptT)
2526
import Control.Monad.Rec.Class (class MonadRec)
2627
import Control.Monad.State (class MonadState, StateT(..), evalStateT, gets, mapStateT, modify_, runStateT)
@@ -136,3 +137,9 @@ fail message = failWithPosition message =<< position
136137
-- | Fail with a message and a position.
137138
failWithPosition :: forall m s a. Monad m => String -> Position -> ParserT s m a
138139
failWithPosition message pos = throwError (ParseError message pos)
140+
141+
-- | If parsing fails inside this labelled context, then prepend the `String`
142+
-- | to the error `String` in the `ParseError`.
143+
label :: forall m s a. Monad m => String -> ParserT s m a -> ParserT s m a
144+
label messagePrefix p = catchError p $ \ (ParseError message pos) ->
145+
throwError $ ParseError (messagePrefix <> message) pos

‎test/Main.purs

+13-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Data.Tuple (Tuple(..))
1313
import Effect (Effect)
1414
import Effect.Console (logShow)
1515
import Test.Assert (assert')
16-
import Text.Parsing.Parser (Parser, ParserT, runParser, parseErrorPosition)
16+
import Text.Parsing.Parser (Parser, ParserT, ParseError(..), runParser, parseErrorPosition, label)
1717
import Text.Parsing.Parser.Combinators (endBy1, sepBy1, optionMaybe, try, chainl, between)
1818
import Text.Parsing.Parser.Expr (Assoc(..), Operator(..), buildExprParser)
1919
import Text.Parsing.Parser.Language (javaStyle, haskellStyle, haskellDef)
@@ -496,3 +496,15 @@ main = do
496496

497497
haskellStyleTest
498498
javaStyleTest
499+
500+
case runParser "aa" p of
501+
Right _ -> assert' "error: ParseError expected!" false
502+
Left (ParseError message pos) -> do
503+
let messageExpected = "context1context2Expected \"b\""
504+
assert' ("expected message: " <> messageExpected <> ", message: " <> message) (message == messageExpected)
505+
logShow messageExpected
506+
where
507+
p = label "context1" $ do
508+
_ <- string "a"
509+
label "context2" $ do
510+
string "b"

0 commit comments

Comments
 (0)
Please sign in to comment.