Skip to content

Commit e4ce69d

Browse files
committed
test(scanner): port the rest of the lexer tests from AngularDart
Closes angular#64
1 parent 5162b3c commit e4ce69d

File tree

4 files changed

+298
-123
lines changed

4 files changed

+298
-123
lines changed

modules/change_detection/src/parser/scanner.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,15 @@ const $TILDE = 126;
163163
const $NBSP = 160;
164164

165165

166+
export class ScannerError extends Error {
167+
constructor(message) {
168+
this.message = message;
169+
}
170+
171+
toString() {
172+
return this.message;
173+
}
174+
}
166175

167176
export class Scanner {
168177
@FIELD('final input:String')
@@ -214,8 +223,8 @@ export class Scanner {
214223
switch (peek) {
215224
case $PERIOD:
216225
this.advance();
217-
return isDigit(peek) ? scanNumber(start) :
218-
newCharacterToken(start, $PERIOD);
226+
return isDigit(this.peek) ? this.scanNumber(start) :
227+
newCharacterToken(start, $PERIOD);
219228
case $LPAREN: case $RPAREN:
220229
case $LBRACE: case $RBRACE:
221230
case $LBRACKET: case $RBRACKET:
@@ -250,7 +259,7 @@ export class Scanner {
250259
return this.scanToken();
251260
}
252261

253-
this.error(`Unexpected character [${StringWrapper.fromCharCode(peek)}]`);
262+
this.error(`Unexpected character [${StringWrapper.fromCharCode(peek)}]`, 0);
254263
return null;
255264
}
256265

@@ -305,7 +314,7 @@ export class Scanner {
305314
} else if (isExponentStart(this.peek)) {
306315
this.advance();
307316
if (isExponentSign(this.peek)) this.advance();
308-
if (!isDigit(this.peek)) this.error('Invalid exponent');
317+
if (!isDigit(this.peek)) this.error('Invalid exponent', -1);
309318
simple = false;
310319
} else {
311320
break;
@@ -324,7 +333,7 @@ export class Scanner {
324333
var quote:int = this.peek;
325334
this.advance(); // Skip initial quote.
326335

327-
var buffer:StringJoiner; //ckck
336+
var buffer:StringJoiner;
328337
var marker:int = this.index;
329338
var input:string = this.input;
330339

@@ -337,7 +346,11 @@ export class Scanner {
337346
if (this.peek == $u) {
338347
// 4 character hex code for unicode character.
339348
var hex:string = input.substring(this.index + 1, this.index + 5);
340-
unescapedCode = NumberWrapper.parseInt(hex, 16);
349+
try {
350+
unescapedCode = NumberWrapper.parseInt(hex, 16);
351+
} catch (e) {
352+
this.error(`Invalid unicode escape [\\u${hex}]`, 0);
353+
}
341354
for (var i:int = 0; i < 5; i++) {
342355
this.advance();
343356
}
@@ -348,7 +361,7 @@ export class Scanner {
348361
buffer.add(StringWrapper.fromCharCode(unescapedCode));
349362
marker = this.index;
350363
} else if (this.peek == $EOF) {
351-
this.error('Unterminated quote');
364+
this.error('Unterminated quote', 0);
352365
} else {
353366
this.advance();
354367
}
@@ -367,9 +380,9 @@ export class Scanner {
367380
return newStringToken(start, unescaped);
368381
}
369382

370-
error(message:string) {
371-
var position:int = this.index;
372-
throw `Lexer Error: ${message} at column ${position} in expression [${input}]`;
383+
error(message:string, offset:int) {
384+
var position:int = this.index + offset;
385+
throw new ScannerError(`Lexer Error: ${message} at column ${position} in expression [${this.input}]`);
373386
}
374387
}
375388

modules/change_detection/test/parser/lexer_spec.js

Lines changed: 0 additions & 108 deletions
This file was deleted.

0 commit comments

Comments
 (0)