From 4b25a4dfabaf18ad7180321debc72ff451c07082 Mon Sep 17 00:00:00 2001 From: ukyo Date: Tue, 2 Oct 2012 04:54:23 +0900 Subject: [PATCH] fix function regexps. Its ignore invalid coffeescipts. --- lib/ace/mode/coffee_highlight_rules.js | 70 ++++++++----- lib/ace/mode/coffee_highlight_rules_test.js | 107 +++++++++----------- 2 files changed, 94 insertions(+), 83 deletions(-) diff --git a/lib/ace/mode/coffee_highlight_rules.js b/lib/ace/mode/coffee_highlight_rules.js index 98253f49..c6d7b1bd 100644 --- a/lib/ace/mode/coffee_highlight_rules.js +++ b/lib/ace/mode/coffee_highlight_rules.js @@ -87,12 +87,10 @@ define(function(require, exports, module) { "variable.language": variableLanguage }, "identifier"); - var headRe = "[$A-Za-z_\\x7f-\\uffff]"; - - var functionRe = { + var functionRules = { "({args})->": { token: ["paren.lparen", "text", "paren.lparen", "text", "variable.parameter", "text", "paren.rparen", "text", "paren.rparen", "text", "storage.type"], - regex: "(\\()(\\s*)(\\{)(\\s*)(" + headRe + "[$\\w\\s,\\x7f-\\uffff]*" + ")(\\s*)(\\})(\\s*)(\\))(\\s*)([\\-=]>)" + regex: "(\\()(\\s*)(\\{)(\\s*)([$@A-Za-z_\\x7f-\\uffff][$@\\w\\s,\\x7f-\\uffff]*)(\\s*)(\\})(\\s*)(\\))(\\s*)([\\-=]>)" }, "({})->": { token: ["paren.lparen", "text", "paren.lparen", "text", "paren.rparen", "text", "paren.rparen", "text", "storage.type"], @@ -100,14 +98,13 @@ define(function(require, exports, module) { }, "(args)->": { token: ["paren.lparen", "text", "variable.parameter", "text", "paren.rparen", "text", "storage.type"], - regex: "(\\()(\\s*)(" + [headRe, headRe + "[$\\w\\x7f-\\uffff]", headRe + "[^#)]*[^#(){}=,\\/\\\\]"].join("|") + ")(\\s*)(\\))(\\s*)([\\-=]>)" + regex: "(\\()(\\s*)([$@A-Za-z_\\x7f-\\uffff][\\s\\x21-\\uffff]*)(\\s*)(\\))(\\s*)([\\-=]>)" }, "()->": { token: ["paren.lparen", "text", "paren.rparen", "text", "storage.type"], regex: "(\\()(\\s*)(\\))(\\s*)([\\-=]>)" } }; - this.$rules = { start : [ @@ -177,44 +174,69 @@ define(function(require, exports, module) { regex : "(class)(\\s+)(" + identifier + ")" }, { //play = ({args}) -> + token : [ + "entity.name.function", "text", "keyword.operator", "text" + ].concat(functionRules["({args})->"].token), + regex : "(" + identifier + ")(\\s*)(=)(\\s*)" + functionRules["({args})->"].regex + }, { //play : ({args}) -> token : [ - "entity.name.function", "text", "keyword.operator", "text" - ].concat(functionRe["({args})->"].token), - regex : "(" + identifier + ")(\\s*)(=|:)(\\s*)" + functionRe["({args})->"].regex + "entity.name.function", "text", "punctuation.operator", "text" + ].concat(functionRules["({args})->"].token), + regex : "(" + identifier + ")(\\s*)(:)(\\s*)" + functionRules["({args})->"].regex }, { //play = ({}) -> + token : [ + "entity.name.function", "text", "keyword.operator", "text" + ].concat(functionRules["({})->"].token), + regex : "(" + identifier + ")(\\s*)(=)(\\s*)" + functionRules["({})->"].regex + }, { //play : ({}) -> token : [ - "entity.name.function", "text", "keyword.operator", "text" - ].concat(functionRe["({})->"].token), - regex : "(" + identifier + ")(\\s*)(=|:)(\\s*)" + functionRe["({})->"].regex + "entity.name.function", "text", "punctuation.operator", "text" + ].concat(functionRules["({})->"].token), + regex : "(" + identifier + ")(\\s*)(:)(\\s*)" + functionRules["({})->"].regex }, { //play = (args) -> + token : [ + "entity.name.function", "text", "keyword.operator", "text" + ].concat(functionRules["(args)->"].token), + regex : "(" + identifier + ")(\\s*)(=)(\\s*)" + functionRules["(args)->"].regex + }, { //play : (args) -> token : [ - "entity.name.function", "text", "keyword.operator", "text" - ].concat(functionRe["(args)->"].token), - regex : "(" + identifier + ")(\\s*)(=|:)(\\s*)" + functionRe["(args)->"].regex + "entity.name.function", "text", "punctuation.operator", "text" + ].concat(functionRules["(args)->"].token), + regex : "(" + identifier + ")(\\s*)(:)(\\s*)" + functionRules["(args)->"].regex }, { //play = () -> - //play : () -> token : [ "entity.name.function", "text", "keyword.operator", "text" - ].concat(functionRe["()->"].token), - regex : "(" + identifier + ")(\\s*)(=|:)(\\s*)" + functionRe["()->"].regex + ].concat(functionRules["()->"].token), + regex : "(" + identifier + ")(\\s*)(=)(\\s*)" + functionRules["()->"].regex + }, { + //play : () -> + token : [ + "entity.name.function", "text", "punctuation.operator", "text" + ].concat(functionRules["()->"].token), + regex : "(" + identifier + ")(\\s*)(:)(\\s*)" + functionRules["()->"].regex }, { //play = -> - //play : -> token : [ "entity.name.function", "text", "keyword.operator", "text", "storage.type" ], - regex : "(" + identifier + ")(\\s*)(=|:)(\\s*)([\\-=]>)" + regex : "(" + identifier + ")(\\s*)(=)(\\s*)([\\-=]>)" + }, { + //play : -> + token : [ + "entity.name.function", "text", "punctuation.operator", "text", "storage.type" + ], + regex : "(" + identifier + ")(\\s*)(:)(\\s*)([\\-=]>)" }, - functionRe["({args})->"], - functionRe["({})->"], - functionRe["(args)->"], - functionRe["()->"] + functionRules["({args})->"], + functionRules["({})->"], + functionRules["(args)->"], + functionRules["()->"] , { token : "identifier", regex : "(?:(?:\\.|::)\\s*)" + identifier diff --git a/lib/ace/mode/coffee_highlight_rules_test.js b/lib/ace/mode/coffee_highlight_rules_test.js index d3914f02..4c7f35eb 100644 --- a/lib/ace/mode/coffee_highlight_rules_test.js +++ b/lib/ace/mode/coffee_highlight_rules_test.js @@ -42,6 +42,7 @@ module.exports = { setUp : function() { this.tokenizer = new Mode().getTokenizer(); this.testTokens = function(tokens, correct) { + assert.equal(tokens.length, correct.length); correct.forEach(function(type, i) { assert.equal(tokens[i].type, type); }); @@ -60,16 +61,21 @@ module.exports = { "entity.name.function", "text", "keyword.operator", "text", "paren.lparen", "paren.lparen", "variable.parameter", "paren.rparen", "paren.rparen", "text", "storage.type" ]; - - assert.equal(tokens.length, 11); this.testTokens(tokens, correct); - tokens = this.tokenizer.getLineTokens("foo = ({arg1, arg2}) ->", "start").tokens; - assert.equal(tokens.length, 11); + tokens = this.tokenizer.getLineTokens("foo = ({a1, a2}) ->", "start").tokens; this.testTokens(tokens, correct); - tokens = this.tokenizer.getLineTokens("foo : ({arg1, arg2}) ->", "start").tokens; - assert.equal(tokens.length, 11); + tokens = this.tokenizer.getLineTokens("foo = ({@a1, a2}) ->", "start").tokens; + 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", "punctuation.operator", "text", + "paren.lparen", "paren.lparen", "variable.parameter", "paren.rparen", "paren.rparen", "text", "storage.type" + ]; this.testTokens(tokens, correct); }, @@ -105,12 +111,6 @@ module.exports = { "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; @@ -122,47 +122,35 @@ module.exports = { this.testTokens(tokens, correct); }, + "test: tokenize function: 'foo : ({}) ->'": function() { + var tokens = this.tokenizer.getLineTokens("foo : ({}) ->", "start").tokens; + var correct = [ + "entity.name.function", "text", "punctuation.operator", "text", + "paren.lparen", "paren.lparen", "paren.rparen", "paren.rparen", "text", "storage.type" + ]; + 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" ]; - 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); + + tokens = this.tokenizer.getLineTokens("foo = (@arg1 = /abc/, arg2 = 'name') ->", "start").tokens; 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; + var tokens = this.tokenizer.getLineTokens("foo=(/args) ->", "start").tokens; assert.notEqual(tokens[0].type, "entity.name.function"); }, @@ -172,16 +160,29 @@ module.exports = { "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; + 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: 'foo : () ->'": function() { + var tokens = this.tokenizer.getLineTokens("foo : () ->", "start").tokens; + var correct = [ + "entity.name.function", "text", "punctuation.operator", "text", + "paren.lparen", "paren.rparen", "text", "storage.type" + ]; + this.testTokens(tokens, correct); + + tokens = this.tokenizer.getLineTokens("foo : ( ) ->", "start").tokens; + var correct = [ + "entity.name.function", "text", "punctuation.operator", "text", + "paren.lparen", "text", "paren.rparen", "text", "storage.type" + ]; this.testTokens(tokens, correct); }, @@ -191,12 +192,6 @@ module.exports = { "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); }, @@ -205,12 +200,14 @@ module.exports = { var correct = [ "entity.name.function", "text", "keyword.operator", "text", "storage.type" ]; - - assert.equal(tokens.length, 5); this.testTokens(tokens, correct); + }, - this.tokenizer.getLineTokens("foo : ->", "start").tokens; - assert.equal(tokens.length, 5); + "test: tokenize function: 'foo : ->'": function() { + var tokens = this.tokenizer.getLineTokens("foo : ->", "start").tokens; + var correct = [ + "entity.name.function", "text", "punctuation.operator", "text", "storage.type" + ]; this.testTokens(tokens, correct); }, @@ -220,8 +217,6 @@ module.exports = { "identifier", "text", "identifier", "punctuation.operator", "text", "constant.numeric", "punctuation.operator", "text", "paren.lparen", "variable.parameter", "paren.rparen", "text", "storage.type" ]; - - assert.equal(tokens.length, 13); this.testTokens(tokens, correct); }, @@ -230,8 +225,6 @@ module.exports = { var correct = [ "keyword", "text", "language.support.class" ]; - - assert.equal(tokens.length, 3); this.testTokens(tokens, correct); }, @@ -240,8 +233,6 @@ module.exports = { var correct = [ "keyword", "text", "language.support.class", "text", "keyword", "text", "language.support.class" ]; - - assert.equal(tokens.length, 7); this.testTokens(tokens, correct); }, @@ -250,8 +241,6 @@ module.exports = { var correct = [ "identifier", "punctuation.operator", "identifier", "punctuation.operator", "identifier" ]; - - assert.equal(tokens.length, 5); this.testTokens(tokens, correct); },