You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
8.2 KiB

7 months ago
  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) {
  127. state.tokenize.pop()
  128. return state.tokenize[state.tokenize.length-1](stream, state)
  129. }
  130. return next[0] == "." ? null : "variable"
  131. }
  132. function tokenComment(stream, state) {
  133. var maybeEnd = false, ch;
  134. while (ch = stream.next()) {
  135. if (ch == "/" && maybeEnd) {
  136. state.tokenize.pop();
  137. break;
  138. }
  139. maybeEnd = (ch == "*");
  140. }
  141. return "comment";
  142. }
  143. function expectExpression(last, newline) {
  144. return !last || last == "operator" || last == "->" || /[\.\[\{\(,;:]/.test(last) ||
  145. last == "newstatement" || last == "keyword" || last == "proplabel" ||
  146. (last == "standalone" && !newline);
  147. }
  148. function Context(indented, column, type, align, prev) {
  149. this.indented = indented;
  150. this.column = column;
  151. this.type = type;
  152. this.align = align;
  153. this.prev = prev;
  154. }
  155. function pushContext(state, col, type) {
  156. return state.context = new Context(state.indented, col, type, null, state.context);
  157. }
  158. function popContext(state) {
  159. var t = state.context.type;
  160. if (t == ")" || t == "]" || t == "}")
  161. state.indented = state.context.indented;
  162. return state.context = state.context.prev;
  163. }
  164. // Interface
  165. return {
  166. startState: function(basecolumn) {
  167. return {
  168. tokenize: [tokenBase],
  169. context: new Context((basecolumn || 0) - config.indentUnit, 0, "top", false),
  170. indented: 0,
  171. startOfLine: true,
  172. lastToken: null
  173. };
  174. },
  175. token: function(stream, state) {
  176. var ctx = state.context;
  177. if (stream.sol()) {
  178. if (ctx.align == null) ctx.align = false;
  179. state.indented = stream.indentation();
  180. state.startOfLine = true;
  181. // Automatic semicolon insertion
  182. if (ctx.type == "statement" && !expectExpression(state.lastToken, true)) {
  183. popContext(state); ctx = state.context;
  184. }
  185. }
  186. if (stream.eatSpace()) return null;
  187. curPunc = null;
  188. var style = state.tokenize[state.tokenize.length-1](stream, state);
  189. if (style == "comment") return style;
  190. if (ctx.align == null) ctx.align = true;
  191. if ((curPunc == ";" || curPunc == ":") && ctx.type == "statement") popContext(state);
  192. // Handle indentation for {x -> \n ... }
  193. else if (curPunc == "->" && ctx.type == "statement" && ctx.prev.type == "}") {
  194. popContext(state);
  195. state.context.align = false;
  196. }
  197. else if (curPunc == "{") pushContext(state, stream.column(), "}");
  198. else if (curPunc == "[") pushContext(state, stream.column(), "]");
  199. else if (curPunc == "(") pushContext(state, stream.column(), ")");
  200. else if (curPunc == "}") {
  201. while (ctx.type == "statement") ctx = popContext(state);
  202. if (ctx.type == "}") ctx = popContext(state);
  203. while (ctx.type == "statement") ctx = popContext(state);
  204. }
  205. else if (curPunc == ctx.type) popContext(state);
  206. else if (ctx.type == "}" || ctx.type == "top" || (ctx.type == "statement" && curPunc == "newstatement"))
  207. pushContext(state, stream.column(), "statement");
  208. state.startOfLine = false;
  209. state.lastToken = curPunc || style;
  210. return style;
  211. },
  212. indent: function(state, textAfter) {
  213. if (!state.tokenize[state.tokenize.length-1].isBase) return CodeMirror.Pass;
  214. var firstChar = textAfter && textAfter.charAt(0), ctx = state.context;
  215. if (ctx.type == "statement" && !expectExpression(state.lastToken, true)) ctx = ctx.prev;
  216. var closing = firstChar == ctx.type;
  217. if (ctx.type == "statement") return ctx.indented + (firstChar == "{" ? 0 : config.indentUnit);
  218. else if (ctx.align) return ctx.column + (closing ? 0 : 1);
  219. else return ctx.indented + (closing ? 0 : config.indentUnit);
  220. },
  221. electricChars: "{}",
  222. closeBrackets: {triples: "'\""},
  223. fold: "brace",
  224. blockCommentStart: "/*",
  225. blockCommentEnd: "*/",
  226. lineComment: "//"
  227. };
  228. });
  229. CodeMirror.defineMIME("text/x-groovy", "groovy");
  230. });