From 558c11596f99c8b0fc88dd100314469c3f0b2015 Mon Sep 17 00:00:00 2001 From: Alex Chen Date: Mon, 21 May 2018 19:09:47 -0600 Subject: [PATCH] lib: refactor the account name parser --- hledger-lib/Hledger/Read/Common.hs | 24 +++++++++--------------- hledger-lib/Hledger/Utils/Parse.hs | 5 ++++- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/hledger-lib/Hledger/Read/Common.hs b/hledger-lib/Hledger/Read/Common.hs index afd412410..332d88734 100644 --- a/hledger-lib/Hledger/Read/Common.hs +++ b/hledger-lib/Hledger/Read/Common.hs @@ -463,21 +463,15 @@ modifiedaccountnamep = do -- (This parser will also consume one following space, if present.) accountnamep :: TextParser m AccountName accountnamep = do - astr <- do - c <- nonspace - cs <- striptrailingspace <$> many (nonspace <|> singlespace) - return $ c:cs - let a = T.pack astr - when (accountNameFromComponents (accountNameComponents a) /= a) - (fail $ "account name seems ill-formed: "++astr) - return a - where - singlespace = try (do {spacenonewline; do {notFollowedBy spacenonewline; return ' '}}) - striptrailingspace "" = "" - striptrailingspace s = if last s == ' ' then init s else s - --- accountnamechar = notFollowedBy (oneOf "()[]") >> nonspace --- "account name character (non-bracket, non-parenthesis, non-whitespace)" + firstPart <- part + otherParts <- many $ try $ singleSpace *> part + let account = T.unwords $ firstPart : otherParts + when (accountNameFromComponents (accountNameComponents account) /= account) + (fail $ "account name seems ill-formed: " ++ T.unpack account) + pure account + where + part = takeWhile1P Nothing (not . isSpace) + singleSpace = void spacenonewline *> notFollowedBy spacenonewline --- ** amounts diff --git a/hledger-lib/Hledger/Utils/Parse.hs b/hledger-lib/Hledger/Utils/Parse.hs index 254fc958a..c920683b1 100644 --- a/hledger-lib/Hledger/Utils/Parse.hs +++ b/hledger-lib/Hledger/Utils/Parse.hs @@ -75,8 +75,11 @@ showDateParseError e = printf "date parse error (%s)" (intercalate ", " $ tail $ nonspace :: TextParser m Char nonspace = satisfy (not . isSpace) +isNonNewlineSpace :: Char -> Bool +isNonNewlineSpace c = c /= '\n' && isSpace c + spacenonewline :: (Stream s, Char ~ Token s) => ParsecT Void s m Char -spacenonewline = satisfy (`elem` " \v\f\t") +spacenonewline = satisfy isNonNewlineSpace restofline :: TextParser m String restofline = anyChar `manyTill` newline