<commit_before>{-# LANGUAGE LambdaCase        #-}
{-# LANGUAGE OverloadedStrings #-}

{-
  Copyright 2017 The CodeWorld Authors. All rights reserved.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-}

module ParseCode (checkParsedCode) where

import qualified Data.ByteString as B
import           Data.ByteString.Char8 (pack)
import           Data.List.Split (splitOn)
import           Data.Generics
import           Language.Haskell.Exts

checkParsedCode :: FilePath -> FilePath -> IO Bool
checkParsedCode src err = do
    result <- parseFile src
    let parseList = words $ show result
        parseresult = head parseList
    if parseresult == "ParseFailed" then do
        source <- readFile src
        let sourceSplitList = splitOn "\n" source
            errLin = errLineLocation parseList
            errCol = errColumnLocation parseList
        B.writeFile err (pack ("ParseError, Error at: program.hs:"
                ++ (show $ errLin + 1) ++ ":" ++ (show errCol)
                ++ "\n>  " ++ (sourceSplitList !! errLin)))
        return False
        else do
            let outp = getErrors (getSrcSpanInfo result)
            if outp == [] then return True
                else do
                    B.writeFile err (pack (show outp))
                    return False

getSrcSpanInfo :: ParseResult (Module SrcSpanInfo) -> Module SrcSpanInfo
getSrcSpanInfo (ParseOk x) = x

getErrors :: Module SrcSpanInfo -> [String]<commit_msg>Remove quotes around error message<commit_after>{-# LANGUAGE LambdaCase        #-}
{-# LANGUAGE OverloadedStrings #-}

{-
  Copyright 2017 The CodeWorld Authors. All rights reserved.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-}

module ParseCode (checkParsedCode) where

import qualified Data.ByteString as B
import           Data.ByteString.Char8 (pack)
import           Data.List.Split (splitOn)
import           Data.Generics
import           Language.Haskell.Exts

checkParsedCode :: FilePath -> FilePath -> IO Bool
checkParsedCode src err = do
    result <- parseFile src
    let parseList = words $ show result
        parseresult = head parseList
    if parseresult == "ParseFailed" then do
        source <- readFile src
        let sourceSplitList = splitOn "\n" source
            errLin = errLineLocation parseList
            errCol = errColumnLocation parseList
        B.writeFile err (pack ("ParseError, Error at: program.hs:"
                ++ (show $ errLin + 1) ++ ":" ++ (show errCol)
                ++ "\n>  " ++ (sourceSplitList !! errLin)))
        return False
        else do
            let outp = getErrors (getSrcSpanInfo result)
            if outp == [] then return True
                else do
                    let outpString = tail $ tail $ init $ init (show outp)
                    B.writeFile err (pack outpString)
                    return False

getSrcSpanInfo :: ParseResult (Module SrcSpanInfo) -> Module SrcSpanInfo
getSrcSpanInfo (ParseOk x) = x

getErrors :: Module SrcSpanInfo -> [String]
{'fraction_non_alphanumeric': 0.07535795026375283, 'fraction_numerical': 0.005023863350916855, 'mean_word_length': 2.492982456140351, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4421890', 'n_tokens_mistral': 1067, 'n_tokens_neox': 1031, 'n_words': 412}