From 38db7eb24da2e1075272b170b8cffc6a15d2e5b8 Mon Sep 17 00:00:00 2001 From: Dmitry Astapov Date: Mon, 14 Oct 2019 21:46:35 +0100 Subject: [PATCH] lib: implement `skip end` in csv reader --- hledger-lib/Hledger/Read/CsvReader.hs | 1 + hledger-lib/hledger_csv.m4.md | 6 +++--- tests/csv.test | 7 ++++++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/hledger-lib/Hledger/Read/CsvReader.hs b/hledger-lib/Hledger/Read/CsvReader.hs index f8c620df7..3b176ef77 100644 --- a/hledger-lib/Hledger/Read/CsvReader.hs +++ b/hledger-lib/Hledger/Read/CsvReader.hs @@ -225,6 +225,7 @@ validateCsv rules numhdrlines (Right rs) = validate $ applyConditionalSkips $ dr case getEffectiveAssignment rules r "skip" of Nothing -> Nothing Just "" -> Just 1 + Just "end" -> Just maxBound Just x -> Just (read x) applyConditionalSkips [] = [] applyConditionalSkips (r:rest) = diff --git a/hledger-lib/hledger_csv.m4.md b/hledger-lib/hledger_csv.m4.md index dee69ce7b..04a484fb6 100644 --- a/hledger-lib/hledger_csv.m4.md +++ b/hledger-lib/hledger_csv.m4.md @@ -178,12 +178,12 @@ Note, interpolation strips any outer whitespace, so a CSV value like ## conditional block `if` *`PATTERN`*\ -    *`FIELDASSIGNMENTS or skip N`*... +    *`FIELDASSIGNMENTS` or `skip N` or `skip end`*... `if`\ *`PATTERN`*\ *`PATTERN`*...\ -    *`FIELDASSIGNMENTS or skip N`*... +    *`FIELDASSIGNMENTS` or `skip N` or `skip end`*... This applies one or more field assignments, only to those CSV records matched by one of the PATTERNs. The patterns are case-insensitive regular expressions which match anywhere @@ -192,7 +192,7 @@ specific field). When there are multiple patterns they can be written on separate lines, unindented. The field assignments are on separate lines indented by at least one space. -Instead of field assignments you can specify `skip N` to skip the next N records (including the one that matchied). +Instead of field assignments you can specify `skip N` to skip the next N records (including the one that matchied). Special form `skip end` will cause the rest of the file to be skipped. Examples: ```rules diff --git a/tests/csv.test b/tests/csv.test index b26df9f38..696bfd6bc 100644 --- a/tests/csv.test +++ b/tests/csv.test @@ -359,6 +359,8 @@ AND THIS AND THIS ONE 10/2009/09,Flubber Co,50 *** END OF FILE *** +More lines of the trailer here +They all should be ignored RULES fields date, description, amount date-format %d/%Y/%m @@ -366,9 +368,12 @@ currency $ account1 assets:myacct if HEADER -END OF FILE skip +if +END OF FILE + skip end + if MIDDLE skip 3