groovy.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://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 transient " +
  23. "try void volatile while");
  24. var blockKeywords = words("catch class do else finally for if switch try while enum interface def");
  25. var atoms = words("null true false this");
  26. var curPunc;
  27. function tokenBase(stream, state) {
  28. var ch = stream.next();
  29. if (ch == '"' || ch == "'") {
  30. return startString(ch, stream, state);
  31. }
  32. if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
  33. curPunc = ch;
  34. return null;
  35. }
  36. if (/\d/.test(ch)) {
  37. stream.eatWhile(/[\w\.]/);
  38. if (stream.eat(/eE/)) { stream.eat(/\+\-/); stream.eatWhile(/\d/); }
  39. return "number";
  40. }
  41. if (ch == "/") {
  42. if (stream.eat("*")) {
  43. state.tokenize.push(tokenComment);
  44. return tokenComment(stream, state);
  45. }
  46. if (stream.eat("/")) {
  47. stream.skipToEnd();
  48. return "comment";
  49. }
  50. if (expectExpression(state.lastToken)) {
  51. return startString(ch, stream, state);
  52. }
  53. }
  54. if (ch == "-" && stream.eat(">")) {
  55. curPunc = "->";
  56. return null;
  57. }
  58. if (/[+\-*&%=<>!?|\/~]/.test(ch)) {
  59. stream.eatWhile(/[+\-*&%=<>|~]/);
  60. return "operator";
  61. }
  62. stream.eatWhile(/[\w\$_]/);
  63. if (ch == "@") { stream.eatWhile(/[\w\$_\.]/); return "meta"; }
  64. if (state.lastToken == ".") return "property";
  65. if (stream.eat(":")) { curPunc = "proplabel"; return "property"; }
  66. var cur = stream.current();
  67. if (atoms.propertyIsEnumerable(cur)) { return "atom"; }
  68. if (keywords.propertyIsEnumerable(cur)) {
  69. if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement";
  70. return "keyword";
  71. }
  72. return "variable";
  73. }
  74. tokenBase.isBase = true;
  75. function startString(quote, stream, state) {
  76. var tripleQuoted = false;
  77. if (quote != "/" && stream.eat(quote)) {
  78. if (stream.eat(quote)) tripleQuoted = true;
  79. else return "string";
  80. }
  81. function t(stream, state) {
  82. var escaped = false, next, end = !tripleQuoted;
  83. while ((next = stream.next()) != null) {
  84. if (next == quote && !escaped) {
  85. if (!tripleQuoted) { break; }
  86. if (stream.match(quote + quote)) { end = true; break; }
  87. }
  88. if (quote == '"' && next == "$" && !escaped && stream.eat("{")) {
  89. state.tokenize.push(tokenBaseUntilBrace());
  90. return "string";
  91. }
  92. escaped = !escaped && next == "\\";
  93. }
  94. if (end) state.tokenize.pop();
  95. return "string";
  96. }
  97. state.tokenize.push(t);
  98. return t(stream, state);
  99. }
  100. function tokenBaseUntilBrace() {
  101. var depth = 1;
  102. function t(stream, state) {
  103. if (stream.peek() == "}") {
  104. depth--;
  105. if (depth == 0) {
  106. state.tokenize.pop();
  107. return state.tokenize[state.tokenize.length-1](stream, state);
  108. }
  109. } else if (stream.peek() == "{") {
  110. depth++;
  111. }
  112. return tokenBase(stream, state);
  113. }
  114. t.isBase = true;
  115. return t;
  116. }
  117. function tokenComment(stream, state) {
  118. var maybeEnd = false, ch;
  119. while (ch = stream.next()) {
  120. if (ch == "/" && maybeEnd) {
  121. state.tokenize.pop();
  122. break;
  123. }
  124. maybeEnd = (ch == "*");
  125. }
  126. return "comment";
  127. }
  128. function expectExpression(last) {
  129. return !last || last == "operator" || last == "->" || /[\.\[\{\(,;:]/.test(last) ||
  130. last == "newstatement" || last == "keyword" || last == "proplabel";
  131. }
  132. function Context(indented, column, type, align, prev) {
  133. this.indented = indented;
  134. this.column = column;
  135. this.type = type;
  136. this.align = align;
  137. this.prev = prev;
  138. }
  139. function pushContext(state, col, type) {
  140. return state.context = new Context(state.indented, col, type, null, state.context);
  141. }
  142. function popContext(state) {
  143. var t = state.context.type;
  144. if (t == ")" || t == "]" || t == "}")
  145. state.indented = state.context.indented;
  146. return state.context = state.context.prev;
  147. }
  148. // Interface
  149. return {
  150. startState: function(basecolumn) {
  151. return {
  152. tokenize: [tokenBase],
  153. context: new Context((basecolumn || 0) - config.indentUnit, 0, "top", false),
  154. indented: 0,
  155. startOfLine: true,
  156. lastToken: null
  157. };
  158. },
  159. token: function(stream, state) {
  160. var ctx = state.context;
  161. if (stream.sol()) {
  162. if (ctx.align == null) ctx.align = false;
  163. state.indented = stream.indentation();
  164. state.startOfLine = true;
  165. // Automatic semicolon insertion
  166. if (ctx.type == "statement" && !expectExpression(state.lastToken)) {
  167. popContext(state); ctx = state.context;
  168. }
  169. }
  170. if (stream.eatSpace()) return null;
  171. curPunc = null;
  172. var style = state.tokenize[state.tokenize.length-1](stream, state);
  173. if (style == "comment") return style;
  174. if (ctx.align == null) ctx.align = true;
  175. if ((curPunc == ";" || curPunc == ":") && ctx.type == "statement") popContext(state);
  176. // Handle indentation for {x -> \n ... }
  177. else if (curPunc == "->" && ctx.type == "statement" && ctx.prev.type == "}") {
  178. popContext(state);
  179. state.context.align = false;
  180. }
  181. else if (curPunc == "{") pushContext(state, stream.column(), "}");
  182. else if (curPunc == "[") pushContext(state, stream.column(), "]");
  183. else if (curPunc == "(") pushContext(state, stream.column(), ")");
  184. else if (curPunc == "}") {
  185. while (ctx.type == "statement") ctx = popContext(state);
  186. if (ctx.type == "}") ctx = popContext(state);
  187. while (ctx.type == "statement") ctx = popContext(state);
  188. }
  189. else if (curPunc == ctx.type) popContext(state);
  190. else if (ctx.type == "}" || ctx.type == "top" || (ctx.type == "statement" && curPunc == "newstatement"))
  191. pushContext(state, stream.column(), "statement");
  192. state.startOfLine = false;
  193. state.lastToken = curPunc || style;
  194. return style;
  195. },
  196. indent: function(state, textAfter) {
  197. if (!state.tokenize[state.tokenize.length-1].isBase) return 0;
  198. var firstChar = textAfter && textAfter.charAt(0), ctx = state.context;
  199. if (ctx.type == "statement" && !expectExpression(state.lastToken)) ctx = ctx.prev;
  200. var closing = firstChar == ctx.type;
  201. if (ctx.type == "statement") return ctx.indented + (firstChar == "{" ? 0 : config.indentUnit);
  202. else if (ctx.align) return ctx.column + (closing ? 0 : 1);
  203. else return ctx.indented + (closing ? 0 : config.indentUnit);
  204. },
  205. electricChars: "{}",
  206. fold: "brace"
  207. };
  208. });
  209. CodeMirror.defineMIME("text/x-groovy", "groovy");
  210. });