# HG changeset patch # User Evan Hanson # Date 1608791706 -46800 # Thu Dec 24 19:35:06 2020 +1300 # Node ID d2ae14a69c89e964305b7b604b340197d4c4f1bb # Parent 7b55f61fa8fec440819af5d94f3fc545f4e30f7b Improve input format heuristics diff --git a/README.md b/README.md --- a/README.md +++ b/README.md @@ -89,6 +89,8 @@ - The program should probably just execute jq when `--json` is used with JSON inputs. - There is no validation of command line flags for compatibility. - Error messages for invalid S-expression inputs are not very helpful. +- Inputs must be entirely in one format, you cannot mix JSON and S-expressions. +- It is not currently possible to specify the format of the input. - There are no tests. ## License diff --git a/sq.scm b/sq.scm --- a/sq.scm +++ b/sq.scm @@ -137,15 +137,26 @@ (define (proxy input output) (let-values (((input* rewind) (make-rewindable-input-port input))) (handle-exceptions e - (if ((condition-predicate 'syntax) e) - (proxy-string (rewind) output) - (proxy-scheme (rewind) output)) - ;; skip whitespace - (read-token char-whitespace? input*) - ;; look for a parenthesis or valid scheme datum - (or (eq? (peek-char input*) #\() - (read input*)) - (signal 'scheme)))) + (if (eq? e 'scheme) + (proxy-scheme (rewind) output) + (proxy-string (rewind) output)) + (let loop () + (read-token char-whitespace? input*) + (or (let ((x (peek-char input*))) + (cond + ((eq? x #\{) (signal 'json)) + ((eq? x #\[) (signal 'json)) + ((eq? x #\() (signal 'scheme)) + ((eq? x #\#) (signal 'scheme)) + (else #f))) + (let ((x (read input*))) + (cond + ((eq? x 'true) (signal 'json)) + ((eq? x 'false) (signal 'json)) + ((eq? x 'null) (loop)) + ((string? x) (loop)) + ((integer? x) (loop)) + (else (signal 'scheme))))))))) (define (proxy-scheme input output) (copy-port* input output read json-write*))