clojure-mode icon indicating copy to clipboard operation
clojure-mode copied to clipboard

No highlighting for radix numbers

Open MrEbbinghaus opened this issue 3 years ago • 1 comments

Clojure allows for numbers with a radix between 2 and 36. Also, the BigInt N suffix is allowed after a hexadecimal number.

From https://clojure.org/reference/reader#_literals:

When possible, they can be specified in any base with radix from 2 to 36 (see Long.parseLong()); for example 2r101010, 052, 8r52, 0x2a, 36r16, and 42 are all the same Long.

image

Here are some regex from [clojure.tools.reader] describing all numbers. (https://github.com/clojure/tools.reader/blob/master/src/main/clojure/clojure/tools/reader/impl/commons.clj#L46-L48)

(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)?")

MrEbbinghaus avatar Nov 10 '21 10:11 MrEbbinghaus

I looked at the regex. There are more issues:

  • 0b101010 is not a valid Clojure number. You would write: 2r101010.
  • Similar, octals are not written with 0o prefix.
    • It should be just a 0 or, more explicit, 8r (052 8r52)
    • And they can begin with + or -
  • The x and the r for hexadecimal numbers and radix numbers are case-insensitive.
 Number {
    ("+" | "-")? (std.digit+ ("." std.digit* "M"?)? | "." std.digit+) (("e" | "E") ("+" | "-")? std.digit+ "M"?)? |
    ("+" | "-")? std.digit+ ("M" | "N") |
    ("+" | "-")? std.digit+ "/" std.digit+ |
    ("+" | "-")? "0x" (std.digit | $[a-fA-F])+ |
    "0b" $[01]+ |
    "0o" $[0-7]+
  }

MrEbbinghaus avatar Nov 10 '21 10:11 MrEbbinghaus