add function regexps and tests

This commit is contained in:
ukyo 2012-10-01 00:18:47 +09:00
commit 1510119e7f
2 changed files with 220 additions and 92 deletions

View file

@ -87,7 +87,27 @@ define(function(require, exports, module) {
"variable.language": variableLanguage
}, "identifier");
var functionRe = "(\\()([^)#]*)(\\))(\\s*)([\\-=]>)"
var headRe = "[$A-Za-z_\\x7f-\\uffff]";
var functionRe = {
"({args})->": {
token: ["paren.lparen", "text", "paren.lparen", "text", "variable.parameter", "text", "paren.rparen", "text", "paren.rparen", "text", "storage.type"],
regex: "(\\()(\\s*)(\\{)(\\s*)(" + [headRe, headRe + "[$\\w\\s,\\x7f-\\uffff]*"].join("|") + ")(\\s*)(\\})(\\s*)(\\))(\\s*)([\\-=]>)"
},
"({})->": {
token: ["paren.lparen", "text", "paren.lparen", "text", "paren.rparen", "text", "paren.rparen", "text", "storage.type"],
regex: "(\\()(\\s*)(\\{)(\\s*)(\\})(\\s*)(\\))(\\s*)([\\-=]>)"
},
"(args)->": {
token: ["paren.lparen", "text", "variable.parameter", "text", "paren.rparen", "text", "storage.type"],
regex: "(\\()(\\s*)(" + [headRe, headRe + "[$\\w\\x7f-\\uffff]", headRe + "[^#)]*[^#(){}=,\\/\\\\]"].join("|") + ")(\\s*)(\\))(\\s*)([\\-=]>)"
},
"()->": {
token: ["paren.lparen", "text", "paren.rparen", "text", "storage.type"],
regex: "(\\()(\\s*)(\\))(\\s*)([\\-=]>)"
}
};
this.$rules = {
start : [
@ -155,14 +175,34 @@ define(function(require, exports, module) {
"keyword", "text", "language.support.class"
],
regex : "(class)(\\s+)(" + identifier + ")"
}, {
//play = ({args}) ->
//play : ({args}) ->
token : [
"entity.name.function", "text", "keyword.operator", "text"
].concat(functionRe["({args})->"].token),
regex : "(" + identifier + ")(\\s*)(=|:)(\\s*)" + functionRe["({args})->"].regex
}, {
//play = ({}) ->
//play : ({}) ->
token : [
"entity.name.function", "text", "keyword.operator", "text"
].concat(functionRe["({})->"].token),
regex : "(" + identifier + ")(\\s*)(=|:)(\\s*)" + functionRe["({})->"].regex
}, {
//play = (args) ->
//play : (args) ->
token : [
"entity.name.function", "text", "keyword.operator", "text",
"paren.lparen", "variable.parameter", "paren.rparen", "text", "storage.type"
],
regex : "(" + identifier + ")(\\s*)(=|:)(\\s*)" + functionRe
"entity.name.function", "text", "keyword.operator", "text"
].concat(functionRe["(args)->"].token),
regex : "(" + identifier + ")(\\s*)(=|:)(\\s*)" + functionRe["(args)->"].regex
}, {
//play = () ->
//play : () ->
token : [
"entity.name.function", "text", "keyword.operator", "text"
].concat(functionRe["()->"].token),
regex : "(" + identifier + ")(\\s*)(=|:)(\\s*)" + functionRe["()->"].regex
}, {
//play = ->
//play : ->
@ -170,13 +210,12 @@ define(function(require, exports, module) {
"entity.name.function", "text", "keyword.operator", "text", "storage.type"
],
regex : "(" + identifier + ")(\\s*)(=|:)(\\s*)([\\-=]>)"
}, {
//(args) ->
token : [
"paren.lparen", "variable.parameter", "paren.rparen", "text", "storage.type"
],
regex : functionRe
}, {
},
functionRe["({args})->"],
functionRe["({})->"],
functionRe["(args)->"],
functionRe["()->"]
, {
token : "identifier",
regex : "(?:(?:\\.|::)\\s*)" + identifier
}, {

View file

@ -41,6 +41,11 @@ var assert = require("../test/assertions");
module.exports = {
setUp : function() {
this.tokenizer = new Mode().getTokenizer();
this.testTokens = function(tokens, correct) {
correct.forEach(function(type, i) {
assert.equal(tokens[i].type, type);
});
};
},
"test: tokenize keyword": function() {
@ -49,121 +54,205 @@ module.exports = {
assert.equal(tokens[0].type, "keyword");
},
"test: tokenize function: 'foo = (args) ->'": function() {
var tokens = this.tokenizer.getLineTokens("foo = (args) ->", "start").tokens;
console.log(tokens);
assert.equal(tokens.length, 9);
[
"test: tokenize function: 'foo = ({args}) ->'": function() {
var tokens = this.tokenizer.getLineTokens("foo = ({args}) ->", "start").tokens;
var correct = [
"entity.name.function", "text", "keyword.operator", "text",
"paren.lparen", "variable.parameter", "paren.rparen", "text", "storage.type"
].forEach(function(type, i) {
assert.equal(tokens[i].type, type);
});
},
"test: tokenize function: 'window.foo = (args) ->'": function() {
var tokens = this.tokenizer.getLineTokens("window.foo = (args) ->", "start").tokens;
console.log(tokens);
"paren.lparen", "paren.lparen", "variable.parameter", "paren.rparen", "paren.rparen", "text", "storage.type"
];
assert.equal(tokens.length, 11);
[
"variable.language", "punctuation.operator", "entity.name.function", "text", "keyword.operator", "text",
"paren.lparen", "variable.parameter", "paren.rparen", "text", "storage.type"
].forEach(function(type, i) {
assert.equal(tokens[i].type, type);
});
this.testTokens(tokens, correct);
tokens = this.tokenizer.getLineTokens("foo = ({arg1, arg2}) ->", "start").tokens;
assert.equal(tokens.length, 11);
this.testTokens(tokens, correct);
tokens = this.tokenizer.getLineTokens("foo : ({arg1, arg2}) ->", "start").tokens;
assert.equal(tokens.length, 11);
this.testTokens(tokens, correct);
},
"test: tokenize function: 'foo : (args) ->'": function() {
var tokens = this.tokenizer.getLineTokens("foo : (args) ->", "start").tokens;
assert.equal(tokens.length, 9);
[
"test: tokenize function: invalid case: 'foo = ({args}) ->'": function() {
var tokens = this.tokenizer.getLineTokens("foo = ({0abc}) ->", "start").tokens;
assert.notEqual(tokens[0].type, "entity.name.function");
tokens = this.tokenizer.getLineTokens("foo = ({/abc}) ->", "start").tokens;
assert.notEqual(tokens[0].type, "entity.name.function");
tokens = this.tokenizer.getLineTokens("foo = ({abc/}) ->", "start").tokens;
assert.notEqual(tokens[0].type, "entity.name.function");
tokens = this.tokenizer.getLineTokens("foo = ({#abc}) ->", "start").tokens;
assert.notEqual(tokens[0].type, "entity.name.function");
tokens = this.tokenizer.getLineTokens("foo = ({abc#}) ->", "start").tokens;
assert.notEqual(tokens[0].type, "entity.name.function");
tokens = this.tokenizer.getLineTokens("foo = ({)abc}) ->", "start").tokens;
assert.notEqual(tokens[0].type, "entity.name.function");
tokens = this.tokenizer.getLineTokens("foo = ({abc)}) ->", "start").tokens;
assert.notEqual(tokens[0].type, "entity.name.function");
tokens = this.tokenizer.getLineTokens("foo = ({a{bc}) ->", "start").tokens;
assert.notEqual(tokens[0].type, "entity.name.function");
},
"test: tokenize function: 'foo = ({}) ->'": function() {
var tokens = this.tokenizer.getLineTokens("foo = ({}) ->", "start").tokens;
var correct = [
"entity.name.function", "text", "keyword.operator", "text",
"paren.lparen", "paren.lparen", "paren.rparen", "paren.rparen", "text", "storage.type"
];
assert.equal(tokens.length, 10);
this.testTokens(tokens, correct);
tokens = this.tokenizer.getLineTokens("foo : ({}) ->", "start").tokens;
assert.equal(tokens.length, 10);
this.testTokens(tokens, correct);
tokens = this.tokenizer.getLineTokens("foo = ({ }) ->", "start").tokens;
correct = [
"entity.name.function", "text", "keyword.operator", "text",
"paren.lparen", "paren.lparen", "text", "paren.rparen", "paren.rparen", "text", "storage.type"
];
assert.equal(tokens.length, 11);
this.testTokens(tokens, correct);
},
"test: tokenize function: 'foo = (args) ->'": function() {
var tokens = this.tokenizer.getLineTokens("foo = (args) ->", "start").tokens;
var correct = [
"entity.name.function", "text", "keyword.operator", "text",
"paren.lparen", "variable.parameter", "paren.rparen", "text", "storage.type"
].forEach(function(type, i) {
assert.equal(tokens[i].type, type);
});
];
assert.equal(tokens.length, 9);
this.testTokens(tokens, correct);
tokens = this.tokenizer.getLineTokens("foo = (arg1, arg2) ->", "start").tokens;
assert.equal(tokens.length, 9);
this.testTokens(tokens, correct);
tokens = this.tokenizer.getLineTokens("foo = (arg1 = 1, arg2 = 'name') ->", "start").tokens;
assert.equal(tokens.length, 9);
this.testTokens(tokens, correct);
},
"test: tokenize function: invalid case: 'foo=(args) ->'": function() {
var tokens = this.tokenizer.getLineTokens("foo=(args#) ->", "start").tokens;
assert.notEqual(tokens[0].type, "entity.name.function");
tokens = this.tokenizer.getLineTokens("foo=(args=) ->", "start").tokens;
assert.notEqual(tokens[0].type, "entity.name.function");
tokens = this.tokenizer.getLineTokens("foo=(args{) ->", "start").tokens;
assert.notEqual(tokens[0].type, "entity.name.function");
tokens = this.tokenizer.getLineTokens("foo=(args}) ->", "start").tokens;
assert.notEqual(tokens[0].type, "entity.name.function");
tokens = this.tokenizer.getLineTokens("foo=(}args) ->", "start").tokens;
assert.notEqual(tokens[0].type, "entity.name.function");
tokens = this.tokenizer.getLineTokens("foo=(a)rgs) ->", "start").tokens;
assert.notEqual(tokens[0].type, "entity.name.function");
tokens = this.tokenizer.getLineTokens("foo=(args/) ->", "start").tokens;
assert.notEqual(tokens[0].type, "entity.name.function");
tokens = this.tokenizer.getLineTokens("foo=(args\\) ->", "start").tokens;
assert.notEqual(tokens[0].type, "entity.name.function");
},
"test: tokenize function: 'foo = () ->'": function() {
var tokens = this.tokenizer.getLineTokens("foo = () ->", "start").tokens;
var correct = [
"entity.name.function", "text", "keyword.operator", "text",
"paren.lparen", "paren.rparen", "text", "storage.type"
];
assert.equal(tokens.length, 8);
this.testTokens(tokens, correct);
tokens = this.tokenizer.getLineTokens("foo : ( ) ->", "start").tokens;
correct = [
"entity.name.function", "text", "keyword.operator", "text",
"paren.lparen", "text", "paren.rparen", "text", "storage.type"
];
assert.equal(tokens.length, 9);
this.testTokens(tokens, correct);
},
"test: tokenize function: 'window.foo = (args) ->'": function() {
var tokens = this.tokenizer.getLineTokens("window.foo = (args) ->", "start").tokens;
var correct = [
"variable.language", "punctuation.operator", "entity.name.function", "text", "keyword.operator", "text",
"paren.lparen", "variable.parameter", "paren.rparen", "text", "storage.type"
];
assert.equal(tokens.length, 11);
this.testTokens(tokens, correct);
this.tokenizer.getLineTokens("window.foo = (args) ->", "start").tokens;
assert.equal(tokens.length, 11);
this.testTokens(tokens, correct);
},
"test: tokenize function: 'foo = ->'": function() {
var tokens = this.tokenizer.getLineTokens("foo = ->", "start").tokens;
assert.equal(tokens.length, 5);
[
var correct = [
"entity.name.function", "text", "keyword.operator", "text", "storage.type"
].forEach(function(type, i) {
assert.equal(tokens[i].type, type);
});
},
];
"test: tokenize function: 'foo : ->'": function() {
var tokens = this.tokenizer.getLineTokens("foo : ->", "start").tokens;
assert.equal(tokens.length, 5);
[
"entity.name.function", "text", "keyword.operator", "text", "storage.type"
].forEach(function(type, i) {
assert.equal(tokens[i].type, type);
});
},
this.testTokens(tokens, correct);
"test: tokenize function(invalid code): 'foo:(args#) ->'": function() {
var tokens = this.tokenizer.getLineTokens("foo:(args#) ->", "start").tokens;
this.tokenizer.getLineTokens("foo : ->", "start").tokens;
assert.equal(tokens.length, 5);
[
"identifier", "punctuation.operator", "paren.lparen", "identifier", "comment"
].forEach(function(type, i) {
assert.equal(tokens[i].type, type);
});
this.testTokens(tokens, correct);
},
"test: tokenize function: '(args) ->'": function() {
var tokens = this.tokenizer.getLineTokens("(args) ->", "start").tokens;
assert.equal(tokens.length, 5);
[
"paren.lparen", "variable.parameter", "paren.rparen", "text", "storage.type"
].forEach(function(type, i) {
assert.equal(tokens[i].type, type);
});
},
"test: tokenize function(callback): 'foo bar: 1, (args) ->'": function() {
"test: tokenize callback function: 'foo bar: 1, (args) ->'": function() {
var tokens = this.tokenizer.getLineTokens("foo bar: 1, (args) ->", "start").tokens;
assert.equal(tokens.length, 13);
[
var correct = [
"identifier", "text", "identifier", "punctuation.operator", "text", "constant.numeric", "punctuation.operator", "text",
"paren.lparen", "variable.parameter", "paren.rparen", "text", "storage.type"
].forEach(function(type, i) {
assert.equal(tokens[i].type, type);
});
];
assert.equal(tokens.length, 13);
this.testTokens(tokens, correct);
},
"test: tokenize class: 'class Foo'": function() {
var tokens = this.tokenizer.getLineTokens("class Foo", "start").tokens;
assert.equal(tokens.length, 3);
[
var correct = [
"keyword", "text", "language.support.class"
].forEach(function(type, i) {
assert.equal(tokens[i].type, type);
});
];
assert.equal(tokens.length, 3);
this.testTokens(tokens, correct);
},
"test: tokenize class 'class Foo extends Bar'": function() {
var tokens = this.tokenizer.getLineTokens("class Foo extends Bar", "start").tokens;
assert.equal(tokens.length, 7);
[
var correct = [
"keyword", "text", "language.support.class", "text", "keyword", "text", "language.support.class"
].forEach(function(type, i) {
assert.equal(tokens[i].type, type);
});
];
assert.equal(tokens.length, 7);
this.testTokens(tokens, correct);
},
"test: tokenize illegal name property: 'foo.static.function'": function() {
var tokens = this.tokenizer.getLineTokens("foo.static.function", "start").tokens;
assert.equal(tokens.length, 5);
[
var correct = [
"identifier", "punctuation.operator", "identifier", "punctuation.operator", "identifier"
].forEach(function(type, i) {
assert.equal(tokens[i].type, type);
});
];
assert.equal(tokens.length, 5);
this.testTokens(tokens, correct);
},
// TODO: disable. not yet implemented