diff options
| author | Roman Smrž <roman.smrz@seznam.cz> | 2026-11-21 12:20:02 +0100 |
|---|---|---|
| committer | Roman Smrž <roman.smrz@seznam.cz> | 2026-11-23 19:55:55 +0100 |
| commit | 37ea590bd3544ca7ed4f181b37fe3622abaeada0 (patch) | |
| tree | 8c4ce08b910d669669c7b4ea0e8df9a9340cea6e | |
| parent | b56ba75e24a38fff01aba03ac68aeb655ead2daf (diff) | |
| -rw-r--r-- | src/Parser.hs | 31 | ||||
| -rw-r--r-- | test/asset/parser/invalid-utf8.et | 4 | ||||
| -rw-r--r-- | test/asset/parser/utf8.et | 4 | ||||
| -rw-r--r-- | test/script/parser.et | 5 |
4 files changed, 42 insertions, 2 deletions
diff --git a/src/Parser.hs b/src/Parser.hs index b59dc57..e877272 100644 --- a/src/Parser.hs +++ b/src/Parser.hs @@ -11,6 +11,7 @@ import Control.Monad.State import Data.ByteString.Lazy qualified as BL import Data.IORef +import Data.List.NonEmpty (NonEmpty(..)) import Data.Map qualified as M import Data.Maybe import Data.Proxy @@ -258,10 +259,12 @@ parseTestFile builtinTypes parsedModules mbModuleName path = do let projectRoot = iterate takeDirectory absPath !! length current parseTestFile builtinTypes parsedModules (Just mname) $ projectRoot </> foldr (</>) "" (map T.unpack imported) <.> takeExtension absPath } - mbContent <- (Just . TL.decodeUtf8 <$> BL.readFile path) `catchIOError` \e -> + mbContent <- (Just . decodeSource path <$> BL.readFile path) `catchIOError` \e -> if isDoesNotExistError e then return Nothing else ioError e case mbContent of - Just content -> do + Just (Left bundle) -> do + return $ Left $ ImportModuleError bundle + Just (Right content) -> do runTestParser content initState (parseTestModule absPath) >>= \case Left bundle -> do return $ Left $ ImportModuleError bundle @@ -269,3 +272,27 @@ parseTestFile builtinTypes parsedModules mbModuleName path = do modifyIORef parsedModules (( absPath, testModule ) : ) return $ Right testModule Nothing -> return $ Left $ maybe (FileNotFound path) ModuleNotFound mbModuleName + +decodeSource :: FilePath -> BL.ByteString -> Either (ParseErrorBundle TestStream CustomTestError) TestStream +decodeSource path bytes = + case TL.decodeUtf8' bytes of + Right content -> Right content + -- Decode leniently with two different replacement characters; the + -- results first differ at the position of the first invalid byte. + Left _ -> do + let decodeWith c = TL.decodeUtf8With (\_ _ -> Just c) bytes + content = decodeWith '\xFFFD' + content' = decodeWith '\xFFFE' + off = case TL.commonPrefixes content content' of + Just ( prefix, _, _ ) -> TL.length prefix + Nothing -> 0 + Left ParseErrorBundle + { bundleErrors = FancyError (fromIntegral off) (S.singleton $ ErrorFail "invalid UTF-8 encoding") :| [] + , bundlePosState = PosState + { pstateInput = content + , pstateOffset = 0 + , pstateSourcePos = initialPos path + , pstateTabWidth = defaultTabWidth + , pstateLinePrefix = "" + } + } diff --git a/test/asset/parser/invalid-utf8.et b/test/asset/parser/invalid-utf8.et new file mode 100644 index 0000000..7f6b4fd --- /dev/null +++ b/test/asset/parser/invalid-utf8.et @@ -0,0 +1,4 @@ +# Script with invalid UTF-8: the byte 0xE9 is Latin-1 é, not valid UTF-8 +test Test: + let x = "café" + guard (x == x) diff --git a/test/asset/parser/utf8.et b/test/asset/parser/utf8.et new file mode 100644 index 0000000..d0c06e1 --- /dev/null +++ b/test/asset/parser/utf8.et @@ -0,0 +1,4 @@ +# Script with valid non-ASCII UTF-8 content: Šíleně žluťoučký kůň … +test Test: + let x = "… úpěl ďábelské ódy “‘quoted’”" + guard (x == x) diff --git a/test/script/parser.et b/test/script/parser.et index 5c45876..5843afe 100644 --- a/test/script/parser.et +++ b/test/script/parser.et @@ -22,3 +22,8 @@ test Parser: expect_load_failure using p of "function-fail" giving "parse-error" expect_load_success using p of "tags" expect_load_success using p of "type-annotation" + +test ParseUtf8: + spawn as p + expect_load_success using p of "utf8" + expect_load_failure using p of "invalid-utf8" giving "parse-error" |