Improve input format heuristics
2 files changed, 22 insertions(+), 9 deletions(-)

M README.md
M sq.scm
M README.md +2 -0
@@ 89,6 89,8 @@ obviously useless since sq works by send
 - 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

          
M sq.scm +20 -9
@@ 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*))