2
0

ruby.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. CodeMirror.defineMode("ruby", function(config) {
  2. function wordObj(words) {
  3. var o = {};
  4. for (var i = 0, e = words.length; i < e; ++i) o[words[i]] = true;
  5. return o;
  6. }
  7. var keywords = wordObj([
  8. "alias", "and", "BEGIN", "begin", "break", "case", "class", "def", "defined?", "do", "else",
  9. "elsif", "END", "end", "ensure", "false", "for", "if", "in", "module", "next", "not", "or",
  10. "redo", "rescue", "retry", "return", "self", "super", "then", "true", "undef", "unless",
  11. "until", "when", "while", "yield", "nil", "raise", "throw", "catch", "fail", "loop", "callcc",
  12. "caller", "lambda", "proc", "public", "protected", "private", "require", "load",
  13. "require_relative", "extend", "autoload"
  14. ]);
  15. var indentWords = wordObj(["def", "class", "case", "for", "while", "do", "module", "then",
  16. "catch", "loop", "proc", "begin"]);
  17. var dedentWords = wordObj(["end", "until"]);
  18. var matching = {"[": "]", "{": "}", "(": ")"};
  19. var curPunc;
  20. function chain(newtok, stream, state) {
  21. state.tokenize.push(newtok);
  22. return newtok(stream, state);
  23. }
  24. function tokenBase(stream, state) {
  25. curPunc = null;
  26. if (stream.sol() && stream.match("=begin") && stream.eol()) {
  27. state.tokenize.push(readBlockComment);
  28. return "comment";
  29. }
  30. if (stream.eatSpace()) return null;
  31. var ch = stream.next(), m;
  32. if (ch == "`" || ch == "'" || ch == '"' ||
  33. (ch == "/" && !stream.eol() && stream.peek() != " ")) {
  34. return chain(readQuoted(ch, "string", ch == '"' || ch == "`"), stream, state);
  35. } else if (ch == "%") {
  36. var style, embed = false;
  37. if (stream.eat("s")) style = "atom";
  38. else if (stream.eat(/[WQ]/)) { style = "string"; embed = true; }
  39. else if (stream.eat(/[wxqr]/)) style = "string";
  40. var delim = stream.eat(/[^\w\s]/);
  41. if (!delim) return "operator";
  42. if (matching.propertyIsEnumerable(delim)) delim = matching[delim];
  43. return chain(readQuoted(delim, style, embed, true), stream, state);
  44. } else if (ch == "#") {
  45. stream.skipToEnd();
  46. return "comment";
  47. } else if (ch == "<" && (m = stream.match(/^<-?[\`\"\']?([a-zA-Z_?]\w*)[\`\"\']?(?:;|$)/))) {
  48. return chain(readHereDoc(m[1]), stream, state);
  49. } else if (ch == "0") {
  50. if (stream.eat("x")) stream.eatWhile(/[\da-fA-F]/);
  51. else if (stream.eat("b")) stream.eatWhile(/[01]/);
  52. else stream.eatWhile(/[0-7]/);
  53. return "number";
  54. } else if (/\d/.test(ch)) {
  55. stream.match(/^[\d_]*(?:\.[\d_]+)?(?:[eE][+\-]?[\d_]+)?/);
  56. return "number";
  57. } else if (ch == "?") {
  58. while (stream.match(/^\\[CM]-/)) {}
  59. if (stream.eat("\\")) stream.eatWhile(/\w/);
  60. else stream.next();
  61. return "string";
  62. } else if (ch == ":") {
  63. if (stream.eat("'")) return chain(readQuoted("'", "atom", false), stream, state);
  64. if (stream.eat('"')) return chain(readQuoted('"', "atom", true), stream, state);
  65. stream.eatWhile(/[\w\?]/);
  66. return "atom";
  67. } else if (ch == "@") {
  68. stream.eat("@");
  69. stream.eatWhile(/[\w\?]/);
  70. return "variable-2";
  71. } else if (ch == "$") {
  72. stream.next();
  73. stream.eatWhile(/[\w\?]/);
  74. return "variable-3";
  75. } else if (/\w/.test(ch)) {
  76. stream.eatWhile(/[\w\?]/);
  77. if (stream.eat(":")) return "atom";
  78. return "ident";
  79. } else if (ch == "|" && (state.varList || state.lastTok == "{" || state.lastTok == "do")) {
  80. curPunc = "|";
  81. return null;
  82. } else if (/[\(\)\[\]{}\\;]/.test(ch)) {
  83. curPunc = ch;
  84. return null;
  85. } else if (ch == "-" && stream.eat(">")) {
  86. return "arrow";
  87. } else if (/[=+\-\/*:\.^%<>~|]/.test(ch)) {
  88. stream.eatWhile(/[=+\-\/*:\.^%<>~|]/);
  89. return "operator";
  90. } else {
  91. return null;
  92. }
  93. }
  94. function tokenBaseUntilBrace() {
  95. var depth = 1;
  96. return function(stream, state) {
  97. if (stream.peek() == "}") {
  98. depth--;
  99. if (depth == 0) {
  100. state.tokenize.pop();
  101. return state.tokenize[state.tokenize.length-1](stream, state);
  102. }
  103. } else if (stream.peek() == "{") {
  104. depth++;
  105. }
  106. return tokenBase(stream, state);
  107. };
  108. }
  109. function readQuoted(quote, style, embed, unescaped) {
  110. return function(stream, state) {
  111. var escaped = false, ch;
  112. while ((ch = stream.next()) != null) {
  113. if (ch == quote && (unescaped || !escaped)) {
  114. state.tokenize.pop();
  115. break;
  116. }
  117. if (embed && ch == "#" && !escaped && stream.eat("{")) {
  118. state.tokenize.push(tokenBaseUntilBrace(arguments.callee));
  119. break;
  120. }
  121. escaped = !escaped && ch == "\\";
  122. }
  123. return style;
  124. };
  125. }
  126. function readHereDoc(phrase) {
  127. return function(stream, state) {
  128. if (stream.match(phrase)) state.tokenize.pop();
  129. else stream.skipToEnd();
  130. return "string";
  131. };
  132. }
  133. function readBlockComment(stream, state) {
  134. if (stream.sol() && stream.match("=end") && stream.eol())
  135. state.tokenize.pop();
  136. stream.skipToEnd();
  137. return "comment";
  138. }
  139. return {
  140. startState: function() {
  141. return {tokenize: [tokenBase],
  142. indented: 0,
  143. context: {type: "top", indented: -config.indentUnit},
  144. continuedLine: false,
  145. lastTok: null,
  146. varList: false};
  147. },
  148. token: function(stream, state) {
  149. if (stream.sol()) state.indented = stream.indentation();
  150. var style = state.tokenize[state.tokenize.length-1](stream, state), kwtype;
  151. if (style == "ident") {
  152. var word = stream.current();
  153. style = keywords.propertyIsEnumerable(stream.current()) ? "keyword"
  154. : /^[A-Z]/.test(word) ? "tag"
  155. : (state.lastTok == "def" || state.lastTok == "class" || state.varList) ? "def"
  156. : "variable";
  157. if (indentWords.propertyIsEnumerable(word)) kwtype = "indent";
  158. else if (dedentWords.propertyIsEnumerable(word)) kwtype = "dedent";
  159. else if ((word == "if" || word == "unless") && stream.column() == stream.indentation())
  160. kwtype = "indent";
  161. }
  162. if (curPunc || (style && style != "comment")) state.lastTok = word || curPunc || style;
  163. if (curPunc == "|") state.varList = !state.varList;
  164. if (kwtype == "indent" || /[\(\[\{]/.test(curPunc))
  165. state.context = {prev: state.context, type: curPunc || style, indented: state.indented};
  166. else if ((kwtype == "dedent" || /[\)\]\}]/.test(curPunc)) && state.context.prev)
  167. state.context = state.context.prev;
  168. if (stream.eol())
  169. state.continuedLine = (curPunc == "\\" || style == "operator");
  170. return style;
  171. },
  172. indent: function(state, textAfter) {
  173. if (state.tokenize[state.tokenize.length-1] != tokenBase) return 0;
  174. var firstChar = textAfter && textAfter.charAt(0);
  175. var ct = state.context;
  176. var closing = ct.type == matching[firstChar] ||
  177. ct.type == "keyword" && /^(?:end|until|else|elsif|when|rescue)\b/.test(textAfter);
  178. return ct.indented + (closing ? 0 : config.indentUnit) +
  179. (state.continuedLine ? config.indentUnit : 0);
  180. },
  181. electricChars: "}de", // enD and rescuE
  182. lineComment: "#"
  183. };
  184. });
  185. CodeMirror.defineMIME("text/x-ruby", "ruby");