From ad9654f6927317929413e326166d2ae1f5ece417 Mon Sep 17 00:00:00 2001 From: Danila Danko Date: Mon, 2 Dec 2024 22:33:23 +0300 Subject: [PATCH] refactor(eo-phi-normalizer): share modified parsing error message --- eo-phi-normalizer/src/Language/EO/Phi.hs | 5 ++--- eo-phi-normalizer/src/Language/EO/Phi/Rules/Common.hs | 10 +++++----- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/eo-phi-normalizer/src/Language/EO/Phi.hs b/eo-phi-normalizer/src/Language/EO/Phi.hs index e055a36c2..055295d44 100644 --- a/eo-phi-normalizer/src/Language/EO/Phi.hs +++ b/eo-phi-normalizer/src/Language/EO/Phi.hs @@ -36,13 +36,12 @@ import Language.EO.Phi.Syntax.Abs qualified as Phi import Language.EO.Phi.Syntax.Par qualified as Phi import Language.EO.Phi.Normalize +import Language.EO.Phi.Rules.Common (parseWith) import Language.EO.Phi.Syntax -- | Parse a 'Program' or return a parsing error. parseProgram :: String -> Either String Phi.Program -parseProgram input = Phi.pProgram tokens - where - tokens = Phi.myLexer input +parseProgram = parseWith Phi.pProgram -- | Parse an 'Object' or return a parsing error. parseObject :: String -> Either String Phi.Object 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 f5f234610..804fe11e7 100644 --- a/eo-phi-normalizer/src/Language/EO/Phi/Rules/Common.hs +++ b/eo-phi-normalizer/src/Language/EO/Phi/Rules/Common.hs @@ -71,20 +71,20 @@ instance IsString ObjectHead where fromString = unsafeParseWith pObjectHead instance IsString MetaId where fromString = unsafeParseWith pMetaId parseWith :: ([Token] -> Either String a) -> String -> Either String a -parseWith parser input = parser tokens +parseWith parser input = either (\x -> Left (escapeNonASCII [fmt|{x}\non the input:\n{input}|])) Right parsed where tokens = myLexer input + parsed = parser tokens + escapeNonASCII :: String -> String + escapeNonASCII = foldMap (\x -> if ord x < 256 then [x] else [fmt|\\{ord x}|]) -- | Parse a 'Object' from a 'String'. -- May throw an 'error` if input has a syntactical or lexical errors. unsafeParseWith :: ([Token] -> Either String a) -> String -> a unsafeParseWith parser input = case parseWith parser input of - Left parseError -> error (escapeNonASCII parseError <> "\non input\n" <> escapeNonASCII input <> "\n") + Left parseError -> error parseError 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 = ()