61c51736a06694cae4cc17fc5e6dc8611fbef8c0da9c22bb2756b88c3aa1ee37b3adecc32d97e4a72f79ed7c65c8ef88d0061ed6b56fe0cd6a504f000936ba 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/5/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. CodeMirror.defineMode("groovy", function(config) {
  13. function words(str) {
  14. var obj = {}, words = str.split(" ");
  15. for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
  16. return obj;
  17. }
  18. var keywords = words(
  19. "abstract as assert boolean break byte case catch char class const continue def default " +
  20. "do double else enum extends final finally float for goto if implements import in " +
  21. "instanceof int interface long native new package private protected public return " +
  22. "short static strictfp super switch synchronized threadsafe throw throws trait transient " +
  23. "try void volatile while");
  24. var blockKeywords = words("catch class def do else enum finally for if interface switch trait try while");
  25. var standaloneKeywords = words("return break continue");
  26. var atoms = words("null true false this");
  27. var curPunc;
  28. function tokenBase(stream, state) {
  29. var ch = stream.next();
  30. if (ch == '"' || ch == "'") {
  31. return startString(ch, stream, state);
  32. }
  33. if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
  34. curPunc = ch;
  35. return null;
  36. }
  37. if (/\d/.test(ch)) {
  38. stream.eatWhile(/[\w\.]/);
  39. if (stream.eat(/eE/)) { stream.eat(/\+\-/); stream.eatWhile(/\d/); }
  40. return "number";
  41. }
  42. if (ch == "/") {
  43. if (stream.eat("*")) {
  44. state.tokenize.push(tokenComment);
  45. return tokenComment(stream, state);
  46. }
  47. if (stream.eat("/")) {
  48. stream.skipToEnd();
  49. return "comment";
  50. }
  51. if (expectExpression(state.lastToken, false)) {
  52. return startString(ch, stream, state);
  53. }
  54. }
  55. if (ch == "-" && stream.eat(">")) {
  56. curPunc = "->";
  57. return null;
  58. }
  59. if (/[+\-*&%=<>!?|\/~]/.test(ch)) {
  60. stream.eatWhile(/[+\-*&%=<>|~]/);
  61. return "operator";
  62. }
  63. stream.eatWhile(/[\w\$_]/);
  64. if (ch == "@") { stream.eatWhile(/[\w\$_\.]/); return "meta"; }
  65. if (state.lastToken == ".") return "property";
  66. if (stream.eat(":")) { curPunc = "proplabel"; return "property"; }
  67. var cur = stream.current();
  68. if (atoms.propertyIsEnumerable(cur)) { return "atom"; }
  69. if (keywords.propertyIsEnumerable(cur)) {
  70. if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement";
  71. else if (standaloneKeywords.propertyIsEnumerable(cur)) curPunc = "standalone";
  72. return "keyword";
  73. }
  74. return "variable";
  75. }
  76. tokenBase.isBase = true;
  77. function startString(quote, stream, state) {
  78. var tripleQuoted = false;
  79. if (quote != "/" && stream.eat(quote)) {
  80. if (stream.eat(quote)) tripleQuoted = true;
  81. else return "string";
  82. }
  83. function t(stream, state) {
  84. var escaped = false, next, end = !tripleQuoted;
  85. while ((next = stream.next()) != null) {
  86. if (next == quote && !escaped) {
  87. if (!tripleQuoted) { break; }
  88. if (stream.match(quote + quote)) { end = true; break; }
  89. }
  90. if (quote == '"' && next == "$" && !escaped) {
  91. if (stream.eat("{")) {
  92. state.tokenize.push(tokenBaseUntilBrace());
  93. return "string";
  94. } else if (stream.match(/^\w/, false)) {
  95. state.tokenize.push(tokenVariableDeref);
  96. return "string";
  97. }
  98. }
  99. escaped = !escaped && next == "\\";
  100. }
  101. if (end) state.tokenize.pop();
  102. return "string";
  103. }
  104. state.tokenize.push(t);
  105. return t(stream, state);
  106. }
  107. function tokenBaseUntilBrace() {
  108. var depth = 1;
  109. function t(stream, state) {
  110. if (stream.peek() == "}") {
  111. depth--;
  112. if (depth == 0) {
  113. state.tokenize.pop();
  114. return state.tokenize[state.tokenize.length-1](stream, state);
  115. }
  116. } else if (stream.peek() == "{") {
  117. depth++;
  118. }
  119. return tokenBase(stream, state);
  120. }
  121. t.isBase = true;
  122. return t;
  123. }
  124. function tokenVariableDeref(stream, state) {
  125. var next = stream.match(/^(\.|[\w\$_]+)/)
  126. if (!next || !stream.match(next[0] == "." ? /^[\w$_]/ : /^\./)) state.tokenize.pop()
  127. if (!next) return state.tokenize[state.tokenize.length-1](stream, state)
  128. return next[0] == "." ? null : "variable"
  129. }
  130. function tokenComment(stream, state) {
  131. var maybeEnd = false, ch;
  132. while (ch = stream.next()) {
  133. if (ch == "/" && maybeEnd) {
  134. state.tokenize.pop();
  135. break;
  136. }
  137. maybeEnd = (ch == "*");
  138. }
  139. return "comment";
  140. }
  141. function expectExpression(last, newline) {
  142. return !last || last == "operator" || last == "->" || /[\.\[\{\(,;:]/.test(last) ||
  143. last == "newstatement" || last == "keyword" || last == "proplabel" ||
  144. (last == "standalone" && !newline);
  145. }
  146. function Context(indented, column, type, align, prev) {
  147. this.indented = indented;
  148. this.column = column;
  149. this.type = type;
  150. this.align = align;
  151. this.prev = prev;
  152. }
  153. function pushContext(state, col, type) {
  154. return state.context = new Context(state.indented, col, type, null, state.context);
  155. }
  156. function popContext(state) {
  157. var t = state.context.type;
  158. if (t == ")" || t == "]" || t == "}")
  159. state.indented = state.context.indented;
  160. return state.context = state.context.prev;
  161. }
  162. // Interface
  163. return {
  164. startState: function(basecolumn) {
  165. return {
  166. tokenize: [tokenBase],
  167. context: new Context((basecolumn || 0) - config.indentUnit, 0, "top", false),
  168. indented: 0,
  169. startOfLine: true,
  170. lastToken: null
  171. };
  172. },
  173. token: function(stream, state) {
  174. var ctx = state.context;
  175. if (stream.sol()) {
  176. if (ctx.align == null) ctx.align = false;
  177. state.indented = stream.indentation();
  178. state.startOfLine = true;
  179. // Automatic semicolon insertion
  180. if (ctx.type == "statement" && !expectExpression(state.lastToken, true)) {
  181. popContext(state); ctx = state.context;
  182. }
  183. }
  184. if (stream.eatSpace()) return null;
  185. curPunc = null;
  186. var style = state.tokenize[state.tokenize.length-1](stream, state);
  187. if (style == "comment") return style;
  188. if (ctx.align == null) ctx.align = true;
  189. if ((curPunc == ";" || curPunc == ":") && ctx.type == "statement") popContext(state);
  190. // Handle indentation for {x -> \n ... }
  191. else if (curPunc == "->" && ctx.type == "statement" && ctx.prev.type == "}") {
  192. popContext(state);
  193. state.context.align = false;
  194. }
  195. else if (curPunc == "{") pushContext(state, stream.column(), "}");
  196. else if (curPunc == "[") pushContext(state, stream.column(), "]");
  197. else if (curPunc == "(") pushContext(state, stream.column(), ")");
  198. else if (curPunc == "}") {
  199. while (ctx.type == "statement") ctx = popContext(state);
  200. if (ctx.type == "}") ctx = popContext(state);
  201. while (ctx.type == "statement") ctx = popContext(state);
  202. }
  203. else if (curPunc == ctx.type) popContext(state);
  204. else if (ctx.type == "}" || ctx.type == "top" || (ctx.type == "statement" && curPunc == "newstatement"))
  205. pushContext(state, stream.column(), "statement");
  206. state.startOfLine = false;
  207. state.lastToken = curPunc || style;
  208. return style;
  209. },
  210. indent: function(state, textAfter) {
  211. if (!state.tokenize[state.tokenize.length-1].isBase) return CodeMirror.Pass;
  212. var firstChar = textAfter && textAfter.charAt(0), ctx = state.context;
  213. if (ctx.type == "statement" && !expectExpression(state.lastToken, true)) ctx = ctx.prev;
  214. var closing = firstChar == ctx.type;
  215. if (ctx.type == "statement") return ctx.indented + (firstChar == "{" ? 0 : config.indentUnit);
  216. else if (ctx.align) return ctx.column + (closing ? 0 : 1);
  217. else return ctx.indented + (closing ? 0 : config.indentUnit);
  218. },
  219. electricChars: "{}",
  220. closeBrackets: {triples: "'\""},
  221. fold: "brace",
  222. blockCommentStart: "/*",
  223. blockCommentEnd: "*/",
  224. lineComment: "//"
  225. };
  226. });
  227. CodeMirror.defineMIME("text/x-groovy", "groovy");
  228. });