Polissage : CLI.Command.Balance : is_worth.
[comptalang.git] / lib / Hcompta / Filter / Read.hs
index 27f00a9a21f986470157f2cc4fc1761af35241e4..8f7604f5aca21566e48ccb51b30dd0262087a649 100644 (file)
@@ -8,7 +8,7 @@ module Hcompta.Filter.Read where
 import           Prelude hiding (filter)
 -- import           Control.Applicative ((<$>), (<*))
 import           Control.Exception (assert)
-import           Control.Monad (liftM, join, when)
+import           Control.Monad (liftM, join, when, (>=>), void)
 -- import           Control.Monad.Trans.Except (ExceptT(..), throwE)
 import qualified Data.Char
 import           Data.Data
@@ -94,7 +94,7 @@ filter_text
  => ParsecT s u m (String -> r Filter_Text)
 filter_text =
        R.choice_try
-        [ R.char '~' >> return (\s -> Regex.of_StringM s >>= (return . Filter_Text_Regex))
+        [ R.char '~' >> return (Regex.of_StringM >=> (return . Filter_Text_Regex))
         , R.char '=' >> return (\s -> return (Filter_Text_Exact $ Text.pack s))
         ,               return (\s -> return (Filter_Text_Exact $ Text.pack s))
         ]
@@ -114,11 +114,11 @@ filter_ord
  => ParsecT s u m (o -> Filter_Ord o)
 filter_ord =
        R.choice_try
-        [ R.string "="  >> return Filter_Ord_Eq
-        , R.string "<=" >> return Filter_Ord_Le
-        , R.string ">=" >> return Filter_Ord_Ge
-        , R.string "<"  >> return Filter_Ord_Lt
-        , R.string ">"  >> return Filter_Ord_Gt
+        [ R.string "="  >> return (Filter_Ord Eq)
+        , R.string "<=" >> return (Filter_Ord Le) -- NOTE: before "<"
+        , R.string ">=" >> return (Filter_Ord Ge) -- NOTE: before ">"
+        , R.string "<"  >> return (Filter_Ord Lt)
+        , R.string ">"  >> return (Filter_Ord Gt)
         ]
 
 filter_ord_operator
@@ -153,7 +153,7 @@ text none_of =
        where
                borders = R.between (R.char '(') (R.char ')')
                inside = liftM concat $ R.many (R.choice_try [borders preserve_inside, R.many1 $ R.noneOf "()"])
-               preserve_inside = inside >>= (\x -> return $ '(':(x++')':[]))
+               preserve_inside = inside >>= (\x -> return $ '(':(x++[')']))
 
 -- ** Read 'Filter_Bool'
 
@@ -180,8 +180,8 @@ filter_bool_operators =
          ]
        ]
        where
-               binary  name fun assoc = R.Infix  (filter_bool_operator name >> return fun) assoc
-               prefix  name fun       = R.Prefix (filter_bool_operator name >> return fun)
+               binary name fun = R.Infix  (filter_bool_operator name >> return fun)
+               prefix name fun = R.Prefix (filter_bool_operator name >> return fun)
                -- postfix name fun       = Text.Parsec.Expr.Postfix (filter_bool_operator name >> return fun)
 
 filter_bool_operator
@@ -293,8 +293,8 @@ filter_account_section = do
        where
                account_section_end =
                        R.choice_try
-                        [ R.char account_section_sep >> return ()
-                        , R.space_horizontal >> return ()
+                        [ void $ R.char account_section_sep
+                        , void $ R.space_horizontal
                         , R.eof
                         ]
 
@@ -307,8 +307,12 @@ filter_account
  => ParsecT s u m Filter_Account
 filter_account = do
        R.notFollowedBy $ R.space_horizontal
-       R.many1_separated filter_account_section $
-               R.char account_section_sep
+       Filter_Ord o () <-
+               R.option (Filter_Ord Eq ()) $ R.try $
+                       (\f -> f ()) <$> filter_ord
+       fmap (Filter_Account o) $
+               R.many1_separated filter_account_section $
+                       R.char account_section_sep
 
 filter_account_operator
  :: Stream s m Char
@@ -358,8 +362,8 @@ filter_date = do
                return $ do
                        ctx <- R.getState
                        let (year, _, _) = Date.gregorian $ context_date ctx
-                       Date.Read.date Error_Filter_Date (Just year)
-                       >>= return . Bool . Filter_Date_UTC . tst
+                       liftM (Bool . Filter_Date_UTC . tst) $
+                               Date.Read.date Error_Filter_Date (Just year)
         ]
        where
                read_date_pattern
@@ -391,7 +395,7 @@ filter_date = do
                                        , d2 )
                        (hour, minute, second) <-
                                R.option (Interval.unlimited, Interval.unlimited, Interval.unlimited) $ R.try $ do
-                                       R.skipMany1 $ R.space_horizontal
+                                       _ <- R.char '_'
                                        hour   <- read_interval Error_Filter_Date_Interval read2
                                        sep    <- Date.Read.hour_separator
                                        minute <- read_interval Error_Filter_Date_Interval read2
@@ -399,7 +403,7 @@ filter_date = do
                                                _    <- R.char sep
                                                read_interval Error_Filter_Date_Interval $ of_digits <$> R.many1 R.digit
                                        -- tz <- R.option Time.utc $ R.try $ do
-                                       --      R.skipMany $ R.space_horizontal
+                                       --      -- R.skipMany $ R.space_horizontal
                                        --      Date.Read.time_zone
                                        return
                                         ( hour
@@ -437,7 +441,7 @@ read_interval err read_digits = do
                 ]
        R.choice_try
         [ when (l /= Interval.Unlimited_low)
-                (R.string ".." >> return ()) >> do
+                (void $ R.string "..") >> do
                h <- R.choice_try
                         [ Interval.Limited <$> read_digits
                         , return Interval.Unlimited_high
@@ -476,8 +480,8 @@ filter_tag_name = do
        where
                filter_tag_name_end =
                        R.choice_try
-                        [ filter_text_operator >> return ()
-                        , R.space_horizontal >> return ()
+                        [ void $ filter_text_operator
+                        , void $ R.space_horizontal
                         , R.eof
                         ]
 filter_tag_value
@@ -495,7 +499,7 @@ filter_tag_value = do
        where
                filter_tag_value_end =
                        R.choice_try
-                        [ R.space_horizontal >> return ()
+                        [ void $ R.space_horizontal
                         , R.eof
                         ]
 
@@ -597,7 +601,7 @@ filter_balance_terms =
        , jump [ "C" ] filter_amount_operator
                ( Filter.Filter_Balance_Negative
                <$> filter_amount )
-       , jump [ "B", "" ] filter_amount_operator
+       , jump [ "B" ] filter_amount_operator
                ( Filter.Filter_Balance_Amount
                <$> filter_amount )
        , return