groovy.js 7.0 KB

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