From 9a650fbb51600c932ccf0c01e512a336369aa98b Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Tue, 6 Jun 2017 09:45:23 -0400 Subject: [PATCH] showError returns a String. --- src/Data/Syntax/Assignment.hs | 4 +++- src/Parser.hs | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Data/Syntax/Assignment.hs b/src/Data/Syntax/Assignment.hs index b68944390..0ae61e9c8 100644 --- a/src/Data/Syntax/Assignment.hs +++ b/src/Data/Syntax/Assignment.hs @@ -88,6 +88,7 @@ import qualified Data.IntMap.Lazy as IntMap import Data.Ix (inRange) import Data.List.NonEmpty (nonEmpty) import Data.Record +import Data.String import GHC.Stack import qualified Info import Prologue hiding (Alt, get, Location, state) @@ -168,12 +169,13 @@ data ErrorCause symbol deriving (Eq, Show) -- | Pretty-print an Error with reference to the source where it occurred. -showError :: Show symbol => Source.Source -> Error symbol -> ShowS +showError :: Show symbol => Source.Source -> Error symbol -> String showError source error@Error{..} = withSGRCode [SetConsoleIntensity BoldIntensity] (showSourcePos Nothing errorPos) . showString ": " . withSGRCode [SetColor Foreground Vivid Red] (showString "error") . showString ": " . showExpectation error . showChar '\n' . showString context -- actualLines results include line endings, so no newline here . showString (replicate (succ (Info.column errorPos + lineNumberDigits)) ' ') . withSGRCode [SetColor Foreground Vivid Green] (showChar '^') . showChar '\n' . showString (prettyCallStack callStack) . showChar '\n' + $ "" where context = maybe "\n" (toS . Source.sourceText . sconcat) (nonEmpty [ Source.Source (toS (showLineNumber i)) <> Source.Source ": " <> l | (i, l) <- zip [1..] (Source.actualLines source), inRange (Info.line errorPos - 2, Info.line errorPos) i ]) showLineNumber n = let s = show n in replicate (lineNumberDigits - length s) ' ' <> s lineNumberDigits = succ (floor (logBase 10 (fromIntegral (Info.line errorPos) :: Double))) diff --git a/src/Parser.hs b/src/Parser.hs index cd290bd1d..bf152ead1 100644 --- a/src/Parser.hs +++ b/src/Parser.hs @@ -66,7 +66,7 @@ runParser parser = case parser of let Result err term = assign assignment source ast case term of Just term -> do - traverse_ (putStr . ($ "") . showError source) (toList err <> termErrors term) + traverse_ (putStr . showError source) (toList err <> termErrors term) pure term Nothing -> pure (errorTerm source err) TreeSitterParser language tslanguage -> treeSitterParser language tslanguage