1 CodeMirror.defineMode("ruby", function(config) {
2 function wordObj(words) {
4 for (var i = 0, e = words.length; i < e; ++i) o[words[i]] = true;
7 var keywords = wordObj([
8 "alias", "and", "BEGIN", "begin", "break", "case", "class", "def", "defined?", "do", "else",
9 "elsif", "END", "end", "ensure", "false", "for", "if", "in", "module", "next", "not", "or",
10 "redo", "rescue", "retry", "return", "self", "super", "then", "true", "undef", "unless",
11 "until", "when", "while", "yield", "nil", "raise", "throw", "catch", "fail", "loop", "callcc",
12 "caller", "lambda", "proc", "public", "protected", "private", "require", "load",
13 "require_relative", "extend", "autoload", "__END__", "__FILE__", "__LINE__", "__dir__"
15 var indentWords = wordObj(["def", "class", "case", "for", "while", "do", "module", "then",
16 "catch", "loop", "proc", "begin"]);
17 var dedentWords = wordObj(["end", "until"]);
18 var matching = {"[": "]", "{": "}", "(": ")"};
21 function chain(newtok, stream, state) {
22 state.tokenize.push(newtok);
23 return newtok(stream, state);
26 function tokenBase(stream, state) {
28 if (stream.sol() && stream.match("=begin") && stream.eol()) {
29 state.tokenize.push(readBlockComment);
32 if (stream.eatSpace()) return null;
33 var ch = stream.next(), m;
34 if (ch == "`" || ch == "'" || ch == '"') {
35 return chain(readQuoted(ch, "string", ch == '"' || ch == "`"), stream, state);
36 } else if (ch == "/" && !stream.eol() && stream.peek() != " ") {
37 return chain(readQuoted(ch, "string-2", true), stream, state);
38 } else if (ch == "%") {
39 var style = "string", embed = true;
40 if (stream.eat("s")) style = "atom";
41 else if (stream.eat(/[WQ]/)) style = "string";
42 else if (stream.eat(/[r]/)) style = "string-2";
43 else if (stream.eat(/[wxq]/)) { style = "string"; embed = false; }
44 var delim = stream.eat(/[^\w\s]/);
45 if (!delim) return "operator";
46 if (matching.propertyIsEnumerable(delim)) delim = matching[delim];
47 return chain(readQuoted(delim, style, embed, true), stream, state);
48 } else if (ch == "#") {
51 } else if (ch == "<" && (m = stream.match(/^<-?[\`\"\']?([a-zA-Z_?]\w*)[\`\"\']?(?:;|$)/))) {
52 return chain(readHereDoc(m[1]), stream, state);
53 } else if (ch == "0") {
54 if (stream.eat("x")) stream.eatWhile(/[\da-fA-F]/);
55 else if (stream.eat("b")) stream.eatWhile(/[01]/);
56 else stream.eatWhile(/[0-7]/);
58 } else if (/\d/.test(ch)) {
59 stream.match(/^[\d_]*(?:\.[\d_]+)?(?:[eE][+\-]?[\d_]+)?/);
61 } else if (ch == "?") {
62 while (stream.match(/^\\[CM]-/)) {}
63 if (stream.eat("\\")) stream.eatWhile(/\w/);
66 } else if (ch == ":") {
67 if (stream.eat("'")) return chain(readQuoted("'", "atom", false), stream, state);
68 if (stream.eat('"')) return chain(readQuoted('"', "atom", true), stream, state);
70 // :> :>> :< :<< are valid symbols
71 if (stream.eat(/[\<\>]/)) {
76 // :+ :- :/ :* :| :& :! are valid symbols
77 if (stream.eat(/[\+\-\*\/\&\|\:\!]/)) {
81 // Symbols can't start by a digit
82 if (stream.eat(/[a-zA-Z$@_]/)) {
83 stream.eatWhile(/[\w]/);
84 // Only one ? ! = is allowed and only as the last character
85 stream.eat(/[\?\!\=]/);
89 } else if (ch == "@" && stream.match(/^@?[a-zA-Z_]/)) {
91 stream.eatWhile(/[\w]/);
93 } else if (ch == "$") {
94 if (stream.eat(/[a-zA-Z_]/)) {
95 stream.eatWhile(/[\w]/);
96 } else if (stream.eat(/\d/)) {
99 stream.next(); // Must be a special global like $: or $!
102 } else if (/[a-zA-Z_]/.test(ch)) {
103 stream.eatWhile(/[\w]/);
104 stream.eat(/[\?\!]/);
105 if (stream.eat(":")) return "atom";
107 } else if (ch == "|" && (state.varList || state.lastTok == "{" || state.lastTok == "do")) {
110 } else if (/[\(\)\[\]{}\\;]/.test(ch)) {
113 } else if (ch == "-" && stream.eat(">")) {
115 } else if (/[=+\-\/*:\.^%<>~|]/.test(ch)) {
116 stream.eatWhile(/[=+\-\/*:\.^%<>~|]/);
123 function tokenBaseUntilBrace() {
125 return function(stream, state) {
126 if (stream.peek() == "}") {
129 state.tokenize.pop();
130 return state.tokenize[state.tokenize.length-1](stream, state);
132 } else if (stream.peek() == "{") {
135 return tokenBase(stream, state);
138 function tokenBaseOnce() {
139 var alreadyCalled = false;
140 return function(stream, state) {
142 state.tokenize.pop();
143 return state.tokenize[state.tokenize.length-1](stream, state);
145 alreadyCalled = true;
146 return tokenBase(stream, state);
149 function readQuoted(quote, style, embed, unescaped) {
150 return function(stream, state) {
151 var escaped = false, ch;
153 if (state.context.type === 'read-quoted-paused') {
154 state.context = state.context.prev;
158 while ((ch = stream.next()) != null) {
159 if (ch == quote && (unescaped || !escaped)) {
160 state.tokenize.pop();
163 if (embed && ch == "#" && !escaped) {
164 if (stream.eat("{")) {
166 state.context = {prev: state.context, type: 'read-quoted-paused'};
168 state.tokenize.push(tokenBaseUntilBrace());
170 } else if (/[@\$]/.test(stream.peek())) {
171 state.tokenize.push(tokenBaseOnce());
175 escaped = !escaped && ch == "\\";
180 function readHereDoc(phrase) {
181 return function(stream, state) {
182 if (stream.match(phrase)) state.tokenize.pop();
183 else stream.skipToEnd();
187 function readBlockComment(stream, state) {
188 if (stream.sol() && stream.match("=end") && stream.eol())
189 state.tokenize.pop();
195 startState: function() {
196 return {tokenize: [tokenBase],
198 context: {type: "top", indented: -config.indentUnit},
199 continuedLine: false,
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"
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())
218 if (curPunc || (style && style != "comment")) state.lastTok = word || curPunc || style;
219 if (curPunc == "|") state.varList = !state.varList;
221 if (kwtype == "indent" || /[\(\[\{]/.test(curPunc))
222 state.context = {prev: state.context, type: curPunc || style, indented: state.indented};
223 else if ((kwtype == "dedent" || /[\)\]\}]/.test(curPunc)) && state.context.prev)
224 state.context = state.context.prev;
227 state.continuedLine = (curPunc == "\\" || style == "operator");
231 indent: function(state, textAfter) {
232 if (state.tokenize[state.tokenize.length-1] != tokenBase) return 0;
233 var firstChar = textAfter && textAfter.charAt(0);
234 var ct = state.context;
235 var closing = ct.type == matching[firstChar] ||
236 ct.type == "keyword" && /^(?:end|until|else|elsif|when|rescue)\b/.test(textAfter);
237 return ct.indented + (closing ? 0 : config.indentUnit) +
238 (state.continuedLine ? config.indentUnit : 0);
241 electricChars: "}de", // enD and rescuE
246 CodeMirror.defineMIME("text/x-ruby", "ruby");