ruby.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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", "until", "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. if (stream.sol() && stream.match("=begin") && stream.eol()) {
  37. state.tokenize.push(readBlockComment);
  38. return "comment";
  39. }
  40. if (stream.eatSpace()) return null;
  41. var ch = stream.next(), m;
  42. if (ch == "`" || ch == "'" || ch == '"') {
  43. return chain(readQuoted(ch, "string", ch == '"' || ch == "`"), stream, state);
  44. } else if (ch == "/") {
  45. var currentIndex = stream.current().length;
  46. if (stream.skipTo("/")) {
  47. var search_till = stream.current().length;
  48. stream.backUp(stream.current().length - currentIndex);
  49. var balance = 0; // balance brackets
  50. while (stream.current().length < search_till) {
  51. var chchr = stream.next();
  52. if (chchr == "(") balance += 1;
  53. else if (chchr == ")") balance -= 1;
  54. if (balance < 0) break;
  55. }
  56. stream.backUp(stream.current().length - currentIndex);
  57. if (balance == 0)
  58. return chain(readQuoted(ch, "string-2", true), stream, state);
  59. }
  60. return "operator";
  61. } else if (ch == "%") {
  62. var style = "string", embed = true;
  63. if (stream.eat("s")) style = "atom";
  64. else if (stream.eat(/[WQ]/)) style = "string";
  65. else if (stream.eat(/[r]/)) style = "string-2";
  66. else if (stream.eat(/[wxq]/)) { style = "string"; embed = false; }
  67. var delim = stream.eat(/[^\w\s=]/);
  68. if (!delim) return "operator";
  69. if (matching.propertyIsEnumerable(delim)) delim = matching[delim];
  70. return chain(readQuoted(delim, style, embed, true), stream, state);
  71. } else if (ch == "#") {
  72. stream.skipToEnd();
  73. return "comment";
  74. } else if (ch == "<" && (m = stream.match(/^<-?[\`\"\']?([a-zA-Z_?]\w*)[\`\"\']?(?:;|$)/))) {
  75. return chain(readHereDoc(m[1]), stream, state);
  76. } else if (ch == "0") {
  77. if (stream.eat("x")) stream.eatWhile(/[\da-fA-F]/);
  78. else if (stream.eat("b")) stream.eatWhile(/[01]/);
  79. else stream.eatWhile(/[0-7]/);
  80. return "number";
  81. } else if (/\d/.test(ch)) {
  82. stream.match(/^[\d_]*(?:\.[\d_]+)?(?:[eE][+\-]?[\d_]+)?/);
  83. return "number";
  84. } else if (ch == "?") {
  85. while (stream.match(/^\\[CM]-/)) {}
  86. if (stream.eat("\\")) stream.eatWhile(/\w/);
  87. else stream.next();
  88. return "string";
  89. } else if (ch == ":") {
  90. if (stream.eat("'")) return chain(readQuoted("'", "atom", false), stream, state);
  91. if (stream.eat('"')) return chain(readQuoted('"', "atom", true), stream, state);
  92. // :> :>> :< :<< are valid symbols
  93. if (stream.eat(/[\<\>]/)) {
  94. stream.eat(/[\<\>]/);
  95. return "atom";
  96. }
  97. // :+ :- :/ :* :| :& :! are valid symbols
  98. if (stream.eat(/[\+\-\*\/\&\|\:\!]/)) {
  99. return "atom";
  100. }
  101. // Symbols can't start by a digit
  102. if (stream.eat(/[a-zA-Z$@_\xa1-\uffff]/)) {
  103. stream.eatWhile(/[\w$\xa1-\uffff]/);
  104. // Only one ? ! = is allowed and only as the last character
  105. stream.eat(/[\?\!\=]/);
  106. return "atom";
  107. }
  108. return "operator";
  109. } else if (ch == "@" && stream.match(/^@?[a-zA-Z_\xa1-\uffff]/)) {
  110. stream.eat("@");
  111. stream.eatWhile(/[\w\xa1-\uffff]/);
  112. return "variable-2";
  113. } else if (ch == "$") {
  114. if (stream.eat(/[a-zA-Z_]/)) {
  115. stream.eatWhile(/[\w]/);
  116. } else if (stream.eat(/\d/)) {
  117. stream.eat(/\d/);
  118. } else {
  119. stream.next(); // Must be a special global like $: or $!
  120. }
  121. return "variable-3";
  122. } else if (/[a-zA-Z_\xa1-\uffff]/.test(ch)) {
  123. stream.eatWhile(/[\w\xa1-\uffff]/);
  124. stream.eat(/[\?\!]/);
  125. if (stream.eat(":")) return "atom";
  126. return "ident";
  127. } else if (ch == "|" && (state.varList || state.lastTok == "{" || state.lastTok == "do")) {
  128. curPunc = "|";
  129. return null;
  130. } else if (/[\(\)\[\]{}\\;]/.test(ch)) {
  131. curPunc = ch;
  132. return null;
  133. } else if (ch == "-" && stream.eat(">")) {
  134. return "arrow";
  135. } else if (/[=+\-\/*:\.^%<>~|]/.test(ch)) {
  136. var more = stream.eatWhile(/[=+\-\/*:\.^%<>~|]/);
  137. if (ch == "." && !more) curPunc = ".";
  138. return "operator";
  139. } else {
  140. return null;
  141. }
  142. }
  143. function tokenBaseUntilBrace(depth) {
  144. if (!depth) depth = 1;
  145. return function(stream, state) {
  146. if (stream.peek() == "}") {
  147. if (depth == 1) {
  148. state.tokenize.pop();
  149. return state.tokenize[state.tokenize.length-1](stream, state);
  150. } else {
  151. state.tokenize[state.tokenize.length - 1] = tokenBaseUntilBrace(depth - 1);
  152. }
  153. } else if (stream.peek() == "{") {
  154. state.tokenize[state.tokenize.length - 1] = tokenBaseUntilBrace(depth + 1);
  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. curPunc = null;
  223. if (stream.sol()) state.indented = stream.indentation();
  224. var style = state.tokenize[state.tokenize.length-1](stream, state), kwtype;
  225. var thisTok = curPunc;
  226. if (style == "ident") {
  227. var word = stream.current();
  228. style = state.lastTok == "." ? "property"
  229. : keywords.propertyIsEnumerable(stream.current()) ? "keyword"
  230. : /^[A-Z]/.test(word) ? "tag"
  231. : (state.lastTok == "def" || state.lastTok == "class" || state.varList) ? "def"
  232. : "variable";
  233. if (style == "keyword") {
  234. thisTok = word;
  235. if (indentWords.propertyIsEnumerable(word)) kwtype = "indent";
  236. else if (dedentWords.propertyIsEnumerable(word)) kwtype = "dedent";
  237. else if ((word == "if" || word == "unless") && stream.column() == stream.indentation())
  238. kwtype = "indent";
  239. else if (word == "do" && state.context.indented < state.indented)
  240. kwtype = "indent";
  241. }
  242. }
  243. if (curPunc || (style && style != "comment")) state.lastTok = thisTok;
  244. if (curPunc == "|") state.varList = !state.varList;
  245. if (kwtype == "indent" || /[\(\[\{]/.test(curPunc))
  246. state.context = {prev: state.context, type: curPunc || style, indented: state.indented};
  247. else if ((kwtype == "dedent" || /[\)\]\}]/.test(curPunc)) && state.context.prev)
  248. state.context = state.context.prev;
  249. if (stream.eol())
  250. state.continuedLine = (curPunc == "\\" || style == "operator");
  251. return style;
  252. },
  253. indent: function(state, textAfter) {
  254. if (state.tokenize[state.tokenize.length-1] != tokenBase) return 0;
  255. var firstChar = textAfter && textAfter.charAt(0);
  256. var ct = state.context;
  257. var closing = ct.type == matching[firstChar] ||
  258. ct.type == "keyword" && /^(?:end|until|else|elsif|when|rescue)\b/.test(textAfter);
  259. return ct.indented + (closing ? 0 : config.indentUnit) +
  260. (state.continuedLine ? config.indentUnit : 0);
  261. },
  262. electricInput: /^\s*(?:end|rescue|\})$/,
  263. lineComment: "#"
  264. };
  265. });
  266. CodeMirror.defineMIME("text/x-ruby", "ruby");
  267. });