;dev:lib allow comment lines in the "if" table body
This commit is contained in:
parent
5def834346
commit
b0b9e69e4f
@ -600,8 +600,9 @@ conditionalblockp = do
|
|||||||
<?> "conditional block"
|
<?> "conditional block"
|
||||||
|
|
||||||
-- A conditional table: "if" followed by separator, followed by some field names,
|
-- A conditional table: "if" followed by separator, followed by some field names,
|
||||||
-- followed by many lines, each of which has:
|
-- followed by many lines, each of which is either:
|
||||||
-- one matchers, followed by field assignments (as many as there were fields)
|
-- a comment line, or ...
|
||||||
|
-- one matcher, followed by field assignments (as many as there were fields in the header)
|
||||||
conditionaltablep :: CsvRulesParser [ConditionalBlock]
|
conditionaltablep :: CsvRulesParser [ConditionalBlock]
|
||||||
conditionaltablep = do
|
conditionaltablep = do
|
||||||
lift $ dbgparse 8 "trying conditionaltablep"
|
lift $ dbgparse 8 "trying conditionaltablep"
|
||||||
@ -610,18 +611,25 @@ conditionaltablep = do
|
|||||||
sep <- lift $ satisfy (\c -> not (isAlphaNum c || isSpace c))
|
sep <- lift $ satisfy (\c -> not (isAlphaNum c || isSpace c))
|
||||||
fields <- journalfieldnamep `sepBy1` (char sep)
|
fields <- journalfieldnamep `sepBy1` (char sep)
|
||||||
newline
|
newline
|
||||||
body <- flip manyTill (lift eolof) $ do
|
body <- catMaybes <$> (flip manyTill (lift eolof) $
|
||||||
off <- getOffset
|
choice [ commentlinep >> return Nothing
|
||||||
m <- matcherp' $ void $ char sep
|
, fmap Just $ bodylinep sep fields
|
||||||
vs <- T.split (==sep) . T.pack <$> lift restofline
|
])
|
||||||
if (length vs /= length fields)
|
|
||||||
then customFailure $ parseErrorAt off $ ((printf "line of conditional table should have %d values, but this one has only %d" (length fields) (length vs)) :: String)
|
|
||||||
else return (m,vs)
|
|
||||||
when (null body) $
|
when (null body) $
|
||||||
customFailure $ parseErrorAt start $ "start of conditional table found, but no assignment rules afterward"
|
customFailure $ parseErrorAt start $ "start of conditional table found, but no assignment rules afterward"
|
||||||
return $ flip map body $ \(m,vs) ->
|
return $ flip map body $ \(m,vs) ->
|
||||||
CB{cbMatchers=[m], cbAssignments=zip fields vs}
|
CB{cbMatchers=[m], cbAssignments=zip fields vs}
|
||||||
<?> "conditional table"
|
<?> "conditional table"
|
||||||
|
where
|
||||||
|
bodylinep :: Char -> [Text] -> CsvRulesParser (Matcher,[FieldTemplate])
|
||||||
|
bodylinep sep fields = do
|
||||||
|
off <- getOffset
|
||||||
|
m <- matcherp' $ void $ char sep
|
||||||
|
vs <- T.split (==sep) . T.pack <$> lift restofline
|
||||||
|
if (length vs /= length fields)
|
||||||
|
then customFailure $ parseErrorAt off $ ((printf "line of conditional table should have %d values, but this one has only %d" (length fields) (length vs)) :: String)
|
||||||
|
else return (m,vs)
|
||||||
|
|
||||||
|
|
||||||
-- A single matcher, on one line.
|
-- A single matcher, on one line.
|
||||||
matcherp' :: CsvRulesParser () -> CsvRulesParser Matcher
|
matcherp' :: CsvRulesParser () -> CsvRulesParser Matcher
|
||||||
|
|||||||
@ -1144,6 +1144,33 @@ $ ./ssvtest.sh
|
|||||||
|
|
||||||
>=
|
>=
|
||||||
|
|
||||||
|
# ** 60. tabular rules with comments
|
||||||
|
<
|
||||||
|
10/2009/09,Flubber Co,50
|
||||||
|
10/2009/09,Blubber Co,150
|
||||||
|
|
||||||
|
RULES
|
||||||
|
fields date, description, amount
|
||||||
|
date-format %d/%Y/%m
|
||||||
|
currency $
|
||||||
|
account1 assets:myacct
|
||||||
|
if|account2|comment
|
||||||
|
; This rule is for Flubber
|
||||||
|
%description Flubber|acct|
|
||||||
|
; This rule is not for Flubber
|
||||||
|
%amount 150|acct2|cmt2
|
||||||
|
; commented out: %description Flubber|acct3|
|
||||||
|
$ ./csvtest.sh
|
||||||
|
2009-09-10 Flubber Co
|
||||||
|
assets:myacct $50
|
||||||
|
acct $-50
|
||||||
|
|
||||||
|
2009-09-10 Blubber Co ; cmt2
|
||||||
|
assets:myacct $150
|
||||||
|
acct2 $-150
|
||||||
|
|
||||||
|
>=0
|
||||||
|
|
||||||
# ** .
|
# ** .
|
||||||
#<
|
#<
|
||||||
#$ ./csvtest.sh
|
#$ ./csvtest.sh
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user