refactor(Lexer): add types

relates to #9100
This commit is contained in:
Victor Berchet
2016-06-24 12:06:13 -07:00
parent 25e070dd65
commit e676fded21
2 changed files with 25 additions and 36 deletions

View File

@ -25,10 +25,10 @@ const KEYWORDS = ['var', 'let', 'null', 'undefined', 'true', 'false', 'if', 'els
@Injectable()
export class Lexer {
tokenize(text: string): any[] {
var scanner = new _Scanner(text);
var tokens: Token[] = [];
var token = scanner.scanToken();
tokenize(text: string): Token[] {
const scanner = new _Scanner(text);
const tokens: Token[] = [];
let token = scanner.scanToken();
while (token != null) {
tokens.push(token);
token = scanner.scanToken();
@ -43,43 +43,40 @@ export class Token {
public strValue: string) {}
isCharacter(code: number): boolean {
return (this.type == TokenType.Character && this.numValue == code);
return this.type == TokenType.Character && this.numValue == code;
}
isNumber(): boolean { return (this.type == TokenType.Number); }
isNumber(): boolean { return this.type == TokenType.Number; }
isString(): boolean { return (this.type == TokenType.String); }
isString(): boolean { return this.type == TokenType.String; }
isOperator(operater: string): boolean {
return (this.type == TokenType.Operator && this.strValue == operater);
return this.type == TokenType.Operator && this.strValue == operater;
}
isIdentifier(): boolean { return (this.type == TokenType.Identifier); }
isIdentifier(): boolean { return this.type == TokenType.Identifier; }
isKeyword(): boolean { return (this.type == TokenType.Keyword); }
isKeyword(): boolean { return this.type == TokenType.Keyword; }
isKeywordDeprecatedVar(): boolean {
return (this.type == TokenType.Keyword && this.strValue == 'var');
return this.type == TokenType.Keyword && this.strValue == 'var';
}
isKeywordLet(): boolean { return (this.type == TokenType.Keyword && this.strValue == 'let'); }
isKeywordLet(): boolean { return this.type == TokenType.Keyword && this.strValue == 'let'; }
isKeywordNull(): boolean { return (this.type == TokenType.Keyword && this.strValue == 'null'); }
isKeywordNull(): boolean { return this.type == TokenType.Keyword && this.strValue == 'null'; }
isKeywordUndefined(): boolean {
return (this.type == TokenType.Keyword && this.strValue == 'undefined');
return this.type == TokenType.Keyword && this.strValue == 'undefined';
}
isKeywordTrue(): boolean { return (this.type == TokenType.Keyword && this.strValue == 'true'); }
isKeywordTrue(): boolean { return this.type == TokenType.Keyword && this.strValue == 'true'; }
isKeywordFalse(): boolean { return (this.type == TokenType.Keyword && this.strValue == 'false'); }
isKeywordFalse(): boolean { return this.type == TokenType.Keyword && this.strValue == 'false'; }
isError(): boolean { return this.type == TokenType.Error; }
toNumber(): number {
// -1 instead of NULL ok?
return (this.type == TokenType.Number) ? this.numValue : -1;
}
toNumber(): number { return this.type == TokenType.Number ? this.numValue : -1; }
toString(): string {
switch (this.type) {