refactor(lexer): rename to scanner, use ints, etc.
This commit is contained in:
@ -1,9 +1,20 @@
|
||||
import {describe, it, expect} from 'test_lib/test_lib';
|
||||
import {Lexer, Scanner, Token} from 'change_detection/parser/lexer';
|
||||
import {Scanner, Token} from 'change_detection/parser/scanner';
|
||||
import {DOM} from 'facade/dom';
|
||||
import {List, ListWrapper} from "facade/collection";
|
||||
import {StringWrapper} from "facade/lang";
|
||||
|
||||
function lex(text:string):List {
|
||||
var scanner:Scanner = new Scanner(text);
|
||||
var tokens:List<Token> = [];
|
||||
var token:Token = scanner.scanToken();
|
||||
while (token != null) {
|
||||
ListWrapper.push(tokens, token);
|
||||
token = scanner.scanToken();
|
||||
}
|
||||
return tokens;
|
||||
}
|
||||
|
||||
function expectToken(token, index) {
|
||||
expect(token instanceof Token).toBe(true);
|
||||
expect(token.index).toEqual(index);
|
||||
@ -46,16 +57,16 @@ function expectKeywordToken(token, index, keyword) {
|
||||
|
||||
|
||||
export function main() {
|
||||
describe('lexer', function() {
|
||||
describe('scanner', function() {
|
||||
describe('token', function() {
|
||||
it('should tokenize a simple identifier', function() {
|
||||
var tokens:List<int> = Lexer("j");
|
||||
var tokens:List<int> = lex("j");
|
||||
expect(tokens.length).toEqual(1);
|
||||
expectIdentifierToken(tokens[0], 0, 'j');
|
||||
});
|
||||
|
||||
it('should tokenize a dotted identifier', function() {
|
||||
var tokens:List<int> = Lexer("j.k");
|
||||
var tokens:List<int> = lex("j.k");
|
||||
expect(tokens.length).toEqual(3);
|
||||
expectIdentifierToken(tokens[0], 0, 'j');
|
||||
expectCharacterToken (tokens[1], 1, '.');
|
||||
@ -63,34 +74,34 @@ export function main() {
|
||||
});
|
||||
|
||||
it('should tokenize an operator', function() {
|
||||
var tokens:List<int> = Lexer("j-k");
|
||||
var tokens:List<int> = lex("j-k");
|
||||
expect(tokens.length).toEqual(3);
|
||||
expectOperatorToken(tokens[1], 1, '-');
|
||||
});
|
||||
|
||||
it('should tokenize an indexed operator', function() {
|
||||
var tokens:List<int> = Lexer("j[k]");
|
||||
var tokens:List<int> = lex("j[k]");
|
||||
expect(tokens.length).toEqual(4);
|
||||
expectCharacterToken(tokens[1], 1, "[");
|
||||
expectCharacterToken(tokens[3], 3, "]");
|
||||
});
|
||||
|
||||
it('should tokenize numbers', function() {
|
||||
var tokens:List<int> = Lexer("88");
|
||||
var tokens:List<int> = lex("88");
|
||||
expect(tokens.length).toEqual(1);
|
||||
expectNumberToken(tokens[0], 0, 88);
|
||||
});
|
||||
|
||||
it('should tokenize numbers within index ops', function() {
|
||||
expectNumberToken(Lexer("a[22]")[2], 2, 22);
|
||||
expectNumberToken(lex("a[22]")[2], 2, 22);
|
||||
});
|
||||
|
||||
it('should tokenize simple quoted strings', function() {
|
||||
expectStringToken(Lexer('"a"')[0], 0, "a");
|
||||
expectStringToken(lex('"a"')[0], 0, "a");
|
||||
});
|
||||
|
||||
it('should tokenize quoted strings with escaped quotes', function() {
|
||||
expectStringToken(Lexer('"a\\""')[0], 0, 'a"');
|
||||
expectStringToken(lex('"a\\""')[0], 0, 'a"');
|
||||
});
|
||||
|
||||
});
|
||||
|
Reference in New Issue
Block a user