<commit_before>-----------------------------------------------------------------------------
--
-- Module      :  Language.PureScript.Error
-- Copyright   :  (c) 2013-14 <NAME>, (c) 2014 <NAME>, and other contributors
-- License     :  MIT
--
-- Maintainer  :  <NAME> <<EMAIL>>
-- Stability   :  experimental
-- Portability :
--
-- |
--
-----------------------------------------------------------------------------

module Language.PureScript.Errors where

import Prelude.Unsafe (unsafeIndex)
import Data.Array
import qualified Data.Array.Unsafe as Unsafe
import Data.String (joinWith)
import Data.Maybe
import Data.Monoid
import Data.Either
import Data.Monoid.Last
import Data.Foldable (mconcat)

import Control.Monad.Error
import Control.Monad.Error.Class
import Control.Monad.Error.Proxy

import Language.PureScript.Pos
import Language.PureScript.Declarations
import Language.PureScript.Pretty.Values
import Language.PureScript.Pretty.Types
import Language.PureScript.Types

-- |
-- Type for sources of type checking errors
--
data ErrorSource
  -- |
  -- An error which originated at a Value
  --
  = ValueError Value
  -- |
  -- An error which originated at a Type
  --
  | TypeError Type
  
instance showErrorSource :: Show ErrorSource where
  show (ValueError v) = "ValueError (" ++ show v ++ ")"
  show (TypeError t) = "TypeError (" ++ show t ++ ")"

-- |
-- Compilation errors
--
data CompileError = CompileError {
    -- |
    -- Error message
    --
    compileErrorMessage :: String
    -- |
    -- The value where the error occurred
    --
  , compileErrorValue :: Maybe ErrorSource
    -- |
    -- Optional source position information
    --
  , compileErrorPosition :: Maybe SourcePos
  }
  
mkCompileError :: String -> Maybe ErrorSource -> Maybe SourcePos -> CompileError
mkCompileError compileErrorMessage compileErrorValue compileErrorPosition = 
  CompileError { compileErrorMessage: compileErrorMessage
	             , compileErrorValue: compileErrorValue
							 , compileErrorPosition: compileErrorPosition
							 }
  
instance showCompileError :: Show CompileError where
  show (CompileError o) = "CompileError {" ++
    "compileErrorMessage: " ++  show o.compileErrorMessage ++ ", " ++
    "compileErrorValue: " ++    show o.compileErrorValue ++ ", " ++
    "compileErrorPosition: " ++ show o.compileErrorPosition ++ " " ++
	  "}" 

-- |
-- A stack trace for an error
--
data ErrorStack = ErrorStack [CompileError] 

runErrorStack :: ErrorStack -> [CompileError] 
runErrorStack (ErrorStack es) = es

instance showErrorStack :: Show ErrorStack where
  show (ErrorStack es) = "ErrorStack (" ++ show es ++ ")"
  
instance semigroupErrorStack :: Semigroup ErrorStack where
  (<>) (ErrorStack es1) (ErrorStack es2) = ErrorStack (es1 <> es2)
  
instance monoidErrorStack :: Monoid ErrorStack where
  mempty = ErrorStack []

instance errorErrorStack :: Error ErrorStack where
  strMsg s = ErrorStack [mkCompileError s Nothing Nothing]
  noMsg = ErrorStack []
  
unifyError :: WithErrorType ErrorStack
unifyError = WithErrorType

