Fix bug with Lexer not recognizing exponential values and values starting with dots

This commit is contained in:
Shyam Seshadri 2010-10-08 17:11:45 -07:00 committed by Misko Hevery
parent e3ea980c81
commit 8490bb921b
2 changed files with 52 additions and 1 deletions

View file

@ -53,6 +53,9 @@ function lex(text, parseStrings){
tokens.push({index:index, text:ch});
index++;
canStartRegExp = false;
} else if (ch == '.' && isNumber(peek())) {
readNumber();
canStartRegExp = false;
} else if ( ch == ':' || ch == '.' || ch == ',' || ch == ';') {
tokens.push({index:index, text:ch});
index++;
@ -104,6 +107,9 @@ function lex(text, parseStrings){
'A' <= ch && ch <= 'Z' ||
'_' == ch || ch == '$';
}
function isExpOperator(ch) {
return ch == '-' || ch == '+';
}
function readNumber() {
var number = "";
var start = index;
@ -112,7 +118,20 @@ function lex(text, parseStrings){
if (ch == '.' || isNumber(ch)) {
number += ch;
} else {
break;
var peekCh = peek();
if (ch == 'E' && isExpOperator(peekCh)) {
number += ch;
} else if (isExpOperator(ch) &&
peekCh && isNumber(peekCh) &&
number.charAt(number.length - 1) == 'E') {
number += ch;
} else if (isExpOperator(ch) &&
(!peekCh || !isNumber(peekCh)) &&
number.charAt(number.length - 1) == 'E') {
throw 'Lexer found invalid exponential value "' + text + '"';
} else {
break;
}
}
index++;
}

View file

@ -141,6 +141,38 @@ LexerTest.prototype.testNumber = function(){
expect(tokens[0].text).toEqual(0.5);
};
LexerTest.prototype.testNegativeNumber = function(){
var value = createScope().$eval("-0.5");
expect(value).toEqual(-0.5);
value = createScope().$eval("{a:-0.5}");
expect(value).toEqual({a:-0.5});
};
LexerTest.prototype.testNumberExponent = function(){
var tokens = lex("0.5E-10");
expect(tokens[0].text).toEqual(0.5E-10);
expect(createScope().$eval("0.5E-10")).toEqual(0.5E-10);
tokens = lex("0.5E+10");
expect(tokens[0].text).toEqual(0.5E+10);
};
LexerTest.prototype.testNumberExponentInvalid = function(){
assertThrows('Lexer found invalid exponential value "0.5E-"', function(){
lex("0.5E-");
});
assertThrows('Lexer found invalid exponential value "0.5E-A"', function(){
lex("0.5E-A");
});
};
LexerTest.prototype.testNumberStartingWithDot = function(){
var tokens = lex(".5");
expect(tokens[0].text).toEqual(0.5);
};
ParserTest = TestCase('ParserTest');
ParserTest.prototype.testExpressions = function(){