Skip to content

Commit edd0161

Browse files
committed
refactor(Lexer): switch token types to an enum
1 parent 9700e80 commit edd0161

File tree

1 file changed

+42
-40
lines changed
  • modules/angular2/src/change_detection/parser

1 file changed

+42
-40
lines changed

modules/angular2/src/change_detection/parser/lexer.ts

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ import {
88
isPresent
99
} from "angular2/src/facade/lang";
1010

11-
const TOKEN_TYPE_CHARACTER = 1;
12-
const TOKEN_TYPE_IDENTIFIER = 2;
13-
const TOKEN_TYPE_KEYWORD = 3;
14-
const TOKEN_TYPE_STRING = 4;
15-
const TOKEN_TYPE_OPERATOR = 5;
16-
const TOKEN_TYPE_NUMBER = 6;
17-
18-
@Injectable()
19-
export class Lexer {
11+
enum TokenType {
12+
CHARACTER,
13+
IDENTIFIER,
14+
KEYWORD,
15+
STRING,
16+
OPERATOR,
17+
NUMBER
18+
}
19+
20+
@Injectable() export class Lexer {
2021
tokenize(text: string): List<any> {
2122
var scanner = new _Scanner(text);
2223
var tokens = [];
@@ -30,86 +31,87 @@ export class Lexer {
3031
}
3132

3233
export class Token {
33-
constructor(public index: number, public type: number, public numValue: number,
34+
constructor(public index: number, public type: TokenType, public numValue: number,
3435
public strValue: string) {}
3536

3637
isCharacter(code: number): boolean {
37-
return (this.type == TOKEN_TYPE_CHARACTER && this.numValue == code);
38+
return (this.type == TokenType.CHARACTER && this.numValue == code);
3839
}
3940

40-
isNumber(): boolean { return (this.type == TOKEN_TYPE_NUMBER); }
41+
isNumber(): boolean { return (this.type == TokenType.NUMBER); }
4142

42-
isString(): boolean { return (this.type == TOKEN_TYPE_STRING); }
43+
isString(): boolean { return (this.type == TokenType.STRING); }
4344

4445
isOperator(operater: string): boolean {
45-
return (this.type == TOKEN_TYPE_OPERATOR && this.strValue == operater);
46+
return (this.type == TokenType.OPERATOR && this.strValue == operater);
4647
}
4748

48-
isIdentifier(): boolean { return (this.type == TOKEN_TYPE_IDENTIFIER); }
49+
isIdentifier(): boolean { return (this.type == TokenType.IDENTIFIER); }
4950

50-
isKeyword(): boolean { return (this.type == TOKEN_TYPE_KEYWORD); }
51+
isKeyword(): boolean { return (this.type == TokenType.KEYWORD); }
5152

52-
isKeywordVar(): boolean { return (this.type == TOKEN_TYPE_KEYWORD && this.strValue == "var"); }
53+
isKeywordVar(): boolean { return (this.type == TokenType.KEYWORD && this.strValue == "var"); }
5354

54-
isKeywordNull(): boolean { return (this.type == TOKEN_TYPE_KEYWORD && this.strValue == "null"); }
55+
isKeywordNull(): boolean { return (this.type == TokenType.KEYWORD && this.strValue == "null"); }
5556

5657
isKeywordUndefined(): boolean {
57-
return (this.type == TOKEN_TYPE_KEYWORD && this.strValue == "undefined");
58+
return (this.type == TokenType.KEYWORD && this.strValue == "undefined");
5859
}
5960

60-
isKeywordTrue(): boolean { return (this.type == TOKEN_TYPE_KEYWORD && this.strValue == "true"); }
61+
isKeywordTrue(): boolean { return (this.type == TokenType.KEYWORD && this.strValue == "true"); }
6162

62-
isKeywordIf(): boolean { return (this.type == TOKEN_TYPE_KEYWORD && this.strValue == "if"); }
63+
isKeywordIf(): boolean { return (this.type == TokenType.KEYWORD && this.strValue == "if"); }
6364

64-
isKeywordElse(): boolean { return (this.type == TOKEN_TYPE_KEYWORD && this.strValue == "else"); }
65+
isKeywordElse(): boolean { return (this.type == TokenType.KEYWORD && this.strValue == "else"); }
6566

66-
isKeywordFalse(): boolean {
67-
return (this.type == TOKEN_TYPE_KEYWORD && this.strValue == "false");
68-
}
67+
isKeywordFalse(): boolean { return (this.type == TokenType.KEYWORD && this.strValue == "false"); }
6968

7069
toNumber(): number {
7170
// -1 instead of NULL ok?
72-
return (this.type == TOKEN_TYPE_NUMBER) ? this.numValue : -1;
71+
return (this.type == TokenType.NUMBER) ? this.numValue : -1;
7372
}
7473

7574
toString(): string {
76-
var t: number = this.type;
77-
if (t >= TOKEN_TYPE_CHARACTER && t <= TOKEN_TYPE_STRING) {
78-
return this.strValue;
79-
} else if (t == TOKEN_TYPE_NUMBER) {
80-
return this.numValue.toString();
81-
} else {
82-
return null;
75+
switch (this.type) {
76+
case TokenType.CHARACTER:
77+
case TokenType.STRING:
78+
case TokenType.IDENTIFIER:
79+
case TokenType.KEYWORD:
80+
return this.strValue;
81+
case TokenType.NUMBER:
82+
return this.numValue.toString();
83+
default:
84+
return null;
8385
}
8486
}
8587
}
8688

8789
function newCharacterToken(index: number, code: number): Token {
88-
return new Token(index, TOKEN_TYPE_CHARACTER, code, StringWrapper.fromCharCode(code));
90+
return new Token(index, TokenType.CHARACTER, code, StringWrapper.fromCharCode(code));
8991
}
9092

9193
function newIdentifierToken(index: number, text: string): Token {
92-
return new Token(index, TOKEN_TYPE_IDENTIFIER, 0, text);
94+
return new Token(index, TokenType.IDENTIFIER, 0, text);
9395
}
9496

9597
function newKeywordToken(index: number, text: string): Token {
96-
return new Token(index, TOKEN_TYPE_KEYWORD, 0, text);
98+
return new Token(index, TokenType.KEYWORD, 0, text);
9799
}
98100

99101
function newOperatorToken(index: number, text: string): Token {
100-
return new Token(index, TOKEN_TYPE_OPERATOR, 0, text);
102+
return new Token(index, TokenType.OPERATOR, 0, text);
101103
}
102104

103105
function newStringToken(index: number, text: string): Token {
104-
return new Token(index, TOKEN_TYPE_STRING, 0, text);
106+
return new Token(index, TokenType.STRING, 0, text);
105107
}
106108

107109
function newNumberToken(index: number, n: number): Token {
108-
return new Token(index, TOKEN_TYPE_NUMBER, n, "");
110+
return new Token(index, TokenType.NUMBER, n, "");
109111
}
110112

111113

112-
export var EOF: Token = new Token(-1, 0, 0, "");
114+
export var EOF: Token = new Token(-1, TokenType.CHARACTER, 0, "");
113115

114116
export const $EOF = 0;
115117
export const $TAB = 9;

0 commit comments

Comments
 (0)