From 4e9c09fb87c1004b952899a94eef70b1e55934ec Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Tue, 14 Jun 2011 14:27:48 +0000 Subject: [PATCH] refactor --- hledger-lib/Hledger/Data/Matching.hs | 54 ++++++++++++++-------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/hledger-lib/Hledger/Data/Matching.hs b/hledger-lib/Hledger/Data/Matching.hs index 134f14000..2863c7224 100644 --- a/hledger-lib/Hledger/Data/Matching.hs +++ b/hledger-lib/Hledger/Data/Matching.hs @@ -104,6 +104,33 @@ parseQuery d s = (m,qopts) (m':[]) -> m' ms -> MatchAnd ms +-- | Quote-and-prefix-aware version of words - don't split on spaces which +-- are inside quotes, including quotes which may have one of the specified +-- prefixes in front, and maybe an additional not: prefix in front of that. +words'' :: [String] -> String -> [String] +words'' prefixes = fromparse . parsewith maybeprefixedquotedphrases -- XXX + where + maybeprefixedquotedphrases = choice' [prefixedQuotedPattern, quotedPattern, pattern] `sepBy` many1 spacenonewline + prefixedQuotedPattern = do + not' <- optionMaybe $ string "not:" + prefix <- choice' $ map string prefixes + p <- quotedPattern + return $ fromMaybe "" not' ++ prefix ++ stripquotes p + quotedPattern = do + p <- between (oneOf "'\"") (oneOf "'\"") $ many $ noneOf "'\"" + return $ stripquotes p + pattern = many (noneOf " \n\r\"") + +-- -- | Parse the query string as a boolean tree of match patterns. +-- parseMatcher :: String -> Matcher +-- parseMatcher s = either (const (MatchAny)) id $ runParser matcher () "" $ lexmatcher s + +-- lexmatcher :: String -> [String] +-- lexmatcher s = words' s + +-- matcher :: GenParser String () Matcher +-- matcher = undefined + -- keep synced with patterns below, excluding "not" prefixes = map (++":") [ "inacct","inacctonly", @@ -146,33 +173,6 @@ parseBool s = s `elem` truestrings truestrings :: [String] truestrings = ["1","t","true"] --- | Quote-and-prefix-aware version of words - don't split on spaces which --- are inside quotes, including quotes which may have one of the specified --- prefixes in front, and maybe an additional not: prefix in front of that. -words'' :: [String] -> String -> [String] -words'' prefixes = fromparse . parsewith maybeprefixedquotedphrases -- XXX - where - maybeprefixedquotedphrases = choice' [prefixedQuotedPattern, quotedPattern, pattern] `sepBy` many1 spacenonewline - prefixedQuotedPattern = do - not' <- optionMaybe $ string "not:" - prefix <- choice' $ map string prefixes - p <- quotedPattern - return $ fromMaybe "" not' ++ prefix ++ stripquotes p - quotedPattern = do - p <- between (oneOf "'\"") (oneOf "'\"") $ many $ noneOf "'\"" - return $ stripquotes p - pattern = many (noneOf " \n\r\"") - --- -- | Parse the query string as a boolean tree of match patterns. --- parseMatcher :: String -> Matcher --- parseMatcher s = either (const (MatchAny)) id $ runParser matcher () "" $ lexmatcher s - --- lexmatcher :: String -> [String] --- lexmatcher s = words' s - --- matcher :: GenParser String () Matcher --- matcher = undefined - -- | Convert a match expression to its inverse. negateMatcher :: Matcher -> Matcher negateMatcher = MatchNot