Fix number parser (#433)

Fixed number parser #2

Added one more test
This commit is contained in:
Alex Kuzmenko 2017-04-03 23:25:29 +03:00 committed by Daniel Tschinder
parent 9222562b82
commit 22741a8068
13 changed files with 293 additions and 9 deletions

View File

@ -564,7 +564,7 @@ export default class Tokenizer {
readNumber(startsWithDot) {
const start = this.state.pos;
const octal = this.input.charCodeAt(this.state.pos) === 48;
const firstIsZero = this.input.charCodeAt(start) === 48; // '0'
let isFloat = false;
if (!startsWithDot && this.readInt(10) === null) this.raise(start, "Invalid number");
@ -587,10 +587,12 @@ export default class Tokenizer {
let val;
if (isFloat) {
val = parseFloat(str);
} else if (!octal || str.length === 1) {
} else if (!firstIsZero || str.length === 1) {
val = parseInt(str, 10);
} else if (/[89]/.test(str) || this.state.strict) {
} else if (this.state.strict) {
this.raise(start, "Invalid number");
} else if (/[89]/.test(str)) {
val = parseInt(str, 10);
} else {
val = parseInt(str, 8);
}

View File

@ -0,0 +1,69 @@
{
"type": "File",
"start": 0,
"end": 2,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 2
}
},
"program": {
"type": "Program",
"start": 0,
"end": 2,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 2
}
},
"sourceType": "script",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 2,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 2
}
},
"expression": {
"type": "NumericLiteral",
"start": 0,
"end": 2,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 2
}
},
"extra": {
"rawValue": 9,
"raw": "09"
},
"value": 9
}
}
],
"directives": []
}
}

View File

@ -1,3 +0,0 @@
{
"throws": "Invalid number (1:0)"
}

View File

@ -0,0 +1,69 @@
{
"type": "File",
"start": 0,
"end": 3,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 3
}
},
"program": {
"type": "Program",
"start": 0,
"end": 3,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 3
}
},
"sourceType": "script",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 3,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 3
}
},
"expression": {
"type": "NumericLiteral",
"start": 0,
"end": 3,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 3
}
},
"extra": {
"rawValue": 18,
"raw": "018"
},
"value": 18
}
}
],
"directives": []
}
}

View File

@ -1,3 +0,0 @@
{
"throws": "Invalid number (1:0)"
}

View File

@ -0,0 +1,2 @@
'use strict';
const a = 07;

View File

@ -0,0 +1,3 @@
{
"throws": "Invalid number (2:10)"
}

View File

@ -0,0 +1 @@
0111

View File

@ -0,0 +1,69 @@
{
"type": "File",
"start": 0,
"end": 4,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 4
}
},
"program": {
"type": "Program",
"start": 0,
"end": 4,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 4
}
},
"sourceType": "script",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 4,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 4
}
},
"expression": {
"type": "NumericLiteral",
"start": 0,
"end": 4,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 4
}
},
"extra": {
"rawValue": 73,
"raw": "0111"
},
"value": 73
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,2 @@
'use strict';
const a = 08;

View File

@ -0,0 +1,3 @@
{
"throws": "Invalid number (2:10)"
}

View File

@ -0,0 +1 @@
0274134317073

View File

@ -0,0 +1,69 @@
{
"type": "File",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"program": {
"type": "Program",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"sourceType": "script",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"expression": {
"type": "NumericLiteral",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"extra": {
"rawValue": 25257156155,
"raw": "0274134317073"
},
"value": 25257156155
}
}
],
"directives": []
}
}