Merge pull request #725 from ajaxorg/highlighting/js

fix js_highlight_rules
This commit is contained in:
Fabian Jakobs 2012-04-16 02:07:17 -07:00
commit 08b934ec60
13 changed files with 142 additions and 98 deletions

View file

@ -72,7 +72,7 @@ var c_cppHighlightRules = function() {
token : "comment", token : "comment",
regex : "\\/\\/.*$" regex : "\\/\\/.*$"
}, },
new DocCommentHighlightRules().getStartRule("doc-start"), DocCommentHighlightRules.getStartRule("doc-start"),
{ {
token : "comment", // multi line comment token : "comment", // multi line comment
merge : true, merge : true,
@ -171,7 +171,7 @@ var c_cppHighlightRules = function() {
}; };
this.embedRules(DocCommentHighlightRules, "doc-", this.embedRules(DocCommentHighlightRules, "doc-",
[ new DocCommentHighlightRules().getEndRule("start") ]); [ DocCommentHighlightRules.getEndRule("start") ]);
}; };
oop.inherits(c_cppHighlightRules, TextHighlightRules); oop.inherits(c_cppHighlightRules, TextHighlightRules);

View file

@ -69,9 +69,7 @@ var DocCommentHighlightRules = function() {
oop.inherits(DocCommentHighlightRules, TextHighlightRules); oop.inherits(DocCommentHighlightRules, TextHighlightRules);
(function() { DocCommentHighlightRules.getStartRule = function(start) {
this.getStartRule = function(start) {
return { return {
token : "comment.doc", // doc comment token : "comment.doc", // doc comment
merge : true, merge : true,
@ -80,7 +78,7 @@ oop.inherits(DocCommentHighlightRules, TextHighlightRules);
}; };
}; };
this.getEndRule = function (start) { DocCommentHighlightRules.getEndRule = function (start) {
return { return {
token : "comment.doc", // closing comment token : "comment.doc", // closing comment
merge : true, merge : true,
@ -89,7 +87,6 @@ oop.inherits(DocCommentHighlightRules, TextHighlightRules);
}; };
}; };
}).call(DocCommentHighlightRules.prototype);
exports.DocCommentHighlightRules = DocCommentHighlightRules; exports.DocCommentHighlightRules = DocCommentHighlightRules;

View file

@ -64,7 +64,7 @@ var GroovyHighlightRules = function() {
token : "comment", token : "comment",
regex : "\\/\\/.*$" regex : "\\/\\/.*$"
}, },
new DocCommentHighlightRules().getStartRule("doc-start"), DocCommentHighlightRules.getStartRule("doc-start"),
{ {
token : "comment", // multi line comment token : "comment", // multi line comment
merge : true, merge : true,
@ -134,7 +134,7 @@ var GroovyHighlightRules = function() {
}; };
this.embedRules(DocCommentHighlightRules, "doc-", this.embedRules(DocCommentHighlightRules, "doc-",
[ new DocCommentHighlightRules().getEndRule("start") ]); [ DocCommentHighlightRules.getEndRule("start") ]);
}; };
oop.inherits(GroovyHighlightRules, TextHighlightRules); oop.inherits(GroovyHighlightRules, TextHighlightRules);

View file

@ -26,7 +26,7 @@ var HaxeHighlightRules = function() {
token : "comment", token : "comment",
regex : "\\/\\/.*$" regex : "\\/\\/.*$"
}, },
new DocCommentHighlightRules().getStartRule("doc-start"), DocCommentHighlightRules.getStartRule("doc-start"),
{ {
token : "comment", // multi line comment token : "comment", // multi line comment
regex : "\\/\\*", regex : "\\/\\*",
@ -95,7 +95,7 @@ var HaxeHighlightRules = function() {
}; };
this.embedRules(DocCommentHighlightRules, "doc-", this.embedRules(DocCommentHighlightRules, "doc-",
[ new DocCommentHighlightRules().getEndRule("start") ]); [ DocCommentHighlightRules.getEndRule("start") ]);
}; };
oop.inherits(HaxeHighlightRules, TextHighlightRules); oop.inherits(HaxeHighlightRules, TextHighlightRules);

View file

