1
0

kotlin.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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("kotlin", function (config, parserConfig) {
  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 multiLineStrings = parserConfig.multiLineStrings;
  19. var keywords = words(
  20. "package continue return object while break class data trait throw super" +
  21. " when type this else This try val var fun for is in if do as true false null get set");
  22. var softKeywords = words("import" +
  23. " where by get set abstract enum open annotation override private public internal" +
  24. " protected catch out vararg inline finally final ref");
  25. var blockKeywords = words("catch class do else finally for if where try while enum");
  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. // Wildcard import w/o trailing semicolon (import smth.*)
  34. if (ch == "." && stream.eat("*")) {
  35. return "word";
  36. }
  37. if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
  38. curPunc = ch;
  39. return null;
  40. }
  41. if (/\d/.test(ch)) {
  42. if (stream.eat(/eE/)) {
  43. stream.eat(/\+\-/);
  44. stream.eatWhile(/\d/);
  45. }
  46. return "number";
  47. }
  48. if (ch == "/") {
  49. if (stream.eat("*")) {
  50. state.tokenize.push(tokenComment);
  51. return tokenComment(stream, state);
  52. }
  53. if (stream.eat("/")) {
  54. stream.skipToEnd();
  55. return "comment";
  56. }
  57. if (expectExpression(state.lastToken)) {
  58. return startString(ch, stream, state);
  59. }
  60. }
  61. // Commented
  62. if (ch == "-" && stream.eat(">")) {
  63. curPunc = "->";
  64. return null;
  65. }
  66. if (/[\-+*&%=<>!?|\/~]/.test(ch)) {
  67. stream.eatWhile(/[\-+*&%=<>|~]/);
  68. return "operator";
  69. }
  70. stream.eatWhile(/[\w\$_]/);
  71. var cur = stream.current();
  72. if (atoms.propertyIsEnumerable(cur)) {
  73. return "atom";
  74. }
  75. if (softKeywords.propertyIsEnumerable(cur)) {
  76. if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement";
  77. return "softKeyword";
  78. }
  79. if (keywords.propertyIsEnumerable(cur)) {
  80. if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement";
  81. return "keyword";
  82. }
  83. return "word";
  84. }
  85. tokenBase.isBase = true;
  86. function startString(quote, stream, state) {
  87. var tripleQuoted = false;
  88. if (quote != "/" && stream.eat(quote)) {
  89. if (stream.eat(quote)) tripleQuoted = true;
  90. else return "string";
  91. }
  92. function t(stream, state) {
  93. var escaped = false, next, end = !tripleQuoted;
  94. while ((next = stream.next()) != null) {
  95. if (next == quote && !escaped) {
  96. if (!tripleQuoted) {
  97. break;
  98. }
  99. if (stream.match(quote + quote)) {
  100. end = true;
  101. break;
  102. }
  103. }
  104. if (quote == '"' && next == "$" && !escaped && stream.eat("{")) {
  105. state.tokenize.push(tokenBaseUntilBrace());
  106. return "string";
  107. }
  108. if (next == "$" && !escaped && !stream.eat(" ")) {
  109. state.tokenize.push(tokenBaseUntilSpace());
  110. return "string";
  111. }
  112. escaped = !escaped && next == "\\";
  113. }
  114. if (multiLineStrings)
  115. state.tokenize.push(t);
  116. if (end) state.tokenize.pop();
  117. return "string";
  118. }
  119. state.tokenize.push(t);
  120. return t(stream, state);
  121. }
  122. function tokenBaseUntilBrace() {
  123. var depth = 1;
  124. function t(stream, state) {
  125. if (stream.peek() == "}") {
  126. depth--;
  127. if (depth == 0) {
  128. state.tokenize.pop();
  129. return state.tokenize[state.tokenize.length - 1](stream, state);
  130. }
  131. } else if (stream.peek() == "{") {
  132. depth++;
  133. }
  134. return tokenBase(stream, state);
  135. }
  136. t.isBase = true;
  137. return t;
  138. }
  139. function tokenBaseUntilSpace() {
  140. function t(stream, state) {
  141. if (stream.eat(/[\w]/)) {
  142. var isWord = stream.eatWhile(/[\w]/);
  143. if (isWord) {
  144. state.tokenize.pop();
  145. return "word";
  146. }
  147. }
  148. state.tokenize.pop();
  149. return "string";
  150. }
  151. t.isBase = true;
  152. return t;
  153. }
  154. function tokenComment(stream, state) {
  155. var maybeEnd = false, ch;
  156. while (ch = stream.next()) {
  157. if (ch == "/" && maybeEnd) {
  158. state.tokenize.pop();
  159. break;
  160. }
  161. maybeEnd = (ch == "*");
  162. }
  163. return "comment";
  164. }
  165. function expectExpression(last) {
  166. return !last || last == "operator" || last == "->" || /[\.\[\{\(,;:]/.test(last) ||
  167. last == "newstatement" || last == "keyword" || last == "proplabel";
  168. }
  169. function Context(indented, column, type, align, prev) {
  170. this.indented = indented;
  171. this.column = column;
  172. this.type = type;
  173. this.align = align;
  174. this.prev = prev;
  175. }
  176. function pushContext(state, col, type) {
  177. return state.context = new Context(state.indented, col, type, null, state.context);
  178. }
  179. function popContext(state) {
  180. var t = state.context.type;
  181. if (t == ")" || t == "]" || t == "}")
  182. state.indented = state.context.indented;
  183. return state.context = state.context.prev;
  184. }
  185. // Interface
  186. return {
  187. startState: function (basecolumn) {
  188. return {
  189. tokenize: [tokenBase],
  190. context: new Context((basecolumn || 0) - config.indentUnit, 0, "top", false),
  191. indented: 0,
  192. startOfLine: true,
  193. lastToken: null
  194. };
  195. },
  196. token: function (stream, state) {
  197. var ctx = state.context;
  198. if (stream.sol()) {
  199. if (ctx.align == null) ctx.align = false;
  200. state.indented = stream.indentation();
  201. state.startOfLine = true;
  202. // Automatic semicolon insertion
  203. if (ctx.type == "statement" && !expectExpression(state.lastToken)) {
  204. popContext(state);
  205. ctx = state.context;
  206. }
  207. }
  208. if (stream.eatSpace()) return null;
  209. curPunc = null;
  210. var style = state.tokenize[state.tokenize.length - 1](stream, state);
  211. if (style == "comment") return style;
  212. if (ctx.align == null) ctx.align = true;
  213. if ((curPunc == ";" || curPunc == ":") && ctx.type == "statement") popContext(state);
  214. // Handle indentation for {x -> \n ... }
  215. else if (curPunc == "->" && ctx.type == "statement" && ctx.prev.type == "}") {
  216. popContext(state);
  217. state.context.align = false;
  218. }
  219. else if (curPunc == "{") pushContext(state, stream.column(), "}");
  220. else if (curPunc == "[") pushContext(state, stream.column(), "]");
  221. else if (curPunc == "(") pushContext(state, stream.column(), ")");
  222. else if (curPunc == "}") {
  223. while (ctx.type == "statement") ctx = popContext(state);
  224. if (ctx.type == "}") ctx = popContext(state);
  225. while (ctx.type == "statement") ctx = popContext(state);
  226. }
  227. else if (curPunc == ctx.type) popContext(state);
  228. else if (ctx.type == "}" || ctx.type == "top" || (ctx.type == "statement" && curPunc == "newstatement"))
  229. pushContext(state, stream.column(), "statement");
  230. state.startOfLine = false;
  231. state.lastToken = curPunc || style;
  232. return style;
  233. },
  234. indent: function (state, textAfter) {
  235. if (!state.tokenize[state.tokenize.length - 1].isBase) return 0;
  236. var firstChar = textAfter && textAfter.charAt(0), ctx = state.context;
  237. if (ctx.type == "statement" && !expectExpression(state.lastToken)) ctx = ctx.prev;
  238. var closing = firstChar == ctx.type;
  239. if (ctx.type == "statement") {
  240. return ctx.indented + (firstChar == "{" ? 0 : config.indentUnit);
  241. }
  242. else if (ctx.align) return ctx.column + (closing ? 0 : 1);
  243. else return ctx.indented + (closing ? 0 : config.indentUnit);
  244. },
  245. electricChars: "{}"
  246. };
  247. });
  248. CodeMirror.defineMIME("text/x-kotlin", "kotlin");
  249. });