prettyPrintErrorStack :: Boolean -> ErrorStack -> String
prettyPrintErrorStack printFullStack (ErrorStack es) =
  case mconcat $ map (\(CompileError o) -> Last o.compileErrorPosition) es of
    Last (Just sourcePos) -> "Error at " ++ show sourcePos ++ ": \n" ++ prettyPrintErrorStack'
    _ -> prettyPrintErrorStack'
  where
	  
    prettyPrintErrorStack' :: String
    prettyPrintErrorStack' = 
		  if printFullStack 
	    then
		  	joinWith "\n" (map showError (filter isErrorNonEmpty es))
      else
		  	let
          es' = filter isErrorNonEmpty es
        in case length es' of
          1 -> showError (Unsafe.head es')
          _ -> showError (Unsafe.head es') ++ "\n" ++ showError (Unsafe.last es')

stringifyErrorStack :: forall a. Boolean -> Either ErrorStack a -> Either String a
stringifyErrorStack printFullStack = either (Left <<< prettyPrintErrorStack printFullStack) Right

isErrorNonEmpty :: CompileError -> Boolean
isErrorNonEmpty (CompileError { compileErrorMessage = "" }) = false
isErrorNonEmpty _ = true

showError :: CompileError -> String
showError (CompileError { compileErrorMessage = msg, compileErrorValue = Nothing }) = msg
showError (CompileError { compileErrorMessage = msg, compileErrorValue = Just (ValueError val) }) = "Error in value " ++ prettyPrintValue val ++ ":\n" ++ msg
showError (CompileError { compileErrorMessage = msg, compileErrorValue = Just (TypeError ty) }) = "Error in type " ++ prettyPrintType ty ++ ":\n" ++ msg<commit_msg>Simplify error object property names<commit_after>-----------------------------------------------------------------------------
--
-- Module      :  Language.PureScript.Error
-- Copyright   :  (c) 2013-14 <NAME>, (c) 2014 <NAME>, and other contributors
-- License     :  MIT
--
-- Maintainer  :  <NAME> <<EMAIL>>
-- Stability   :  experimental
-- Portability :
--
-- |
--
-----------------------------------------------------------------------------

module Language.PureScript.Errors where

import Prelude.Unsafe (unsafeIndex)
import Data.Array
import qualified Data.Array.Unsafe as Unsafe
import Data.String (joinWith)
import Data.Maybe
import Data.Monoid
import Data.Either
import Data.Monoid.Last
import Data.Foldable (mconcat)

import Control.Monad.Error
import Control.Monad.Error.Class
import Control.Monad.Error.Proxy

import Language.PureScript.Pos
import Language.PureScript.Declarations
import Language.PureScript.Pretty.Values
import Language.PureScript.Pretty.Types
import Language.PureScript.Types

-- |
-- Type for sources of type checking errors
--
data ErrorSource
  -- |
  -- An error which originated at a Value
  --
  = ValueError Value
  -- |
  -- An error which originated at a Type
  --
  | TypeError Type
  
instance showErrorSource :: Show ErrorSource where
  show (ValueError v) = "ValueError (" ++ show v ++ ")"
  show (TypeError t) = "TypeError (" ++ show t ++ ")"

-- |
-- Compilation errors
--
data CompileError = CompileError {
    -- |
    -- Error message
    --
    message :: String
    -- |
    -- The value where the error occurred
    --
  , value :: Maybe ErrorSource
    -- |
    -- Optional source position information
    --
  , position :: Maybe SourcePos
  }
  
mkCompileError :: String -> Maybe ErrorSource -> Maybe SourcePos -> CompileError
mkCompileError message value position = 
  CompileError { message: message
	             , value: value
							 , position: position
							 }
  
instance showCompileError :: Show CompileError where
  show (CompileError o) = "CompileError {" ++
    "message: " ++  show o.message ++ ", " ++
    "value: " ++    show o.value ++ ", " ++
    "position: " ++ show o.position ++ " " ++
	  "}" 

-- |
-- A stack trace for an error
--
data ErrorStack = ErrorStack [CompileError] 

runErrorStack :: ErrorStack -> [CompileError] 
runErrorStack (ErrorStack es) = es

instance showErrorStack :: Show ErrorStack where
  show (ErrorStack es) = "ErrorStack (" ++ show es ++ ")"
  
instance semigroupErrorStack :: Semigroup ErrorStack where
  (<>) (ErrorStack es1) (ErrorStack es2) = ErrorStack (es1 <> es2)
  
instance monoidErrorStack :: Monoid ErrorStack where
  mempty = ErrorStack []

instance errorErrorStack :: Error ErrorStack where
  strMsg s = ErrorStack [mkCompileError s Nothing Nothing]
  noMsg = ErrorStack []
  
unifyError :: WithErrorType ErrorStack
unifyError = WithErrorType

prettyPrintErrorStack :: Boolean -> ErrorStack -> String
prettyPrintErrorStack printFullStack (ErrorStack es) =
  case mconcat $ map (\(CompileError o) -> Last o.position) es of
    Last (Just sourcePos) -> "Error at " ++ show sourcePos ++ ": \n" ++ prettyPrintErrorStack'
    _ -> prettyPrintErrorStack'
  where
	  
    prettyPrintErrorStack' :: String
    prettyPrintErrorStack' = 
		  if printFullStack 
	    then
		  	joinWith "\n" (map showError (filter isErrorNonEmpty es))
      else
		  	let
          es' = filter isErrorNonEmpty es
        in case length es' of
          1 -> showError (Unsafe.head es')
          _ -> showError (Unsafe.head es') ++ "\n" ++ showError (Unsafe.last es')

stringifyErrorStack :: forall a. Boolean -> Either ErrorStack a -> Either String a
stringifyErrorStack printFullStack = either (Left <<< prettyPrintErrorStack printFullStack) Right

isErrorNonEmpty :: CompileError -> Boolean
isErrorNonEmpty (CompileError { message = "" }) = false
isErrorNonEmpty _ = true

showError :: CompileError -> String
showError (CompileError { message = msg, value = Nothing }) = msg
showError (CompileError { message = msg, value = Just (ValueError val) }) = "Error in value " ++ prettyPrintValue val ++ ":\n" ++ msg
showError (CompileError { message = msg, value = Just (TypeError ty) }) = "Error in type " ++ prettyPrintType ty ++ ":\n" ++ msg
{'fraction_non_alphanumeric': 0.14248201849526201, 'fraction_numerical': 0.0034250485215207216, 'mean_word_length': 2.9264903630658896, 'pattern_counts': {'":': 6, '<': 23, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5983408', 'n_tokens_mistral': 2523, 'n_tokens_neox': 2394, 'n_words': 731}