simplify lua highlight rules

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

View file

@ -3,7 +3,7 @@
*
* Copyright (c) 2010, Ajax.org B.V.
* All rights reserved.
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
@ -14,7 +14,7 @@
* * Neither the name of Ajax.org B.V. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@ -86,8 +86,6 @@ var LuaHighlightRules = function() {
"variable.language": "this"
}, "identifier");
var strPre = "";
var decimalInteger = "(?:(?:[1-9]\\d*)|(?:0))";
var hexInteger = "(?:0[xX][\\dA-Fa-f]+)";
var integer = "(?:" + decimalInteger + "|" + hexInteger + ")";
@ -97,138 +95,73 @@ var LuaHighlightRules = function() {
var pointFloat = "(?:(?:" + intPart + "?" + fraction + ")|(?:" + intPart + "\\.))";
var floatNumber = "(?:" + pointFloat + ")";
var comment_stack = [];
this.$rules = {
"start" :
// bracketed comments
[{
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);
"start" : [{
stateName: "bracketedComment",
token : function(value, currentState, stack){
stack.unshift("bracketedComment", value.length);
return "comment";
},
regex : strPre + '\\-\\-\\[\\={5}\\=*\\[.*$',
next : "qcomment5"
regex : /\-\-\[=*\[/,
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",
regex : "\\-\\-.*$"
},
// bracketed strings
{
token : "string", // [[ string
regex : strPre + '\\[\\[.*\\]\\]'
}, {
token : "string", // [=[ string
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}\\=*\\]'
},
// multiline bracketed strings
{
token : "string", // [[ string
regex : strPre + '\\[\\[.*$',
next : "qstring"
}, {
token : "string", // [=[ string
regex : strPre + '\\[\\=\\[.*$',
next : "qstring1"
}, {
token : "string", // [==[ string
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";
stateName: "bracketedString",
token : function(value, currentState, stack){
stack.unshift("bracketedString", value.length);
return "comment";
},
regex : strPre + '\\[\\={5}\\=*\\[.*$',
next : "qstring5"
regex : /\[=*\[/,
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 : '.+'
}
]
},
{
token : "string", // " string
regex : strPre + '"(?:[^\\\\]|\\\\.)*?"'
regex : '"(?:[^\\\\]|\\\\.)*?"'
}, {
token : "string", // ' string
regex : strPre + "'(?:[^\\\\]|\\\\.)*?'"
regex : "'(?:[^\\\\]|\\\\.)*?'"
}, {
token : "constant.numeric", // float
regex : floatNumber
@ -249,135 +182,23 @@ var LuaHighlightRules = function() {
regex : "[\\]\\)\\}]"
}, {
token : "text",
regex : "\\s+"
} ],
"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 : '.+'
regex : "\\s+|\\w+"
} ]
};
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);