| 1 |
var ERRORCLASS = "error"; |
| 2 |
|
| 3 |
function wordRegexp(words) { |
| 4 |
return new RegExp("^((" + words.join(")|(") + "))\\b"); |
| 5 |
} |
| 6 |
|
| 7 |
var operators = /^(?:->|=>|\+[+=]?|-[\-=]?|\*[\*=]?|\/[\/=]?|[=!]=|<[><]?=?|>>?=?|%=?|&=?|\|=?|\^=?|\~|!|\?|(or|and|\|\||&&|\?)=)/; |
| 8 |
var delimiters = /^(?:[()\[\]{},:`=;]|\.\.?\.?)/; |
| 9 |
var identifiers = /^[_A-Za-z$][_A-Za-z$0-9]*/; |
| 10 |
var atProp = /^@[_A-Za-z$][_A-Za-z$0-9]*/; |
| 11 |
|
| 12 |
var wordOperators = wordRegexp(["and", "or", "not", |
| 13 |
"is", "isnt", "in", |
| 14 |
"instanceof", "typeof"]); |
| 15 |
var indentKeywords = ["for", "while", "loop", "if", "unless", "else", |
| 16 |
"switch", "try", "catch", "finally", "class"]; |
| 17 |
var commonKeywords = ["break", "by", "continue", "debugger", "delete", |
| 18 |
"do", "in", "of", "new", "return", "then", |
| 19 |
"this", "@", "throw", "when", "until", "extends"]; |
| 20 |
|
| 21 |
var keywords = wordRegexp(indentKeywords.concat(commonKeywords)); |
| 22 |
|
| 23 |
indentKeywords = wordRegexp(indentKeywords); |
| 24 |
|
| 25 |
|
| 26 |
var stringPrefixes = /^('{3}|\"{3}|['\"])/; |
| 27 |
var regexPrefixes = /^(\/{3}|\/)/; |
| 28 |
var commonConstants = ["Infinity", "NaN", "undefined", "null", "true", "false", "on", "off", "yes", "no"]; |
| 29 |
var constants = wordRegexp(commonConstants); |
| 30 |
|
| 31 |
// Tokenizers |
| 32 |
function tokenBase(stream, state) { |
| 33 |
// Handle scope changes |
| 34 |
if (stream.sol()) { |
| 35 |
if (state.scope.align === null) state.scope.align = false; |
| 36 |
var scopeOffset = state.scope.offset; |
| 37 |
if (stream.eatSpace()) { |
| 38 |
var lineOffset = stream.indentation(); |
| 39 |
if (lineOffset > scopeOffset && state.scope.type == "coffee") { |
| 40 |
return "indent"; |
| 41 |
} else if (lineOffset < scopeOffset) { |
| 42 |
return "dedent"; |
| 43 |
} |
| 44 |
return null; |
| 45 |
} else { |
| 46 |
if (scopeOffset > 0) { |
| 47 |
dedent(stream, state); |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
if (stream.eatSpace()) { |
| 52 |
return null; |
| 53 |
} |
| 54 |
|
| 55 |
var ch = stream.peek(); |
| 56 |
|
| 57 |
// Handle docco title comment (single line) |
| 58 |
if (stream.match("####")) { |
| 59 |
stream.skipToEnd(); |
| 60 |
return "comment"; |
| 61 |
} |
| 62 |
|
| 63 |
// Handle multi line comments |
| 64 |
if (stream.match("###")) { |
| 65 |
state.tokenize = longComment; |
| 66 |
return state.tokenize(stream, state); |
| 67 |
} |
| 68 |
|
| 69 |
// Single line comment |
| 70 |
if (ch === "#") { |
| 71 |
stream.skipToEnd(); |
| 72 |
return "comment"; |
| 73 |
} |
| 74 |
|
| 75 |
// Handle number literals |
| 76 |
if (stream.match(/^-?[0-9\.]/, false)) { |
| 77 |
var floatLiteral = false; |
| 78 |
// Floats |
| 79 |
if (stream.match(/^-?\d*\.\d+(e[\+\-]?\d+)?/i)) { |
| 80 |
floatLiteral = true; |
| 81 |
} |
| 82 |
if (stream.match(/^-?\d+\.\d*/)) { |
| 83 |
floatLiteral = true; |
| 84 |
} |
| 85 |
if (stream.match(/^-?\.\d+/)) { |
| 86 |
floatLiteral = true; |
| 87 |
} |
| 88 |
|
| 89 |
if (floatLiteral) { |
| 90 |
// prevent from getting extra . on 1.. |
| 91 |
if (stream.peek() == "."){ |
| 92 |
stream.backUp(1); |
| 93 |
} |
| 94 |
return "number"; |
| 95 |
} |
| 96 |
// Integers |
| 97 |
var intLiteral = false; |
| 98 |
// Hex |
| 99 |
if (stream.match(/^-?0x[0-9a-f]+/i)) { |
| 100 |
intLiteral = true; |
| 101 |
} |
| 102 |
// Decimal |
| 103 |
if (stream.match(/^-?[1-9]\d*(e[\+\-]?\d+)?/)) { |
| 104 |
intLiteral = true; |
| 105 |
} |
| 106 |
// Zero by itself with no other piece of number. |
| 107 |
if (stream.match(/^-?0(?![\dx])/i)) { |
| 108 |
intLiteral = true; |
| 109 |
} |
| 110 |
if (intLiteral) { |
| 111 |
return "number"; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
// Handle strings |
| 116 |
if (stream.match(stringPrefixes)) { |
| 117 |
state.tokenize = tokenFactory(stream.current(), false, "string"); |
| 118 |
return state.tokenize(stream, state); |
| 119 |
} |
| 120 |
// Handle regex literals |
| 121 |
if (stream.match(regexPrefixes)) { |
| 122 |
if (stream.current() != "/" || stream.match(/^.*\//, false)) { // prevent highlight of division |
| 123 |
state.tokenize = tokenFactory(stream.current(), true, "string.special"); |
| 124 |
return state.tokenize(stream, state); |
| 125 |
} else { |
| 126 |
stream.backUp(1); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
// Handle operators and delimiters |
| 133 |
if (stream.match(operators) || stream.match(wordOperators)) { |
| 134 |
return "operator"; |
| 135 |
} |
| 136 |
if (stream.match(delimiters)) { |
| 137 |
return "punctuation"; |
| 138 |
} |
| 139 |
|
| 140 |
if (stream.match(constants)) { |
| 141 |
return "atom"; |
| 142 |
} |
| 143 |
|
| 144 |
if (stream.match(atProp) || state.prop && stream.match(identifiers)) { |
| 145 |
return "property"; |
| 146 |
} |
| 147 |
|
| 148 |
if (stream.match(keywords)) { |
| 149 |
return "keyword"; |
| 150 |
} |
| 151 |
|
| 152 |
if (stream.match(identifiers)) { |
| 153 |
return "variable"; |
| 154 |
} |
| 155 |
|
| 156 |
// Handle non-detected items |
| 157 |
stream.next(); |
| 158 |
return ERRORCLASS; |
| 159 |
} |
| 160 |
|
| 161 |
function tokenFactory(delimiter, singleline, outclass) { |
| 162 |
return function(stream, state) { |
| 163 |
while (!stream.eol()) { |
| 164 |
stream.eatWhile(/[^'"\/\\]/); |
| 165 |
if (stream.eat("\\")) { |
| 166 |
stream.next(); |
| 167 |
if (singleline && stream.eol()) { |
| 168 |
return outclass; |
| 169 |
} |
| 170 |
} else if (stream.match(delimiter)) { |
| 171 |
state.tokenize = tokenBase; |
| 172 |
return outclass; |
| 173 |
} else { |
| 174 |
stream.eat(/['"\/]/); |
| 175 |
} |
| 176 |
} |
| 177 |
if (singleline) { |
| 178 |
state.tokenize = tokenBase; |
| 179 |
} |
| 180 |
return outclass; |
| 181 |
}; |
| 182 |
} |
| 183 |
|
| 184 |
function longComment(stream, state) { |
| 185 |
while (!stream.eol()) { |
| 186 |
stream.eatWhile(/[^#]/); |
| 187 |
if (stream.match("###")) { |
| 188 |
state.tokenize = tokenBase; |
| 189 |
break; |
| 190 |
} |
| 191 |
stream.eatWhile("#"); |
| 192 |
} |
| 193 |
return "comment"; |
| 194 |
} |
| 195 |
|
| 196 |
function indent(stream, state, type = "coffee") { |
| 197 |
var offset = 0, align = false, alignOffset = null; |
| 198 |
for (var scope = state.scope; scope; scope = scope.prev) { |
| 199 |
if (scope.type === "coffee" || scope.type == "}") { |
| 200 |
offset = scope.offset + stream.indentUnit; |
| 201 |
break; |
| 202 |
} |
| 203 |
} |
| 204 |
if (type !== "coffee") { |
| 205 |
align = null; |
| 206 |
alignOffset = stream.column() + stream.current().length; |
| 207 |
} else if (state.scope.align) { |
| 208 |
state.scope.align = false; |
| 209 |
} |
| 210 |
state.scope = { |
| 211 |
offset: offset, |
| 212 |
type: type, |
| 213 |
prev: state.scope, |
| 214 |
align: align, |
| 215 |
alignOffset: alignOffset |
| 216 |
}; |
| 217 |
} |
| 218 |
|
| 219 |
function dedent(stream, state) { |
| 220 |
if (!state.scope.prev) return; |
| 221 |
if (state.scope.type === "coffee") { |
| 222 |
var _indent = stream.indentation(); |
| 223 |
var matched = false; |
| 224 |
for (var scope = state.scope; scope; scope = scope.prev) { |
| 225 |
if (_indent === scope.offset) { |
| 226 |
matched = true; |
| 227 |
break; |
| 228 |
} |
| 229 |
} |
| 230 |
if (!matched) { |
| 231 |
return true; |
| 232 |
} |
| 233 |
while (state.scope.prev && state.scope.offset !== _indent) { |
| 234 |
state.scope = state.scope.prev; |
| 235 |
} |
| 236 |
return false; |
| 237 |
} else { |
| 238 |
state.scope = state.scope.prev; |
| 239 |
return false; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
function tokenLexer(stream, state) { |
| 244 |
var style = state.tokenize(stream, state); |
| 245 |
var current = stream.current(); |
| 246 |
|
| 247 |
// Handle scope changes. |
| 248 |
if (current === "return") { |
| 249 |
state.dedent = true; |
| 250 |
} |
| 251 |
if (((current === "->" || current === "=>") && stream.eol()) |
| 252 |
|| style === "indent") { |
| 253 |
indent(stream, state); |
| 254 |
} |
| 255 |
var delimiter_index = "[({".indexOf(current); |
| 256 |
if (delimiter_index !== -1) { |
| 257 |
indent(stream, state, "])}".slice(delimiter_index, delimiter_index+1)); |
| 258 |
} |
| 259 |
if (indentKeywords.exec(current)){ |
| 260 |
indent(stream, state); |
| 261 |
} |
| 262 |
if (current == "then"){ |
| 263 |
dedent(stream, state); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
if (style === "dedent") { |
| 268 |
if (dedent(stream, state)) { |
| 269 |
return ERRORCLASS; |
| 270 |
} |
| 271 |
} |
| 272 |
delimiter_index = "])}".indexOf(current); |
| 273 |
if (delimiter_index !== -1) { |
| 274 |
while (state.scope.type == "coffee" && state.scope.prev) |
| 275 |
state.scope = state.scope.prev; |
| 276 |
if (state.scope.type == current) |
| 277 |
state.scope = state.scope.prev; |
| 278 |
} |
| 279 |
if (state.dedent && stream.eol()) { |
| 280 |
if (state.scope.type == "coffee" && state.scope.prev) |
| 281 |
state.scope = state.scope.prev; |
| 282 |
state.dedent = false; |
| 283 |
} |
| 284 |
|
| 285 |
return style == "indent" || style == "dedent" ? null : style; |
| 286 |
} |
| 287 |
|
| 288 |
export const coffeeScript = { |
| 289 |
name: "coffeescript", |
| 290 |
startState: function() { |
| 291 |
return { |
| 292 |
tokenize: tokenBase, |
| 293 |
scope: {offset: 0, type:"coffee", prev: null, align: false}, |
| 294 |
prop: false, |
| 295 |
dedent: 0 |
| 296 |
}; |
| 297 |
}, |
| 298 |
|
| 299 |
token: function(stream, state) { |
| 300 |
var fillAlign = state.scope.align === null && state.scope; |
| 301 |
if (fillAlign && stream.sol()) fillAlign.align = false; |
| 302 |
|
| 303 |
var style = tokenLexer(stream, state); |
| 304 |
if (style && style != "comment") { |
| 305 |
if (fillAlign) fillAlign.align = true; |
| 306 |
state.prop = style == "punctuation" && stream.current() == "." |
| 307 |
} |
| 308 |
|
| 309 |
return style; |
| 310 |
}, |
| 311 |
|
| 312 |
indent: function(state, text) { |
| 313 |
if (state.tokenize != tokenBase) return 0; |
| 314 |
var scope = state.scope; |
| 315 |
var closer = text && "])}".indexOf(text.charAt(0)) > -1; |
| 316 |
if (closer) while (scope.type == "coffee" && scope.prev) scope = scope.prev; |
| 317 |
var closes = closer && scope.type === text.charAt(0); |
| 318 |
if (scope.align) |
| 319 |
return scope.alignOffset - (closes ? 1 : 0); |
| 320 |
else |
| 321 |
return (closes ? scope.prev : scope).offset; |
| 322 |
}, |
| 323 |
|
| 324 |
languageData: { |
| 325 |
commentTokens: {line: "#"} |
| 326 |
} |
| 327 |
}; |