simplify lua highlight rules

This commit is contained in:
nightwing 2012-12-28 19:13:57 +04:00
commit 4cdc48cf63

View file

@ -86,8 +86,6 @@ var LuaHighlightRules = function() {
"variable.language": "this" "variable.language": "this"
}, "identifier"); }, "identifier");
var strPre = "";
var decimalInteger = "(?:(?:[1-9]\\d*)|(?:0))"; var decimalInteger = "(?:(?:[1-9]\\d*)|(?:0))";
var hexInteger = "(?:0[xX][\\dA-Fa-f]+)"; var hexInteger = "(?:0[xX][\\dA-Fa-f]+)";
var integer = "(?:" + decimalInteger + "|" + hexInteger + ")"; var integer = "(?:" + decimalInteger + "|" + hexInteger + ")";
@ -97,138 +95,73 @@ var LuaHighlightRules = function() {
var pointFloat = "(?:(?:" + intPart + "?" + fraction + ")|(?:" + intPart + "\\.))"; var pointFloat = "(?:(?:" + intPart + "?" + fraction + ")|(?:" + intPart + "\\.))";
var floatNumber = "(?:" + pointFloat + ")"; var floatNumber = "(?:" + pointFloat + ")";
var comment_stack = [];
this.$rules = { this.$rules = {
"start" : "start" : [{
stateName: "bracketedComment",
token : function(value, currentState, stack){
// bracketed comments stack.unshift("bracketedComment", value.length);
[{
token : "comment", // --[[ comment
regex : strPre + '\\-\\-\\[\\[.*\\]\\]'
}, {
token : "comment", // --[=[ comment
regex : strPre + '\\-\\-\\[\\=\\[.*\\]\\=\\]'
}, {
token : "comment", // --[==[ comment
regex : strPre + '\\-\\-\\[\\={2}\\[.*\\]\\={2}\\]'
}, {
token : "comment", // --[===[ comment
regex : strPre + '\\-\\-\\[\\={3}\\[.*\\]\\={3}\\]'
}, {
token : "comment", // --[====[ comment
regex : strPre + '\\-\\-\\[\\={4}\\[.*\\]\\={4}\\]'
}, {
token : "comment", // --[====+[ comment
regex : strPre + '\\-\\-\\[\\={5}\\=*\\[.*\\]\\={5}\\=*\\]'
},
// multiline bracketed comments
{
token : "comment", // --[[ comment
regex : strPre + '\\-\\-\\[\\[.*$',
next : "qcomment"
}, {
token : "comment", // --[=[ comment
regex : strPre + '\\-\\-\\[\\=\\[.*$',
next : "qcomment1"
}, {
token : "comment", // --[==[ comment
regex : strPre + '\\-\\-\\[\\={2}\\[.*$',
next : "qcomment2"
}, {
token : "comment", // --[===[ comment
regex : strPre + '\\-\\-\\[\\={3}\\[.*$',
next : "qcomment3"
}, {
token : "comment", // --[====[ comment
regex : strPre + '\\-\\-\\[\\={4}\\[.*$',
next : "qcomment4"
}, {
token : function(value){ // --[====+[ comment
// WARNING: EXTREMELY SLOW, but this is the only way to circumvent the
// limits imposed by the current automaton.
// I've never personally seen any practical code where 5 or more '='s are
// used for string or commenting, so this will rarely be invoked.
var pattern = /\-\-\[(\=+)\[/, match;
// you can never be too paranoid ;)
if ((match = pattern.exec(value)) != null && (match = match[1]) != undefined)
comment_stack.push(match.length);
return "comment"; return "comment";
}, },
regex : strPre + '\\-\\-\\[\\={5}\\=*\\[.*$', regex : /\-\-\[=*\[/,
next : "qcomment5" next : [
{
token : function(value, currentState, stack) {
if (value.length == stack[1]) {
stack.shift();
stack.shift();
this.next = "start";
} else {
this.next = "";
}
return "comment";
},
regex : /(?:[^\\]|\\.)*?\]=*\]/,
next : "start"
}, {
token : "comment",
regex : '.+'
}
]
}, },
// single line comments
{ {
token : "comment", token : "comment",
regex : "\\-\\-.*$" regex : "\\-\\-.*$"
}, },
// bracketed strings
{ {
token : "string", // [[ string stateName: "bracketedString",
regex : strPre + '\\[\\[.*\\]\\]' token : function(value, currentState, stack){
}, { stack.unshift("bracketedString", value.length);
token : "string", // [=[ string return "comment";
regex : strPre + '\\[\\=\\[.*\\]\\=\\]'
}, {
token : "string", // [==[ string
regex : strPre + '\\[\\={2}\\[.*\\]\\={2}\\]'
}, {
token : "string", // [===[ string
regex : strPre + '\\[\\={3}\\[.*\\]\\={3}\\]'
}, {
token : "string", // [====[ string
regex : strPre + '\\[\\={4}\\[.*\\]\\={4}\\]'
}, {
token : "string", // [====+[ string
regex : strPre + '\\[\\={5}\\=*\\[.*\\]\\={5}\\=*\\]'
}, },
regex : /\[=*\[/,
// multiline bracketed strings next : [
{ {
token : "string", // [[ string token : function(value, currentState, stack) {
regex : strPre + '\\[\\[.*$', if (value.length == stack[1]) {
next : "qstring" stack.shift();
}, { stack.shift();
token : "string", // [=[ string this.next = "start";
regex : strPre + '\\[\\=\\[.*$', } else {
next : "qstring1" this.next = "";
}, { }
token : "string", // [==[ string return "comment";
regex : strPre + '\\[\\={2}\\[.*$',
next : "qstring2"
}, {
token : "string", // [===[ string
regex : strPre + '\\[\\={3}\\[.*$',
next : "qstring3"
}, {
token : "string", // [====[ string
regex : strPre + '\\[\\={4}\\[.*$',
next : "qstring4"
}, {
token : function(value){ // --[====+[ string
// WARNING: EXTREMELY SLOW, see above.
var pattern = /\[(\=+)\[/, match;
if ((match = pattern.exec(value)) != null && (match = match[1]) != undefined)
comment_stack.push(match.length);
return "string";
},
regex : strPre + '\\[\\={5}\\=*\\[.*$',
next : "qstring5"
}, },
regex : /(?:[^\\]|\\.)*?\]=*\]/,
next : "start"
}, {
token : "comment",
regex : '.+'
}
]
},
{ {
token : "string", // " string token : "string", // " string
regex : strPre + '"(?:[^\\\\]|\\\\.)*?"' regex : '"(?:[^\\\\]|\\\\.)*?"'
}, { }, {
token : "string", // ' string token : "string", // ' string
regex : strPre + "'(?:[^\\\\]|\\\\.)*?'" regex : "'(?:[^\\\\]|\\\\.)*?'"
}, { }, {
token : "constant.numeric", // float token : "constant.numeric", // float
regex : floatNumber regex : floatNumber
@ -249,135 +182,23 @@ var LuaHighlightRules = function() {
regex : "[\\]\\)\\}]" regex : "[\\]\\)\\}]"
}, { }, {
token : "text", token : "text",
regex : "\\s+" regex : "\\s+|\\w+"
} ],
"qcomment": [ {
token : "comment",
regex : "(?:[^\\\\]|\\\\.)*?\\]\\]",
next : "start"
}, {
token : "comment",
regex : '.+'
} ],
"qcomment1": [ {
token : "comment",
regex : "(?:[^\\\\]|\\\\.)*?\\]\\=\\]",
next : "start"
}, {
token : "comment",
regex : '.+'
} ],
"qcomment2": [ {
token : "comment",
regex : "(?:[^\\\\]|\\\\.)*?\\]\\={2}\\]",
next : "start"
}, {
token : "comment",
regex : '.+'
} ],
"qcomment3": [ {
token : "comment",
regex : "(?:[^\\\\]|\\\\.)*?\\]\\={3}\\]",
next : "start"
}, {
token : "comment",
regex : '.+'
} ],
"qcomment4": [ {
token : "comment",
regex : "(?:[^\\\\]|\\\\.)*?\\]\\={4}\\]",
next : "start"
}, {
token : "comment",
regex : '.+'
} ],
"qcomment5": [ {
token : function(value){
// very hackish, mutates the qcomment5 field on the fly.
var pattern = /\](\=+)\]/, rule = this.rules.qcomment5[0], match;
rule.next = "start";
if ((match = pattern.exec(value)) != null && (match = match[1]) != undefined){
var found = match.length, expected;
if ((expected = comment_stack.pop()) != found){
comment_stack.push(expected);
rule.next = "qcomment5";
}
}
return "comment";
},
regex : "(?:[^\\\\]|\\\\.)*?\\]\\={5}\\=*\\]",
next : "start"
}, {
token : "comment",
regex : '.+'
} ],
"qstring": [ {
token : "string",
regex : "(?:[^\\\\]|\\\\.)*?\\]\\]",
next : "start"
}, {
token : "string",
regex : '.+'
} ],
"qstring1": [ {
token : "string",
regex : "(?:[^\\\\]|\\\\.)*?\\]\\=\\]",
next : "start"
}, {
token : "string",
regex : '.+'
} ],
"qstring2": [ {
token : "string",
regex : "(?:[^\\\\]|\\\\.)*?\\]\\={2}\\]",
next : "start"
}, {
token : "string",
regex : '.+'
} ],
"qstring3": [ {
token : "string",
regex : "(?:[^\\\\]|\\\\.)*?\\]\\={3}\\]",
next : "start"
}, {
token : "string",
regex : '.+'
} ],
"qstring4": [ {
token : "string",
regex : "(?:[^\\\\]|\\\\.)*?\\]\\={4}\\]",
next : "start"
}, {
token : "string",
regex : '.+'
} ],
"qstring5": [ {
token : function(value){
// very hackish, mutates the qstring5 field on the fly.
var pattern = /\](\=+)\]/, rule = this.rules.qstring5[0], match;
rule.next = "start";
if ((match = pattern.exec(value)) != null && (match = match[1]) != undefined){
var found = match.length, expected;
if ((expected = comment_stack.pop()) != found){
comment_stack.push(expected);
rule.next = "qstring5";
}
}
return "string";
},
regex : "(?:[^\\\\]|\\\\.)*?\\]\\={5}\\=*\\]",
next : "start"
}, {
token : "string",
regex : '.+'
} ] } ]
}; };
var id = 0;
for (var key in this.$rules) {
var rule = this.$rules[key];
for (var i = 0; i < rule.length; i++) {
var state = rule[i];
if (state.next && Array.isArray(state.next)) {
console.log(state.next)
var stateName = state.stateName || ("state" + id++);
this.$rules[stateName] = state.next;
state.next = stateName;
}
}
}
} }
oop.inherits(LuaHighlightRules, TextHighlightRules); oop.inherits(LuaHighlightRules, TextHighlightRules);