Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to tell a lisp reader function to ignore errors during parsing
    primarykey
    data
    text
    <p>I need a way to suppress any error messages raised when parsing code using <code>read-from-string</code> so that I can read from <code>Clojure</code> code using something like this:</p> <pre><code>(let* ((string-with-code " '(func UpperCase \"string\")") (brace-pos (position #\( string-with-code)) (end-of-token (+ brace-pos (position #\Space (subseq string-with-code brace-pos)))) (first-token (subseq string-with-code (incf brace-pos) end-of-token))) (format t "Found function: '~a'" (read-from-string first-token))) ;; ==&gt; Found function: 'func' </code></pre> <p>It basically prints the function name from the lisp code in the string. It works Ok until you try to use the dot operator(<code>.</code>) as the first item in the list. <code>Clojure</code> uses the <code>.</code> both to <code>cons</code> and to access classes in a Java package and hence valid code like: </p> <pre><code>(defmacro chain ([x form] `(. ~x ~form)) ([x form &amp; more] `(chain (. ~x ~form) ~@more))) </code></pre> <p>would cause an error: </p> <pre><code>*** - READ from #&lt;INPUT STRING-INPUT-STREAM&gt;: token "." not allowed here </code></pre> <p>if I were to walk it printing every function in the code. I want <strong>a way to ignore/suppress the error messages from <code>read-from-string</code></strong> for this code to work preferably without modifying the way the lisp reader works.</p> <p><strong>EDIT :</strong></p> <p>A complete program: </p> <pre><code>(defvar string-with-code "(defmacro chain ([x form] `(d ~x ~form)) ([x form &amp; more] `(chain (. ~x ~form) ~@more))) ") (defvar end-of-token 0) (defvar first-token 0) (defun functions-in-string (code) (let ((bpos (position #\( code))) (unless (null bpos) (setq end-of-token (+ bpos (position #\Space (subseq code bpos)))) (setq first-token (subseq code (incf bpos) end-of-token)) (ignore-errors (format t "Found function: '~(~A~)'~%" (read-from-string first-token))) (functions-in-string (subseq code end-of-token))))) ;; (ignore-errors ;; (functions-in-string 0 code)) (functions-in-string string-with-code) </code></pre> <p><strong>OUTPUT :</strong> </p> <pre><code>Found function: 'defmacro' Found function: '[x' Found function: 'd' Found function: '[x' Found function: 'chain' ;; You'll get the error below if ignore-errors wraps around the function call ;; *** - READ from #&lt;INPUT STRING-INPUT-STREAM&gt;: token "." not allowed here </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload