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.

groovy.js 7.8KB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/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 && stream.eat("{")) {
  91. state.tokenize.push(tokenBaseUntilBrace());
  92. return "string";
  93. }
  94. escaped = !escaped && next == "\\";
  95. }
  96. if (end) state.tokenize.pop();
  97. return "string";
  98. }
  99. state.tokenize.push(t);
  100. return t(stream, state);
  101. }
  102. function tokenBaseUntilBrace() {
  103. var depth = 1;
  104. function t(stream, state) {
  105. if (stream.peek() == "}") {
  106. depth--;
  107. if (depth == 0) {
  108. state.tokenize.pop();
  109. return state.tokenize[state.tokenize.length-1](stream, state);
  110. }
  111. } else if (stream.peek() == "{") {
  112. depth++;
  113. }
  114. return tokenBase(stream, state);
  115. }
  116. t.isBase = true;
  117. return t;
  118. }
  119. function tokenComment(stream, state) {
  120. var maybeEnd = false, ch;
  121. while (ch = stream.next()) {
  122. if (ch == "/" && maybeEnd) {
  123. state.tokenize.pop();
  124. break;
  125. }
  126. maybeEnd = (ch == "*");
  127. }
  128. return "comment";
  129. }
  130. function expectExpression(last, newline) {
  131. return !last || last == "operator" || last == "->" || /[\.\[\{\(,;:]/.test(last) ||
  132. last == "newstatement" || last == "keyword" || last == "proplabel" ||
  133. (last == "standalone" && !newline);
  134. }
  135. function Context(indented, column, type, align, prev) {
  136. this.indented = indented;
  137. this.column = column;
  138. this.type = type;
  139. this.align = align;
  140. this.prev = prev;
  141. }
  142. function pushContext(state, col, type) {
  143. return state.context = new Context(state.indented, col, type, null, state.context);
  144. }
  145. function popContext(state) {
  146. var t = state.context.type;
  147. if (t == ")" || t == "]" || t == "}")
  148. state.indented = state.context.indented;
  149. return state.context = state.context.prev;
  150. }
  151. // Interface
  152. return {
  153. startState: function(basecolumn) {
  154. return {
  155. tokenize: [tokenBase],
  156. context: new Context((basecolumn || 0) - config.indentUnit, 0, "top", false),
  157. indented: 0,
  158. startOfLine: true,
  159. lastToken: null
  160. };
  161. },
  162. token: function(stream, state) {
  163. var ctx = state.context;
  164. if (stream.sol()) {
  165. if (ctx.align == null) ctx.align = false;
  166. state.indented = stream.indentation();
  167. state.startOfLine = true;
  168. // Automatic semicolon insertion
  169. if (ctx.type == "statement" && !expectExpression(state.lastToken, true)) {
  170. popContext(state); ctx = state.context;
  171. }
  172. }
  173. if (stream.eatSpace()) return null;
  174. curPunc = null;
  175. var style = state.tokenize[state.tokenize.length-1](stream, state);
  176. if (style == "comment") return style;
  177. if (ctx.align == null) ctx.align = true;
  178. if ((curPunc == ";" || curPunc == ":") && ctx.type == "statement") popContext(state);
  179. // Handle indentation for {x -> \n ... }
  180. else if (curPunc == "->" && ctx.type == "statement" && ctx.prev.type == "}") {
  181. popContext(state);
  182. state.context.align = false;
  183. }
  184. else if (curPunc == "{") pushContext(state, stream.column(), "}");
  185. else if (curPunc == "[") pushContext(state, stream.column(), "]");
  186. else if (curPunc == "(") pushContext(state, stream.column(), ")");
  187. else if (curPunc == "}") {
  188. while (ctx.type == "statement") ctx = popContext(state);
  189. if (ctx.type == "}") ctx = popContext(state);
  190. while (ctx.type == "statement") ctx = popContext(state);
  191. }
  192. else if (curPunc == ctx.type) popContext(state);
  193. else if (ctx.type == "}" || ctx.type == "top" || (ctx.type == "statement" && curPunc == "newstatement"))
  194. pushContext(state, stream.column(), "statement");
  195. state.startOfLine = false;
  196. state.lastToken = curPunc || style;
  197. return style;
  198. },
  199. indent: function(state, textAfter) {
  200. if (!state.tokenize[state.tokenize.length-1].isBase) return CodeMirror.Pass;
  201. var firstChar = textAfter && textAfter.charAt(0), ctx = state.context;
  202. if (ctx.type == "statement" && !expectExpression(state.lastToken, true)) ctx = ctx.prev;
  203. var closing = firstChar == ctx.type;
  204. if (ctx.type == "statement") return ctx.indented + (firstChar == "{" ? 0 : config.indentUnit);
  205. else if (ctx.align) return ctx.column + (closing ? 0 : 1);
  206. else return ctx.indented + (closing ? 0 : config.indentUnit);
  207. },
  208. electricChars: "{}",
  209. closeBrackets: {triples: "'\""},
  210. fold: "brace",
  211. blockCommentStart: "/*",
  212. blockCommentEnd: "*/",
  213. lineComment: "//"
  214. };
  215. });
  216. CodeMirror.defineMIME("text/x-groovy", "groovy");
  217. });