commodity & amount style parser cleanups
This commit is contained in:
parent
8ae303f685
commit
9b4b85b4bf
@ -138,7 +138,7 @@ type Year = Integer
|
|||||||
-- is saved for later use by eg the add command.
|
-- is saved for later use by eg the add command.
|
||||||
data JournalContext = Ctx {
|
data JournalContext = Ctx {
|
||||||
ctxYear :: !(Maybe Year) -- ^ the default year most recently specified with Y
|
ctxYear :: !(Maybe Year) -- ^ the default year most recently specified with Y
|
||||||
, ctxCommodityAndStyle :: !(Maybe (Commodity,AmountStyle)) -- ^ the default commodity and amount style most recently specified with D
|
, ctxDefaultCommodityAndStyle :: !(Maybe (Commodity,AmountStyle)) -- ^ the default commodity and amount style most recently specified with D
|
||||||
, ctxAccount :: ![AccountName] -- ^ the current stack of parent accounts/account name components
|
, ctxAccount :: ![AccountName] -- ^ the current stack of parent accounts/account name components
|
||||||
-- specified with "account" directive(s). Concatenated, these
|
-- specified with "account" directive(s). Concatenated, these
|
||||||
-- are the account prefix prepended to parsed account names.
|
-- are the account prefix prepended to parsed account names.
|
||||||
|
|||||||
@ -115,11 +115,11 @@ setYear y = updateState (\ctx -> ctx{ctxYear=Just y})
|
|||||||
getYear :: GenParser tok JournalContext (Maybe Integer)
|
getYear :: GenParser tok JournalContext (Maybe Integer)
|
||||||
getYear = liftM ctxYear getState
|
getYear = liftM ctxYear getState
|
||||||
|
|
||||||
setCommodityAndStyle :: (Commodity,AmountStyle) -> GenParser tok JournalContext ()
|
setDefaultCommodityAndStyle :: (Commodity,AmountStyle) -> GenParser tok JournalContext ()
|
||||||
setCommodityAndStyle cs = updateState (\ctx -> ctx{ctxCommodityAndStyle=Just cs})
|
setDefaultCommodityAndStyle cs = updateState (\ctx -> ctx{ctxDefaultCommodityAndStyle=Just cs})
|
||||||
|
|
||||||
getCommodityAndStyle :: GenParser tok JournalContext (Maybe (Commodity,AmountStyle))
|
getDefaultCommodityAndStyle :: GenParser tok JournalContext (Maybe (Commodity,AmountStyle))
|
||||||
getCommodityAndStyle = ctxCommodityAndStyle `fmap` getState
|
getDefaultCommodityAndStyle = ctxDefaultCommodityAndStyle `fmap` getState
|
||||||
|
|
||||||
pushParentAccount :: String -> GenParser tok JournalContext ()
|
pushParentAccount :: String -> GenParser tok JournalContext ()
|
||||||
pushParentAccount parent = updateState addParentAccount
|
pushParentAccount parent = updateState addParentAccount
|
||||||
@ -268,7 +268,7 @@ defaultcommoditydirective = do
|
|||||||
char 'D' <?> "default commodity"
|
char 'D' <?> "default commodity"
|
||||||
many1 spacenonewline
|
many1 spacenonewline
|
||||||
Amount{..} <- amountp
|
Amount{..} <- amountp
|
||||||
setCommodityAndStyle (acommodity, astyle)
|
setDefaultCommodityAndStyle (acommodity, astyle)
|
||||||
restofline
|
restofline
|
||||||
return $ return id
|
return $ return id
|
||||||
|
|
||||||
@ -663,7 +663,7 @@ leftsymbolamount = do
|
|||||||
c <- commoditysymbol
|
c <- commoditysymbol
|
||||||
sp <- many spacenonewline
|
sp <- many spacenonewline
|
||||||
(q,prec,dec,sep,seppos) <- numberp
|
(q,prec,dec,sep,seppos) <- numberp
|
||||||
let s = amountstyle{ascommodityside=L, ascommodityspaced=not $ null sp, asdecimalpoint=dec, asprecision=prec, asseparator=sep, asseparatorpositions=seppos}
|
let s = amountstyle{ascommodityside=L, ascommodityspaced=not $ null sp, asprecision=prec, asdecimalpoint=dec, asseparator=sep, asseparatorpositions=seppos}
|
||||||
p <- priceamount
|
p <- priceamount
|
||||||
let applysign = if sign=="-" then negate else id
|
let applysign = if sign=="-" then negate else id
|
||||||
return $ applysign $ Amount c q p s
|
return $ applysign $ Amount c q p s
|
||||||
@ -675,7 +675,7 @@ rightsymbolamount = do
|
|||||||
sp <- many spacenonewline
|
sp <- many spacenonewline
|
||||||
c <- commoditysymbol
|
c <- commoditysymbol
|
||||||
p <- priceamount
|
p <- priceamount
|
||||||
let s = amountstyle{ascommodityside=R, ascommodityspaced=not $ null sp, asdecimalpoint=dec, asprecision=prec, asseparator=sep, asseparatorpositions=seppos}
|
let s = amountstyle{ascommodityside=R, ascommodityspaced=not $ null sp, asprecision=prec, asdecimalpoint=dec, asseparator=sep, asseparatorpositions=seppos}
|
||||||
return $ Amount c q p s
|
return $ Amount c q p s
|
||||||
<?> "right-symbol amount"
|
<?> "right-symbol amount"
|
||||||
|
|
||||||
@ -683,10 +683,11 @@ nosymbolamount :: GenParser Char JournalContext Amount
|
|||||||
nosymbolamount = do
|
nosymbolamount = do
|
||||||
(q,prec,dec,sep,seppos) <- numberp
|
(q,prec,dec,sep,seppos) <- numberp
|
||||||
p <- priceamount
|
p <- priceamount
|
||||||
defcs <- getCommodityAndStyle
|
-- apply the most recently seen default commodity and style to this commodityless amount
|
||||||
|
defcs <- getDefaultCommodityAndStyle
|
||||||
let (c,s) = case defcs of
|
let (c,s) = case defcs of
|
||||||
Just (defc,defs) -> (defc, defs{asprecision=max (asprecision defs) prec})
|
Just (defc,defs) -> (defc, defs{asprecision=max (asprecision defs) prec})
|
||||||
Nothing -> ("", amountstyle{asdecimalpoint=dec, asprecision=prec, asseparator=sep, asseparatorpositions=seppos})
|
Nothing -> ("", amountstyle{asprecision=prec, asdecimalpoint=dec, asseparator=sep, asseparatorpositions=seppos})
|
||||||
return $ Amount c q p s
|
return $ Amount c q p s
|
||||||
<?> "no-symbol amount"
|
<?> "no-symbol amount"
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user