Fix: merge conflict
[myslice.git] / to-be-integrated / third-party / codemirror-3.15 / mode / ruby / ruby.js
1 CodeMirror.defineMode("ruby", function(config) {
2   function wordObj(words) {
3     var o = {};
4     for (var i = 0, e = words.length; i < e; ++i) o[words[i]] = true;
5     return o;
6   }
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__"
14   ]);
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 = {"[": "]", "{": "}", "(": ")"};
19   var curPunc;
20
21   function chain(newtok, stream, state) {
22     state.tokenize.push(newtok);
23     return newtok(stream, state);
24   }
25
26   function tokenBase(stream, state) {
27     curPunc = null;
28     if (stream.sol() && stream.match("=begin") && stream.eol()) {
29       state.tokenize.push(readBlockComment);
30       return "comment";
31     }
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 == "#") {
49       stream.skipToEnd();
50       return "comment";
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]/);
57       return "number";
58     } else if (/\d/.test(ch)) {
59       stream.match(/^[\d_]*(?:\.[\d_]+)?(?:[eE][+\-]?[\d_]+)?/);
60       return "number";
61     } else if (ch == "?") {
62       while (stream.match(/^\\[CM]-/)) {}
63       if (stream.eat("\\")) stream.eatWhile(/\w/);
64       else stream.next();
65       return "string";
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);
69
70       // :> :>> :< :<< are valid symbols
71       if (stream.eat(/[\<\>]/)) {
72         stream.eat(/[\<\>]/);
73         return "atom";
74       }
75
76       // :+ :- :/ :* :| :& :! are valid symbols
77       if (stream.eat(/[\+\-\*\/\&\|\:\!]/)) {
78         return "atom";
79       }
80
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(/[\?\!\=]/);
86         return "atom";
87       }
88       return "operator";
89     } else if (ch == "@" && stream.match(/^@?[a-zA-Z_]/)) {
90       stream.eat("@");
91       stream.eatWhile(/[\w]/);
92       return "variable-2";
93     } else if (ch == "$") {
94       if (stream.eat(/[a-zA-Z_]/)) {
95         stream.eatWhile(/[\w]/);
96       } else if (stream.eat(/\d/)) {
97         stream.eat(/\d/);
98       } else {
99         stream.next(); // Must be a special global like $: or $!
100       }
101       return "variable-3";
102     } else if (/[a-zA-Z_]/.test(ch)) {
103       stream.eatWhile(/[\w]/);
104       stream.eat(/[\?\!]/);
105       if (stream.eat(":")) return "atom";
106       return "ident";
107     } else if (ch == "|" && (state.varList || state.lastTok == "{" || state.lastTok == "do")) {
108       curPunc = "|";
109       return null;
110     } else if (/[\(\)\[\]{}\\;]/.test(ch)) {
111       curPunc = ch;
112       return null;
113     } else if (ch == "-" && stream.eat(">")) {
114       return "arrow";
115     } else if (/[=+\-\/*:\.^%<>~|]/.test(ch)) {
116       stream.eatWhile(/[=+\-\/*:\.^%<>~|]/);
117       return "operator";
118     } else {
119       return null;
120     }
121   }
122
123   function tokenBaseUntilBrace() {
124     var depth = 1;
125     return function(stream, state) {
126       if (stream.peek() == "}") {
127         depth--;
128         if (depth == 0) {
129           state.tokenize.pop();
130           return state.tokenize[state.tokenize.length-1](stream, state);
131         }
132       } else if (stream.peek() == "{") {
133         depth++;
134       }
135       return tokenBase(stream, state);
136     };
137   }
138   function tokenBaseOnce() {
139     var alreadyCalled = false;
140     return function(stream, state) {
141       if (alreadyCalled) {
142         state.tokenize.pop();
143         return state.tokenize[state.tokenize.length-1](stream, state);
144       }
145       alreadyCalled = true;
146       return tokenBase(stream, state);
147     };
148   }
149   function readQuoted(quote, style, embed, unescaped) {
150     return function(stream, state) {
151       var escaped = false, ch;
152
153       if (state.context.type === 'read-quoted-paused') {
154         state.context = state.context.prev;
155         stream.eat("}");
156       }
157
158       while ((ch = stream.next()) != null) {
159         if (ch == quote && (unescaped || !escaped)) {
160           state.tokenize.pop();
161           break;
162         }
163         if (embed && ch == "#" && !escaped) {
164           if (stream.eat("{")) {
165             if (quote == "}") {
166               state.context = {prev: state.context, type: 'read-quoted-paused'};
167             }
168             state.tokenize.push(tokenBaseUntilBrace());
169             break;
170           } else if (/[@\$]/.test(stream.peek())) {
171             state.tokenize.push(tokenBaseOnce());
172             break;
173           }
174         }
175         escaped = !escaped && ch == "\\";
176       }
177       return style;
178     };
179   }
180   function readHereDoc(phrase) {
181     return function(stream, state) {
182       if (stream.match(phrase)) state.tokenize.pop();
183       else stream.skipToEnd();
184       return "string";
185     };
186   }
187   function readBlockComment(stream, state) {
188     if (stream.sol() && stream.match("=end") && stream.eol())
189       state.tokenize.pop();
190     stream.skipToEnd();
191     return "comment";
192   }
193
194   return {
195     startState: function() {
196       return {tokenize: [tokenBase],
197               indented: 0,
198               context: {type: "top", indented: -config.indentUnit},
199               continuedLine: false,
200               lastTok: null,
201               varList: false};
202     },
203
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"
212           : "variable";
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())
216           kwtype = "indent";
217       }
218       if (curPunc || (style && style != "comment")) state.lastTok = word || curPunc || style;
219       if (curPunc == "|") state.varList = !state.varList;
220
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;
225
226       if (stream.eol())
227         state.continuedLine = (curPunc == "\\" || style == "operator");
228       return style;
229     },
230
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);
239     },
240
241     electricChars: "}de", // enD and rescuE
242     lineComment: "#"
243   };
244 });
245
246 CodeMirror.defineMIME("text/x-ruby", "ruby");
247