groovy.js 7.3 KB

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