Skip to content

Commit 499bc0d

Browse files
author
Alexander Pánek
committed
Pass through octal and binary literals as-is
See coffeescript6/discuss#45
1 parent c3f5b8d commit 499bc0d

File tree

2 files changed

+63
-7
lines changed

2 files changed

+63
-7
lines changed

src/lexer.coffee

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,10 @@ exports.Lexer = class Lexer
191191
# Be careful not to interfere with ranges-in-progress.
192192
numberToken: ->
193193
return 0 unless match = NUMBER.exec @chunk
194+
194195
number = match[0]
195196
lexedLength = number.length
197+
196198
if /^0[BOX]/.test number
197199
@error "radix prefix in '#{number}' must be lowercase", offset: 1
198200
else if /E/.test(number) and not /^0x/.test number
@@ -202,14 +204,12 @@ exports.Lexer = class Lexer
202204
@error "decimal literal '#{number}' must not be prefixed with '0'", length: lexedLength
203205
else if /^0\d+/.test number
204206
@error "octal literal '#{number}' must be prefixed with '0o'", length: lexedLength
205-
if octalLiteral = /^0o([0-7]+)/.exec number
206-
numberValue = parseInt(octalLiteral[1], 8)
207-
number = "0x#{numberValue.toString 16}"
208-
else if binaryLiteral = /^0b([01]+)/.exec number
209-
numberValue = parseInt(binaryLiteral[1], 2)
210-
number = "0x#{numberValue.toString 16}"
211-
else
207+
208+
numberValue = number
209+
210+
unless /^(0o[0-7]+)/.test(number) or /^(0b[01]+)/.test(number)
212211
numberValue = parseFloat(number)
212+
213213
tag = if numberValue is Infinity then 'INFINITY' else 'NUMBER'
214214
@token tag, number, 0, lexedLength
215215
lexedLength

test/numbers.coffee

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,26 @@
1313
# Binary Integer Literals
1414
# Binary notation is understood as would be decimal notation.
1515

16+
toJS = (str) ->
17+
CoffeeScript.compile str, bare: yes
18+
.replace /^\s+|\s+$/g, '' # Trim leading/trailing whitespace
19+
1620
test "Parser recognises binary numbers", ->
1721
eq 4, 0b100
1822

23+
test "Binary literals are compiled to binary literals", ->
24+
input = """
25+
a = 0b100
26+
"""
27+
28+
output = """
29+
var a;
30+
31+
a = 0b100;
32+
"""
33+
34+
eq toJS(input), output
35+
1936
# Decimal Integer Literals
2037

2138
test "call methods directly on numbers", ->
@@ -24,6 +41,32 @@ test "call methods directly on numbers", ->
2441

2542
eq -1, 3 -4
2643

44+
test "Decimal literals are compiled to decimal literals", ->
45+
input = """
46+
a = 10
47+
"""
48+
49+
output = """
50+
var a;
51+
52+
a = 10;
53+
"""
54+
55+
eq toJS(input), output
56+
57+
test "Decimal literals are compiled to decimal literals", ->
58+
input = """
59+
a = 10.1
60+
"""
61+
62+
output = """
63+
var a;
64+
65+
a = 10.1;
66+
"""
67+
68+
eq toJS(input), output
69+
2770
#764: Numbers should be indexable
2871
eq Number::toString, 42['toString']
2972

@@ -66,6 +109,19 @@ test "Python-style octal literal notation '0o777'", ->
66109
eq Number::toString, 0o777['toString']
67110
eq Number::toString, 0o777.toString
68111

112+
test "Octal literals are compiled to octal literals", ->
113+
input = """
114+
a = 0o123
115+
"""
116+
117+
output = """
118+
var a;
119+
120+
a = 0o123;
121+
"""
122+
123+
eq toJS(input), output
124+
69125
test "#2060: Disallow uppercase radix prefixes and exponential notation", ->
70126
for char in ['b', 'o', 'x', 'e']
71127
program = "0#{char}0"

0 commit comments

Comments
 (0)