ruby.js 10.0 KB

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