Skip to content

Commit

Permalink
Pass through octal and binary literals as-is
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Pánek committed Sep 26, 2016
1 parent c3f5b8d commit 499bc0d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/lexer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,10 @@ exports.Lexer = class Lexer
# Be careful not to interfere with ranges-in-progress.
numberToken: ->
return 0 unless match = NUMBER.exec @chunk

number = match[0]
lexedLength = number.length

if /^0[BOX]/.test number
@error "radix prefix in '#{number}' must be lowercase", offset: 1
else if /E/.test(number) and not /^0x/.test number
Expand All @@ -202,14 +204,12 @@ exports.Lexer = class Lexer
@error "decimal literal '#{number}' must not be prefixed with '0'", length: lexedLength
else if /^0\d+/.test number
@error "octal literal '#{number}' must be prefixed with '0o'", length: lexedLength
if octalLiteral = /^0o([0-7]+)/.exec number
numberValue = parseInt(octalLiteral[1], 8)
number = "0x#{numberValue.toString 16}"
else if binaryLiteral = /^0b([01]+)/.exec number
numberValue = parseInt(binaryLiteral[1], 2)
number = "0x#{numberValue.toString 16}"
else

numberValue = number

unless /^(0o[0-7]+)/.test(number) or /^(0b[01]+)/.test(number)
numberValue = parseFloat(number)

tag = if numberValue is Infinity then 'INFINITY' else 'NUMBER'
@token tag, number, 0, lexedLength
lexedLength
Expand Down
56 changes: 56 additions & 0 deletions test/numbers.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,26 @@
# Binary Integer Literals
# Binary notation is understood as would be decimal notation.

toJS = (str) ->
CoffeeScript.compile str, bare: yes
.replace /^\s+|\s+$/g, '' # Trim leading/trailing whitespace

test "Parser recognises binary numbers", ->
eq 4, 0b100

test "Binary literals are compiled to binary literals", ->
input = """
a = 0b100
"""

output = """
var a;
a = 0b100;
"""

eq toJS(input), output

# Decimal Integer Literals

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

eq -1, 3 -4

test "Decimal literals are compiled to decimal literals", ->
input = """
a = 10
"""

output = """
var a;
a = 10;
"""

eq toJS(input), output

test "Decimal literals are compiled to decimal literals", ->
input = """
a = 10.1
"""

output = """
var a;
a = 10.1;
"""

eq toJS(input), output

#764: Numbers should be indexable
eq Number::toString, 42['toString']

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

test "Octal literals are compiled to octal literals", ->
input = """
a = 0o123
"""

output = """
var a;
a = 0o123;
"""

eq toJS(input), output

test "#2060: Disallow uppercase radix prefixes and exponential notation", ->
for char in ['b', 'o', 'x', 'e']
program = "0#{char}0"
Expand Down

0 comments on commit 499bc0d

Please sign in to comment.