Skip to content

Commit d7d52aa

Browse files
committed
refactor(lexer): rename to scanner, use ints, etc.
1 parent 3482fb1 commit d7d52aa

File tree

2 files changed

+58
-59
lines changed

2 files changed

+58
-59
lines changed

modules/change_detection/src/parser/lexer.js renamed to modules/change_detection/src/parser/scanner.js

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {List, ListWrapper, SetWrapper} from "facade/collection";
2-
import {FIELD, NumberWrapper, StringJoiner, StringWrapper} from "facade/lang";
2+
import {int, FIELD, NumberWrapper, StringJoiner, StringWrapper} from "facade/lang";
33

44
// TODO(chirayu): Rewrite as consts when possible.
55
export var TOKEN_TYPE_CHARACTER = 1;
@@ -12,21 +12,21 @@ export var TOKEN_TYPE_NUMBER = 6;
1212
export class Token {
1313
@FIELD('final index:int')
1414
@FIELD('final type:int')
15-
@FIELD('final _intValue:int')
15+
@FIELD('final _numValue:int')
1616
@FIELD('final _strValue:int')
17-
constructor(index:number/*int*/, type:number/*int*/, intValue:number/*int*/, strValue:string) {
17+
constructor(index:int, type:int, numValue:number, strValue:string) {
1818
/**
1919
* NOTE: To ensure that this constructor creates the same hidden class each time, ensure that
2020
* all the fields are assigned to in the exact same order in each run of this constructor.
2121
*/
2222
this.index = index;
2323
this.type = type;
24-
this._intValue = intValue;
24+
this._numValue = numValue;
2525
this._strValue = strValue;
2626
}
2727

28-
isCharacter(code:number/*int*/):boolean {
29-
return (this.type == TOKEN_TYPE_CHARACTER && this._intValue == code);
28+
isCharacter(code:int):boolean {
29+
return (this.type == TOKEN_TYPE_CHARACTER && this._numValue == code);
3030
}
3131

3232
isNumber():boolean {
@@ -65,44 +65,44 @@ export class Token {
6565
return (this.type == TOKEN_TYPE_KEYWORD && this._strValue == "false");
6666
}
6767

68-
toNumber():number/*int*/ {
68+
toNumber():number {
6969
// -1 instead of NULL ok?
70-
return (this.type == TOKEN_TYPE_NUMBER) ? this._intValue : -1;
70+
return (this.type == TOKEN_TYPE_NUMBER) ? this._numValue : -1;
7171
}
7272

7373
toString():string {
74-
var type:number/*int*/ = this.type;
74+
var type:int = this.type;
7575
if (type >= TOKEN_TYPE_CHARACTER && type <= TOKEN_TYPE_STRING) {
7676
return this._strValue;
7777
} else if (type == TOKEN_TYPE_NUMBER) {
78-
return this._intValue.toString();
78+
return this._numValue.toString();
7979
} else {
8080
return null;
8181
}
8282
}
8383
}
8484

85-
function newCharacterToken(index:number/*int*/, code:number/*int*/):Token {
85+
function newCharacterToken(index:int, code:int):Token {
8686
return new Token(index, TOKEN_TYPE_CHARACTER, code, StringWrapper.fromCharCode(code));
8787
}
8888

89-
function newIdentifierToken(index:number/*int*/, text:string):Token {
89+
function newIdentifierToken(index:int, text:string):Token {
9090
return new Token(index, TOKEN_TYPE_IDENTIFIER, 0, text);
9191
}
9292

93-
function newKeywordToken(index:number/*int*/, text:string):Token {
93+
function newKeywordToken(index:int, text:string):Token {
9494
return new Token(index, TOKEN_TYPE_KEYWORD, 0, text);
9595
}
9696

97-
function newOperatorToken(index:number/*int*/, text:string):Token {
97+
function newOperatorToken(index:int, text:string):Token {
9898
return new Token(index, TOKEN_TYPE_OPERATOR, 0, text);
9999
}
100100

101-
function newStringToken(index:number/*int*/, text:string):Token {
101+
function newStringToken(index:int, text:string):Token {
102102
return new Token(index, TOKEN_TYPE_STRING, 0, text);
103103
}
104104

105-
function newNumberToken(index:number/*int*/, n:number/*int*/):Token {
105+
function newNumberToken(index:int, n:number):Token {
106106
return new Token(index, TOKEN_TYPE_NUMBER, n, "");
107107
}
108108

@@ -211,7 +211,7 @@ export class Scanner {
211211
if (isIdentifierStart(peek)) return this.scanIdentifier();
212212
if (isDigit(peek)) return this.scanNumber(index);
213213

214-
var start:number/*int*/ = index;
214+
var start:int = index;
215215
switch (peek) {
216216
case $PERIOD:
217217
this.advance();
@@ -255,21 +255,21 @@ export class Scanner {
255255
return null;
256256
}
257257

258-
scanCharacter(start:number/*int*/, code:number/*int*/):Token {
258+
scanCharacter(start:int, code:int):Token {
259259
assert(this.peek == code);
260260
this.advance();
261261
return newCharacterToken(start, code);
262262
}
263263

264264

265-
scanOperator(start:number/*int*/, str:string):Token {
265+
scanOperator(start:int, str:string):Token {
266266
assert(this.peek == StringWrapper.charCodeAt(str, 0));
267267
assert(SetWrapper.has(OPERATORS, str));
268268
this.advance();
269269
return newOperatorToken(start, str);
270270
}
271271

272-
scanComplexOperator(start:number/*int*/, code:number/*int*/, one:string, two:string):Token {
272+
scanComplexOperator(start:int, code:int, one:string, two:string):Token {
273273
assert(this.peek == StringWrapper.charCodeAt(one, 0));
274274
this.advance();
275275
var str:string = one;
@@ -283,7 +283,7 @@ export class Scanner {
283283

284284
scanIdentifier():Token {
285285
assert(isIdentifierStart(this.peek));
286-
var start:number/*int*/ = this.index;
286+
var start:int = this.index;
287287
this.advance();
288288
while (isIdentifierPart(this.peek)) this.advance();
289289
var str:string = this.input.substring(start, this.index);
@@ -294,7 +294,7 @@ export class Scanner {
294294
}
295295
}
296296

297-
scanNumber(start:number/*int*/):Token {
297+
scanNumber(start:int):Token {
298298
assert(isDigit(this.peek));
299299
var simple:boolean = (this.index === start);
300300
this.advance(); // Skip initial digit.
@@ -315,31 +315,31 @@ export class Scanner {
315315
}
316316
var str:string = this.input.substring(start, this.index);
317317
// TODO
318-
var value:number = simple ? NumberWrapper.parseIntAutoRadix(str) : NumberWrapper.parseDouble(str);
318+
var value:number = simple ? NumberWrapper.parseIntAutoRadix(str) : NumberWrapper.parseFloat(str);
319319
return newNumberToken(start, value);
320320
}
321321

322322
scanString():Token {
323323
assert(this.peek == $SQ || this.peek == $DQ);
324-
var start:number/*int*/ = this.index;
325-
var quote:number/*int*/ = this.peek;
324+
var start:int = this.index;
325+
var quote:int = this.peek;
326326
this.advance(); // Skip initial quote.
327327

328328
var buffer:StringJoiner; //ckck
329-
var marker:number/*int*/ = this.index;
329+
var marker:int = this.index;
330330
var input:string = this.input;
331331

332332
while (this.peek != quote) {
333333
if (this.peek == $BACKSLASH) {
334334
if (buffer == null) buffer = new StringJoiner();
335335
buffer.add(input.substring(marker, this.index));
336336
this.advance();
337-
var unescapedCode:number/*int*/;
337+
var unescapedCode:int;
338338
if (this.peek == $u) {
339339
// 4 character hex code for unicode character.
340340
var hex:string = input.substring(this.index + 1, this.index + 5);
341341
unescapedCode = NumberWrapper.parseInt(hex, 16);
342-
for (var i:number/*int*/ = 0; i < 5; i++) {
342+
for (var i:int = 0; i < 5; i++) {
343343
this.advance();
344344
}
345345
} else {
@@ -369,43 +369,43 @@ export class Scanner {
369369
}
370370

371371
error(message:string) {
372-
var position:number/*int*/ = this.index + this.offset;
372+
var position:int = this.index + this.offset;
373373
throw `Lexer Error: ${message} at column ${position} in expression [${input}]`;
374374
}
375375
}
376376

377-
function isWhitespace(code:number/*int*/):boolean {
377+
function isWhitespace(code:int):boolean {
378378
return (code >= $TAB && code <= $SPACE) || (code == $NBSP);
379379
}
380380

381-
function isIdentifierStart(code:number/*int*/):boolean {
381+
function isIdentifierStart(code:int):boolean {
382382
return ($a <= code && code <= $z) ||
383383
($A <= code && code <= $Z) ||
384384
(code == $_) ||
385385
(code == $$);
386386
}
387387

388-
function isIdentifierPart(code:number/*int*/):boolean {
388+
function isIdentifierPart(code:int):boolean {
389389
return ($a <= code && code <= $z) ||
390390
($A <= code && code <= $Z) ||
391391
($0 <= code && code <= $9) ||
392392
(code == $_) ||
393393
(code == $$);
394394
}
395395

396-
function isDigit(code:number/*int*/):boolean {
396+
function isDigit(code:int):boolean {
397397
return $0 <= code && code <= $9;
398398
}
399399

400-
function isExponentStart(code:number/*int*/):boolean {
400+
function isExponentStart(code:int):boolean {
401401
return code == $e || code == $E;
402402
}
403403

404-
function isExponentSign(code:number/*int*/):boolean {
404+
function isExponentSign(code:int):boolean {
405405
return code == $MINUS || code == $PLUS;
406406
}
407407

408-
function unescape(code:number/*int*/):number/*int*/ {
408+
function unescape(code:int):int {
409409
switch(code) {
410410
case $n: return $LF;
411411
case $f: return $FF;
@@ -445,16 +445,4 @@ var KEYWORDS = SetWrapper.createFromList([
445445
'undefined',
446446
'true',
447447
'false',
448-
]);
449-
450-
451-
export function Lexer(text:string):List {
452-
var scanner:Scanner = new Scanner(text);
453-
var tokens:List<Token> = [];
454-
var token:Token = scanner.scanToken();
455-
while (token != null) {
456-
ListWrapper.push(tokens, token);
457-
token = scanner.scanToken();
458-
}
459-
return tokens;
460-
}
448+
]);

modules/change_detection/test/parser/lexer_spec.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
import {describe, it, expect} from 'test_lib/test_lib';
2-
import {Lexer, Scanner, Token} from 'change_detection/parser/lexer';
2+
import {Scanner, Token} from 'change_detection/parser/scanner';
33
import {DOM} from 'facade/dom';
44
import {List, ListWrapper} from "facade/collection";
55
import {StringWrapper} from "facade/lang";
66

7+
function lex(text:string):List {
8+
var scanner:Scanner = new Scanner(text);
9+
var tokens:List<Token> = [];
10+
var token:Token = scanner.scanToken();
11+
while (token != null) {
12+
ListWrapper.push(tokens, token);
13+
token = scanner.scanToken();
14+
}
15+
return tokens;
16+
}
17+
718
function expectToken(token, index) {
819
expect(token instanceof Token).toBe(true);
920
expect(token.index).toEqual(index);
@@ -46,51 +57,51 @@ function expectKeywordToken(token, index, keyword) {
4657

4758

4859
export function main() {
49-
describe('lexer', function() {
60+
describe('scanner', function() {
5061
describe('token', function() {
5162
it('should tokenize a simple identifier', function() {
52-
var tokens:List<int> = Lexer("j");
63+
var tokens:List<int> = lex("j");
5364
expect(tokens.length).toEqual(1);
5465
expectIdentifierToken(tokens[0], 0, 'j');
5566
});
5667

5768
it('should tokenize a dotted identifier', function() {
58-
var tokens:List<int> = Lexer("j.k");
69+
var tokens:List<int> = lex("j.k");
5970
expect(tokens.length).toEqual(3);
6071
expectIdentifierToken(tokens[0], 0, 'j');
6172
expectCharacterToken (tokens[1], 1, '.');
6273
expectIdentifierToken(tokens[2], 2, 'k');
6374
});
6475

6576
it('should tokenize an operator', function() {
66-
var tokens:List<int> = Lexer("j-k");
77+
var tokens:List<int> = lex("j-k");
6778
expect(tokens.length).toEqual(3);
6879
expectOperatorToken(tokens[1], 1, '-');
6980
});
7081

7182
it('should tokenize an indexed operator', function() {
72-
var tokens:List<int> = Lexer("j[k]");
83+
var tokens:List<int> = lex("j[k]");
7384
expect(tokens.length).toEqual(4);
7485
expectCharacterToken(tokens[1], 1, "[");
7586
expectCharacterToken(tokens[3], 3, "]");
7687
});
7788

7889
it('should tokenize numbers', function() {
79-
var tokens:List<int> = Lexer("88");
90+
var tokens:List<int> = lex("88");
8091
expect(tokens.length).toEqual(1);
8192
expectNumberToken(tokens[0], 0, 88);
8293
});
8394

8495
it('should tokenize numbers within index ops', function() {
85-
expectNumberToken(Lexer("a[22]")[2], 2, 22);
96+
expectNumberToken(lex("a[22]")[2], 2, 22);
8697
});
8798

8899
it('should tokenize simple quoted strings', function() {
89-
expectStringToken(Lexer('"a"')[0], 0, "a");
100+
expectStringToken(lex('"a"')[0], 0, "a");
90101
});
91102

92103
it('should tokenize quoted strings with escaped quotes', function() {
93-
expectStringToken(Lexer('"a\\""')[0], 0, 'a"');
104+
expectStringToken(lex('"a\\""')[0], 0, 'a"');
94105
});
95106

96107
});

0 commit comments

Comments
 (0)