Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #2060 Disallow uppercase radix prefixes and exponential notation #2061

Merged
merged 1 commit into from
Jan 21, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Issue #2060 Disallow uppercase radix prefixes and exponential notation
geraldalewis committed Jan 20, 2012

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 34e517de09d86aeba694619f055f4cbcfabe7470
22 changes: 13 additions & 9 deletions lib/coffee-script/lexer.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 11 additions & 7 deletions src/lexer.coffee
Original file line number Diff line number Diff line change
@@ -133,14 +133,18 @@ exports.Lexer = class Lexer
numberToken: ->
return 0 unless match = NUMBER.exec @chunk
number = match[0]
if /[E]/.test number
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/E/.test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Also noticed the exponent error was the only one that didn't reference the source of the error. I'll patch and push a new version.

@error "exponential notation must be indicated with a lowercase 'e'"
else if /[BOX]/.test number
@error "radix prefixes must be lowercase '#{number}'"
else if /^0[89]/.test number
@error "decimal literals '#{number}' must not be prefixed with '0'"
else if /^0[0-7]/.test number
@error "octal literals '#{number}' must be prefixed with '0o'"
lexedLength = number.length
if nonStrictOctalLiteral = /^0\d+/.test number
dec = if /[89]/.test number then "\"#{number}\" " else ''
oct = if dec then '' else "\"#{number}\" "
@error "decimal literals #{dec}must not be prefixed with '0'; octal literals #{oct}must be prefixed with '0o'"
if octalLiteral = /0o([0-7]+)/i.exec number
if octalLiteral = /0o([0-7]+)/.exec number
number = (parseInt octalLiteral[1], 8).toString()
if binaryLiteral = /0b([01]+)/i.exec number
if binaryLiteral = /0b([01]+)/.exec number
number = (parseInt binaryLiteral[1], 2).toString()
@token 'NUMBER', number
lexedLength
@@ -596,9 +600,9 @@ IDENTIFIER = /// ^
///

NUMBER = ///
^ 0x[\da-f]+ | # hex
^ 0b[01]+ | # binary
^ 0o[0-7]+ | # octal
^ 0x[\da-f]+ | # hex
^ \d*\.?\d+ (?:e[+-]?\d+)? # decimal
///i

9 changes: 6 additions & 3 deletions test/numbers.coffee
Original file line number Diff line number Diff line change
@@ -15,7 +15,6 @@

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

# Decimal Integer Literals

@@ -57,12 +56,16 @@ test '#1168: leading floating point suppresses newline', ->

test "Python-style octal literal notation '0o777'", ->
eq 511, 0o777
eq 511, 0O777
eq 1, 0o1
eq 1, 0O1
eq 1, 0o00001
eq parseInt('0777', 8), 0o777
eq '777', 0o777.toString 8
eq 4, 0o4.valueOf()
eq Number::toString, 0o777['toString']
eq Number::toString, 0o777.toString

test "#2060: Disallow uppercase radix prefixes and exponential notation", ->
for char in ['b', 'o', 'x', 'e']
program = "0#{char}0"
doesNotThrow -> CoffeeScript.compile program, bare: yes
throws -> CoffeeScript.compile program.toUpperCase(), bare: yes