parent
25e070dd65
commit
e676fded21
|
@ -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) {
|
||||
|
|
|
@ -8,47 +8,45 @@
|
|||
|
||||
import {Lexer, Token} from '@angular/compiler/src/expression_parser/lexer';
|
||||
|
||||
import {StringWrapper} from '../../src/facade/lang';
|
||||
|
||||
function lex(text: string): any[] {
|
||||
return new Lexer().tokenize(text);
|
||||
}
|
||||
|
||||
function expectToken(token: any, index: any) {
|
||||
function expectToken(token: any, index: number) {
|
||||
expect(token instanceof Token).toBe(true);
|
||||
expect(token.index).toEqual(index);
|
||||
}
|
||||
|
||||
function expectCharacterToken(token: any, index: any, character: any) {
|
||||
function expectCharacterToken(token: any, index: number, character: string) {
|
||||
expect(character.length).toBe(1);
|
||||
expectToken(token, index);
|
||||
expect(token.isCharacter(StringWrapper.charCodeAt(character, 0))).toBe(true);
|
||||
expect(token.isCharacter(character.charCodeAt(0))).toBe(true);
|
||||
}
|
||||
|
||||
function expectOperatorToken(token: any, index: any, operator: any) {
|
||||
function expectOperatorToken(token: any, index: number, operator: string) {
|
||||
expectToken(token, index);
|
||||
expect(token.isOperator(operator)).toBe(true);
|
||||
}
|
||||
|
||||
function expectNumberToken(token: any, index: any, n: any) {
|
||||
function expectNumberToken(token: any, index: number, n: number) {
|
||||
expectToken(token, index);
|
||||
expect(token.isNumber()).toBe(true);
|
||||
expect(token.toNumber()).toEqual(n);
|
||||
}
|
||||
|
||||
function expectStringToken(token: any, index: any, str: any) {
|
||||
function expectStringToken(token: any, index: number, str: string) {
|
||||
expectToken(token, index);
|
||||
expect(token.isString()).toBe(true);
|
||||
expect(token.toString()).toEqual(str);
|
||||
}
|
||||
|
||||
function expectIdentifierToken(token: any, index: any, identifier: any) {
|
||||
function expectIdentifierToken(token: any, index: number, identifier: string) {
|
||||
expectToken(token, index);
|
||||
expect(token.isIdentifier()).toBe(true);
|
||||
expect(token.toString()).toEqual(identifier);
|
||||
}
|
||||
|
||||
function expectKeywordToken(token: any, index: any, keyword: any) {
|
||||
function expectKeywordToken(token: any, index: number, keyword: string) {
|
||||
expectToken(token, index);
|
||||
expect(token.isKeyword()).toBe(true);
|
||||
expect(token.toString()).toEqual(keyword);
|
||||
|
@ -213,12 +211,6 @@ export function main() {
|
|||
expectNumberToken(tokens[0], 0, 0.5);
|
||||
});
|
||||
|
||||
// NOTE(deboer): NOT A LEXER TEST
|
||||
// it('should tokenize negative number', () => {
|
||||
// var tokens:Token[] = lex("-0.5");
|
||||
// expectNumberToken(tokens[0], 0, -0.5);
|
||||
// });
|
||||
|
||||
it('should tokenize number with exponent', function() {
|
||||
var tokens: Token[] = lex('0.5E-10');
|
||||
expect(tokens.length).toEqual(1);
|
||||
|
|
Loading…
Reference in New Issue