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