ruby.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. if (regexpAhead(stream))
  46. return chain(readQuoted(ch, "string-2", true), stream, state);
  47. else
  48. return "operator";
  49. } else if (ch == "%") {
  50. var style = "string", embed = true;
  51. if (stream.eat("s")) style = "atom";
  52. else if (stream.eat(/[WQ]/)) style = "string";
  53. else if (stream.eat(/[r]/)) style = "string-2";
  54. else if (stream.eat(/[wxq]/)) { style = "string"; embed = false; }
  55. var delim = stream.eat(/[^\w\s=]/);
  56. if (!delim) return "operator";
  57. if (matching.propertyIsEnumerable(delim)) delim = matching[delim];
  58. return chain(readQuoted(delim, style, embed, true), stream, state);
  59. } else if (ch == "#") {
  60. stream.skipToEnd();
  61. return "comment";
  62. } else if (ch == "<" && (m = stream.match(/^<-?[\`\"\']?([a-zA-Z_?]\w*)[\`\"\']?(?:;|$)/))) {
  63. return chain(readHereDoc(m[1]), stream, state);
  64. } else if (ch == "0") {
  65. if (stream.eat("x")) stream.eatWhile(/[\da-fA-F]/);
  66. else if (stream.eat("b")) stream.eatWhile(/[01]/);
  67. else stream.eatWhile(/[0-7]/);
  68. return "number";
  69. } else if (/\d/.test(ch)) {
  70. stream.match(/^[\d_]*(?:\.[\d_]+)?(?:[eE][+\-]?[\d_]+)?/);
  71. return "number";
  72. } else if (ch == "?") {
  73. while (stream.match(/^\\[CM]-/)) {}
  74. if (stream.eat("\\")) stream.eatWhile(/\w/);
  75. else stream.next();
  76. return "string";
  77. } else if (ch == ":") {
  78. if (stream.eat("'")) return chain(readQuoted("'", "atom", false), stream, state);
  79. if (stream.eat('"')) return chain(readQuoted('"', "atom", true), stream, state);
  80. // :> :>> :< :<< are valid symbols
  81. if (stream.eat(/[\<\>]/)) {
  82. stream.eat(/[\<\>]/);
  83. return "atom";
  84. }
  85. // :+ :- :/ :* :| :& :! are valid symbols
  86. if (stream.eat(/[\+\-\*\/\&\|\:\!]/)) {
  87. return "atom";
  88. }
  89. // Symbols can't start by a digit
  90. if (stream.eat(/[a-zA-Z$@_\xa1-\uffff]/)) {
  91. stream.eatWhile(/[\w$\xa1-\uffff]/);
  92. // Only one ? ! = is allowed and only as the last character
  93. stream.eat(/[\?\!\=]/);
  94. return "atom";
  95. }
  96. return "operator";
  97. } else if (ch == "@" && stream.match(/^@?[a-zA-Z_\xa1-\uffff]/)) {
  98. stream.eat("@");
  99. stream.eatWhile(/[\w\xa1-\uffff]/);
  100. return "variable-2";
  101. } else if (ch == "$") {
  102. if (stream.eat(/[a-zA-Z_]/)) {
  103. stream.eatWhile(/[\w]/);
  104. } else if (stream.eat(/\d/)) {
  105. stream.eat(/\d/);
  106. } else {
  107. stream.next(); // Must be a special global like $: or $!
  108. }
  109. return "variable-3";
  110. } else if (/[a-zA-Z_\xa1-\uffff]/.test(ch)) {
  111. stream.eatWhile(/[\w\xa1-\uffff]/);
  112. stream.eat(/[\?\!]/);
  113. if (stream.eat(":")) return "atom";
  114. return "ident";
  115. } else if (ch == "|" && (state.varList || state.lastTok == "{" || state.lastTok == "do")) {
  116. curPunc = "|";
  117. return null;
  118. } else if (/[\(\)\[\]{}\\;]/.test(ch)) {
  119. curPunc = ch;
  120. return null;
  121. } else if (ch == "-" && stream.eat(">")) {
  122. return "arrow";
  123. } else if (/[=+\-\/*:\.^%<>~|]/.test(ch)) {
  124. var more = stream.eatWhile(/[=+\-\/*:\.^%<>~|]/);
  125. if (ch == "." && !more) curPunc = ".";
  126. return "operator";
  127. } else {
  128. return null;
  129. }
  130. }
  131. function regexpAhead(stream) {
  132. var start = stream.pos, depth = 0, next, found = false, escaped = false
  133. while ((next = stream.next()) != null) {
  134. if (!escaped) {
  135. if ("[{(".indexOf(next) > -1) {
  136. depth++
  137. } else if ("]})".indexOf(next) > -1) {
  138. depth--
  139. if (depth < 0) break
  140. } else if (next == "/" && depth == 0) {
  141. found = true
  142. break
  143. }
  144. escaped = next == "\\"
  145. } else {
  146. escaped = false
  147. }
  148. }
  149. stream.backUp(stream.pos - start)
  150. return found
  151. }
  152. function tokenBaseUntilBrace(depth) {
  153. if (!depth) depth = 1;
  154. return function(stream, state) {
  155. if (stream.peek() == "}") {
  156. if (depth == 1) {
  157. state.tokenize.pop();
  158. return state.tokenize[state.tokenize.length-1](stream, state);
  159. } else {
  160. state.tokenize[state.tokenize.length - 1] = tokenBaseUntilBrace(depth - 1);
  161. }
  162. } else if (stream.peek() == "{") {
  163. state.tokenize[state.tokenize.length - 1] = tokenBaseUntilBrace(depth + 1);
  164. }
  165. return tokenBase(stream, state);
  166. };
  167. }
  168. function tokenBaseOnce() {
  169. var alreadyCalled = false;
  170. return function(stream, state) {
  171. if (alreadyCalled) {
  172. state.tokenize.pop();
  173. return state.tokenize[state.tokenize.length-1](stream, state);
  174. }
  175. alreadyCalled = true;
  176. return tokenBase(stream, state);
  177. };
  178. }
  179. function readQuoted(quote, style, embed, unescaped) {
  180. return function(stream, state) {
  181. var escaped = false, ch;
  182. if (state.context.type === 'read-quoted-paused') {
  183. state.context = state.context.prev;
  184. stream.eat("}");
  185. }
  186. while ((ch = stream.next()) != null) {
  187. if (ch == quote && (unescaped || !escaped)) {
  188. state.tokenize.pop();
  189. break;
  190. }
  191. if (embed && ch == "#" && !escaped) {
  192. if (stream.eat("{")) {
  193. if (quote == "}") {
  194. state.context = {prev: state.context, type: 'read-quoted-paused'};
  195. }
  196. state.tokenize.push(tokenBaseUntilBrace());
  197. break;
  198. } else if (/[@\$]/.test(stream.peek())) {
  199. state.tokenize.push(tokenBaseOnce());
  200. break;
  201. }
  202. }
  203. escaped = !escaped && ch == "\\";
  204. }
  205. return style;
  206. };
  207. }
  208. function readHereDoc(phrase) {
  209. return function(stream, state) {
  210. if (stream.match(phrase)) state.tokenize.pop();
  211. else stream.skipToEnd();
  212. return "string";
  213. };
  214. }
  215. function readBlockComment(stream, state) {
  216. if (stream.sol() && stream.match("=end") && stream.eol())
  217. state.tokenize.pop();
  218. stream.skipToEnd();
  219. return "comment";
  220. }
  221. return {
  222. startState: function() {
  223. return {tokenize: [tokenBase],
  224. indented: 0,
  225. context: {type: "top", indented: -config.indentUnit},
  226. continuedLine: false,
  227. lastTok: null,
  228. varList: false};
  229. },
  230. token: function(stream, state) {
  231. curPunc = null;
  232. if (stream.sol()) state.indented = stream.indentation();
  233. var style = state.tokenize[state.tokenize.length-1](stream, state), kwtype;
  234. var thisTok = curPunc;
  235. if (style == "ident") {
  236. var word = stream.current();
  237. style = state.lastTok == "." ? "property"
  238. : keywords.propertyIsEnumerable(stream.current()) ? "keyword"
  239. : /^[A-Z]/.test(word) ? "tag"
  240. : (state.lastTok == "def" || state.lastTok == "class" || state.varList) ? "def"
  241. : "variable";
  242. if (style == "keyword") {
  243. thisTok = word;
  244. if (indentWords.propertyIsEnumerable(word)) kwtype = "indent";
  245. else if (dedentWords.propertyIsEnumerable(word)) kwtype = "dedent";
  246. else if ((word == "if" || word == "unless") && stream.column() == stream.indentation())
  247. kwtype = "indent";
  248. else if (word == "do" && state.context.indented < state.indented)
  249. kwtype = "indent";
  250. }
  251. }
  252. if (curPunc || (style && style != "comment")) state.lastTok = thisTok;
  253. if (curPunc == "|") state.varList = !state.varList;
  254. if (kwtype == "indent" || /[\(\[\{]/.test(curPunc))
  255. state.context = {prev: state.context, type: curPunc || style, indented: state.indented};
  256. else if ((kwtype == "dedent" || /[\)\]\}]/.test(curPunc)) && state.context.prev)
  257. state.context = state.context.prev;
  258. if (stream.eol())
  259. state.continuedLine = (curPunc == "\\" || style == "operator");
  260. return style;
  261. },
  262. indent: function(state, textAfter) {
  263. if (state.tokenize[state.tokenize.length-1] != tokenBase) return 0;
  264. var firstChar = textAfter && textAfter.charAt(0);
  265. var ct = state.context;
  266. var closing = ct.type == matching[firstChar] ||
  267. ct.type == "keyword" && /^(?:end|until|else|elsif|when|rescue)\b/.test(textAfter);
  268. return ct.indented + (closing ? 0 : config.indentUnit) +
  269. (state.continuedLine ? config.indentUnit : 0);
  270. },
  271. electricInput: /^\s*(?:end|rescue|elsif|else|\})$/,
  272. lineComment: "#"
  273. };
  274. });
  275. CodeMirror.defineMIME("text/x-ruby", "ruby");
  276. });