From a8690f248940cb5396bc051e7a7bcbce7c3c7ae1 Mon Sep 17 00:00:00 2001 From: liamzee Date: Sat, 23 Sep 2023 00:43:51 +0800 Subject: [PATCH 1/2] Fixed small issue with calls to lookup, even though it'll be deleted. Added a bit of hacky code to normalize a keyword before map lookup. --- cabal-parser/src/Text/Cabal/Parser.hs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/cabal-parser/src/Text/Cabal/Parser.hs b/cabal-parser/src/Text/Cabal/Parser.hs index 0292bd22e4..8fd3e39958 100644 --- a/cabal-parser/src/Text/Cabal/Parser.hs +++ b/cabal-parser/src/Text/Cabal/Parser.hs @@ -12,6 +12,7 @@ import Text.Cabal.Types import Text.Cabal.Value import Text.Megaparsec import Text.Megaparsec.Char +import Data.Char (isSpace) -------------------------------- -- Parser @@ -67,7 +68,7 @@ fieldParser indentation = do hspace ((kw, vals), fieldLoc) <- annotateSrcLoc $ do (keyword, kwLoc) <- annotateSrcLoc $ choice [keywordParser, externalKeywordParser] - let valParser = fromMaybe defaultValueParser $ M.lookup keyword allKeywords + let valParser = fromMaybe defaultValueParser $ M.lookup (normalize keyword) allKeywords values <- valuesParser valParser indentNum pure (KeyWord keyword (Annotation Nothing kwLoc), values) pure (Just indentNum, Field kw vals (Annotation Nothing fieldLoc)) @@ -80,6 +81,13 @@ fieldParser indentation = do kw <- keywordParser pure $ prefix <> kw + normalize :: T.Text -> T.Text + normalize = + T.toLower + . (`snoc` ':') + . T.dropWhileEnd (\u -> isSpace u && u /= '\n' && u /= '\r') + . T.dropEnd 1 + -- a keyword is some word ending with a colon keywordParser :: Parser T.Text keywordParser = do From 40b99ad84e7e545e124978926a045cb1d34e687c Mon Sep 17 00:00:00 2001 From: Liam <104887453+liamzee@users.noreply.github.com> Date: Fri, 22 Sep 2023 13:19:08 -0400 Subject: [PATCH 2/2] Update Parser.hs Realized I had a bug where snoc wouldn't be available, and added commenting to change. --- cabal-parser/src/Text/Cabal/Parser.hs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cabal-parser/src/Text/Cabal/Parser.hs b/cabal-parser/src/Text/Cabal/Parser.hs index 8fd3e39958..24448abd15 100644 --- a/cabal-parser/src/Text/Cabal/Parser.hs +++ b/cabal-parser/src/Text/Cabal/Parser.hs @@ -81,10 +81,14 @@ fieldParser indentation = do kw <- keywordParser pure $ prefix <> kw + -- Used to normalize the parsed value such that if the keyword + -- matches, the lookup will succeed. Previously, the lookup + -- required an exact match. + normalize :: T.Text -> T.Text normalize = - T.toLower - . (`snoc` ':') + (`T.snoc` ':') + . T.toLower . T.dropWhileEnd (\u -> isSpace u && u /= '\n' && u /= '\r') . T.dropEnd 1