ruby.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. (function(mod) {
  2. if (typeof exports == "object" && typeof module == "object") // CommonJS
  3. mod(require("../../lib/codemirror"));
  4. else if (typeof define == "function" && define.amd) // AMD
  5. define(["../../lib/codemirror"], mod);
  6. else // Plain browser env
  7. mod(CodeMirror);
  8. })(function(CodeMirror) {
  9. "use strict";
  10. CodeMirror.defineMode("ruby", function(config) {
  11. function wordObj(words) {
  12. var o = {};
  13. for (var i = 0, e = words.length; i < e; ++i) o[words[i]] = true;
  14. return o;
  15. }
  16. var keywords = wordObj([
  17. "alias", "and", "BEGIN", "begin", "break", "case", "class", "def", "defined?", "do", "else",
  18. "elsif", "END", "end", "ensure", "false", "for", "if", "in", "module", "next", "not", "or",
  19. "redo", "rescue", "retry", "return", "self", "super", "then", "true", "undef", "unless",
  20. "until", "when", "while", "yield", "nil", "raise", "throw", "catch", "fail", "loop", "callcc",
  21. "caller", "lambda", "proc", "public", "protected", "private", "require", "load",
  22. "require_relative", "extend", "autoload", "__END__", "__FILE__", "__LINE__", "__dir__"
  23. ]);
  24. var indentWords = wordObj(["def", "class", "case", "for", "while", "module", "then",
  25. "catch", "loop", "proc", "begin"]);
  26. var dedentWords = wordObj(["end", "until"]);
  27. var matching = {"[": "]", "{": "}", "(": ")"};
  28. var curPunc;
  29. function chain(newtok, stream, state) {
  30. state.tokenize.push(newtok);
  31. return newtok(stream, state);
  32. }
  33. function tokenBase(stream, state) {
  34. curPunc = null;
  35. if (stream.sol() && stream.match("=begin") && stream.eol()) {
  36. state.tokenize.push(readBlockComment);
  37. return "comment";
  38. }
  39. if (stream.eatSpace()) return null;
  40. var ch = stream.next(), m;
  41. if (ch == "`" || ch == "'" || ch == '"') {
  42. return chain(readQuoted(ch, "string", ch == '"' || ch == "`"), stream, state);
  43. } else if (ch == "/" && !stream.eol() && stream.peek() != " ") {
  44. if (stream.eat("=")) return "operator";
  45. return chain(readQuoted(ch, "string-2", true), stream, state);
  46. } else if (ch == "%") {
  47. var style = "string", embed = true;
  48. if (stream.eat("s")) style = "atom";
  49. else if (stream.eat(/[WQ]/)) style = "string";
  50. else if (stream.eat(/[r]/)) style = "string-2";
  51. else if (stream.eat(/[wxq]/)) { style = "string"; embed = false; }
  52. var delim = stream.eat(/[^\w\s]/);
  53. if (!delim) return "operator";
  54. if (matching.propertyIsEnumerable(delim)) delim = matching[delim];
  55. return chain(readQuoted(delim, style, embed, true), stream, state);
  56. } else if (ch == "#") {
  57. stream.skipToEnd();
  58. return "comment";
  59. } else if (ch == "<" && (m = stream.match(/^<-?[\`\"\']?([a-zA-Z_?]\w*)[\`\"\']?(?:;|$)/))) {
  60. return chain(readHereDoc(m[1]), stream, state);
  61. } else if (ch == "0") {
  62. if (stream.eat("x")) stream.eatWhile(/[\da-fA-F]/);
  63. else if (stream.eat("b")) stream.eatWhile(/[01]/);
  64. else stream.eatWhile(/[0-7]/);
  65. return "number";
  66. } else if (/\d/.test(ch)) {
  67. stream.match(/^[\d_]*(?:\.[\d_]+)?(?:[eE][+\-]?[\d_]+)?/);
  68. return "number";
  69. } else if (ch == "?") {
  70. while (stream.match(/^\\[CM]-/)) {}
  71. if (stream.eat("\\")) stream.eatWhile(/\w/);
  72. else stream.next();
  73. return "string";
  74. } else if (ch == ":") {
  75. if (stream.eat("'")) return chain(readQuoted("'", "atom", false), stream, state);
  76. if (stream.eat('"')) return chain(readQuoted('"', "atom", true), stream, state);
  77. // :> :>> :< :<< are valid symbols
  78. if (stream.eat(/[\<\>]/)) {
  79. stream.eat(/[\<\>]/);
  80. return "atom";
  81. }
  82. // :+ :- :/ :* :| :& :! are valid symbols
  83. if (stream.eat(/[\+\-\*\/\&\|\:\!]/)) {
  84. return "atom";
  85. }
  86. // Symbols can't start by a digit
  87. if (stream.eat(/[a-zA-Z$@_]/)) {
  88. stream.eatWhile(/[\w]/);
  89. // Only one ? ! = is allowed and only as the last character
  90. stream.eat(/[\?\!\=]/);
  91. return "atom";
  92. }
  93. return "operator";
  94. } else if (ch == "@" && stream.match(/^@?[a-zA-Z_]/)) {
  95. stream.eat("@");
  96. stream.eatWhile(/[\w]/);
  97. return "variable-2";
  98. } else if (ch == "$") {
  99. if (stream.eat(/[a-zA-Z_]/)) {
  100. stream.eatWhile(/[\w]/);
  101. } else if (stream.eat(/\d/)) {
  102. stream.eat(/\d/);
  103. } else {
  104. stream.next(); // Must be a special global like $: or $!
  105. }
  106. return "variable-3";
  107. } else if (/[a-zA-Z_]/.test(ch)) {
  108. stream.eatWhile(/[\w]/);
  109. stream.eat(/[\?\!]/);
  110. if (stream.eat(":")) return "atom";
  111. return "ident";
  112. } else if (ch == "|" && (state.varList || state.lastTok == "{" || state.lastTok == "do")) {
  113. curPunc = "|";
  114. return null;
  115. } else if (/[\(\)\[\]{}\\;]/.test(ch)) {
  116. curPunc = ch;
  117. return null;
  118. } else if (ch == "-" && stream.eat(">")) {
  119. return "arrow";
  120. } else if (/[=+\-\/*:\.^%<>~|]/.test(ch)) {
  121. stream.eatWhile(/[=+\-\/*:\.^%<>~|]/);
  122. return "operator";
  123. } else {
  124. return null;
  125. }
  126. }
  127. function tokenBaseUntilBrace() {
  128. var depth = 1;
  129. return function(stream, state) {
  130. if (stream.peek() == "}") {
  131. depth--;
  132. if (depth == 0) {
  133. state.tokenize.pop();
  134. return state.tokenize[state.tokenize.length-1](stream, state);
  135. }
  136. } else if (stream.peek() == "{") {
  137. depth++;
  138. }
  139. return tokenBase(stream, state);
  140. };
  141. }
  142. function tokenBaseOnce() {
  143. var alreadyCalled = false;
  144. return function(stream, state) {
  145. if (alreadyCalled) {
  146. state.tokenize.pop();
  147. return state.tokenize[state.tokenize.length-1](stream, state);
  148. }
  149. alreadyCalled = true;
  150. return tokenBase(stream, state);
  151. };
  152. }
  153. function readQuoted(quote, style, embed, unescaped) {
  154. return function(stream, state) {
  155. var escaped = false, ch;
  156. if (state.context.type === 'read-quoted-paused') {
  157. state.context = state.context.prev;
  158. stream.eat("}");
  159. }
  160. while ((ch = stream.next()) != null) {
  161. if (ch == quote && (unescaped || !escaped)) {
  162. state.tokenize.pop();
  163. break;
  164. }
  165. if (embed && ch == "#" && !escaped) {
  166. if (stream.eat("{")) {
  167. if (quote == "}") {
  168. state.context = {prev: state.context, type: 'read-quoted-paused'};
  169. }
  170. state.tokenize.push(tokenBaseUntilBrace());
  171. break;
  172. } else if (/[@\$]/.test(stream.peek())) {
  173. state.tokenize.push(tokenBaseOnce());
  174. break;
  175. }
  176. }
  177. escaped = !escaped && ch == "\\";
  178. }
  179. return style;
  180. };
  181. }
  182. function readHereDoc(phrase) {
  183. return function(stream, state) {
  184. if (stream.match(phrase)) state.tokenize.pop();
  185. else stream.skipToEnd();
  186. return "string";
  187. };
  188. }
  189. function readBlockComment(stream, state) {
  190. if (stream.sol() && stream.match("=end") && stream.eol())
  191. state.tokenize.pop();
  192. stream.skipToEnd();
  193. return "comment";
  194. }
  195. return {
  196. startState: function() {
  197. return {tokenize: [tokenBase],
  198. indented: 0,
  199. context: {type: "top", indented: -config.indentUnit},
  200. continuedLine: false,
  201. lastTok: null,
  202. varList: false};
  203. },
  204. token: function(stream, state) {
  205. if (stream.sol()) state.indented = stream.indentation();
  206. var style = state.tokenize[state.tokenize.length-1](stream, state), kwtype;
  207. if (style == "ident") {
  208. var word = stream.current();
  209. style = keywords.propertyIsEnumerable(stream.current()) ? "keyword"
  210. : /^[A-Z]/.test(word) ? "tag"
  211. : (state.lastTok == "def" || state.lastTok == "class" || state.varList) ? "def"
  212. : "variable";
  213. if (indentWords.propertyIsEnumerable(word)) kwtype = "indent";
  214. else if (dedentWords.propertyIsEnumerable(word)) kwtype = "dedent";
  215. else if ((word == "if" || word == "unless") && stream.column() == stream.indentation())
  216. kwtype = "indent";
  217. else if (word == "do" && state.context.indented < state.indented)
  218. kwtype = "indent";
  219. }
  220. if (curPunc || (style && style != "comment")) state.lastTok = word || curPunc || style;
  221. if (curPunc == "|") state.varList = !state.varList;
  222. if (kwtype == "indent" || /[\(\[\{]/.test(curPunc))
  223. state.context = {prev: state.context, type: curPunc || style, indented: state.indented};
  224. else if ((kwtype == "dedent" || /[\)\]\}]/.test(curPunc)) && state.context.prev)
  225. state.context = state.context.prev;
  226. if (stream.eol())
  227. state.continuedLine = (curPunc == "\\" || style == "operator");
  228. return style;
  229. },
  230. indent: function(state, textAfter) {
  231. if (state.tokenize[state.tokenize.length-1] != tokenBase) return 0;
  232. var firstChar = textAfter && textAfter.charAt(0);
  233. var ct = state.context;
  234. var closing = ct.type == matching[firstChar] ||
  235. ct.type == "keyword" && /^(?:end|until|else|elsif|when|rescue)\b/.test(textAfter);
  236. return ct.indented + (closing ? 0 : config.indentUnit) +
  237. (state.continuedLine ? config.indentUnit : 0);
  238. },
  239. electricChars: "}de", // enD and rescuE
  240. lineComment: "#"
  241. };
  242. });
  243. CodeMirror.defineMIME("text/x-ruby", "ruby");
  244. });