Skip to content

number parser returns 13 for "015" #1

Open
kjw opened this Issue May 4, 2012 · 4 comments

2 participants

@kjw
kjw commented May 4, 2012

Due to the use of clojure's reader in zetta.parser.seq/number.

Clojure's reader interprets a sequence of digits with leading 0s as base 8, e.g. (parse-number "010") => 8 , (parse-number "08") => nil and (parse-number "07") => 7.

@ujihisa
Vancouver Clojure Club member
ujihisa commented May 10, 2012

src/zetta/parser/seq.clj

307 (let [m (.getDeclaredMethod clojure.lang.LispReader
308                             "matchNumber"
309                             (into-array [String]))]
310   (.setAccessible m true)
311   (defn- read-number [s]
312     (.invoke m clojure.lang.LispReader (into-array [s]))))

Roman: instead of using LispReader as kjw pointed out, how about using Java's BigInteger constructor, and convert it to appropriate Clojure data type?

(defn- read-number [x]
  (let [big-int (BigInteger. x)]
    (if (< big-int Long/MAX_VALUE)
      (long big-int)
      (bigint big-int))))
@ujihisa
Vancouver Clojure Club member
ujihisa commented May 10, 2012

and also we should provide the previous zetta.parser.seq/number as another name, like zetta.parser.seq/number-clojure

@ujihisa
Vancouver Clojure Club member
ujihisa commented May 10, 2012

note that Roman, the author of zetta-parser, is busy in May so let's just wait until the end of month :)

@kjw
kjw commented May 10, 2012

Problem with using BigInteger. is it doesn't read rationals. Would be nice to keep that functionality (though there's probably a case for having separate integer and number parsers).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.