Skip to content

Commit 8490bb9

Browse files
Shyam Seshadrimhevery
authored andcommitted
Fix bug with Lexer not recognizing exponential values and values starting with dots
1 parent e3ea980 commit 8490bb9

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

src/Parser.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ function lex(text, parseStrings){
5353
tokens.push({index:index, text:ch});
5454
index++;
5555
canStartRegExp = false;
56+
} else if (ch == '.' && isNumber(peek())) {
57+
readNumber();
58+
canStartRegExp = false;
5659
} else if ( ch == ':' || ch == '.' || ch == ',' || ch == ';') {
5760
tokens.push({index:index, text:ch});
5861
index++;
@@ -104,6 +107,9 @@ function lex(text, parseStrings){
104107
'A' <= ch && ch <= 'Z' ||
105108
'_' == ch || ch == '$';
106109
}
110+
function isExpOperator(ch) {
111+
return ch == '-' || ch == '+';
112+
}
107113
function readNumber() {
108114
var number = "";
109115
var start = index;
@@ -112,7 +118,20 @@ function lex(text, parseStrings){
112118
if (ch == '.' || isNumber(ch)) {
113119
number += ch;
114120
} else {
115-
break;
121+
var peekCh = peek();
122+
if (ch == 'E' && isExpOperator(peekCh)) {
123+
number += ch;
124+
} else if (isExpOperator(ch) &&
125+
peekCh && isNumber(peekCh) &&
126+
number.charAt(number.length - 1) == 'E') {
127+
number += ch;
128+
} else if (isExpOperator(ch) &&
129+
(!peekCh || !isNumber(peekCh)) &&
130+
number.charAt(number.length - 1) == 'E') {
131+
throw 'Lexer found invalid exponential value "' + text + '"';
132+
} else {
133+
break;
134+
}
116135
}
117136
index++;
118137
}

test/ParserTest.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,38 @@ LexerTest.prototype.testNumber = function(){
141141
expect(tokens[0].text).toEqual(0.5);
142142
};
143143

144+
LexerTest.prototype.testNegativeNumber = function(){
145+
var value = createScope().$eval("-0.5");
146+
expect(value).toEqual(-0.5);
147+
148+
value = createScope().$eval("{a:-0.5}");
149+
expect(value).toEqual({a:-0.5});
150+
};
151+
152+
LexerTest.prototype.testNumberExponent = function(){
153+
var tokens = lex("0.5E-10");
154+
expect(tokens[0].text).toEqual(0.5E-10);
155+
expect(createScope().$eval("0.5E-10")).toEqual(0.5E-10);
156+
157+
tokens = lex("0.5E+10");
158+
expect(tokens[0].text).toEqual(0.5E+10);
159+
};
160+
161+
LexerTest.prototype.testNumberExponentInvalid = function(){
162+
assertThrows('Lexer found invalid exponential value "0.5E-"', function(){
163+
lex("0.5E-");
164+
});
165+
assertThrows('Lexer found invalid exponential value "0.5E-A"', function(){
166+
lex("0.5E-A");
167+
});
168+
};
169+
170+
LexerTest.prototype.testNumberStartingWithDot = function(){
171+
var tokens = lex(".5");
172+
expect(tokens[0].text).toEqual(0.5);
173+
};
174+
175+
144176
ParserTest = TestCase('ParserTest');
145177

146178
ParserTest.prototype.testExpressions = function(){

0 commit comments

Comments
 (0)