| 1 |
// Tokenizer |
| 2 |
|
| 3 |
function kw(type) {return {type: type, style: "keyword"};} |
| 4 |
var A = kw("keyword a"), B = kw("keyword b"), C = kw("keyword c"); |
| 5 |
var operator = kw("operator"), atom = {type: "atom", style: "atom"}, attribute = {type:"attribute", style: "attribute"}; |
| 6 |
var type = kw("typedef"); |
| 7 |
var keywords = { |
| 8 |
"if": A, "while": A, "else": B, "do": B, "try": B, |
| 9 |
"return": C, "break": C, "continue": C, "new": C, "throw": C, |
| 10 |
"var": kw("var"), "inline":attribute, "static": attribute, "using":kw("import"), |
| 11 |
"public": attribute, "private": attribute, "cast": kw("cast"), "import": kw("import"), "macro": kw("macro"), |
| 12 |
"function": kw("function"), "catch": kw("catch"), "untyped": kw("untyped"), "callback": kw("cb"), |
| 13 |
"for": kw("for"), "switch": kw("switch"), "case": kw("case"), "default": kw("default"), |
| 14 |
"in": operator, "never": kw("property_access"), "trace":kw("trace"), |
| 15 |
"class": type, "abstract":type, "enum":type, "interface":type, "typedef":type, "extends":type, "implements":type, "dynamic":type, |
| 16 |
"true": atom, "false": atom, "null": atom |
| 17 |
}; |
| 18 |
|
| 19 |
var isOperatorChar = /[+\-*&%=<>!?|]/; |
| 20 |
|
| 21 |
function chain(stream, state, f) { |
| 22 |
state.tokenize = f; |
| 23 |
return f(stream, state); |
| 24 |
} |
| 25 |
|
| 26 |
function toUnescaped(stream, end) { |
| 27 |
var escaped = false, next; |
| 28 |
while ((next = stream.next()) != null) { |
| 29 |
if (next == end && !escaped) |
| 30 |
return true; |
| 31 |
escaped = !escaped && next == "\\"; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
// Used as scratch variables to communicate multiple values without |
| 36 |
// consing up tons of objects. |
| 37 |
var type, content; |
| 38 |
function ret(tp, style, cont) { |
| 39 |
type = tp; content = cont; |
| 40 |
return style; |
| 41 |
} |
| 42 |
|
| 43 |
function haxeTokenBase(stream, state) { |
| 44 |
var ch = stream.next(); |
| 45 |
if (ch == '"' || ch == "'") { |
| 46 |
return chain(stream, state, haxeTokenString(ch)); |
| 47 |
} else if (/[\[\]{}\(\),;\:\.]/.test(ch)) { |
| 48 |
return ret(ch); |
| 49 |
} else if (ch == "0" && stream.eat(/x/i)) { |
| 50 |
stream.eatWhile(/[\da-f]/i); |
| 51 |
return ret("number", "number"); |
| 52 |
} else if (/\d/.test(ch) || ch == "-" && stream.eat(/\d/)) { |
| 53 |
stream.match(/^\d*(?:\.\d*(?!\.))?(?:[eE][+\-]?\d+)?/); |
| 54 |
return ret("number", "number"); |
| 55 |
} else if (state.reAllowed && (ch == "~" && stream.eat(/\//))) { |
| 56 |
toUnescaped(stream, "/"); |
| 57 |
stream.eatWhile(/[gimsu]/); |
| 58 |
return ret("regexp", "string.special"); |
| 59 |
} else if (ch == "/") { |
| 60 |
if (stream.eat("*")) { |
| 61 |
return chain(stream, state, haxeTokenComment); |
| 62 |
} else if (stream.eat("/")) { |
| 63 |
stream.skipToEnd(); |
| 64 |
return ret("comment", "comment"); |
| 65 |
} else { |
| 66 |
stream.eatWhile(isOperatorChar); |
| 67 |
return ret("operator", null, stream.current()); |
| 68 |
} |
| 69 |
} else if (ch == "#") { |
| 70 |
stream.skipToEnd(); |
| 71 |
return ret("conditional", "meta"); |
| 72 |
} else if (ch == "@") { |
| 73 |
stream.eat(/:/); |
| 74 |
stream.eatWhile(/[\w_]/); |
| 75 |
return ret ("metadata", "meta"); |
| 76 |
} else if (isOperatorChar.test(ch)) { |
| 77 |
stream.eatWhile(isOperatorChar); |
| 78 |
return ret("operator", null, stream.current()); |
| 79 |
} else { |
| 80 |
var word; |
| 81 |
if(/[A-Z]/.test(ch)) { |
| 82 |
stream.eatWhile(/[\w_<>]/); |
| 83 |
word = stream.current(); |
| 84 |
return ret("type", "type", word); |
| 85 |
} else { |
| 86 |
stream.eatWhile(/[\w_]/); |
| 87 |
var word = stream.current(), known = keywords.propertyIsEnumerable(word) && keywords[word]; |
| 88 |
return (known && state.kwAllowed) ? ret(known.type, known.style, word) : |
| 89 |
ret("variable", "variable", word); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
function haxeTokenString(quote) { |
| 95 |
return function(stream, state) { |
| 96 |
if (toUnescaped(stream, quote)) |
| 97 |
state.tokenize = haxeTokenBase; |
| 98 |
return ret("string", "string"); |
| 99 |
}; |
| 100 |
} |
| 101 |
|
| 102 |
function haxeTokenComment(stream, state) { |
| 103 |
var maybeEnd = false, ch; |
| 104 |
while (ch = stream.next()) { |
| 105 |
if (ch == "/" && maybeEnd) { |
| 106 |
state.tokenize = haxeTokenBase; |
| 107 |
break; |
| 108 |
} |
| 109 |
maybeEnd = (ch == "*"); |
| 110 |
} |
| 111 |
return ret("comment", "comment"); |
| 112 |
} |
| 113 |
|
| 114 |
// Parser |
| 115 |
|
| 116 |
var atomicTypes = {"atom": true, "number": true, "variable": true, "string": true, "regexp": true}; |
| 117 |
|
| 118 |
function HaxeLexical(indented, column, type, align, prev, info) { |
| 119 |
this.indented = indented; |
| 120 |
this.column = column; |
| 121 |
this.type = type; |
| 122 |
this.prev = prev; |
| 123 |
this.info = info; |
| 124 |
if (align != null) this.align = align; |
| 125 |
} |
| 126 |
|
| 127 |
function inScope(state, varname) { |
| 128 |
for (var v = state.localVars; v; v = v.next) |
| 129 |
if (v.name == varname) return true; |
| 130 |
} |
| 131 |
|
| 132 |
function parseHaxe(state, style, type, content, stream) { |
| 133 |
var cc = state.cc; |
| 134 |
// Communicate our context to the combinators. |
| 135 |
// (Less wasteful than consing up a hundred closures on every call.) |
| 136 |
cx.state = state; cx.stream = stream; cx.marked = null, cx.cc = cc; |
| 137 |
|
| 138 |
if (!state.lexical.hasOwnProperty("align")) |
| 139 |
state.lexical.align = true; |
| 140 |
|
| 141 |
while(true) { |
| 142 |
var combinator = cc.length ? cc.pop() : statement; |
| 143 |
if (combinator(type, content)) { |
| 144 |
while(cc.length && cc[cc.length - 1].lex) |
| 145 |
cc.pop()(); |
| 146 |
if (cx.marked) return cx.marked; |
| 147 |
if (type == "variable" && inScope(state, content)) return "variableName.local"; |
| 148 |
if (type == "variable" && imported(state, content)) return "variableName.special"; |
| 149 |
return style; |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
function imported(state, typename) { |
| 155 |
if (/[a-z]/.test(typename.charAt(0))) |
| 156 |
return false; |
| 157 |
var len = state.importedtypes.length; |
| 158 |
for (var i = 0; i<len; i++) |
| 159 |
if(state.importedtypes[i]==typename) return true; |
| 160 |
} |
| 161 |
|
| 162 |
function registerimport(importname) { |
| 163 |
var state = cx.state; |
| 164 |
for (var t = state.importedtypes; t; t = t.next) |
| 165 |
if(t.name == importname) return; |
| 166 |
state.importedtypes = { name: importname, next: state.importedtypes }; |
| 167 |
} |
| 168 |
// Combinator utils |
| 169 |
|
| 170 |
var cx = {state: null, column: null, marked: null, cc: null}; |
| 171 |
function pass() { |
| 172 |
for (var i = arguments.length - 1; i >= 0; i--) cx.cc.push(arguments[i]); |
| 173 |
} |
| 174 |
function cont() { |
| 175 |
pass.apply(null, arguments); |
| 176 |
return true; |
| 177 |
} |
| 178 |
function inList(name, list) { |
| 179 |
for (var v = list; v; v = v.next) |
| 180 |
if (v.name == name) return true; |
| 181 |
return false; |
| 182 |
} |
| 183 |
function register(varname) { |
| 184 |
var state = cx.state; |
| 185 |
if (state.context) { |
| 186 |
cx.marked = "def"; |
| 187 |
if (inList(varname, state.localVars)) return; |
| 188 |
state.localVars = {name: varname, next: state.localVars}; |
| 189 |
} else if (state.globalVars) { |
| 190 |
if (inList(varname, state.globalVars)) return; |
| 191 |
state.globalVars = {name: varname, next: state.globalVars}; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
// Combinators |
| 196 |
|
| 197 |
var defaultVars = {name: "this", next: null}; |
| 198 |
function pushcontext() { |
| 199 |
if (!cx.state.context) cx.state.localVars = defaultVars; |
| 200 |
cx.state.context = {prev: cx.state.context, vars: cx.state.localVars}; |
| 201 |
} |
| 202 |
function popcontext() { |
| 203 |
cx.state.localVars = cx.state.context.vars; |
| 204 |
cx.state.context = cx.state.context.prev; |
| 205 |
} |
| 206 |
popcontext.lex = true; |
| 207 |
function pushlex(type, info) { |
| 208 |
var result = function() { |
| 209 |
var state = cx.state; |
| 210 |
state.lexical = new HaxeLexical(state.indented, cx.stream.column(), type, null, state.lexical, info); |
| 211 |
}; |
| 212 |
result.lex = true; |
| 213 |
return result; |
| 214 |
} |
| 215 |
function poplex() { |
| 216 |
var state = cx.state; |
| 217 |
if (state.lexical.prev) { |
| 218 |
if (state.lexical.type == ")") |
| 219 |
state.indented = state.lexical.indented; |
| 220 |
state.lexical = state.lexical.prev; |
| 221 |
} |
| 222 |
} |
| 223 |
poplex.lex = true; |
| 224 |
|
| 225 |
function expect(wanted) { |
| 226 |
function f(type) { |
| 227 |
if (type == wanted) return cont(); |
| 228 |
else if (wanted == ";") return pass(); |
| 229 |
else return cont(f); |
| 230 |
} |
| 231 |
return f; |
| 232 |
} |
| 233 |
|
| 234 |
function statement(type) { |
| 235 |
if (type == "@") return cont(metadef); |
| 236 |
if (type == "var") return cont(pushlex("vardef"), vardef1, expect(";"), poplex); |
| 237 |
if (type == "keyword a") return cont(pushlex("form"), expression, statement, poplex); |
| 238 |
if (type == "keyword b") return cont(pushlex("form"), statement, poplex); |
| 239 |
if (type == "{") return cont(pushlex("}"), pushcontext, block, poplex, popcontext); |
| 240 |
if (type == ";") return cont(); |
| 241 |
if (type == "attribute") return cont(maybeattribute); |
| 242 |
if (type == "function") return cont(functiondef); |
| 243 |
if (type == "for") return cont(pushlex("form"), expect("("), pushlex(")"), forspec1, expect(")"), |
| 244 |
poplex, statement, poplex); |
| 245 |
if (type == "variable") return cont(pushlex("stat"), maybelabel); |
| 246 |
if (type == "switch") return cont(pushlex("form"), expression, pushlex("}", "switch"), expect("{"), |
| 247 |
block, poplex, poplex); |
| 248 |
if (type == "case") return cont(expression, expect(":")); |
| 249 |
if (type == "default") return cont(expect(":")); |
| 250 |
if (type == "catch") return cont(pushlex("form"), pushcontext, expect("("), funarg, expect(")"), |
| 251 |
statement, poplex, popcontext); |
| 252 |
if (type == "import") return cont(importdef, expect(";")); |
| 253 |
if (type == "typedef") return cont(typedef); |
| 254 |
return pass(pushlex("stat"), expression, expect(";"), poplex); |
| 255 |
} |
| 256 |
function expression(type) { |
| 257 |
if (atomicTypes.hasOwnProperty(type)) return cont(maybeoperator); |
| 258 |
if (type == "type" ) return cont(maybeoperator); |
| 259 |
if (type == "function") return cont(functiondef); |
| 260 |
if (type == "keyword c") return cont(maybeexpression); |
| 261 |
if (type == "(") return cont(pushlex(")"), maybeexpression, expect(")"), poplex, maybeoperator); |
| 262 |
if (type == "operator") return cont(expression); |
| 263 |
if (type == "[") return cont(pushlex("]"), commasep(maybeexpression, "]"), poplex, maybeoperator); |
| 264 |
if (type == "{") return cont(pushlex("}"), commasep(objprop, "}"), poplex, maybeoperator); |
| 265 |
return cont(); |
| 266 |
} |
| 267 |
function maybeexpression(type) { |
| 268 |
if (type.match(/[;\}\)\],]/)) return pass(); |
| 269 |
return pass(expression); |
| 270 |
} |
| 271 |
|
| 272 |
function maybeoperator(type, value) { |
| 273 |
if (type == "operator" && /\+\+|--/.test(value)) return cont(maybeoperator); |
| 274 |
if (type == "operator" || type == ":") return cont(expression); |
| 275 |
if (type == ";") return; |
| 276 |
if (type == "(") return cont(pushlex(")"), commasep(expression, ")"), poplex, maybeoperator); |
| 277 |
if (type == ".") return cont(property, maybeoperator); |
| 278 |
if (type == "[") return cont(pushlex("]"), expression, expect("]"), poplex, maybeoperator); |
| 279 |
} |
| 280 |
|
| 281 |
function maybeattribute(type) { |
| 282 |
if (type == "attribute") return cont(maybeattribute); |
| 283 |
if (type == "function") return cont(functiondef); |
| 284 |
if (type == "var") return cont(vardef1); |
| 285 |
} |
| 286 |
|
| 287 |
function metadef(type) { |
| 288 |
if(type == ":") return cont(metadef); |
| 289 |
if(type == "variable") return cont(metadef); |
| 290 |
if(type == "(") return cont(pushlex(")"), commasep(metaargs, ")"), poplex, statement); |
| 291 |
} |
| 292 |
function metaargs(type) { |
| 293 |
if(type == "variable") return cont(); |
| 294 |
} |
| 295 |
|
| 296 |
function importdef (type, value) { |
| 297 |
if(type == "variable" && /[A-Z]/.test(value.charAt(0))) { registerimport(value); return cont(); } |
| 298 |
else if(type == "variable" || type == "property" || type == "." || value == "*") return cont(importdef); |
| 299 |
} |
| 300 |
|
| 301 |
function typedef (type, value) |
| 302 |
{ |
| 303 |
if(type == "variable" && /[A-Z]/.test(value.charAt(0))) { registerimport(value); return cont(); } |
| 304 |
else if (type == "type" && /[A-Z]/.test(value.charAt(0))) { return cont(); } |
| 305 |
} |
| 306 |
|
| 307 |
function maybelabel(type) { |
| 308 |
if (type == ":") return cont(poplex, statement); |
| 309 |
return pass(maybeoperator, expect(";"), poplex); |
| 310 |
} |
| 311 |
function property(type) { |
| 312 |
if (type == "variable") {cx.marked = "property"; return cont();} |
| 313 |
} |
| 314 |
function objprop(type) { |
| 315 |
if (type == "variable") cx.marked = "property"; |
| 316 |
if (atomicTypes.hasOwnProperty(type)) return cont(expect(":"), expression); |
| 317 |
} |
| 318 |
function commasep(what, end) { |
| 319 |
function proceed(type) { |
| 320 |
if (type == ",") return cont(what, proceed); |
| 321 |
if (type == end) return cont(); |
| 322 |
return cont(expect(end)); |
| 323 |
} |
| 324 |
return function(type) { |
| 325 |
if (type == end) return cont(); |
| 326 |
else return pass(what, proceed); |
| 327 |
}; |
| 328 |
} |
| 329 |
function block(type) { |
| 330 |
if (type == "}") return cont(); |
| 331 |
return pass(statement, block); |
| 332 |
} |
| 333 |
function vardef1(type, value) { |
| 334 |
if (type == "variable"){register(value); return cont(typeuse, vardef2);} |
| 335 |
return cont(); |
| 336 |
} |
| 337 |
function vardef2(type, value) { |
| 338 |
if (value == "=") return cont(expression, vardef2); |
| 339 |
if (type == ",") return cont(vardef1); |
| 340 |
} |
| 341 |
function forspec1(type, value) { |
| 342 |
if (type == "variable") { |
| 343 |
register(value); |
| 344 |
return cont(forin, expression) |
| 345 |
} else { |
| 346 |
return pass() |
| 347 |
} |
| 348 |
} |
| 349 |
function forin(_type, value) { |
| 350 |
if (value == "in") return cont(); |
| 351 |
} |
| 352 |
function functiondef(type, value) { |
| 353 |
//function names starting with upper-case letters are recognised as types, so cludging them together here. |
| 354 |
if (type == "variable" || type == "type") {register(value); return cont(functiondef);} |
| 355 |
if (value == "new") return cont(functiondef); |
| 356 |
if (type == "(") return cont(pushlex(")"), pushcontext, commasep(funarg, ")"), poplex, typeuse, statement, popcontext); |
| 357 |
} |
| 358 |
function typeuse(type) { |
| 359 |
if(type == ":") return cont(typestring); |
| 360 |
} |
| 361 |
function typestring(type) { |
| 362 |
if(type == "type") return cont(); |
| 363 |
if(type == "variable") return cont(); |
| 364 |
if(type == "{") return cont(pushlex("}"), commasep(typeprop, "}"), poplex); |
| 365 |
} |
| 366 |
function typeprop(type) { |
| 367 |
if(type == "variable") return cont(typeuse); |
| 368 |
} |
| 369 |
function funarg(type, value) { |
| 370 |
if (type == "variable") {register(value); return cont(typeuse);} |
| 371 |
} |
| 372 |
|
| 373 |
// Interface |
| 374 |
export const haxe = { |
| 375 |
name: "haxe", |
| 376 |
startState: function(indentUnit) { |
| 377 |
var defaulttypes = ["Int", "Float", "String", "Void", "Std", "Bool", "Dynamic", "Array"]; |
| 378 |
var state = { |
| 379 |
tokenize: haxeTokenBase, |
| 380 |
reAllowed: true, |
| 381 |
kwAllowed: true, |
| 382 |
cc: [], |
| 383 |
lexical: new HaxeLexical(-indentUnit, 0, "block", false), |
| 384 |
importedtypes: defaulttypes, |
| 385 |
context: null, |
| 386 |
indented: 0 |
| 387 |
}; |
| 388 |
return state; |
| 389 |
}, |
| 390 |
|
| 391 |
token: function(stream, state) { |
| 392 |
if (stream.sol()) { |
| 393 |
if (!state.lexical.hasOwnProperty("align")) |
| 394 |
state.lexical.align = false; |
| 395 |
state.indented = stream.indentation(); |
| 396 |
} |
| 397 |
if (stream.eatSpace()) return null; |
| 398 |
var style = state.tokenize(stream, state); |
| 399 |
if (type == "comment") return style; |
| 400 |
state.reAllowed = !!(type == "operator" || type == "keyword c" || type.match(/^[\[{}\(,;:]$/)); |
| 401 |
state.kwAllowed = type != '.'; |
| 402 |
return parseHaxe(state, style, type, content, stream); |
| 403 |
}, |
| 404 |
|
| 405 |
indent: function(state, textAfter, cx) { |
| 406 |
if (state.tokenize != haxeTokenBase) return 0; |
| 407 |
var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical; |
| 408 |
if (lexical.type == "stat" && firstChar == "}") lexical = lexical.prev; |
| 409 |
var type = lexical.type, closing = firstChar == type; |
| 410 |
if (type == "vardef") return lexical.indented + 4; |
| 411 |
else if (type == "form" && firstChar == "{") return lexical.indented; |
| 412 |
else if (type == "stat" || type == "form") return lexical.indented + cx.unit; |
| 413 |
else if (lexical.info == "switch" && !closing) |
| 414 |
return lexical.indented + (/^(?:case|default)\b/.test(textAfter) ? cx.unit : 2 * cx.unit); |
| 415 |
else if (lexical.align) return lexical.column + (closing ? 0 : 1); |
| 416 |
else return lexical.indented + (closing ? 0 : cx.unit); |
| 417 |
}, |
| 418 |
|
| 419 |
languageData: { |
| 420 |
indentOnInput: /^\s*[{}]$/, |
| 421 |
commentTokens: {line: "//", block: {open: "/*", close: "*/"}} |
| 422 |
} |
| 423 |
}; |
| 424 |
|
| 425 |
export const hxml = { |
| 426 |
name: "hxml", |
| 427 |
startState: function () { |
| 428 |
return { |
| 429 |
define: false, |
| 430 |
inString: false |
| 431 |
}; |
| 432 |
}, |
| 433 |
token: function (stream, state) { |
| 434 |
var ch = stream.peek(); |
| 435 |
var sol = stream.sol(); |
| 436 |
|
| 437 |
///* comments */ |
| 438 |
if (ch == "#") { |
| 439 |
stream.skipToEnd(); |
| 440 |
return "comment"; |
| 441 |
} |
| 442 |
if (sol && ch == "-") { |
| 443 |
var style = "variable-2"; |
| 444 |
|
| 445 |
stream.eat(/-/); |
| 446 |
|
| 447 |
if (stream.peek() == "-") { |
| 448 |
stream.eat(/-/); |
| 449 |
style = "keyword a"; |
| 450 |
} |
| 451 |
|
| 452 |
if (stream.peek() == "D") { |
| 453 |
stream.eat(/[D]/); |
| 454 |
style = "keyword c"; |
| 455 |
state.define = true; |
| 456 |
} |
| 457 |
|
| 458 |
stream.eatWhile(/[A-Z]/i); |
| 459 |
return style; |
| 460 |
} |
| 461 |
|
| 462 |
var ch = stream.peek(); |
| 463 |
|
| 464 |
if (state.inString == false && ch == "'") { |
| 465 |
state.inString = true; |
| 466 |
stream.next(); |
| 467 |
} |
| 468 |
|
| 469 |
if (state.inString == true) { |
| 470 |
if (stream.skipTo("'")) { |
| 471 |
|
| 472 |
} else { |
| 473 |
stream.skipToEnd(); |
| 474 |
} |
| 475 |
|
| 476 |
if (stream.peek() == "'") { |
| 477 |
stream.next(); |
| 478 |
state.inString = false; |
| 479 |
} |
| 480 |
|
| 481 |
return "string"; |
| 482 |
} |
| 483 |
|
| 484 |
stream.next(); |
| 485 |
return null; |
| 486 |
}, |
| 487 |
languageData: { |
| 488 |
commentTokens: {line: "#"} |
| 489 |
} |
| 490 |
} |