1
0

clike.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. CodeMirror.defineMode("clike", function(config, parserConfig) {
  2. var indentUnit = config.indentUnit,
  3. keywords = parserConfig.keywords || {},
  4. blockKeywords = parserConfig.blockKeywords || {},
  5. atoms = parserConfig.atoms || {},
  6. hooks = parserConfig.hooks || {},
  7. multiLineStrings = parserConfig.multiLineStrings;
  8. var isOperatorChar = /[+\-*&%=<>!?|\/]/;
  9. var curPunc;
  10. function tokenBase(stream, state) {
  11. var ch = stream.next();
  12. if (hooks[ch]) {
  13. var result = hooks[ch](stream, state);
  14. if (result !== false) return result;
  15. }
  16. if (ch == '"' || ch == "'") {
  17. state.tokenize = tokenString(ch);
  18. return state.tokenize(stream, state);
  19. }
  20. if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
  21. curPunc = ch;
  22. return null
  23. }
  24. if (/\d/.test(ch)) {
  25. stream.eatWhile(/[\w\.]/);
  26. return "number";
  27. }
  28. if (ch == "/") {
  29. if (stream.eat("*")) {
  30. state.tokenize = tokenComment;
  31. return tokenComment(stream, state);
  32. }
  33. if (stream.eat("/")) {
  34. stream.skipToEnd();
  35. return "comment";
  36. }
  37. }
  38. if (isOperatorChar.test(ch)) {
  39. stream.eatWhile(isOperatorChar);
  40. return "operator";
  41. }
  42. stream.eatWhile(/[\w\$_]/);
  43. var cur = stream.current();
  44. if (keywords.propertyIsEnumerable(cur)) {
  45. if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement";
  46. return "keyword";
  47. }
  48. if (atoms.propertyIsEnumerable(cur)) return "atom";
  49. return "word";
  50. }
  51. function tokenString(quote) {
  52. return function(stream, state) {
  53. var escaped = false, next, end = false;
  54. while ((next = stream.next()) != null) {
  55. if (next == quote && !escaped) {end = true; break;}
  56. escaped = !escaped && next == "\\";
  57. }
  58. if (end || !(escaped || multiLineStrings))
  59. state.tokenize = tokenBase;
  60. return "string";
  61. };
  62. }
  63. function tokenComment(stream, state) {
  64. var maybeEnd = false, ch;
  65. while (ch = stream.next()) {
  66. if (ch == "/" && maybeEnd) {
  67. state.tokenize = tokenBase;
  68. break;
  69. }
  70. maybeEnd = (ch == "*");
  71. }
  72. return "comment";
  73. }
  74. function Context(indented, column, type, align, prev) {
  75. this.indented = indented;
  76. this.column = column;
  77. this.type = type;
  78. this.align = align;
  79. this.prev = prev;
  80. }
  81. function pushContext(state, col, type) {
  82. return state.context = new Context(state.indented, col, type, null, state.context);
  83. }
  84. function popContext(state) {
  85. var t = state.context.type;
  86. if (t == ")" || t == "]" || t == "}")
  87. state.indented = state.context.indented;
  88. return state.context = state.context.prev;
  89. }
  90. // Interface
  91. return {
  92. startState: function(basecolumn) {
  93. return {
  94. tokenize: null,
  95. context: new Context((basecolumn || 0) - indentUnit, 0, "top", false),
  96. indented: 0,
  97. startOfLine: true
  98. };
  99. },
  100. token: function(stream, state) {
  101. var ctx = state.context;
  102. if (stream.sol()) {
  103. if (ctx.align == null) ctx.align = false;
  104. state.indented = stream.indentation();
  105. state.startOfLine = true;
  106. }
  107. if (stream.eatSpace()) return null;
  108. curPunc = null;
  109. var style = (state.tokenize || tokenBase)(stream, state);
  110. if (style == "comment" || style == "meta") return style;
  111. if (ctx.align == null) ctx.align = true;
  112. if ((curPunc == ";" || curPunc == ":") && ctx.type == "statement") popContext(state);
  113. else if (curPunc == "{") pushContext(state, stream.column(), "}");
  114. else if (curPunc == "[") pushContext(state, stream.column(), "]");
  115. else if (curPunc == "(") pushContext(state, stream.column(), ")");
  116. else if (curPunc == "}") {
  117. while (ctx.type == "statement") ctx = popContext(state);
  118. if (ctx.type == "}") ctx = popContext(state);
  119. while (ctx.type == "statement") ctx = popContext(state);
  120. }
  121. else if (curPunc == ctx.type) popContext(state);
  122. else if (ctx.type == "}" || ctx.type == "top" || (ctx.type == "statement" && curPunc == "newstatement"))
  123. pushContext(state, stream.column(), "statement");
  124. state.startOfLine = false;
  125. return style;
  126. },
  127. indent: function(state, textAfter) {
  128. if (state.tokenize != tokenBase && state.tokenize != null) return 0;
  129. var firstChar = textAfter && textAfter.charAt(0), ctx = state.context, closing = firstChar == ctx.type;
  130. if (ctx.type == "statement") return ctx.indented + (firstChar == "{" ? 0 : indentUnit);
  131. else if (ctx.align) return ctx.column + (closing ? 0 : 1);
  132. else return ctx.indented + (closing ? 0 : indentUnit);
  133. },
  134. electricChars: "{}"
  135. };
  136. });
  137. (function() {
  138. function words(str) {
  139. var obj = {}, words = str.split(" ");
  140. for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
  141. return obj;
  142. }
  143. var cKeywords = "auto if break int case long char register continue return default short do sizeof " +
  144. "double static else struct entry switch extern typedef float union for unsigned " +
  145. "goto while enum void const signed volatile";
  146. function cppHook(stream, state) {
  147. if (!state.startOfLine) return false;
  148. stream.skipToEnd();
  149. return "meta";
  150. }
  151. // C#-style strings where "" escapes a quote.
  152. function tokenAtString(stream, state) {
  153. var next;
  154. while ((next = stream.next()) != null) {
  155. if (next == '"' && !stream.eat('"')) {
  156. state.tokenize = null;
  157. break;
  158. }
  159. }
  160. return "string";
  161. }
  162. CodeMirror.defineMIME("text/x-csrc", {
  163. name: "clike",
  164. keywords: words(cKeywords),
  165. blockKeywords: words("case do else for if switch while struct"),
  166. atoms: words("null"),
  167. hooks: {"#": cppHook}
  168. });
  169. CodeMirror.defineMIME("text/x-c++src", {
  170. name: "clike",
  171. keywords: words(cKeywords + " asm dynamic_cast namespace reinterpret_cast try bool explicit new " +
  172. "static_cast typeid catch operator template typename class friend private " +
  173. "this using const_cast inline public throw virtual delete mutable protected " +
  174. "wchar_t"),
  175. blockKeywords: words("catch class do else finally for if struct switch try while"),
  176. atoms: words("true false null"),
  177. hooks: {"#": cppHook}
  178. });
  179. CodeMirror.defineMIME("text/x-java", {
  180. name: "clike",
  181. keywords: words("abstract assert boolean break byte case catch char class const continue default " +
  182. "do double else enum extends final finally float for goto if implements import " +
  183. "instanceof int interface long native new package private protected public " +
  184. "return short static strictfp super switch synchronized this throw throws transient " +
  185. "try void volatile while"),
  186. blockKeywords: words("catch class do else finally for if switch try while"),
  187. atoms: words("true false null"),
  188. hooks: {
  189. "@": function(stream, state) {
  190. stream.eatWhile(/[\w\$_]/);
  191. return "meta";
  192. }
  193. }
  194. });
  195. CodeMirror.defineMIME("text/x-csharp", {
  196. name: "clike",
  197. keywords: words("abstract as base bool break byte case catch char checked class const continue decimal" +
  198. " default delegate do double else enum event explicit extern finally fixed float for" +
  199. " foreach goto if implicit in int interface internal is lock long namespace new object" +
  200. " operator out override params private protected public readonly ref return sbyte sealed short" +
  201. " sizeof stackalloc static string struct switch this throw try typeof uint ulong unchecked" +
  202. " unsafe ushort using virtual void volatile while add alias ascending descending dynamic from get" +
  203. " global group into join let orderby partial remove select set value var yield"),
  204. blockKeywords: words("catch class do else finally for foreach if struct switch try while"),
  205. atoms: words("true false null"),
  206. hooks: {
  207. "@": function(stream, state) {
  208. if (stream.eat('"')) {
  209. state.tokenize = tokenAtString;
  210. return tokenAtString(stream, state);
  211. }
  212. stream.eatWhile(/[\w\$_]/);
  213. return "meta";
  214. }
  215. }
  216. });
  217. CodeMirror.defineMIME("text/x-groovy", {
  218. name: "clike",
  219. keywords: words("abstract as assert boolean break byte case catch char class const continue def default " +
  220. "do double else enum extends final finally float for goto if implements import " +
  221. "in instanceof int interface long native new package property private protected public " +
  222. "return short static strictfp super switch synchronized this throw throws transient " +
  223. "try void volatile while"),
  224. atoms: words("true false null"),
  225. hooks: {
  226. "@": function(stream, state) {
  227. stream.eatWhile(/[\w\$_]/);
  228. return "meta";
  229. }
  230. }
  231. });
  232. }());