From 18f02c2d19070f303a0e1ec6940188641e667739 Mon Sep 17 00:00:00 2001 From: Tim Macdonald Date: Fri, 3 Nov 2023 15:35:09 +0000 Subject: [PATCH] Support reader conditionals --- src/marginalia/parser.clj | 10 ++++++---- test/marginalia/parse_test.clj | 30 +++++++++++++++++++++--------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/marginalia/parser.clj b/src/marginalia/parser.clj index 9c116c7e..7c832927 100644 --- a/src/marginalia/parser.clj +++ b/src/marginalia/parser.clj @@ -4,9 +4,10 @@ (ns marginalia.parser "Provides the parsing facilities for Marginalia." (:refer-clojure :exclude [replace]) - (:use [clojure [string :only (join replace lower-case)]] - [cljs.tagged-literals :only [*cljs-data-readers*]] - [clojure.tools.namespace :only (read-file-ns-decl)])) + (:require + [cljs.tagged-literals :refer [*cljs-data-readers*]] + [clojure.string :refer [join replace lower-case]] + [clojure.tools.namespace :refer [read-file-ns-decl]])) ;; Extracted from clojure.contrib.reflect @@ -175,7 +176,8 @@ (let [start (.getLineNumber reader) form (binding [*comments* sub-level-comments] (try (. clojure.lang.LispReader - (read reader false :_eof false)) + (read reader {:read-cond :allow + :eof :_eof})) (catch Exception ex (let [msg (str "Problem parsing near line " start " <" (.readLine reader) ">" diff --git a/test/marginalia/parse_test.clj b/test/marginalia/parse_test.clj index 3e70cf42..40700ab2 100644 --- a/test/marginalia/parse_test.clj +++ b/test/marginalia/parse_test.clj @@ -26,11 +26,23 @@ (is (= :code type)) (is (= "the docstring" docstring)))) +(def reader-conditional-fn + "(defn error + \"Returns a language-appropriate error\" + [^String msg] + #?(:clj (Exception. msg) + :cljs (js/Error. msg)))") + +(deftest test-reader-conditional + (let [{:keys [type docstring]} (first (marginalia.parser/parse reader-conditional-fn))] + (is (= :code type)) + (is (= "Returns a language-appropriate error" docstring)))) + (deftest inline-comments (testing "inline comments ignored by default" (binding [p/*comments-enabled* (atom true)] (let [result (p/parse - "(ns test) + "(ns test) (defn foo \"docstring\" @@ -45,7 +57,7 @@ (binding [p/*comments-enabled* (atom true)] ;; tests that prelude is appended to docstring (let [result (p/parse - "(ns test) + "(ns test) ;; A (defn foo @@ -62,7 +74,7 @@ (binding [p/*comments-enabled* (atom true) p/*lift-inline-comments* true] (let [result (p/parse - "(ns test) + "(ns test) (defn foo \"docstring\" @@ -78,7 +90,7 @@ (binding [p/*comments-enabled* (atom true) p/*lift-inline-comments* true] (let [result (p/parse - "(ns test) + "(ns test) (defn foo \"docstring\" @@ -94,7 +106,7 @@ p/*lift-inline-comments* true] ;; A and B should be separate paragraphs (let [result (p/parse - "(ns test) + "(ns test) (defn foo \"docstring\" @@ -113,7 +125,7 @@ p/*lift-inline-comments* true] ;; prelude A follows docstring, then B and C as separate paragraphs (let [result (p/parse - "(ns test) + "(ns test) ;; A (defn foo @@ -133,7 +145,7 @@ p/*lift-inline-comments* true] ;; this checks that consecutive comment lines stay in the same paragraph (let [result (p/parse - "(ns test) + "(ns test) ;; A (defn foo @@ -155,7 +167,7 @@ ;; this checks that a comment above the function doesn't merge in ;; when separated by a blank line (let [result (p/parse - "(ns test) + "(ns test) ;; A @@ -179,7 +191,7 @@ ;; this checks that a comment above the function does merge in ;; when a blank comment joins it to the function (let [result (p/parse - "(ns test) + "(ns test) ;; A ;;