From 0918c7f1c01c3f4e6a5a3f634ef88818ed541bb1 Mon Sep 17 00:00:00 2001 From: nightwing Date: Fri, 24 Aug 2012 12:08:06 +0400 Subject: [PATCH 1/3] cleanup highlight_rules --- lib/ace/lib/lang.js | 8 ++++++++ lib/ace/mode/html_highlight_rules.js | 5 +++-- lib/ace/mode/json_highlight_rules.js | 2 +- lib/ace/mode/xml_util.js | 11 +++++------ 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/ace/lib/lang.js b/lib/ace/lib/lang.js index 3b862bdb..769bfdda 100644 --- a/lib/ace/lib/lang.js +++ b/lib/ace/lib/lang.js @@ -101,6 +101,14 @@ exports.arrayToMap = function(arr) { }; +exports.createMap = function(props) { + var map = Object.create(null); + for (var i in props) { + map[i] = props[i]; + } + return map; +}; + /* * splice out of 'array' anything that === 'value' */ diff --git a/lib/ace/mode/html_highlight_rules.js b/lib/ace/mode/html_highlight_rules.js index 4f51df77..96ec0149 100644 --- a/lib/ace/mode/html_highlight_rules.js +++ b/lib/ace/mode/html_highlight_rules.js @@ -39,12 +39,13 @@ define(function(require, exports, module) { "use strict"; var oop = require("../lib/oop"); +var lang = require("../lib/lang"); var CssHighlightRules = require("./css_highlight_rules").CssHighlightRules; var JavaScriptHighlightRules = require("./javascript_highlight_rules").JavaScriptHighlightRules; var xmlUtil = require("./xml_util"); var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules; -var tagMap = { +var tagMap = lang.createMap({ a : 'anchor', button : 'form', form : 'form', @@ -61,7 +62,7 @@ var tagMap = { tfoot : 'table', th : 'table', tr : 'table' -}; +}); var HtmlHighlightRules = function() { diff --git a/lib/ace/mode/json_highlight_rules.js b/lib/ace/mode/json_highlight_rules.js index 5a039869..e2ba3be8 100644 --- a/lib/ace/mode/json_highlight_rules.js +++ b/lib/ace/mode/json_highlight_rules.js @@ -84,7 +84,7 @@ var JsonHighlightRules = function() { "string" : [ { token : "constant.language.escape", - regex : /\\(?:x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4})/ + regex : /\\(?:x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4}|["\\\/bfnrt])/ }, { token : "string", regex : '[^"\\\\]+', diff --git a/lib/ace/mode/xml_util.js b/lib/ace/mode/xml_util.js index 6aaaeb94..994f5b2d 100644 --- a/lib/ace/mode/xml_util.js +++ b/lib/ace/mode/xml_util.js @@ -78,13 +78,12 @@ exports.tag = function(states, name, nextState, tagMap) { }, { //token : "meta.tag", - token : function(value) { - if (tagMap && tagMap[value]) { - return "meta.tag.tag-name" + '.' + tagMap[value]; - } else { + token : !tagMap ? "meta.tag.tag-name" : function(value) { + if (tagMap[value]) + return "meta.tag.tag-name." + tagMap[value]; + else return "meta.tag.tag-name"; - } - }, + }, merge : true, regex : "[-_a-zA-Z0-9:]+", next : name + "_embed_attribute_list" From e92ed07a1992a69fa5c2046373a173b89c451210 Mon Sep 17 00:00:00 2001 From: nightwing Date: Fri, 24 Aug 2012 09:55:12 +0400 Subject: [PATCH 2/3] simpler regexp for js identifiers non identifier chars in \u00a1-\uffff range are errors anyway so they can be highlighted either as operator or as identifier using this instead of detailed unicode regexps reduces parse time by ~3x, and gives the same result for any valid js --- lib/ace/mode/javascript_highlight_rules.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/ace/mode/javascript_highlight_rules.js b/lib/ace/mode/javascript_highlight_rules.js index ad3efba8..c45186c5 100644 --- a/lib/ace/mode/javascript_highlight_rules.js +++ b/lib/ace/mode/javascript_highlight_rules.js @@ -93,11 +93,7 @@ var JavaScriptHighlightRules = function() { ); // TODO: Unicode escape sequences - var identifierRe = "[" + unicode.packages.L + "\\$_][" - + unicode.packages.L - + unicode.packages.Mn + unicode.packages.Mc - + unicode.packages.Nd - + unicode.packages.Pc + "\\$_]*\\b"; + var identifierRe = "[a-zA-Z\\$_\u00a1-\uffff][a-zA-Z\d\\$_\u00a1-\uffff]*\\b"; var escapedRe = "\\\\(?:x[0-9a-fA-F]{2}|" + // hex "u[0-9a-fA-F]{4}|" + // unicode From 3945fe924a6284991dd3c9eacdaa59a1b30dc7b4 Mon Sep 17 00:00:00 2001 From: nightwing Date: Fri, 24 Aug 2012 10:31:00 +0400 Subject: [PATCH 3/3] add highlight rules for glsl --- demo/kitchen-sink/demo.js | 2 + demo/kitchen-sink/docs/glsl.glsl | 20 +++++ lib/ace/mode/glsl.js | 59 ++++++++++++++ lib/ace/mode/glsl_highlight_rules.js | 92 ++++++++++++++++++++++ lib/ace/mode/javascript_highlight_rules.js | 6 +- 5 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 demo/kitchen-sink/docs/glsl.glsl create mode 100644 lib/ace/mode/glsl.js create mode 100644 lib/ace/mode/glsl_highlight_rules.js diff --git a/demo/kitchen-sink/demo.js b/demo/kitchen-sink/demo.js index 7a2f2e76..2755b955 100644 --- a/demo/kitchen-sink/demo.js +++ b/demo/kitchen-sink/demo.js @@ -96,6 +96,7 @@ var modesByName = { csharp: ["C#" , "cs"], css: ["CSS" , "css"], diff: ["Diff" , "diff|patch"], + glsl: ["Glsl" , "glsl|frag|vert"], golang: ["Go" , "go"], groovy: ["Groovy" , "groovy"], haxe: ["haXe" , "hx"], @@ -177,6 +178,7 @@ var docs = { "docs/csharp.cs": "C#", "docs/css.css": "CSS", "docs/diff.diff": "Diff", + "docs/glsl.glsl": "Glsl", "docs/golang.go": "Go", "docs/groovy.groovy": "Groovy", "docs/Haxe.hx": "haXe", diff --git a/demo/kitchen-sink/docs/glsl.glsl b/demo/kitchen-sink/docs/glsl.glsl new file mode 100644 index 00000000..e32bf07d --- /dev/null +++ b/demo/kitchen-sink/docs/glsl.glsl @@ -0,0 +1,20 @@ +uniform float amplitude; +attribute float displacement; +varying vec3 vNormal; + +void main() { + + vNormal = normal; + + // multiply our displacement by the + // amplitude. The amp will get animated + // so we'll have animated displacement + vec3 newPosition = position + + normal * + vec3(displacement * + amplitude); + + gl_Position = projectionMatrix * + modelViewMatrix * + vec4(newPosition,1.0); +} \ No newline at end of file diff --git a/lib/ace/mode/glsl.js b/lib/ace/mode/glsl.js new file mode 100644 index 00000000..63dc643b --- /dev/null +++ b/lib/ace/mode/glsl.js @@ -0,0 +1,59 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Ajax.org Code Editor (ACE). + * + * The Initial Developer of the Original Code is + * Ajax.org B.V. + * Portions created by the Initial Developer are Copyright (C) 2010 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +define(function(require, exports, module) { +"use strict"; + +var oop = require("../lib/oop"); +var CMode = require("./c_cpp").Mode; +var Tokenizer = require("../tokenizer").Tokenizer; +var glslHighlightRules = require("./glsl_highlight_rules").glslHighlightRules; +var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent; +var Range = require("../range").Range; +var CstyleBehaviour = require("./behaviour/cstyle").CstyleBehaviour; +var CStyleFoldMode = require("./folding/cstyle").FoldMode; + +var Mode = function() { + this.$tokenizer = new Tokenizer(new glslHighlightRules().getRules()); + this.$outdent = new MatchingBraceOutdent(); + this.$behaviour = new CstyleBehaviour(); + this.foldingRules = new CStyleFoldMode(); +}; +oop.inherits(Mode, CMode); + +exports.Mode = Mode; +}); diff --git a/lib/ace/mode/glsl_highlight_rules.js b/lib/ace/mode/glsl_highlight_rules.js new file mode 100644 index 00000000..ec83ae02 --- /dev/null +++ b/lib/ace/mode/glsl_highlight_rules.js @@ -0,0 +1,92 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Ajax.org Code Editor (ACE). + * + * The Initial Developer of the Original Code is + * Ajax.org B.V. + * Portions created by the Initial Developer are Copyright (C) 2010 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +define(function(require, exports, module) { +"use strict"; + +var oop = require("../lib/oop"); +var lang = require("../lib/lang"); +var c_cppHighlightRules = require("./c_cpp_highlight_rules").c_cppHighlightRules; + +var glslHighlightRules = function() { + + var keywords = lang.arrayToMap( + ("attribute|const|uniform|varying|break|continue|do|for|while|" + + "if|else|in|out|inout|float|int|void|bool|true|false|" + + "lowp|mediump|highp|precision|invariant|discard|return|mat2|mat3|" + + "mat4|vec2|vec3|vec4|ivec2|ivec3|ivec4|bvec2|bvec3|bvec4|sampler2D|" + + "samplerCube|struct").split("|") + ); + + var buildinConstants = lang.arrayToMap( + ("radians|degrees|sin|cos|tan|asin|acos|atan|pow|" + + "exp|log|exp2|log2|sqrt|inversesqrt|abs|sign|floor|ceil|fract|mod|" + + "min|max|clamp|mix|step|smoothstep|length|distance|dot|cross|" + + "normalize|faceforward|reflect|refract|matrixCompMult|lessThan|" + + "lessThanEqual|greaterThan|greaterThanEqual|equal|notEqual|any|all|" + + "not|dFdx|dFdy|fwidth|texture2D|texture2DProj|texture2DLod|" + + "texture2DProjLod|textureCube|textureCubeLod|" + + "gl_MaxVertexAttribs|gl_MaxVertexUniformVectors|gl_MaxVaryingVectors|" + + "gl_MaxVertexTextureImageUnits|gl_MaxCombinedTextureImageUnits|" + + "gl_MaxTextureImageUnits|gl_MaxFragmentUniformVectors|gl_MaxDrawBuffers|" + + "gl_DepthRangeParameters|gl_DepthRange|" + + // The following two are only for MIME x-shader/x-vertex. + "gl_Position|gl_PointSize|" + + // The following five are only for MIME x-shader/x-fragment. + "gl_FragCoord|gl_FrontFacing|gl_PointCoord|gl_FragColor|gl_FragData").split("|") + ); + + this.$rules = new c_cppHighlightRules().$rules; + this.$rules.start.forEach(function(rule) { + if (typeof rule.token == "function") + rule.token = function(value) { + if (value == "this") + return "variable.language"; + else if (keywords.hasOwnProperty(value)) + return "keyword"; + else if (buildinConstants.hasOwnProperty(value)) + return "constant.language"; + else + return "identifier"; + }; + }) +}; + +oop.inherits(glslHighlightRules, c_cppHighlightRules); + +exports.glslHighlightRules = glslHighlightRules; +}); diff --git a/lib/ace/mode/javascript_highlight_rules.js b/lib/ace/mode/javascript_highlight_rules.js index c45186c5..ad3efba8 100644 --- a/lib/ace/mode/javascript_highlight_rules.js +++ b/lib/ace/mode/javascript_highlight_rules.js @@ -93,7 +93,11 @@ var JavaScriptHighlightRules = function() { ); // TODO: Unicode escape sequences - var identifierRe = "[a-zA-Z\\$_\u00a1-\uffff][a-zA-Z\d\\$_\u00a1-\uffff]*\\b"; + var identifierRe = "[" + unicode.packages.L + "\\$_][" + + unicode.packages.L + + unicode.packages.Mn + unicode.packages.Mc + + unicode.packages.Nd + + unicode.packages.Pc + "\\$_]*\\b"; var escapedRe = "\\\\(?:x[0-9a-fA-F]{2}|" + // hex "u[0-9a-fA-F]{4}|" + // unicode