-
Notifications
You must be signed in to change notification settings - Fork 64
/
commons.clj
131 lines (117 loc) · 4.58 KB
/
commons.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
;; Copyright (c) Nicola Mometto, Rich Hickey & contributors.
;; The use and distribution terms for this software are covered by the
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file epl-v10.html at the root of this distribution.
;; By using this software in any fashion, you are agreeing to be bound by
;; the terms of this license.
;; You must not remove this notice, or any other, from this software.
(ns clojure.tools.reader.impl.commons
(:refer-clojure :exclude [char])
(:require [clojure.tools.reader.reader-types :refer [peek-char read-char]]
[clojure.tools.reader.impl.errors :refer [reader-error]]
[clojure.tools.reader.impl.utils :refer [numeric? newline? char]])
(:import (clojure.lang BigInt Numbers)
(java.util.regex Pattern Matcher)
java.lang.reflect.Constructor))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; helpers
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn number-literal?
"Checks whether the reader is at the start of a number literal"
[reader initch]
(or (numeric? initch)
(and (or (identical? \+ initch) (identical? \- initch))
(numeric? (peek-char reader)))))
(defn read-past
"Read until first character that doesn't match pred, returning
char."
[pred rdr]
(loop [ch (read-char rdr)]
(if (pred ch)
(recur (read-char rdr))
ch)))
(defn skip-line
"Advances the reader to the end of a line. Returns the reader"
[reader]
(loop []
(when-not (newline? (read-char reader))
(recur)))
reader)
(def ^Pattern int-pattern #"([-+]?)(?:(0)|([1-9][0-9]*)|0[xX]([0-9A-Fa-f]+)|0([0-7]+)|([1-9][0-9]?)[rR]([0-9A-Za-z]+)|0[0-9]+)(N)?")
(def ^Pattern ratio-pattern #"([-+]?[0-9]+)/([0-9]+)")
(def ^Pattern float-pattern #"([-+]?[0-9]+(\.[0-9]*)?([eE][-+]?[0-9]+)?)(M)?")
(defn- match-int
[^Matcher m]
(if (.group m 2)
(if (.group m 8) 0N 0)
(let [negate? (= "-" (.group m 1))
a (cond
(.group m 3) [(.group m 3) 10]
(.group m 4) [(.group m 4) 16]
(.group m 5) [(.group m 5) 8]
(.group m 7) [(.group m 7) (Integer/parseInt (.group m 6))]
:else [nil nil])
^String n (a 0)]
(when n
(let [bn (BigInteger. n (int (a 1)))
bn (if negate? (.negate bn) bn)]
(if (.group m 8)
(BigInt/fromBigInteger bn)
(if (< (.bitLength bn) 64)
(.longValue bn)
(BigInt/fromBigInteger bn))))))))
(defn- match-ratio
[^Matcher m]
(let [^String numerator (.group m 1)
^String denominator (.group m 2)
numerator (if (.startsWith numerator "+")
(subs numerator 1)
numerator)]
(/ (-> numerator BigInteger. BigInt/fromBigInteger Numbers/reduceBigInt)
(-> denominator BigInteger. BigInt/fromBigInteger Numbers/reduceBigInt))))
(defn- match-float
[^String s ^Matcher m]
(if (.group m 4)
(BigDecimal. ^String (.group m 1))
(Double/parseDouble s)))
(defn match-number [^String s]
(let [int-matcher (.matcher int-pattern s)]
(if (.matches int-matcher)
(match-int int-matcher)
(let [float-matcher (.matcher float-pattern s)]
(if (.matches float-matcher)
(match-float s float-matcher)
(let [ratio-matcher (.matcher ratio-pattern s)]
(when (.matches ratio-matcher)
(match-ratio ratio-matcher))))))))
(defn parse-symbol
"Parses a string into a vector of the namespace and symbol"
[^String token]
(when-not (or (= "" token)
(.endsWith token ":")
(.startsWith token "::"))
(let [ns-idx (.indexOf token "/")]
(if-let [^String ns (and (pos? ns-idx)
(subs token 0 ns-idx))]
(let [ns-idx (inc ns-idx)]
(when-not (== ns-idx (count token))
(let [sym (subs token ns-idx)]
(when (and (not (numeric? (nth sym 0)))
(not (= "" sym))
(not (.endsWith ns ":"))
(or (= sym "/")
(== -1 (.indexOf sym "/"))))
[ns sym]))))
(when (or (= token "/")
(== -1 (.indexOf token "/")))
[nil token])))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; readers
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn read-comment
[rdr & _]
(skip-line rdr))
(defn throwing-reader
[msg]
(fn [rdr & _]
(reader-error rdr msg)))