Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support reader conditionals #183

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading