1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE NamedFieldPuns #-}
4 {-# LANGUAGE OverloadedStrings #-}
5 {-# LANGUAGE ScopedTypeVariables #-}
6 {-# LANGUAGE TupleSections #-}
7 module Hcompta.Format.Ledger.Read where
9 import Control.Applicative ((<$>), (<*>), (<*))
10 import qualified Control.Exception as Exception
11 import Control.Arrow ((***))
12 import Control.Monad (guard, join, liftM, (>=>))
13 import Control.Monad.IO.Class (liftIO)
14 import Control.Monad.Trans.Except (ExceptT(..), throwE)
15 import qualified Data.Char
16 import qualified Data.Decimal
17 import qualified Data.Either
18 import qualified Data.List
19 import Data.List.NonEmpty (NonEmpty(..))
20 import qualified Data.Map.Strict as Data.Map
21 import Data.Maybe (fromMaybe)
22 import qualified Data.Time.Calendar as Time
23 import qualified Data.Time.Clock as Time
24 import qualified Data.Time.LocalTime as Time
25 import Data.Time.LocalTime (TimeZone(..))
26 import Data.Typeable ()
27 import qualified Text.Parsec as R
28 import Text.Parsec (Stream, ParsecT, (<|>), (<?>))
29 import qualified Data.Text.IO as Text.IO (readFile)
30 import qualified Data.Text as Text
31 import qualified System.FilePath.Posix as Path
33 import qualified Hcompta.Model.Account as Account
34 import Hcompta.Model.Account (Account)
35 import qualified Hcompta.Model.Amount as Amount
36 import Hcompta.Model.Amount (Amount)
37 import qualified Hcompta.Model.Amount.Style as Style
38 import qualified Hcompta.Model.Amount.Unit as Unit
39 import Hcompta.Model.Amount.Unit (Unit)
40 import qualified Hcompta.Model.Transaction as Transaction
41 import Hcompta.Model.Transaction (Transaction, Comment)
42 import qualified Hcompta.Model.Transaction.Posting as Posting
43 import Hcompta.Model.Transaction (Posting)
44 import qualified Hcompta.Model.Transaction.Tag as Tag
45 import Hcompta.Model.Transaction (Tag)
46 import qualified Hcompta.Model.Date as Date
47 import Hcompta.Model.Date (Date)
48 import Hcompta.Format.Ledger.Journal as Journal
49 import qualified Hcompta.Lib.Regex as Regex
50 import Hcompta.Lib.Regex (Regex)
51 import Hcompta.Lib.Parsec as R
52 import qualified Hcompta.Lib.Path as Path
56 { context_account_prefix :: !(Maybe Account)
57 , context_aliases_exact :: !(Data.Map.Map Account Account)
58 , context_aliases_joker :: ![(Account.Joker, Account)]
59 , context_aliases_regex :: ![(Regex, Account)]
60 , context_date :: !Date
61 , context_unit_and_style :: !(Maybe (Amount.Unit, Amount.Style))
62 , context_journal :: !Journal
63 , context_year :: !Date.Year
66 nil_Context :: Context
69 { context_account_prefix = Nothing
70 , context_aliases_exact = Data.Map.empty
71 , context_aliases_joker = []
72 , context_aliases_regex = []
73 , context_date = Date.nil
74 , context_unit_and_style = Nothing
75 , context_journal = Journal.nil
76 , context_year = (\(year, _ , _) -> year) $
77 Time.toGregorian $ Time.utctDay $
78 Journal.last_read_time Journal.nil
81 -- | Parse either '-' into 'negate', or '+' or '' into 'id'.
82 sign :: (Stream s m Char, Num i) => ParsecT s u m (i -> i)
84 (R.char '-' >> return negate)
85 <|> (R.char '+' >> return id)
88 -- * Parsing 'Account'
90 account_name_sep :: Char
91 account_name_sep = ':'
93 -- | Parse an 'Account'.
94 account :: Stream s m Char => ParsecT s u m Account
96 R.notFollowedBy $ space_horizontal
97 Account.from_List <$> do
98 many1_separated account_name $ R.char account_name_sep
100 -- | Parse an Account.'Account.Name'.
101 account_name :: Stream s m Char => ParsecT s u m Account.Name
104 R.many1 $ R.try account_name_char
106 account_name_char :: Stream s m Char => ParsecT s u m Char
107 account_name_char = do
110 _ | c == comment_begin -> R.parserZero
111 _ | c == account_name_sep -> R.parserZero
112 _ | is_space_horizontal c -> do
113 _ <- R.notFollowedBy $ space_horizontal
114 return c <* (R.lookAhead $ R.try $
115 ( R.try (R.char account_name_sep)
116 <|> account_name_char
118 _ | not (Data.Char.isSpace c) -> return c
121 -- | Parse an Account.'Account.Joker_Name'.
122 account_joker_name :: Stream s m Char => ParsecT s u m Account.Joker_Name
123 account_joker_name = do
124 n <- R.option Nothing $ (Just <$> account_name)
126 Nothing -> R.char account_name_sep >> (return $ Account.Joker_Any)
127 Just n' -> return $ Account.Joker_Name n'
129 -- | Parse an Account.'Account.Joker'.
130 account_joker :: Stream s m Char => ParsecT s u m Account.Joker
132 R.notFollowedBy $ space_horizontal
133 many1_separated account_joker_name $ R.char account_name_sep
135 -- | Parse a 'Regex'.
136 account_regex :: Stream s m Char => ParsecT s u m Regex
138 re <- R.many1 $ R.satisfy (not . is_space_horizontal)
141 -- | Parse an Account.'Account.Filter'.
142 account_pattern :: Stream s m Char => ParsecT s u m Account.Pattern
145 [ Account.Pattern_Exact <$> (R.char '=' >> account)
146 , Account.Pattern_Joker <$> (R.char '*' >> account_joker)
147 , Account.Pattern_Regex <$> (R.option '~' (R.char '~') >> account_regex)
150 -- * Parsing 'Amount'
152 -- | Parse an 'Amount'.
153 amount :: Stream s m Char => ParsecT s u m Amount
157 R.option Nothing $ do
159 s <- R.many $ space_horizontal
160 return $ Just $ (u, not $ null s)
161 (quantity_, style) <- do
168 , grouping_fractional
171 [ quantity '_' ',' '_' <* (R.notFollowedBy $ R.oneOf ",._")
172 , quantity '_' '.' '_' <* (R.notFollowedBy $ R.oneOf ",._")
173 , quantity ',' '.' '_' <* (R.notFollowedBy $ R.oneOf ",._")
174 , quantity '.' ',' '_' <* (R.notFollowedBy $ R.oneOf ",._")
176 let int = Data.List.concat integral
177 let frac_flat = Data.List.concat fractional
178 let frac = reverse $ Data.List.dropWhile (== '0') $ reverse $ frac_flat
179 let place = length frac
181 let mantissa = R.integer_of_digits 10 $ int ++ frac
183 ( Data.Decimal.Decimal
188 , Style.grouping_integral
189 , Style.grouping_fractional
190 , Style.precision = fromIntegral $ length frac_flat
193 (unit_, unit_side, unit_spaced) <-
196 return (u, Just Style.Side_Left, Just s)
198 R.option (Unit.nil, Nothing, Nothing) $ do
199 s <- R.many $ space_horizontal
201 return $ (u, Just Style.Side_Right, Just $ not $ null s)
204 { Amount.quantity = left_signing $ quantity_
205 , Amount.style = style
209 , Amount.unit = unit_
214 { integral :: [String]
215 , fractional :: [String]
216 , fractioning :: Maybe Style.Fractioning
217 , grouping_integral :: Maybe Style.Grouping
218 , grouping_fractional :: Maybe Style.Grouping
221 -- | Parse a 'Quantity'.
224 => Char -- ^ Integral grouping separator.
225 -> Char -- ^ Fractioning separator.
226 -> Char -- ^ Fractional grouping separator.
227 -> ParsecT s u m Quantity
228 quantity int_group_sep frac_sep frac_group_sep = do
229 (integral, grouping_integral) <- do
232 [] -> return ([], Nothing)
234 t <- R.many $ R.char int_group_sep >> R.many1 R.digit
236 return (digits, grouping_of_digits int_group_sep digits)
237 (fractional, fractioning, grouping_fractional) <-
240 _ -> R.option ([], Nothing, Nothing)) $ do
241 fractioning <- R.char frac_sep
243 t <- R.many $ R.char frac_group_sep >> R.many1 R.digit
245 return (digits, Just fractioning
246 , grouping_of_digits frac_group_sep $ reverse digits)
253 , grouping_fractional
256 grouping_of_digits :: Char -> [String] -> Maybe Style.Grouping
257 grouping_of_digits group_sep digits =
262 Style.Grouping group_sep $
263 canonicalize_grouping $
265 canonicalize_grouping :: [Int] -> [Int]
266 canonicalize_grouping groups =
267 Data.List.foldl -- NOTE: remove duplicates at beginning and reverse.
268 (\acc l0 -> case acc of
269 l1:_ -> if l0 == l1 then acc else l0:acc
271 case groups of -- NOTE: keep only longer at beginning.
272 l0:l1:t -> if l0 > l1 then groups else l1:t
275 -- | Parse an 'Unit'.
276 unit :: Stream s m Char => ParsecT s u m Unit
278 (quoted <|> unquoted) <?> "unit"
280 unquoted :: Stream s m Char => ParsecT s u m Unit
285 case Data.Char.generalCategory c of
286 Data.Char.CurrencySymbol -> True
287 Data.Char.LowercaseLetter -> True
288 Data.Char.ModifierLetter -> True
289 Data.Char.OtherLetter -> True
290 Data.Char.TitlecaseLetter -> True
291 Data.Char.UppercaseLetter -> True
293 quoted :: Stream s m Char => ParsecT s u m Unit
296 R.between (R.char '"') (R.char '"') $
302 directive_alias :: Stream s m Char => ParsecT s Context m ()
304 _ <- R.string "alias"
305 R.skipMany1 $ space_horizontal
306 pattern <- account_pattern
307 R.skipMany $ space_horizontal
309 R.skipMany $ space_horizontal
311 R.skipMany $ space_horizontal
313 Account.Pattern_Exact acct -> R.modifyState $ \ctx -> ctx{context_aliases_exact=
314 Data.Map.insert acct repl $ context_aliases_exact ctx}
315 Account.Pattern_Joker jokr -> R.modifyState $ \ctx -> ctx{context_aliases_joker=
316 (jokr, repl):context_aliases_joker ctx}
317 Account.Pattern_Regex regx -> R.modifyState $ \ctx -> ctx{context_aliases_regex=
318 (regx, repl):context_aliases_regex ctx}
321 -- | Parse the year, month and day separator: '/' or '-'.
322 date_separator :: Stream s m Char => ParsecT s u m Char
323 date_separator = R.satisfy (\c -> c == '/' || c == '-')
325 -- | Parse the hour, minute and second separator: ':'.
326 hour_separator :: Stream s m Char => ParsecT s u m Char
327 hour_separator = R.char ':'
331 -- | Parse a 'Date' in [YYYY/]MM/DD [HH:MM[:SS][TZ]] format.
332 date :: Stream s m Char => Maybe Integer -> ParsecT s u m Date
334 n0 <- R.many1 R.digit
335 day_sep <- date_separator
336 n1 <- R.try (R.count 2 R.digit) <|> R.count 1 R.digit
337 n2 <- R.option Nothing $ R.try $ do
339 Just <$> do R.try (R.count 2 R.digit) <|> R.count 1 R.digit
341 case (n2, def_year) of
342 (Nothing, Nothing) -> fail "year or day is missing"
343 (Nothing, Just year) -> return (year, n0, n1)
344 (Just d, _) -> return (R.integer_of_digits 10 n0, n1, d)
345 let month = fromInteger $ R.integer_of_digits 10 m
346 let day = fromInteger $ R.integer_of_digits 10 d
347 guard $ month >= 1 && month <= 12
348 guard $ day >= 1 && day <= 31
349 day_ <- case Time.fromGregorianValid year month day of
350 Nothing -> fail "invalid day"
351 Just day_ -> return day_
352 (hour, minu, sec, tz) <-
353 R.option (0, 0, 0, Time.utc) $ R.try $ do
354 R.skipMany1 $ space_horizontal
355 hour <- R.try (R.count 2 R.digit) <|> R.count 1 R.digit
356 sep <- hour_separator
357 minu <- R.try (R.count 2 R.digit) <|> R.count 1 R.digit
358 sec <- R.option Nothing $ R.try $ do
360 Just <$> (R.try (R.count 2 R.digit) <|> R.count 1 R.digit)
362 tz <- R.option Time.utc $ R.try $ do
363 R.skipMany $ space_horizontal
366 ( R.integer_of_digits 10 hour
367 , R.integer_of_digits 10 minu
368 , maybe 0 (R.integer_of_digits 10) sec
370 guard $ hour >= 0 && hour <= 23
371 guard $ minu >= 0 && minu <= 59
372 guard $ sec >= 0 && sec <= 60 -- NOTE: allow leap second
373 tod <- case Time.makeTimeOfDayValid
377 Nothing -> fail "invalid time of day"
378 Just tod -> return tod
381 (Time.LocalTime day_ tod)
385 time_zone :: Stream s m Char => ParsecT s u m TimeZone
387 -- DOC: http://www.timeanddate.com/time/zones/
388 -- TODO: only a few time zones are suported below.
389 -- TODO: check the timeZoneSummerOnly values
391 [ R.char 'A' >> R.choice
392 [ R.string "ST" >> return (TimeZone ((-4) * 60) False "AST")
393 , R.string "DT" >> return (TimeZone ((-3) * 60) True "ADT")
394 , return (TimeZone ((-1) * 60) False "A")
396 , R.char 'B' >> R.choice
397 [ R.string "ST" >> return (TimeZone ((-11) * 60) False "BST")
398 , R.string "DT" >> return (TimeZone ((-10) * 60) True "BDT")
400 , R.char 'C' >> R.choice
401 [ R.char 'E' >> R.choice
402 [ R.string "T" >> return (TimeZone ((1) * 60) True "CET")
403 , R.string "ST" >> return (TimeZone ((2) * 60) False "CEST")
405 , R.string "ST" >> return (TimeZone ((-6) * 60) False "CST")
406 , R.string "DT" >> return (TimeZone ((-5) * 60) True "CDT")
408 , R.char 'E' >> R.choice
409 [ R.string "ST" >> return (TimeZone ((-5) * 60) False "EST")
410 , R.string "DT" >> return (TimeZone ((-4) * 60) True "EDT")
412 , R.string "GMT" >> return (TimeZone 0 False "GMT")
413 , R.char 'H' >> R.choice
414 [ R.string "ST" >> return (TimeZone ((-10) * 60) False "HST")
415 , R.string "DT" >> return (TimeZone (( -9) * 60) True "HDT")
417 , R.char 'M' >> R.choice
418 [ R.string "ST" >> return (TimeZone ((-7) * 60) False "MST")
419 , R.string "DT" >> return (TimeZone ((-6) * 60) True "MDT")
420 , return (TimeZone ((-12) * 60) False "M")
422 , R.char 'N' >> R.choice
423 [ R.string "ST" >> return (TimeZone ((-3) * 60 - 30) False "NST")
424 , return (TimeZone (1 * 60) False "N")
426 , R.char 'P' >> R.choice
427 [ R.string "ST" >> return (TimeZone ((-8) * 60) False "PST")
428 , R.string "DT" >> return (TimeZone ((-7) * 60) True "PDT")
430 , R.char 'Y' >> R.choice
431 [ R.string "ST" >> return (TimeZone ((-9) * 60) False "YST")
432 , R.string "DT" >> return (TimeZone ((-8) * 60) True "YDT")
433 , return (TimeZone (12 * 60) False "Y")
435 , R.char 'Z' >> return (TimeZone 0 False "Z")
439 time_zone_digits :: Stream s m Char => ParsecT s u m TimeZone
440 {-# INLINEABLE time_zone_digits #-}
441 time_zone_digits = do
443 hour <- R.integer_of_digits 10 <$> R.count 2 R.digit
444 _ <- R.option ':' (R.char ':')
445 minute <- R.integer_of_digits 10 <$> R.count 2 R.digit
447 { timeZoneMinutes = sign_ (fromInteger hour * 60 + fromInteger minute)
448 , timeZoneSummerOnly = False
449 , timeZoneName = Time.timeZoneOffsetString tz
453 -- * Parsing 'Comment'
455 comment_begin :: Char
458 comment :: Stream s m Char => ParsecT s u m Comment
460 _ <- R.char comment_begin
462 R.manyTill R.anyChar (R.lookAhead R.new_line <|> R.eof)
465 comments :: Stream s m Char => ParsecT s u m [Comment]
468 R.skipMany $ R.satisfy Data.Char.isSpace
469 many1_separated comment $
472 R.try space_horizontal
473 <|> (R.new_line >> space_horizontal)
478 tag_value_sep :: Char
485 tag :: Stream s m Char => ParsecT s u m Tag
488 _ <- R.char tag_value_sep
493 tag_name :: Stream s m Char => ParsecT s u m Tag.Name
496 R.many1 $ R.satisfy (\c -> c /= tag_value_sep && not (Data.Char.isSpace c))
498 tag_value :: Stream s m Char => ParsecT s u m Tag.Value
501 R.manyTill R.anyChar $ do
503 R.try (R.char tag_sep >> R.many space_horizontal >> tag_name >> R.char tag_value_sep >> return ())
507 tags :: Stream s m Char => ParsecT s u m Tag.By_Name
510 R.many_separated tag $ do
512 R.skipMany $ space_horizontal
515 not_tag :: Stream s m Char => ParsecT s u m ()
517 R.skipMany $ R.try $ do
518 R.skipMany $ R.satisfy
519 (\c -> c /= tag_value_sep
520 && not (Data.Char.isSpace c))
523 -- * Parsing 'Posting'
525 -- | Parse a 'Posting'.
526 posting :: Stream s m Char => ParsecT s Context m (Posting, Posting.Type)
529 sourcepos <- R.getPosition
530 comments_ <- comments
531 R.skipMany1 $ space_horizontal
533 R.skipMany $ space_horizontal
535 let (type_, account_) = posting_type acct
539 _ <- R.count 2 space_horizontal
540 R.skipMany $ space_horizontal
541 Amount.from_List <$> do
542 R.many_separated amount $ R.try $ do
543 R.skipMany $ space_horizontal
544 _ <- R.char amount_sep
545 R.skipMany $ space_horizontal
547 , return Data.Map.empty
549 R.skipMany $ space_horizontal
550 -- TODO: balance assertion
552 comments__ <- (comments_ ++) <$> comments
553 let tags_ = tags_of_comments comments__
555 case Data.Map.lookup "date" tags_ of
558 let date2s = Data.Map.lookup "date2" tags_ -- NOTE: support hledger's date2
559 dates_ <- (flip mapM) (dates ++ fromMaybe [] date2s) $
560 R.runParserT (date (Just $ context_year ctx) <* R.eof) () ""
562 Left ko -> fail $ show ko
563 Right ok -> return ok
564 case (dates, date2s) of -- NOTE: put hledger's date2 at least in second position
566 return $ context_date ctx:dates_
568 return (Posting.Posting
569 { Posting.account=account_
570 , Posting.amounts=amounts_
571 , Posting.comments=comments__
572 , Posting.dates=dates_
573 , Posting.sourcepos=sourcepos
574 , Posting.status=status_
582 tags_of_comments :: [Comment] -> Tag.By_Name
584 Data.Map.unionsWith (++)
586 ( Data.Either.either (const Data.Map.empty) id
587 . R.runParser (not_tag >> tags <* R.eof) () "" )
589 status :: Stream s m Char => ParsecT s u m Transaction.Status
592 R.skipMany $ space_horizontal
593 _ <- (R.char '*' <|> R.char '!') <?> "status"
598 -- | Return the Posting.'Posting.Type' and stripped 'Account' of the given 'Account'.
599 posting_type :: Account -> (Posting.Type, Account)
601 fromMaybe (Posting.Type_Regular, acct) $ do
604 case Text.stripPrefix virtual_begin name of
607 Text.stripSuffix virtual_end name'
608 >>= return . Text.strip
609 guard $ not $ Text.null name''
610 Just (Posting.Type_Virtual, name'':|[])
613 Text.stripPrefix virtual_balanced_begin name
614 >>= Text.stripSuffix virtual_balanced_end
615 >>= return . Text.strip
616 guard $ not $ Text.null name'
617 Just (Posting.Type_Virtual_Balanced, name':|[])
618 first_name:|acct' -> do
619 let rev_acct' = Data.List.reverse acct'
620 let last_name = Data.List.head rev_acct'
621 case Text.stripPrefix virtual_begin first_name
622 >>= return . Text.stripStart of
623 Just first_name' -> do
625 Text.stripSuffix virtual_end last_name
626 >>= return . Text.stripEnd
627 guard $ not $ Text.null first_name'
628 guard $ not $ Text.null last_name'
630 ( Posting.Type_Virtual
632 Data.List.reverse (last_name':Data.List.tail rev_acct')
636 Text.stripPrefix virtual_balanced_begin first_name
637 >>= return . Text.stripStart
639 Text.stripSuffix virtual_balanced_end last_name
640 >>= return . Text.stripEnd
641 guard $ not $ Text.null first_name'
642 guard $ not $ Text.null last_name'
644 ( Posting.Type_Virtual_Balanced
646 Data.List.reverse (last_name':Data.List.tail rev_acct')
649 virtual_begin = Text.singleton posting_type_virtual_begin
650 virtual_end = Text.singleton posting_type_virtual_end
651 virtual_balanced_begin = Text.singleton posting_type_virtual_balanced_begin
652 virtual_balanced_end = Text.singleton posting_type_virtual_balanced_end
654 posting_type_virtual_begin :: Char
655 posting_type_virtual_begin = '('
656 posting_type_virtual_balanced_begin :: Char
657 posting_type_virtual_balanced_begin = '['
658 posting_type_virtual_end :: Char
659 posting_type_virtual_end = ')'
660 posting_type_virtual_balanced_end :: Char
661 posting_type_virtual_balanced_end = ']'
663 -- * Parsing 'Transaction'
665 transaction :: Stream s m Char => ParsecT s Context m Transaction
667 sourcepos <- R.getPosition
669 comments_before <- comments
670 date_ <- date (Just $ context_year ctx)
672 R.option [] $ R.try $ do
673 R.skipMany $ space_horizontal
675 R.skipMany $ space_horizontal
677 (date (Just $ context_year ctx)) $
679 R.many $ space_horizontal
681 >> (R.many $ space_horizontal)
682 R.skipMany $ space_horizontal
684 code_ <- R.option "" $ R.try code
685 R.skipMany $ space_horizontal
686 description_ <- description
687 R.skipMany $ space_horizontal
688 comments_after <- comments
690 Data.Map.unionWith (++)
691 (tags_of_comments comments_before)
692 (tags_of_comments comments_after)
694 postings_ <- many1_separated posting R.new_line
695 let (postings, postings__) =
696 (Posting.from_List . Data.List.map fst) *** id $
698 ((Posting.Type_Regular ==) . snd)
700 let (virtual_postings, balanced_virtual_postings) =
701 join (***) (Posting.from_List . Data.List.map fst) $
703 ((Posting.Type_Virtual ==) . snd)
706 Transaction.Transaction
707 { Transaction.code=code_
708 , Transaction.comments_before
709 , Transaction.comments_after
710 , Transaction.dates=(date_, dates_)
711 , Transaction.description=description_
712 , Transaction.postings
713 , Transaction.virtual_postings
714 , Transaction.balanced_virtual_postings
715 , Transaction.sourcepos
716 , Transaction.status=status_
717 , Transaction.tags=tags_
724 code :: Stream s m Char => ParsecT s Context m Transaction.Code
727 R.skipMany $ space_horizontal
728 R.between (R.char '(') (R.char ')') $
729 R.many $ R.satisfy (\c -> c /= ')' && not (is_space_horizontal c))
732 description :: Stream s m Char => ParsecT s u m Transaction.Description
735 R.many $ R.try description_char
738 description_char :: Stream s m Char => ParsecT s u m Char
739 description_char = do
742 _ | c == comment_begin -> R.parserZero
743 _ | is_space_horizontal c -> return c <* (R.lookAhead $ R.try $ description_char)
744 _ | not (Data.Char.isSpace c) -> return c
747 -- * Parsing directives
749 default_year :: Stream s m Char => ParsecT s Context m ()
751 year <- R.integer_of_digits 10 <$> R.many1 R.digit
752 context_ <- R.getState
753 R.setState context_{context_year=year}
755 default_unit_and_style :: Stream s m Char => ParsecT s Context m ()
756 default_unit_and_style = do
757 R.skipMany1 space_horizontal
759 R.skipMany space_horizontal >> R.new_line >> R.skipMany space_horizontal
760 context_ <- R.getState
761 R.setState context_{context_unit_and_style=Just $
762 ( Amount.unit amount_
763 , Amount.style amount_ )}
765 include :: Stream s IO Char => ParsecT s Context IO ()
767 sourcepos <- R.getPosition
768 R.skipMany1 $ space_horizontal
769 filename <- R.manyTill R.anyChar (R.lookAhead R.new_line <|> R.eof)
770 context_ <- R.getState
771 let journal_ = context_journal context_
772 let cwd = Path.takeDirectory (R.sourceName sourcepos)
773 file_ <- liftIO $ Path.abs cwd filename
774 (journal_included, context_included) <- liftIO $
777 (\ko -> fail $ concat -- TODO: i18n by using a custom data type
781 , ":\n", show (ko::Exception.IOException)
783 >>= R.runParserT (R.and_state $ journal_rec file_)
784 context_{context_journal = Journal.nil}
787 Left ko -> fail $ show ko
788 Right ok -> return ok
790 context_included{context_journal=
791 journal_{Journal.includes=
792 journal_included{Journal.file=file_}
793 : Journal.includes journal_}}
796 -- * Parsing 'Journal'
798 journal :: Stream s IO Char => FilePath -> ParsecT s Context IO Journal
800 currentLocalTime <- liftIO $
802 <$> Time.getCurrentTimeZone
803 <*> Time.getCurrentTime
804 let (currentLocalYear, _, _) = Time.toGregorian $ Time.localDay currentLocalTime
805 context_ <- R.getState
806 R.setState $ context_{context_year=currentLocalYear}
810 journal_rec :: Stream s IO Char => FilePath -> ParsecT s Context IO Journal
811 journal_rec file_ = do
812 last_read_time <- liftIO $ Time.getCurrentTime
816 [ R.string "Y" >> return default_year
817 , R.string "D" >> return default_unit_and_style
818 , R.string "!include" >> return include
819 ] <?> "directive") >>= id)
822 context_' <- R.getState
823 let j = context_journal context_'
824 R.setState $ context_'{context_journal=
825 j{Journal.transactions=
826 Data.Map.insertWith (flip (++))
827 -- NOTE: flip-ing preserves order but slows down
828 -- when many transactions have the very same date.
829 (Date.to_UTC $ fst $ Transaction.dates t) [t]
830 (Journal.transactions j)}}
833 R.skipMany $ R.satisfy Data.Char.isSpace
835 journal_ <- context_journal <$> R.getState
838 { Journal.file = file_
839 , Journal.last_read_time
840 , Journal.includes = reverse $ Journal.includes journal_
843 -- ** Parsing 'Journal' from a file
845 file :: FilePath -> ExceptT String IO Journal
849 (liftM Right $ Text.IO.readFile path) $
850 \ko -> return $ Left $ show (ko::Exception.IOException)
851 >>= liftIO . R.runParserT (journal path) nil_Context path
853 Left ko -> throwE $ show ko
854 Right ok -> ExceptT $ return $ Right ok