Skip to content

Commit

Permalink
modules/dsv/fsm/dsv-context: Validate row lengths
Browse files Browse the repository at this point in the history
* modules/dsv/fsm/dsv-context.scm (add-row): When "validate?" option is set,
  check if rows have consistent lengths.
* modules/dsv/rfc4180.scm (dsv->scm): Add "validate?" option.
* modules/dsv/unix.scm (dsv->scm): Add "validate?" option.
* modules/dsv/cli/common.scm (print-file): Validate input data.
* modules/dsv.scm (dsv->scm): Handle "validate?" option.
* NEWS: Update.
  • Loading branch information
artyom-poptsov committed Nov 25, 2023
1 parent 6e23426 commit eee9d40
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 2 deletions.
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Copyright (C) Artyom V. Poptsov <[email protected]>
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.

* Unreleased
** Parsers now can check if the table rows have consistent lengths

* Changes in version 0.7.1 (2023-10-22)
** =dsv= now handles =--number= option
This option allows to add numbers to rows and columns of a table.
Expand Down
3 changes: 3 additions & 0 deletions modules/dsv.scm
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
#:key
(format 'unix)
(comment-prefix 'default)
(validate? #f)
(debug-mode? #f)
(log-driver #f)
(log-opt '()))
Expand All @@ -100,10 +101,12 @@ values, or throw 'dsv-parser-error' on an error."
(unix:dsv->scm port
#:delimiter delimiter
#:comment-prefix comment-prefix
#:validate? validate?
#:debug-mode? debug-mode?))
((rfc4180)
(rfc4180:dsv->scm port
#:delimiter delimiter
#:validate? validate?
#:debug-mode? debug-mode?))
(else
(dsv-error "Unknown format" format))))
Expand Down
1 change: 1 addition & 0 deletions modules/dsv/cli/common.scm
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ of numbers, or #f if an error occurred."
(error "Could not determine a file delimiter" input-port))

(let* ((table (remove-empty-rows (dsv->scm input-port delim #:format fmt
#:validate? #t
#:debug-mode? debug-mode?)))
(table (if filter-col-proc
(table-filter-column filter-col-proc table)
Expand Down
36 changes: 34 additions & 2 deletions modules/dsv/fsm/dsv-context.scm
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,41 @@
context
(add-field context)))

(define (throw-row-length-error context row row-length expected-row-length)
(context-log-error context
"Inconsistent length mismatch on line ~a: expected ~a, got ~a"
(context-row-number context)
expected-row-length
row-length)
(error (format #f
"Inconsistent row length on line ~a: expected ~a, got ~a; "
(context-row-number context)
expected-row-length
row-length)
(context-port context)
(context-row-number context)
(context-col-number context)
row
context))

(define* (add-row context #:optional char)
(clear-stanza
(push-event-to-result context (context-stanza/reversed context))))
(let ((result (context-result context))
(stanza (context-stanza/reversed context)))
(if (or (not (hash-ref (context-custom-data context) 'validate?))
(null? result))
(clear-stanza
(push-event-to-result context (context-stanza/reversed context)))
(let* ((stanza-length (length stanza))
(last-row (car result))
(last-row-length (length last-row)))
(if (not (equal? stanza-length last-row-length))
(throw-row-length-error context
stanza
stanza-length
last-row-length)
(clear-stanza
(push-event-to-result context
(context-stanza/reversed context))))))))

(define* (prepare-result context #:optional char)
(reverse-result context))
Expand Down
2 changes: 2 additions & 0 deletions modules/dsv/rfc4180.scm
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
(define* (dsv->scm port
#:key
(debug-mode? #f)
(validate? #f)
(delimiter %default-delimiter))
(let* ((fsm (make <rfc4180-fsm>
#:pre-action pre-action
Expand All @@ -119,6 +120,7 @@
`((delimiter . ,(if (equal? delimiter 'default)
%default-delimiter
delimiter))
(validate? . ,validate?)
(comment-prefix . #f)))))))
(context-result context)))

Expand Down
2 changes: 2 additions & 0 deletions modules/dsv/unix.scm
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
#:key
(debug-mode? #f)
(delimiter %default-delimiter)
(validate? #f)
(comment-prefix %default-comment-prefix))
(let* ((fsm (make <unix-fsm>
#:pre-action pre-action
Expand All @@ -85,6 +86,7 @@
`((delimiter . ,(if (equal? delimiter 'default)
%default-delimiter
delimiter))
(validate? . ,validate?)
(comment-prefix . ,(if (equal? comment-prefix 'default)
%default-comment-prefix
comment-prefix))))))))
Expand Down

0 comments on commit eee9d40

Please sign in to comment.