glsl.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Full source:
  2. //
  3. // https://github.com/hughsk/glsl-editor
  4. //
  5. // (C) Copyright Hugh Kennedy
  6. //
  7. // This software is released under the MIT license:
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a
  10. // copy of this software and associated documentation files (the "Software"),
  11. // to deal in the Software without restriction, including without limitation
  12. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. // and/or sell copies of the Software, and to permit persons to whom the
  14. // Software is furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  24. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEAL-
  25. // INGS IN THE SOFTWARE.
  26. // The original source code has been slightly modified for the purpose of
  27. // integration (tschw).
  28. (function(mod) {
  29. if (typeof exports == "object" && typeof module == "object") // CommonJS
  30. mod(require("../../lib/codemirror"));
  31. else if (typeof define == "function" && define.amd) // AMD
  32. define(["../../lib/codemirror"], mod);
  33. else // Plain browser env
  34. mod(CodeMirror);
  35. })(function(CodeMirror) {
  36. "use strict";
  37. CodeMirror.defineMode("glsl", function(config, parserConfig) {
  38. var indentUnit = config.indentUnit,
  39. keywords = parserConfig.keywords || words(glslKeywords),
  40. builtins = parserConfig.builtins || words(glslBuiltins),
  41. blockKeywords = parserConfig.blockKeywords || words("case do else for if switch while struct"),
  42. atoms = parserConfig.atoms || words("null"),
  43. hooks = parserConfig.hooks || {},
  44. multiLineStrings = parserConfig.multiLineStrings;
  45. var isOperatorChar = /[+\-*&%=<>!?|\/]/;
  46. var curPunc;
  47. function tokenBase(stream, state) {
  48. var ch = stream.next();
  49. if (hooks[ch]) {
  50. var result = hooks[ch](stream, state);
  51. if (result !== false) return result;
  52. }
  53. if (ch == '"' || ch == "'") {
  54. state.tokenize = tokenString(ch);
  55. return state.tokenize(stream, state);
  56. }
  57. if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
  58. curPunc = ch;
  59. return "bracket";
  60. }
  61. if (/\d/.test(ch)) {
  62. stream.eatWhile(/[\w\.]/);
  63. return "number";
  64. }
  65. if (ch == "/") {
  66. if (stream.eat("*")) {
  67. state.tokenize = tokenComment;
  68. return tokenComment(stream, state);
  69. }
  70. if (stream.eat("/")) {
  71. stream.skipToEnd();
  72. return "comment";
  73. }
  74. }
  75. if (ch == "#") {
  76. stream.eatWhile(/[\S]+/);
  77. stream.eatWhile(/[\s]+/);
  78. stream.eatWhile(/[\S]+/);
  79. stream.eatWhile(/[\s]+/);
  80. return "comment";
  81. }
  82. if (isOperatorChar.test(ch)) {
  83. stream.eatWhile(isOperatorChar);
  84. return "operator";
  85. }
  86. stream.eatWhile(/[\w\$_]/);
  87. var cur = stream.current();
  88. if (keywords.propertyIsEnumerable(cur)) {
  89. if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement";
  90. return "keyword";
  91. }
  92. if (builtins.propertyIsEnumerable(cur)) {
  93. return "builtin";
  94. }
  95. if (atoms.propertyIsEnumerable(cur)) return "atom";
  96. return "word";
  97. }
  98. function tokenString(quote) {
  99. return function(stream, state) {
  100. var escaped = false, next, end = false;
  101. while ((next = stream.next()) != null) {
  102. if (next == quote && !escaped) {end = true; break;}
  103. escaped = !escaped && next == "\\";
  104. }
  105. if (end || !(escaped || multiLineStrings))
  106. state.tokenize = tokenBase;
  107. return "string";
  108. };
  109. }
  110. function tokenComment(stream, state) {
  111. var maybeEnd = false, ch;
  112. while (ch = stream.next()) {
  113. if (ch == "/" && maybeEnd) {
  114. state.tokenize = tokenBase;
  115. break;
  116. }
  117. maybeEnd = (ch == "*");
  118. }
  119. return "comment";
  120. }
  121. function Context(indented, column, type, align, prev) {
  122. this.indented = indented;
  123. this.column = column;
  124. this.type = type;
  125. this.align = align;
  126. this.prev = prev;
  127. }
  128. function pushContext(state, col, type) {
  129. return state.context = new Context(state.indented, col, type, null, state.context);
  130. }
  131. function popContext(state) {
  132. var t = state.context.type;
  133. if (t == ")" || t == "]" || t == "}")
  134. state.indented = state.context.indented;
  135. return state.context = state.context.prev;
  136. }
  137. // Interface
  138. return {
  139. startState: function(basecolumn) {
  140. return {
  141. tokenize: null,
  142. context: new Context((basecolumn || 0) - indentUnit, 0, "top", false),
  143. indented: 0,
  144. startOfLine: true
  145. };
  146. },
  147. token: function(stream, state) {
  148. var ctx = state.context;
  149. if (stream.sol()) {
  150. if (ctx.align == null) ctx.align = false;
  151. state.indented = stream.indentation();
  152. state.startOfLine = true;
  153. }
  154. if (stream.eatSpace()) return null;
  155. curPunc = null;
  156. var style = (state.tokenize || tokenBase)(stream, state);
  157. if (style == "comment" || style == "meta") return style;
  158. if (ctx.align == null) ctx.align = true;
  159. if ((curPunc == ";" || curPunc == ":") && ctx.type == "statement") popContext(state);
  160. else if (curPunc == "{") pushContext(state, stream.column(), "}");
  161. else if (curPunc == "[") pushContext(state, stream.column(), "]");
  162. else if (curPunc == "(") pushContext(state, stream.column(), ")");
  163. else if (curPunc == "}") {
  164. while (ctx.type == "statement") ctx = popContext(state);
  165. if (ctx.type == "}") ctx = popContext(state);
  166. while (ctx.type == "statement") ctx = popContext(state);
  167. }
  168. else if (curPunc == ctx.type) popContext(state);
  169. else if (ctx.type == "}" || ctx.type == "top" || (ctx.type == "statement" && curPunc == "newstatement"))
  170. pushContext(state, stream.column(), "statement");
  171. state.startOfLine = false;
  172. return style;
  173. },
  174. indent: function(state, textAfter) {
  175. if (state.tokenize != tokenBase && state.tokenize != null) return 0;
  176. var firstChar = textAfter && textAfter.charAt(0), ctx = state.context, closing = firstChar == ctx.type;
  177. if (ctx.type == "statement") return ctx.indented + (firstChar == "{" ? 0 : indentUnit);
  178. else if (ctx.align) return ctx.column + (closing ? 0 : 1);
  179. else return ctx.indented + (closing ? 0 : indentUnit);
  180. },
  181. electricChars: "{}"
  182. };
  183. });
  184. function words(str) {
  185. var obj = {}, words = str.split(" ");
  186. for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
  187. return obj;
  188. }
  189. var glslKeywords = "attribute const uniform varying break continue " +
  190. "do for while if else in out inout float int void bool true false " +
  191. "lowp mediump highp precision invariant discard return mat2 mat3 " +
  192. "mat4 vec2 vec3 vec4 ivec2 ivec3 ivec4 bvec2 bvec3 bvec4 sampler2D " +
  193. "samplerCube struct gl_FragCoord gl_FragColor gl_Position";
  194. var glslBuiltins = "radians degrees sin cos tan asin acos atan pow " +
  195. "exp log exp2 log2 sqrt inversesqrt abs sign floor ceil fract mod " +
  196. "min max clamp mix step smoothstep length distance dot cross " +
  197. "normalize faceforward reflect refract matrixCompMult lessThan " +
  198. "lessThanEqual greaterThan greaterThanEqual equal notEqual any all " +
  199. "not dFdx dFdy fwidth texture2D texture2DProj texture2DLod " +
  200. "texture2DProjLod textureCube textureCubeLod require export";
  201. function cppHook(stream, state) {
  202. if (!state.startOfLine) return false;
  203. stream.skipToEnd();
  204. return "meta";
  205. }
  206. ;(function() {
  207. CodeMirror.defineMIME("text/x-glsl", {
  208. name: "glsl",
  209. keywords: words(glslKeywords),
  210. builtins: words(glslBuiltins),
  211. blockKeywords: words("case do else for if switch while struct"),
  212. atoms: words("null"),
  213. hooks: {"#": cppHook}
  214. });
  215. }());
  216. });