You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

285 lines
10 KiB

3 years ago
  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$@_\xa1-\uffff]/)) {
  104. stream.eatWhile(/[\w$\xa1-\uffff]/);
  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_\xa1-\uffff]/)) {
  111. stream.eat("@");
  112. stream.eatWhile(/[\w\xa1-\uffff]/);
  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_\xa1-\uffff]/.test(ch)) {
  124. stream.eatWhile(/[\w\xa1-\uffff]/);
  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(depth) {
  145. if (!depth) depth = 1;
  146. return function(stream, state) {
  147. if (stream.peek() == "}") {
  148. if (depth == 1) {
  149. state.tokenize.pop();
  150. return state.tokenize[state.tokenize.length-1](stream, state);
  151. } else {
  152. state.tokenize[state.tokenize.length - 1] = tokenBaseUntilBrace(depth - 1);
  153. }
  154. } else if (stream.peek() == "{") {
  155. state.tokenize[state.tokenize.length - 1] = tokenBaseUntilBrace(depth + 1);
  156. }
  157. return tokenBase(stream, state);
  158. };
  159. }
  160. function tokenBaseOnce() {
  161. var alreadyCalled = false;
  162. return function(stream, state) {
  163. if (alreadyCalled) {
  164. state.tokenize.pop();
  165. return state.tokenize[state.tokenize.length-1](stream, state);
  166. }
  167. alreadyCalled = true;
  168. return tokenBase(stream, state);
  169. };
  170. }
  171. function readQuoted(quote, style, embed, unescaped) {
  172. return function(stream, state) {
  173. var escaped = false, ch;
  174. if (state.context.type === 'read-quoted-paused') {
  175. state.context = state.context.prev;
  176. stream.eat("}");
  177. }
  178. while ((ch = stream.next()) != null) {
  179. if (ch == quote && (unescaped || !escaped)) {
  180. state.tokenize.pop();
  181. break;
  182. }
  183. if (embed && ch == "#" && !escaped) {
  184. if (stream.eat("{")) {
  185. if (quote == "}") {
  186. state.context = {prev: state.context, type: 'read-quoted-paused'};
  187. }
  188. state.tokenize.push(tokenBaseUntilBrace());
  189. break;
  190. } else if (/[@\$]/.test(stream.peek())) {
  191. state.tokenize.push(tokenBaseOnce());
  192. break;
  193. }
  194. }
  195. escaped = !escaped && ch == "\\";
  196. }
  197. return style;
  198. };
  199. }
  200. function readHereDoc(phrase) {
  201. return function(stream, state) {
  202. if (stream.match(phrase)) state.tokenize.pop();
  203. else stream.skipToEnd();
  204. return "string";
  205. };
  206. }
  207. function readBlockComment(stream, state) {
  208. if (stream.sol() && stream.match("=end") && stream.eol())
  209. state.tokenize.pop();
  210. stream.skipToEnd();
  211. return "comment";
  212. }
  213. return {
  214. startState: function() {
  215. return {tokenize: [tokenBase],
  216. indented: 0,
  217. context: {type: "top", indented: -config.indentUnit},
  218. continuedLine: false,
  219. lastTok: null,
  220. varList: false};
  221. },
  222. token: function(stream, state) {
  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. electricChars: "}de", // enD and rescuE
  263. lineComment: "#"
  264. };
  265. });
  266. CodeMirror.defineMIME("text/x-ruby", "ruby");
  267. });