parent
25e070dd65
commit
e676fded21
|
@ -25,10 +25,10 @@ const KEYWORDS = ['var', 'let', 'null', 'undefined', 'true', 'false', 'if', 'els
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class Lexer {
|
export class Lexer {
|
||||||
tokenize(text: string): any[] {
|
tokenize(text: string): Token[] {
|
||||||
var scanner = new _Scanner(text);
|
const scanner = new _Scanner(text);
|
||||||
var tokens: Token[] = [];
|
const tokens: Token[] = [];
|
||||||
var token = scanner.scanToken();
|
let token = scanner.scanToken();
|
||||||
while (token != null) {
|
while (token != null) {
|
||||||
tokens.push(token);
|
tokens.push(token);
|
||||||
token = scanner.scanToken();
|
token = scanner.scanToken();
|
||||||
|
@ -43,43 +43,40 @@ export class Token {
|
||||||
public strValue: string) {}
|
public strValue: string) {}
|
||||||
|
|
||||||
isCharacter(code: number): boolean {
|
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 {
|
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 {
|
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 {
|
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; }
|
isError(): boolean { return this.type == TokenType.Error; }
|
||||||
|
|
||||||
toNumber(): number {
|
toNumber(): number { return this.type == TokenType.Number ? this.numValue : -1; }
|
||||||
// -1 instead of NULL ok?
|
|
||||||
return (this.type == TokenType.Number) ? this.numValue : -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
toString(): string {
|
toString(): string {
|
||||||
switch (this.type) {
|
switch (this.type) {
|
||||||
|
|
|
@ -8,47 +8,45 @@
|
||||||
|
|
||||||
import {Lexer, Token} from '@angular/compiler/src/expression_parser/lexer';
|
import {Lexer, Token} from '@angular/compiler/src/expression_parser/lexer';
|
||||||
|
|
||||||
import {StringWrapper} from '../../src/facade/lang';
|
|
||||||
|
|
||||||
function lex(text: string): any[] {
|
function lex(text: string): any[] {
|
||||||
return new Lexer().tokenize(text);
|
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 instanceof Token).toBe(true);
|
||||||
expect(token.index).toEqual(index);
|
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);
|
expect(character.length).toBe(1);
|
||||||
expectToken(token, index);
|
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);
|
expectToken(token, index);
|
||||||
expect(token.isOperator(operator)).toBe(true);
|
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);
|
expectToken(token, index);
|
||||||
expect(token.isNumber()).toBe(true);
|
expect(token.isNumber()).toBe(true);
|
||||||
expect(token.toNumber()).toEqual(n);
|
expect(token.toNumber()).toEqual(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
function expectStringToken(token: any, index: any, str: any) {
|
function expectStringToken(token: any, index: number, str: string) {
|
||||||
expectToken(token, index);
|
expectToken(token, index);
|
||||||
expect(token.isString()).toBe(true);
|
expect(token.isString()).toBe(true);
|
||||||
expect(token.toString()).toEqual(str);
|
expect(token.toString()).toEqual(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
function expectIdentifierToken(token: any, index: any, identifier: any) {
|
function expectIdentifierToken(token: any, index: number, identifier: string) {
|
||||||
expectToken(token, index);
|
expectToken(token, index);
|
||||||
expect(token.isIdentifier()).toBe(true);
|
expect(token.isIdentifier()).toBe(true);
|
||||||
expect(token.toString()).toEqual(identifier);
|
expect(token.toString()).toEqual(identifier);
|
||||||
}
|
}
|
||||||
|
|
||||||
function expectKeywordToken(token: any, index: any, keyword: any) {
|
function expectKeywordToken(token: any, index: number, keyword: string) {
|
||||||
expectToken(token, index);
|
expectToken(token, index);
|
||||||
expect(token.isKeyword()).toBe(true);
|
expect(token.isKeyword()).toBe(true);
|
||||||
expect(token.toString()).toEqual(keyword);
|
expect(token.toString()).toEqual(keyword);
|
||||||
|
@ -213,12 +211,6 @@ export function main() {
|
||||||
expectNumberToken(tokens[0], 0, 0.5);
|
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() {
|
it('should tokenize number with exponent', function() {
|
||||||
var tokens: Token[] = lex('0.5E-10');
|
var tokens: Token[] = lex('0.5E-10');
|
||||||
expect(tokens.length).toEqual(1);
|
expect(tokens.length).toEqual(1);
|
||||||
|
|
Loading…
Reference in New Issue