lib: refactor: tighten up amountwithoutpricep

This commit is contained in:
Simon Michael 2018-08-17 06:47:55 +01:00
parent e9c5d13ac1
commit e35dd19cc8

View File

@ -529,48 +529,37 @@ amountwithoutpricep = do
leftsymbolamountp mult sign = label "amount" $ do leftsymbolamountp mult sign = label "amount" $ do
c <- lift commoditysymbolp c <- lift commoditysymbolp
suggestedStyle <- getAmountStyle c suggestedStyle <- getAmountStyle c
commodityspaced <- lift $ skipMany' spacenonewline commodityspaced <- lift $ skipMany' spacenonewline
sign2 <- lift $ signp sign2 <- lift $ signp
posBeforeNum <- getPosition posBeforeNum <- getPosition
ambiguousRawNum <- lift rawnumberp ambiguousRawNum <- lift rawnumberp
mExponent <- lift $ optional $ try exponentp mExponent <- lift $ optional $ try exponentp
posAfterNum <- getPosition posAfterNum <- getPosition
let numRegion = (posBeforeNum, posAfterNum) let numRegion = (posBeforeNum, posAfterNum)
(q,prec,mdec,mgrps) <- lift $ interpretNumber numRegion suggestedStyle ambiguousRawNum mExponent
(q,prec,mdec,mgrps) <- lift $
interpretNumber numRegion suggestedStyle ambiguousRawNum mExponent
let s = amountstyle{ascommodityside=L, ascommodityspaced=commodityspaced, asprecision=prec, asdecimalpoint=mdec, asdigitgroups=mgrps} let s = amountstyle{ascommodityside=L, ascommodityspaced=commodityspaced, asprecision=prec, asdecimalpoint=mdec, asdigitgroups=mgrps}
return $ Amount c (sign (sign2 q)) NoPrice s mult return $ Amount c (sign (sign2 q)) NoPrice s mult
rightornosymbolamountp rightornosymbolamountp :: Bool -> (Decimal -> Decimal) -> JournalParser m Amount
:: Bool -> (Decimal -> Decimal) -> JournalParser m Amount
rightornosymbolamountp mult sign = label "amount" $ do rightornosymbolamountp mult sign = label "amount" $ do
posBeforeNum <- getPosition posBeforeNum <- getPosition
ambiguousRawNum <- lift rawnumberp ambiguousRawNum <- lift rawnumberp
mExponent <- lift $ optional $ try exponentp mExponent <- lift $ optional $ try exponentp
posAfterNum <- getPosition posAfterNum <- getPosition
let numRegion = (posBeforeNum, posAfterNum) let numRegion = (posBeforeNum, posAfterNum)
mSpaceAndCommodity <- lift $ optional $ try $ (,) <$> skipMany' spacenonewline <*> commoditysymbolp
mSpaceAndCommodity <- lift $ optional $ try $
(,) <$> skipMany' spacenonewline <*> commoditysymbolp
case mSpaceAndCommodity of case mSpaceAndCommodity of
-- right symbol amount
Just (commodityspaced, c) -> do Just (commodityspaced, c) -> do
suggestedStyle <- getAmountStyle c suggestedStyle <- getAmountStyle c
(q,prec,mdec,mgrps) <- lift $ (q,prec,mdec,mgrps) <- lift $ interpretNumber numRegion suggestedStyle ambiguousRawNum mExponent
interpretNumber numRegion suggestedStyle ambiguousRawNum mExponent
let s = amountstyle{ascommodityside=R, ascommodityspaced=commodityspaced, asprecision=prec, asdecimalpoint=mdec, asdigitgroups=mgrps} let s = amountstyle{ascommodityside=R, ascommodityspaced=commodityspaced, asprecision=prec, asdecimalpoint=mdec, asdigitgroups=mgrps}
return $ Amount c (sign q) NoPrice s mult return $ Amount c (sign q) NoPrice s mult
-- no symbol amount
Nothing -> do Nothing -> do
suggestedStyle <- getDefaultAmountStyle suggestedStyle <- getDefaultAmountStyle
(q,prec,mdec,mgrps) <- lift $ (q,prec,mdec,mgrps) <- lift $ interpretNumber numRegion suggestedStyle ambiguousRawNum mExponent
interpretNumber numRegion suggestedStyle ambiguousRawNum mExponent -- if a default commodity has been set, apply it and its style to this amount
-- apply the most recently seen default commodity and style to this commodityless amount
defcs <- getDefaultCommodityAndStyle 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})