Skip to content

Commit

Permalink
Support reader conditionals (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsmacdonald committed Nov 3, 2023
1 parent 477df0b commit 7c6863d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
10 changes: 6 additions & 4 deletions src/marginalia/parser.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) ">"
Expand Down
30 changes: 21 additions & 9 deletions test/marginalia/parse_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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\"
Expand All @@ -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
Expand All @@ -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\"
Expand All @@ -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\"
Expand All @@ -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\"
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
;;
Expand Down

0 comments on commit 7c6863d

Please sign in to comment.