Skip to content

Commit

Permalink
include exponent to Number (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Sep 28, 2016
1 parent fca0f7a commit 5cf1ce3
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/parser/const.js
Expand Up @@ -15,7 +15,7 @@ var SPACE = 32;
var TokenType = {
Whitespace: WHITESPACE,
Identifier: IDENTIFIER,
DecimalNumber: NUMBER,
Number: NUMBER,
String: STRING,
Comment: COMMENT,
Punctuator: PUNCTUATOR,
Expand Down
11 changes: 1 addition & 10 deletions lib/parser/index.js
Expand Up @@ -15,7 +15,7 @@ var SPACE_NODE = { type: 'Space' };

var WHITESPACE = TokenType.Whitespace;
var IDENTIFIER = TokenType.Identifier;
var DECIMALNUMBER = TokenType.DecimalNumber;
var DECIMALNUMBER = TokenType.Number;
var STRING = TokenType.String;
var COMMENT = TokenType.Comment;
var EXCLAMATIONMARK = TokenType.ExclamationMark;
Expand Down Expand Up @@ -1456,15 +1456,6 @@ function readNumber() {
tokenType = scanner.lookupType(++offset);
}

if (tokenType === FULLSTOP) {
tokenType = scanner.lookupType(++offset);
}

if (tokenType === DECIMALNUMBER) {
wasDigits = true;
offset++;
}

if (wasDigits) {
scanner.skip(offset);

Expand Down
51 changes: 46 additions & 5 deletions lib/parser/scanner.js
Expand Up @@ -12,7 +12,7 @@ var isHex = require('./utils').isHex;
var NULL = 0;
var WHITESPACE = TokenType.Whitespace;
var IDENTIFIER = TokenType.Identifier;
var NUMBER = TokenType.DecimalNumber;
var NUMBER = TokenType.Number;
var STRING = TokenType.String;
var COMMENT = TokenType.Comment;
var PUNCTUATOR = TokenType.Punctuator;
Expand All @@ -25,6 +25,10 @@ var SPACE = 32;
var STAR = 42;
var SLASH = 47;
var BACK_SLASH = 92;
var FULLSTOP = TokenType.FullStop;
var E = 'e'.charCodeAt(0);
var PLUSSIGN = TokenType.PlusSign;
var HYPHENMINUS = TokenType.HyphenMinus;

var MIN_ARRAY_SIZE = 16 * 1024;
var OFFSET_MASK = 0x00FFFFFF;
Expand Down Expand Up @@ -128,6 +132,38 @@ function findDecimalNumberEnd(source, offset) {
return offset;
}

function findNumberEnd(source, offset, allowFullstop) {
offset = findDecimalNumberEnd(source, offset);

if (allowFullstop && offset + 1 < source.length && source.charCodeAt(offset) === FULLSTOP) {
var code = source.charCodeAt(offset + 1);
if (code >= 48 && code <= 57) {
offset = findDecimalNumberEnd(source, offset + 1);
}
}

if (offset + 1 < source.length) {
if ((source.charCodeAt(offset) | 32) === E) {
var code = source.charCodeAt(offset + 1);
if (code === PLUSSIGN || code === HYPHENMINUS) {
if (offset + 2 >= source.length) {
return offset;
}
code = source.charCodeAt(offset + 2);
if (code >= 48 && code <= 57) {
offset = findDecimalNumberEnd(source, offset + 3);
}
} else {
if (code >= 48 && code <= 57) {
offset = findDecimalNumberEnd(source, offset + 2);
}
}
}
}

return offset;
}

// skip escaped unicode sequence that can ends with space
// [0-9a-f]{1,6}(\r\n|[ \n\r\t\f])?
function findEscaseEnd(source, offset) {
Expand Down Expand Up @@ -188,9 +224,7 @@ function tokenLayout(scanner, source, startPos) {
if (code === STAR && prevType === SLASH) { // /*
type = COMMENT;
offset = findCommentEnd(source, offset + 1);

// rewrite prev token
tokenCount--;
tokenCount--; // rewrite prev token
} else {
type = code;
offset = offset + 1;
Expand All @@ -199,7 +233,10 @@ function tokenLayout(scanner, source, startPos) {
break;

case NUMBER:
offset = findDecimalNumberEnd(source, offset + 1);
offset = findNumberEnd(source, offset + 1, prevType !== FULLSTOP);
if (prevType === FULLSTOP) {
tokenCount--; // rewrite prev token
}
break;

case STRING:
Expand Down Expand Up @@ -387,6 +424,10 @@ Scanner.prototype = {
location.line,
location.column
);
},

getTypes: function() {
return Array.prototype.slice.call(this.offsetAndType, 0, this.tokenCount).map(x => TokenName[x]);
}
};

Expand Down
14 changes: 11 additions & 3 deletions test/fixture/parse-errors.json
Expand Up @@ -32,11 +32,11 @@
}
}, {
"css": ".1px {}",
"error": "Identifier is expected",
"error": "PercentSign is expected",
"position": {
"offset": 1,
"offset": 2,
"line": 1,
"column": 2
"column": 3
}
}, {
"css": "# {}",
Expand Down Expand Up @@ -86,6 +86,14 @@
"line": 1,
"column": 13
}
}, {
"css": "a { width: 20. }",
"error": "Unexpected input",
"position": {
"offset": 13,
"line": 1,
"column": 14
}
}, {
"css": ": {}",
"error": "Identifier is expected",
Expand Down
70 changes: 70 additions & 0 deletions test/fixture/parse/value/Number.json
Expand Up @@ -54,5 +54,75 @@
"type": "Number",
"value": "1.200000"
}
},
"with exponent #1": {
"source": "1.2e2",
"ast": {
"type": "Number",
"value": "1.2e2"
}
},
"with exponent #2": {
"source": "1e2",
"ast": {
"type": "Number",
"value": "1e2"
}
},
"with exponent #3": {
"source": ".2e2",
"ast": {
"type": "Number",
"value": ".2e2"
}
},
"with exponent #4": {
"source": "1.2E2",
"ast": {
"type": "Number",
"value": "1.2E2"
}
},
"with exponent #5": {
"source": "1.2e+2",
"ast": {
"type": "Number",
"value": "1.2e+2"
}
},
"with exponent #6": {
"source": "1.2e-2",
"ast": {
"type": "Number",
"value": "1.2e-2"
}
},
"with sign #1": {
"source": "-1",
"ast": {
"type": "Number",
"value": "-1"
}
},
"with sign #2": {
"source": "-1.2",
"ast": {
"type": "Number",
"value": "-1.2"
}
},
"with sign #3": {
"source": "-.2",
"ast": {
"type": "Number",
"value": "-.2"
}
},
"with sign #4": {
"source": "-1.2e3",
"ast": {
"type": "Number",
"value": "-1.2e3"
}
}
}
3 changes: 0 additions & 3 deletions test/fixture/syntax/generic.json
Expand Up @@ -231,7 +231,6 @@
"valid": [
"123",
"123.456",
"123.",
".456",
"calc(2 * 1.5)"
],
Expand Down Expand Up @@ -277,7 +276,6 @@
"",
"123.456",
"-123.456",
"123.",
".456",
"foo 123"
]
Expand All @@ -298,7 +296,6 @@
"-123",
"123.456",
"-123.456",
"123.",
".456",
"foo 123"
]
Expand Down

0 comments on commit 5cf1ce3

Please sign in to comment.