Skip to content

Commit b0b9e69

Browse files
adeptsimonmichael
authored andcommitted
;dev:lib allow comment lines in the "if" table body
1 parent 5def834 commit b0b9e69

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

hledger-lib/Hledger/Read/RulesReader.hs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -600,8 +600,9 @@ conditionalblockp = do
600600
<?> "conditional block"
601601

602602
-- A conditional table: "if" followed by separator, followed by some field names,
603-
-- followed by many lines, each of which has:
604-
-- one matchers, followed by field assignments (as many as there were fields)
603+
-- followed by many lines, each of which is either:
604+
-- a comment line, or ...
605+
-- one matcher, followed by field assignments (as many as there were fields in the header)
605606
conditionaltablep :: CsvRulesParser [ConditionalBlock]
606607
conditionaltablep = do
607608
lift $ dbgparse 8 "trying conditionaltablep"
@@ -610,18 +611,25 @@ conditionaltablep = do
610611
sep <- lift $ satisfy (\c -> not (isAlphaNum c || isSpace c))
611612
fields <- journalfieldnamep `sepBy1` (char sep)
612613
newline
613-
body <- flip manyTill (lift eolof) $ do
614-
off <- getOffset
615-
m <- matcherp' $ void $ char sep
616-
vs <- T.split (==sep) . T.pack <$> lift restofline
617-
if (length vs /= length fields)
618-
then customFailure $ parseErrorAt off $ ((printf "line of conditional table should have %d values, but this one has only %d" (length fields) (length vs)) :: String)
619-
else return (m,vs)
614+
body <- catMaybes <$> (flip manyTill (lift eolof) $
615+
choice [ commentlinep >> return Nothing
616+
, fmap Just $ bodylinep sep fields
617+
])
620618
when (null body) $
621619
customFailure $ parseErrorAt start $ "start of conditional table found, but no assignment rules afterward"
622620
return $ flip map body $ \(m,vs) ->
623621
CB{cbMatchers=[m], cbAssignments=zip fields vs}
624622
<?> "conditional table"
623+
where
624+
bodylinep :: Char -> [Text] -> CsvRulesParser (Matcher,[FieldTemplate])
625+
bodylinep sep fields = do
626+
off <- getOffset
627+
m <- matcherp' $ void $ char sep
628+
vs <- T.split (==sep) . T.pack <$> lift restofline
629+
if (length vs /= length fields)
630+
then customFailure $ parseErrorAt off $ ((printf "line of conditional table should have %d values, but this one has only %d" (length fields) (length vs)) :: String)
631+
else return (m,vs)
632+
625633

626634
-- A single matcher, on one line.
627635
matcherp' :: CsvRulesParser () -> CsvRulesParser Matcher

hledger/test/csv.test

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,33 @@ $ ./ssvtest.sh
11441144

11451145
>=
11461146

1147+
# ** 60. tabular rules with comments
1148+
<
1149+
10/2009/09,Flubber Co,50
1150+
10/2009/09,Blubber Co,150
1151+
1152+
RULES
1153+
fields date, description, amount
1154+
date-format %d/%Y/%m
1155+
currency $
1156+
account1 assets:myacct
1157+
if|account2|comment
1158+
; This rule is for Flubber
1159+
%description Flubber|acct|
1160+
; This rule is not for Flubber
1161+
%amount 150|acct2|cmt2
1162+
; commented out: %description Flubber|acct3|
1163+
$ ./csvtest.sh
1164+
2009-09-10 Flubber Co
1165+
assets:myacct $50
1166+
acct $-50
1167+
1168+
2009-09-10 Blubber Co ; cmt2
1169+
assets:myacct $150
1170+
acct2 $-150
1171+
1172+
>=0
1173+
11471174
# ** .
11481175
#<
11491176
#$ ./csvtest.sh

0 commit comments

Comments
 (0)