@ -65,7 +65,7 @@ var JavaHighlightRules = function() {
token : "comment", token : "comment",
regex : "\\/\\/.*$" regex : "\\/\\/.*$"
}, },
new DocCommentHighlightRules().getStartRule("doc-start"), DocCommentHighlightRules.getStartRule("doc-start"),
{ {
token : "comment", // multi line comment token : "comment", // multi line comment
merge : true, merge : true,
@ -135,7 +135,7 @@ var JavaHighlightRules = function() {
}; };
this.embedRules(DocCommentHighlightRules, "doc-", this.embedRules(DocCommentHighlightRules, "doc-",
[ new DocCommentHighlightRules().getEndRule("start") ]); [ DocCommentHighlightRules.getEndRule("start") ]);
}; };
oop.inherits(JavaHighlightRules, TextHighlightRules); oop.inherits(JavaHighlightRules, TextHighlightRules);

View file

@ -116,7 +116,7 @@ var JavaScriptHighlightRules = function() {
token : "comment", token : "comment",
regex : /\/\/.*$/ regex : /\/\/.*$/
}, },
new DocCommentHighlightRules().getStartRule("doc-start"), DocCommentHighlightRules.getStartRule("doc-start"),
{ {
token : "comment", // multi line comment token : "comment", // multi line comment
merge : true, merge : true,
@ -124,11 +124,11 @@ var JavaScriptHighlightRules = function() {
next : "comment" next : "comment"
}, { }, {
token : "string", token : "string",
regex : "'", regex : "'(?=.)",
next : "qstring" next : "qstring"
}, { }, {
token : "string", token : "string",
regex : '"', regex : '"(?=.)',
next : "qqstring" next : "qqstring"
}, { }, {
token : "constant.numeric", // hex token : "constant.numeric", // hex
@ -148,11 +148,10 @@ var JavaScriptHighlightRules = function() {
"text", "text",
"storage.type", "storage.type",
"text", "text",
"paren.lparen", "paren.lparen"
"variable.parameter",
"paren.rparen"
], ],
regex : "(" + identifierRe + ")(\\.)(prototype)(\\.)(" + identifierRe +")(\\s*)(=)(\\s*)(function)(\\s*)(\\()(.*?)(\\))" regex : "(" + identifierRe + ")(\\.)(prototype)(\\.)(" + identifierRe +")(\\s*)(=)(\\s*)(function)(\\s*)(\\()",
next: "function_arguments"
}, { // match stuff like: Sound.prototype.play = myfunc }, { // match stuff like: Sound.prototype.play = myfunc
token : [ token : [
"storage.type", "storage.type",
@ -164,7 +163,8 @@ var JavaScriptHighlightRules = function() {
"keyword.operator", "keyword.operator",
"text" "text"
], ],
regex : "(" + identifierRe + ")(\\.)(prototype)(\\.)(" + identifierRe +")(\\s*)(=)(\\s*)" regex : "(" + identifierRe + ")(\\.)(prototype)(\\.)(" + identifierRe +")(\\s*)(=)(\\s*)",
next: "function_arguments"
}, { // match stuff like: Sound.play = function() { } }, { // match stuff like: Sound.play = function() { }
token : [ token : [
"storage.type", "storage.type",
@ -175,11 +175,10 @@ var JavaScriptHighlightRules = function() {
"text", "text",
"storage.type", "storage.type",
"text", "text",
"paren.lparen", "paren.lparen"
"variable.parameter",
"paren.rparen"
], ],
regex : "(" + identifierRe + ")(\\.)(" + identifierRe +")(\\s*)(=)(\\s*)(function)(\\s*)(\\()(.*?)(\\))" regex : "(" + identifierRe + ")(\\.)(" + identifierRe +")(\\s*)(=)(\\s*)(function)(\\s*)(\\()",
next: "function_arguments"
}, { // match stuff like: play = function() { } }, { // match stuff like: play = function() { }
token : [ token : [
"entity.name.function", "entity.name.function",
@ -188,22 +187,20 @@ var JavaScriptHighlightRules = function() {
"text", "text",
"storage.type", "storage.type",
"text", "text",
"paren.lparen", "paren.lparen"
"variable.parameter",
"paren.rparen"
], ],
regex : "(" + identifierRe +")(\\s*)(=)(\\s*)(function)(\\s*)(\\()(.*?)(\\))" regex : "(" + identifierRe +")(\\s*)(=)(\\s*)(function)(\\s*)(\\()",
next: "function_arguments"
}, { // match regular function like: function myFunc(arg) { } }, { // match regular function like: function myFunc(arg) { }
token : [ token : [
"storage.type", "storage.type",
"text", "text",
"entity.name.function", "entity.name.function",
"text", "text",
"paren.lparen", "paren.lparen"
"variable.parameter",
"paren.rparen"
], ],
regex : "(function)(\\s+)(" + identifierRe + ")(\\s*)(\\()(.*?)(\\))" regex : "(function)(\\s+)(" + identifierRe + ")(\\s*)(\\()",
next: "function_arguments"
}, { // match stuff like: foobar: function() { } }, { // match stuff like: foobar: function() { }
token : [ token : [
"entity.name.function", "entity.name.function",
@ -212,22 +209,20 @@ var JavaScriptHighlightRules = function() {
"text", "text",
"storage.type", "storage.type",
"text", "text",
"paren.lparen", "paren.lparen"
"variable.parameter",
"paren.rparen"
], ],
regex : "(" + identifierRe + ")(\\s*)(:)(\\s*)(function)(\\s*)(\\()(.*?)(\\))" regex : "(" + identifierRe + ")(\\s*)(:)(\\s*)(function)(\\s*)(\\()",
next: "function_arguments"
}, { // Attempt to match : function() { } (this is for issues with 'foo': function() { }) }, { // Attempt to match : function() { } (this is for issues with 'foo': function() { })
token : [ token : [
"text", "text",
"text", "text",
"storage.type", "storage.type",
"text", "text",
"paren.lparen", "paren.lparen"
"variable.parameter",
"paren.rparen"
], ],
regex : "(:)(\\s*)(function)?(\\s*)(\\()([^)]*)(\\))" regex : "(:)(\\s*)(function)(\\s*)(\\()",
next: "function_arguments"
}, { }, {
token : "constant.language.boolean", token : "constant.language.boolean",
regex : /(?:true|false)\b/ regex : /(?:true|false)\b/
@ -297,6 +292,7 @@ var JavaScriptHighlightRules = function() {
// regular expressions are only allowed after certain tokens. This // regular expressions are only allowed after certain tokens. This
// makes sure we don't mix up regexps with the divison operator // makes sure we don't mix up regexps with the divison operator
"regex_allowed": [ "regex_allowed": [
DocCommentHighlightRules.getStartRule("doc-start"),
{ {
token : "comment", // multi line comment token : "comment", // multi line comment
merge : true, merge : true,
@ -324,8 +320,7 @@ var JavaScriptHighlightRules = function() {
"regex": [ "regex": [
{ {
token: "regexp.keyword.operator", token: "regexp.keyword.operator",
regex: "\\\\(?:u[\\da-fA-F]{4}|x[\\da-fA-F]{2}|.)", regex: "\\\\(?:u[\\da-fA-F]{4}|x[\\da-fA-F]{2}|.)"
next: "regex"
}, { }, {
// flag // flag
token: "string.regexp", token: "string.regexp",
@ -335,7 +330,6 @@ var JavaScriptHighlightRules = function() {
}, { }, {
token: "string.regexp", token: "string.regexp",
regex: "[^\\\\/\\[]+", regex: "[^\\\\/\\[]+",
next: "regex",
merge: true merge: true
}, { }, {
token: "string.regexp.charachterclass", token: "string.regexp.charachterclass",
@ -351,8 +345,7 @@ var JavaScriptHighlightRules = function() {
"regex_character_class": [ "regex_character_class": [
{ {
token: "regexp.keyword.operator", token: "regexp.keyword.operator",
regex: "\\\\(?:u[\\da-fA-F]{4}|x[\\da-fA-F]{2}|.)", regex: "\\\\(?:u[\\da-fA-F]{4}|x[\\da-fA-F]{2}|.)"
next: "regex_character_class"
}, { }, {
token: "string.regexp.charachterclass", token: "string.regexp.charachterclass",
regex: "]", regex: "]",
@ -361,7 +354,24 @@ var JavaScriptHighlightRules = function() {
}, { }, {
token: "string.regexp.charachterclass", token: "string.regexp.charachterclass",
regex: "[^\\\\\\]]+", regex: "[^\\\\\\]]+",
next: "regex_character_class", merge: true
}, {
token: "empty",
regex: "",
next: "start"
}
],
"function_arguments": [
{
token: "variable.parameter",
regex: identifierRe,
}, {
token: "punctuation.operator",
regex: "[, ]+",
merge: true
}, {
token: "punctuation.operator",
regex: "$",
merge: true merge: true
}, { }, {
token: "empty", token: "empty",
@ -399,11 +409,18 @@ var JavaScriptHighlightRules = function() {
regex : escapedRe regex : escapedRe
}, { }, {
token : "string", token : "string",
regex : '[^"\\\\]+' regex : '[^"\\\\]+',
merge : true
}, { }, {
token : "string", token : "string",
regex : '"', regex : "\\\\$",
next : "start" next : "qqstring",
merge : true
}, {
token : "string",
regex : '"|$',
next : "start",
merge : true
} }
], ],
"qstring" : [ "qstring" : [
@ -412,17 +429,24 @@ var JavaScriptHighlightRules = function() {
regex : escapedRe regex : escapedRe
}, { }, {
token : "string", token : "string",
regex : "[^'\\\\]+" regex : "[^'\\\\]+",
merge : true
}, { }, {
token : "string", token : "string",
regex : "'", regex : "\\\\$",
next : "start" next : "qstring",
merge : true
}, {
token : "string",
regex : "'|$",
next : "start",
merge : true
} }
] ]
}; };
this.embedRules(DocCommentHighlightRules, "doc-", this.embedRules(DocCommentHighlightRules, "doc-",
[ new DocCommentHighlightRules().getEndRule("start") ]); [ DocCommentHighlightRules.getEndRule("start") ]);
}; };
oop.inherits(JavaScriptHighlightRules, TextHighlightRules); oop.inherits(JavaScriptHighlightRules, TextHighlightRules);

View file

@ -71,10 +71,10 @@ module.exports = {
var tokens = this.tokenizer.getLineTokens(line, "start").tokens; var tokens = this.tokenizer.getLineTokens(line, "start").tokens;
assert.equal(27, tokens.length); assert.equal(23, tokens.length);
assert.equal("support.function", tokens[2].type); // charCodeAt assert.equal("support.function", tokens[2].type); // charCodeAt
assert.equal("support.function.dom", tokens[10].type); // getElementById assert.equal("support.function.dom", tokens[10].type); // getElementById
assert.equal("support.function.firebug", tokens[20].type); // log assert.equal("support.function.firebug", tokens[18].type); // log
}, },
"test: tokenize doc comment" : function() { "test: tokenize doc comment" : function() {
@ -181,14 +181,40 @@ module.exports = {
var line = "console.log('Meh\\nNeh');" var line = "console.log('Meh\\nNeh');"
var tokens = this.tokenizer.getLineTokens(line, "start").tokens; var tokens = this.tokenizer.getLineTokens(line, "start").tokens;
assert.equal(11, tokens.length); assert.equal(9, tokens.length);
assert.equal("constant.language.escape", tokens[6].type); assert.equal("constant.language.escape", tokens[5].type);
line = "console.log('\\u1232Feh');"; line = "console.log('\\u1232Feh');";
tokens = this.tokenizer.getLineTokens(line, "start").tokens; tokens = this.tokenizer.getLineTokens(line, "start").tokens;
assert.equal(10, tokens.length); assert.equal(9, tokens.length);
assert.equal("constant.language.escape", tokens[5].type); assert.equal("constant.language.escape", tokens[5].type);
},
"test multiline strings": function() {
var line = "console.log('Meh\\"
var data = this.tokenizer.getLineTokens(line, "start")
assert.equal(5, data.tokens.length);
assert.equal(data.state, "qstring");
line = "console.log('Meh\\ "
data = this.tokenizer.getLineTokens(line, "start")
assert.equal(6, data.tokens.length);
assert.equal(data.state, "start");
line = 'console.log("\\'
data = this.tokenizer.getLineTokens(line, "start")
assert.equal(5, data.tokens.length);
assert.equal(data.state, "qqstring");
line = 'a="'
data = this.tokenizer.getLineTokens(line, "start")
assert.equal(3, data.tokens.length);
assert.equal(data.state, "start");
} }
}; };

View file

@ -442,7 +442,7 @@ var PgsqlHighlightRules = function() {
token : "comment", token : "comment",
regex : "--.*$" regex : "--.*$"
}, },
new DocCommentHighlightRules().getStartRule("doc-start"), DocCommentHighlightRules.getStartRule("doc-start"),
{ {
token : "comment", // multi-line comment token : "comment", // multi-line comment
merge : true, merge : true,
@ -571,7 +571,7 @@ var PgsqlHighlightRules = function() {
] ]
}; };
this.embedRules(DocCommentHighlightRules, "doc-", [ new DocCommentHighlightRules().getEndRule("start") ]); this.embedRules(DocCommentHighlightRules, "doc-", [ DocCommentHighlightRules.getEndRule("start") ]);
this.embedRules(PerlHighlightRules, "perl-", [{token : "string", regex : "\\$perl\\$", next : "statement"}]); this.embedRules(PerlHighlightRules, "perl-", [{token : "string", regex : "\\$perl\\$", next : "statement"}]);
this.embedRules(PythonHighlightRules, "python-", [{token : "string", regex : "\\$python\\$", next : "statement"}]); this.embedRules(PythonHighlightRules, "python-", [{token : "string", regex : "\\$python\\$", next : "statement"}]);
}; };

View file

@ -45,7 +45,7 @@ var DocCommentHighlightRules = require("./doc_comment_highlight_rules").DocComme
var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules; var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
var PhpHighlightRules = function() { var PhpHighlightRules = function() {
var docComment = new DocCommentHighlightRules(); var docComment = DocCommentHighlightRules;
// http://php.net/quickref.php // http://php.net/quickref.php
var builtinFunctions = lang.arrayToMap( var builtinFunctions = lang.arrayToMap(
('abs|acos|acosh|addcslashes|addslashes|aggregate|aggregate_info|aggregate_methods|aggregate_methods_by_list|aggregate_methods_by_regexp|' + ('abs|acos|acosh|addcslashes|addslashes|aggregate|aggregate_info|aggregate_methods|aggregate_methods_by_list|aggregate_methods_by_regexp|' +
@ -1150,7 +1150,7 @@ var PhpHighlightRules = function() {
}; };
this.embedRules(DocCommentHighlightRules, "doc-", this.embedRules(DocCommentHighlightRules, "doc-",
[ new DocCommentHighlightRules().getEndRule("start") ]); [ DocCommentHighlightRules.getEndRule("start") ]);
}; };
oop.inherits(PhpHighlightRules, TextHighlightRules); oop.inherits(PhpHighlightRules, TextHighlightRules);

View file

@ -65,7 +65,7 @@ var scadHighlightRules = function() {
token : "comment", token : "comment",
regex : "\\/\\/.*$" regex : "\\/\\/.*$"
}, },
new DocCommentHighlightRules().getStartRule("start"), DocCommentHighlightRules.getStartRule("start"),
{ {
token : "comment", // multi line comment token : "comment", // multi line comment
merge : true, merge : true,
@ -159,7 +159,7 @@ var scadHighlightRules = function() {
}; };
this.embedRules(DocCommentHighlightRules, "doc-", this.embedRules(DocCommentHighlightRules, "doc-",
[ new DocCommentHighlightRules().getEndRule("start") ]); [ DocCommentHighlightRules.getEndRule("start") ]);
}; };
oop.inherits(scadHighlightRules, TextHighlightRules); oop.inherits(scadHighlightRules, TextHighlightRules);

View file

@ -65,7 +65,7 @@ var ScalaHighlightRules = function() {
token : "comment", token : "comment",
regex : "\\/\\/.*$" regex : "\\/\\/.*$"
}, },
new DocCommentHighlightRules().getStartRule("doc-start"), DocCommentHighlightRules.getStartRule("doc-start"),
{ {
token : "comment", // multi line comment token : "comment", // multi line comment
merge : true, merge : true,
@ -135,7 +135,7 @@ var ScalaHighlightRules = function() {
}; };
this.embedRules(DocCommentHighlightRules, "doc-", this.embedRules(DocCommentHighlightRules, "doc-",
[ new DocCommentHighlightRules().getEndRule("start") ]); [ DocCommentHighlightRules.getEndRule("start") ]);
}; };
oop.inherits(ScalaHighlightRules, TextHighlightRules); oop.inherits(ScalaHighlightRules, TextHighlightRules);

View file

@ -65,8 +65,6 @@ var TextHighlightRules = function() {
var rule = state[i]; var rule = state[i];
if (rule.next) { if (rule.next) {
rule.next = prefix + rule.next; rule.next = prefix + rule.next;
} else {
rule.next = prefix + key;
} }
} }
this.$rules[prefix + key] = state; this.$rules[prefix + key] = state;

View file

@ -119,9 +119,8 @@ var Tokenizer = function(rules, flag) {
else else
type = rule.token; type = rule.token;
var next = rule.next; if (rule.next) {
if (next && next !== currentState) { currentState = rule.next;
currentState = next;
state = this.rules[currentState]; state = this.rules[currentState];
mapping = this.matchMappings[currentState]; mapping = this.matchMappings[currentState];
lastIndex = re.lastIndex; lastIndex = re.lastIndex;