From 30a402e7f6dc353f94150707ad1b35ef52cd3fad Mon Sep 17 00:00:00 2001 From: Danila Danko Date: Fri, 29 Nov 2024 19:44:45 +0300 Subject: [PATCH] feat(eo-phi-normalizer): add simple unicode escaping --- eo-phi-normalizer/src/Language/EO/Phi/Rules/Common.hs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/eo-phi-normalizer/src/Language/EO/Phi/Rules/Common.hs b/eo-phi-normalizer/src/Language/EO/Phi/Rules/Common.hs index c641134aa..f5f234610 100644 --- a/eo-phi-normalizer/src/Language/EO/Phi/Rules/Common.hs +++ b/eo-phi-normalizer/src/Language/EO/Phi/Rules/Common.hs @@ -27,6 +27,7 @@ {-# LANGUAGE DerivingStrategies #-} {-# LANGUAGE GeneralisedNewtypeDeriving #-} {-# LANGUAGE LambdaCase #-} +{-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE RecordWildCards #-} {-# LANGUAGE ViewPatterns #-} {-# OPTIONS_GHC -Wno-orphans #-} @@ -39,7 +40,7 @@ import Control.Arrow (Arrow (first)) import Control.Monad import Data.ByteString (ByteString) import Data.ByteString qualified as ByteString.Strict -import Data.Char (toUpper) +import Data.Char (ord, toUpper) import Data.HashMap.Strict qualified as HashMap import Data.List (intercalate, minimumBy, nubBy, sortOn) import Data.List.NonEmpty (NonEmpty (..), (<|)) @@ -52,6 +53,7 @@ import Language.EO.Phi.Syntax.Abs import Language.EO.Phi.Syntax.Lex (Token) import Language.EO.Phi.Syntax.Par import Numeric (readHex, showHex) +import PyF (fmt) -- $setup -- >>> :set -XOverloadedStrings @@ -78,8 +80,11 @@ parseWith parser input = parser tokens unsafeParseWith :: ([Token] -> Either String a) -> String -> a unsafeParseWith parser input = case parseWith parser input of - Left parseError -> error (parseError <> "\non input\n" <> input <> "\n") + Left parseError -> error (escapeNonASCII parseError <> "\non input\n" <> escapeNonASCII input <> "\n") Right object -> object + where + escapeNonASCII :: String -> String + escapeNonASCII = foldMap (\x -> if ord x < 256 then [x] else [fmt|\\{ord x}|]) -- | State of evaluation is not needed yet, but it might be in the future type EvaluationState = ()