From f991988eeaaa1052f5b9b2bb3ac0f7f81a27dcee Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 26 Apr 2017 14:54:29 -0400 Subject: [PATCH] Show the source of errors. --- src/Data/Syntax/Assignment.hs | 4 +++- test/Data/Syntax/Assignment/Spec.hs | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Data/Syntax/Assignment.hs b/src/Data/Syntax/Assignment.hs index 25c4f261c..190473ae6 100644 --- a/src/Data/Syntax/Assignment.hs +++ b/src/Data/Syntax/Assignment.hs @@ -17,9 +17,11 @@ module Data.Syntax.Assignment ) where import Control.Monad.Free.Freer +import qualified Data.ByteString.Char8 as B import Data.Functor.Classes import Data.Functor.Foldable hiding (Nil) import qualified Data.IntMap.Lazy as IntMap +import Data.List ((!!)) import Data.Record import Data.Text (unpack) import qualified Info @@ -107,7 +109,7 @@ runAssignment = iterFreer run . fmap (\ a state -> Result (state, a)) (Choose choices, Rose (symbol :. _) _ : _) | Just a <- IntMap.lookup (fromEnum symbol) choices -> yield a state -- Nullability: some rules, e.g. 'pure a' and 'many a', should match at the end of input. Either side of an alternation may be nullable, ergo Alt can match at the end of input. (Alt a b, _) -> yield a state <|> yield b state - _ -> Error [expectation <> maybe "end of input" (show . rhead . roseValue) (listToMaybe stateNodes)] + _ -> Error [expectation <> maybe "end of input" (show . rhead . roseValue) (listToMaybe stateNodes) <> ":\n" <> toS (B.lines (Source.sourceText stateSource) !! pred (Info.line statePos)) <> "\n" <> toS (replicate (pred (Info.column statePos)) ' ') <> "^\n"] where state@AssignmentState{..} = dropAnonymous initialState expectation = case assignment of Source -> "Expected a leaf node but got " diff --git a/test/Data/Syntax/Assignment/Spec.hs b/test/Data/Syntax/Assignment/Spec.hs index 698552e6f..34ce0c74f 100644 --- a/test/Data/Syntax/Assignment/Spec.hs +++ b/test/Data/Syntax/Assignment/Spec.hs @@ -51,7 +51,7 @@ spec = do () <$ runAssignment (children red) (startingState "a" [Rose (rec Blue 0 1) [Rose (rec Red 0 1) []]]) `shouldBe` Result () it "does not match if its subrule does not match" $ - (runAssignment (children red) (startingState "a" [Rose (rec Blue 0 1) [Rose (rec Green 0 1) []]])) `shouldBe` Error [ "Expected Red but got Green" ] + (runAssignment (children red) (startingState "a" [Rose (rec Blue 0 1) [Rose (rec Green 0 1) []]])) `shouldBe` Error [ "Expected Red but got Green:\na\n^" ] it "matches nested children" $ do runAssignment