|
1 | 1 | (ns ^:no-doc rewrite-clj.reader
|
2 | 2 | (:refer-clojure :exclude [peek next])
|
3 |
| - (:require #?@(:clj [[clojure.java.io :as io]]) |
| 3 | + (:require [clojure.string :as string] |
| 4 | + #?@(:clj [[clojure.java.io :as io]]) |
4 | 5 | [clojure.tools.reader.edn :as edn]
|
5 | 6 | [clojure.tools.reader.impl.commons :as reader-impl-commons]
|
6 | 7 | [clojure.tools.reader.impl.errors :as reader-impl-errors]
|
|
203 | 204 | (reader-impl-errors/throw-invalid reader :keyword token)))
|
204 | 205 | (reader-impl-errors/throw-single-colon reader))))
|
205 | 206 |
|
| 207 | +(defn- parse-symbol |
| 208 | + "Parses a string into a vector of the namespace and symbol |
| 209 | +
|
| 210 | + Cribbed from clojure/cljs.tools.reader.impl.commons/parse-symbol merging clj and cljs fns into single implementation |
| 211 | + Added in equivalent of TRDR-73 patch to allow array class symbols (e.g. foobar/3)." |
| 212 | + [^String token] |
| 213 | + (when-not (or (identical? "" token) |
| 214 | + (string/ends-with? token ":") |
| 215 | + (string/starts-with? token "::")) |
| 216 | + (if-let [ns-idx (string/index-of token "/")] |
| 217 | + (let [ns (subs token 0 ns-idx) |
| 218 | + ns-idx (inc ns-idx)] |
| 219 | + (when-not (== ns-idx (count token)) |
| 220 | + (let [sym (subs token ns-idx)] |
| 221 | + (cond |
| 222 | + (contains? #{"1" "2" "3" "4" "5" "6" "7" "8" "9"} sym) |
| 223 | + [ns sym] |
| 224 | + |
| 225 | + (and (not (interop/numeric? (nth sym 0))) |
| 226 | + (not (identical? "" sym)) |
| 227 | + (not (string/ends-with? ns ":")) |
| 228 | + (or (identical? sym "/") |
| 229 | + (nil? (string/index-of sym "/")))) |
| 230 | + [ns sym])))) |
| 231 | + (when (or (identical? token "/") |
| 232 | + (nil? (string/index-of token "/"))) |
| 233 | + [nil token])))) |
| 234 | + |
| 235 | +(defn read-symbol |
| 236 | + "Return symbol parsed from `token`. |
| 237 | +
|
| 238 | + Cribbed from clojure/cljs.tools.reader.edn/read-symbol and - adapted to work on string" |
| 239 | + [^String token] |
| 240 | + (case token |
| 241 | + ;; special symbols |
| 242 | + "nil" nil |
| 243 | + "true" true |
| 244 | + "false" false |
| 245 | + "/" '/ |
| 246 | + |
| 247 | + (or (when-let [p (parse-symbol token)] |
| 248 | + (symbol (p 0) (p 1))) |
| 249 | + ;; Throw in same way that tools.reader would when reading a string |
| 250 | + ;; for exeption compatibility. Some users, like clojure-lsp, currently rely |
| 251 | + ;; on parsing exception strings. A user having to resort |
| 252 | + ;; to parsing exception messages is not great, but a separate issue. |
| 253 | + (reader-impl-errors/throw-invalid nil :symbol token)))) |
| 254 | + |
206 | 255 | ;; ## Reader Types
|
207 | 256 |
|
208 | 257 | ;;
|
|
0 commit comments