javascript.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. CodeMirror.defineMode("javascript", function(config, parserConfig) {
  13. var indentUnit = config.indentUnit;
  14. var statementIndent = parserConfig.statementIndent;
  15. var jsonldMode = parserConfig.jsonld;
  16. var jsonMode = parserConfig.json || jsonldMode;
  17. var isTS = parserConfig.typescript;
  18. var wordRE = parserConfig.wordCharacters || /[\w$\xa1-\uffff]/;
  19. // Tokenizer
  20. var keywords = function(){
  21. function kw(type) {return {type: type, style: "keyword"};}
  22. var A = kw("keyword a"), B = kw("keyword b"), C = kw("keyword c"), D = kw("keyword d");
  23. var operator = kw("operator"), atom = {type: "atom", style: "atom"};
  24. return {
  25. "if": kw("if"), "while": A, "with": A, "else": B, "do": B, "try": B, "finally": B,
  26. "return": D, "break": D, "continue": D, "new": kw("new"), "delete": C, "void": C, "throw": C,
  27. "debugger": kw("debugger"), "var": kw("var"), "const": kw("var"), "let": kw("var"),
  28. "function": kw("function"), "catch": kw("catch"),
  29. "for": kw("for"), "switch": kw("switch"), "case": kw("case"), "default": kw("default"),
  30. "in": operator, "typeof": operator, "instanceof": operator,
  31. "true": atom, "false": atom, "null": atom, "undefined": atom, "NaN": atom, "Infinity": atom,
  32. "this": kw("this"), "class": kw("class"), "super": kw("atom"),
  33. "yield": C, "export": kw("export"), "import": kw("import"), "extends": C,
  34. "await": C
  35. };
  36. }();
  37. var isOperatorChar = /[+\-*&%=<>!?|~^@]/;
  38. var isJsonldKeyword = /^@(context|id|value|language|type|container|list|set|reverse|index|base|vocab|graph)"/;
  39. function readRegexp(stream) {
  40. var escaped = false, next, inSet = false;
  41. while ((next = stream.next()) != null) {
  42. if (!escaped) {
  43. if (next == "/" && !inSet) return;
  44. if (next == "[") inSet = true;
  45. else if (inSet && next == "]") inSet = false;
  46. }
  47. escaped = !escaped && next == "\\";
  48. }
  49. }
  50. // Used as scratch variables to communicate multiple values without
  51. // consing up tons of objects.
  52. var type, content;
  53. function ret(tp, style, cont) {
  54. type = tp; content = cont;
  55. return style;
  56. }
  57. function tokenBase(stream, state) {
  58. var ch = stream.next();
  59. if (ch == '"' || ch == "'") {
  60. state.tokenize = tokenString(ch);
  61. return state.tokenize(stream, state);
  62. } else if (ch == "." && stream.match(/^\d[\d_]*(?:[eE][+\-]?[\d_]+)?/)) {
  63. return ret("number", "number");
  64. } else if (ch == "." && stream.match("..")) {
  65. return ret("spread", "meta");
  66. } else if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
  67. return ret(ch);
  68. } else if (ch == "=" && stream.eat(">")) {
  69. return ret("=>", "operator");
  70. } else if (ch == "0" && stream.match(/^(?:x[\dA-Fa-f_]+|o[0-7_]+|b[01_]+)n?/)) {
  71. return ret("number", "number");
  72. } else if (/\d/.test(ch)) {
  73. stream.match(/^[\d_]*(?:n|(?:\.[\d_]*)?(?:[eE][+\-]?[\d_]+)?)?/);
  74. return ret("number", "number");
  75. } else if (ch == "/") {
  76. if (stream.eat("*")) {
  77. state.tokenize = tokenComment;
  78. return tokenComment(stream, state);
  79. } else if (stream.eat("/")) {
  80. stream.skipToEnd();
  81. return ret("comment", "comment");
  82. } else if (expressionAllowed(stream, state, 1)) {
  83. readRegexp(stream);
  84. stream.match(/^\b(([gimyus])(?![gimyus]*\2))+\b/);
  85. return ret("regexp", "string-2");
  86. } else {
  87. stream.eat("=");
  88. return ret("operator", "operator", stream.current());
  89. }
  90. } else if (ch == "`") {
  91. state.tokenize = tokenQuasi;
  92. return tokenQuasi(stream, state);
  93. } else if (ch == "#") {
  94. stream.skipToEnd();
  95. return ret("error", "error");
  96. } else if (isOperatorChar.test(ch)) {
  97. if (ch != ">" || !state.lexical || state.lexical.type != ">") {
  98. if (stream.eat("=")) {
  99. if (ch == "!" || ch == "=") stream.eat("=")
  100. } else if (/[<>*+\-]/.test(ch)) {
  101. stream.eat(ch)
  102. if (ch == ">") stream.eat(ch)
  103. }
  104. }
  105. return ret("operator", "operator", stream.current());
  106. } else if (wordRE.test(ch)) {
  107. stream.eatWhile(wordRE);
  108. var word = stream.current()
  109. if (state.lastType != ".") {
  110. if (keywords.propertyIsEnumerable(word)) {
  111. var kw = keywords[word]
  112. return ret(kw.type, kw.style, word)
  113. }
  114. if (word == "async" && stream.match(/^(\s|\/\*.*?\*\/)*[\[\(\w]/, false))
  115. return ret("async", "keyword", word)
  116. }
  117. return ret("variable", "variable", word)
  118. }
  119. }
  120. function tokenString(quote) {
  121. return function(stream, state) {
  122. var escaped = false, next;
  123. if (jsonldMode && stream.peek() == "@" && stream.match(isJsonldKeyword)){
  124. state.tokenize = tokenBase;
  125. return ret("jsonld-keyword", "meta");
  126. }
  127. while ((next = stream.next()) != null) {
  128. if (next == quote && !escaped) break;
  129. escaped = !escaped && next == "\\";
  130. }
  131. if (!escaped) state.tokenize = tokenBase;
  132. return ret("string", "string");
  133. };
  134. }
  135. function tokenComment(stream, state) {
  136. var maybeEnd = false, ch;
  137. while (ch = stream.next()) {
  138. if (ch == "/" && maybeEnd) {
  139. state.tokenize = tokenBase;
  140. break;
  141. }
  142. maybeEnd = (ch == "*");
  143. }
  144. return ret("comment", "comment");
  145. }
  146. function tokenQuasi(stream, state) {
  147. var escaped = false, next;
  148. while ((next = stream.next()) != null) {
  149. if (!escaped && (next == "`" || next == "$" && stream.eat("{"))) {
  150. state.tokenize = tokenBase;
  151. break;
  152. }
  153. escaped = !escaped && next == "\\";
  154. }
  155. return ret("quasi", "string-2", stream.current());
  156. }
  157. var brackets = "([{}])";
  158. // This is a crude lookahead trick to try and notice that we're
  159. // parsing the argument patterns for a fat-arrow function before we
  160. // actually hit the arrow token. It only works if the arrow is on
  161. // the same line as the arguments and there's no strange noise
  162. // (comments) in between. Fallback is to only notice when we hit the
  163. // arrow, and not declare the arguments as locals for the arrow
  164. // body.
  165. function findFatArrow(stream, state) {
  166. if (state.fatArrowAt) state.fatArrowAt = null;
  167. var arrow = stream.string.indexOf("=>", stream.start);
  168. if (arrow < 0) return;
  169. if (isTS) { // Try to skip TypeScript return type declarations after the arguments
  170. var m = /:\s*(?:\w+(?:<[^>]*>|\[\])?|\{[^}]*\})\s*$/.exec(stream.string.slice(stream.start, arrow))
  171. if (m) arrow = m.index
  172. }
  173. var depth = 0, sawSomething = false;
  174. for (var pos = arrow - 1; pos >= 0; --pos) {
  175. var ch = stream.string.charAt(pos);
  176. var bracket = brackets.indexOf(ch);
  177. if (bracket >= 0 && bracket < 3) {
  178. if (!depth) { ++pos; break; }
  179. if (--depth == 0) { if (ch == "(") sawSomething = true; break; }
  180. } else if (bracket >= 3 && bracket < 6) {
  181. ++depth;
  182. } else if (wordRE.test(ch)) {
  183. sawSomething = true;
  184. } else if (/["'\/`]/.test(ch)) {
  185. for (;; --pos) {
  186. if (pos == 0) return
  187. var next = stream.string.charAt(pos - 1)
  188. if (next == ch && stream.string.charAt(pos - 2) != "\\") { pos--; break }
  189. }
  190. } else if (sawSomething && !depth) {
  191. ++pos;
  192. break;
  193. }
  194. }
  195. if (sawSomething && !depth) state.fatArrowAt = pos;
  196. }
  197. // Parser
  198. var atomicTypes = {"atom": true, "number": true, "variable": true, "string": true, "regexp": true, "this": true, "jsonld-keyword": true};
  199. function JSLexical(indented, column, type, align, prev, info) {
  200. this.indented = indented;
  201. this.column = column;
  202. this.type = type;
  203. this.prev = prev;
  204. this.info = info;
  205. if (align != null) this.align = align;
  206. }
  207. function inScope(state, varname) {
  208. for (var v = state.localVars; v; v = v.next)
  209. if (v.name == varname) return true;
  210. for (var cx = state.context; cx; cx = cx.prev) {
  211. for (var v = cx.vars; v; v = v.next)
  212. if (v.name == varname) return true;
  213. }
  214. }
  215. function parseJS(state, style, type, content, stream) {
  216. var cc = state.cc;
  217. // Communicate our context to the combinators.
  218. // (Less wasteful than consing up a hundred closures on every call.)
  219. cx.state = state; cx.stream = stream; cx.marked = null, cx.cc = cc; cx.style = style;
  220. if (!state.lexical.hasOwnProperty("align"))
  221. state.lexical.align = true;
  222. while(true) {
  223. var combinator = cc.length ? cc.pop() : jsonMode ? expression : statement;
  224. if (combinator(type, content)) {
  225. while(cc.length && cc[cc.length - 1].lex)
  226. cc.pop()();
  227. if (cx.marked) return cx.marked;
  228. if (type == "variable" && inScope(state, content)) return "variable-2";
  229. return style;
  230. }
  231. }
  232. }
  233. // Combinator utils
  234. var cx = {state: null, column: null, marked: null, cc: null};
  235. function pass() {
  236. for (var i = arguments.length - 1; i >= 0; i--) cx.cc.push(arguments[i]);
  237. }
  238. function cont() {
  239. pass.apply(null, arguments);
  240. return true;
  241. }
  242. function inList(name, list) {
  243. for (var v = list; v; v = v.next) if (v.name == name) return true
  244. return false;
  245. }
  246. function register(varname) {
  247. var state = cx.state;
  248. cx.marked = "def";
  249. if (state.context) {
  250. if (state.lexical.info == "var" && state.context && state.context.block) {
  251. // FIXME function decls are also not block scoped
  252. var newContext = registerVarScoped(varname, state.context)
  253. if (newContext != null) {
  254. state.context = newContext
  255. return
  256. }
  257. } else if (!inList(varname, state.localVars)) {
  258. state.localVars = new Var(varname, state.localVars)
  259. return
  260. }
  261. }
  262. // Fall through means this is global
  263. if (parserConfig.globalVars && !inList(varname, state.globalVars))
  264. state.globalVars = new Var(varname, state.globalVars)
  265. }
  266. function registerVarScoped(varname, context) {
  267. if (!context) {
  268. return null
  269. } else if (context.block) {
  270. var inner = registerVarScoped(varname, context.prev)
  271. if (!inner) return null
  272. if (inner == context.prev) return context
  273. return new Context(inner, context.vars, true)
  274. } else if (inList(varname, context.vars)) {
  275. return context
  276. } else {
  277. return new Context(context.prev, new Var(varname, context.vars), false)
  278. }
  279. }
  280. function isModifier(name) {
  281. return name == "public" || name == "private" || name == "protected" || name == "abstract" || name == "readonly"
  282. }
  283. // Combinators
  284. function Context(prev, vars, block) { this.prev = prev; this.vars = vars; this.block = block }
  285. function Var(name, next) { this.name = name; this.next = next }
  286. var defaultVars = new Var("this", new Var("arguments", null))
  287. function pushcontext() {
  288. cx.state.context = new Context(cx.state.context, cx.state.localVars, false)
  289. cx.state.localVars = defaultVars
  290. }
  291. function pushblockcontext() {
  292. cx.state.context = new Context(cx.state.context, cx.state.localVars, true)
  293. cx.state.localVars = null
  294. }
  295. function popcontext() {
  296. cx.state.localVars = cx.state.context.vars
  297. cx.state.context = cx.state.context.prev
  298. }
  299. popcontext.lex = true
  300. function pushlex(type, info) {
  301. var result = function() {
  302. var state = cx.state, indent = state.indented;
  303. if (state.lexical.type == "stat") indent = state.lexical.indented;
  304. else for (var outer = state.lexical; outer && outer.type == ")" && outer.align; outer = outer.prev)
  305. indent = outer.indented;
  306. state.lexical = new JSLexical(indent, cx.stream.column(), type, null, state.lexical, info);
  307. };
  308. result.lex = true;
  309. return result;
  310. }
  311. function poplex() {
  312. var state = cx.state;
  313. if (state.lexical.prev) {
  314. if (state.lexical.type == ")")
  315. state.indented = state.lexical.indented;
  316. state.lexical = state.lexical.prev;
  317. }
  318. }
  319. poplex.lex = true;
  320. function expect(wanted) {
  321. function exp(type) {
  322. if (type == wanted) return cont();
  323. else if (wanted == ";" || type == "}" || type == ")" || type == "]") return pass();
  324. else return cont(exp);
  325. };
  326. return exp;
  327. }
  328. function statement(type, value) {
  329. if (type == "var") return cont(pushlex("vardef", value), vardef, expect(";"), poplex);
  330. if (type == "keyword a") return cont(pushlex("form"), parenExpr, statement, poplex);
  331. if (type == "keyword b") return cont(pushlex("form"), statement, poplex);
  332. if (type == "keyword d") return cx.stream.match(/^\s*$/, false) ? cont() : cont(pushlex("stat"), maybeexpression, expect(";"), poplex);
  333. if (type == "debugger") return cont(expect(";"));
  334. if (type == "{") return cont(pushlex("}"), pushblockcontext, block, poplex, popcontext);
  335. if (type == ";") return cont();
  336. if (type == "if") {
  337. if (cx.state.lexical.info == "else" && cx.state.cc[cx.state.cc.length - 1] == poplex)
  338. cx.state.cc.pop()();
  339. return cont(pushlex("form"), parenExpr, statement, poplex, maybeelse);
  340. }
  341. if (type == "function") return cont(functiondef);
  342. if (type == "for") return cont(pushlex("form"), forspec, statement, poplex);
  343. if (type == "class" || (isTS && value == "interface")) {
  344. cx.marked = "keyword"
  345. return cont(pushlex("form", type == "class" ? type : value), className, poplex)
  346. }
  347. if (type == "variable") {
  348. if (isTS && value == "declare") {
  349. cx.marked = "keyword"
  350. return cont(statement)
  351. } else if (isTS && (value == "module" || value == "enum" || value == "type") && cx.stream.match(/^\s*\w/, false)) {
  352. cx.marked = "keyword"
  353. if (value == "enum") return cont(enumdef);
  354. else if (value == "type") return cont(typename, expect("operator"), typeexpr, expect(";"));
  355. else return cont(pushlex("form"), pattern, expect("{"), pushlex("}"), block, poplex, poplex)
  356. } else if (isTS && value == "namespace") {
  357. cx.marked = "keyword"
  358. return cont(pushlex("form"), expression, statement, poplex)
  359. } else if (isTS && value == "abstract") {
  360. cx.marked = "keyword"
  361. return cont(statement)
  362. } else {
  363. return cont(pushlex("stat"), maybelabel);
  364. }
  365. }
  366. if (type == "switch") return cont(pushlex("form"), parenExpr, expect("{"), pushlex("}", "switch"), pushblockcontext,
  367. block, poplex, poplex, popcontext);
  368. if (type == "case") return cont(expression, expect(":"));
  369. if (type == "default") return cont(expect(":"));
  370. if (type == "catch") return cont(pushlex("form"), pushcontext, maybeCatchBinding, statement, poplex, popcontext);
  371. if (type == "export") return cont(pushlex("stat"), afterExport, poplex);
  372. if (type == "import") return cont(pushlex("stat"), afterImport, poplex);
  373. if (type == "async") return cont(statement)
  374. if (value == "@") return cont(expression, statement)
  375. return pass(pushlex("stat"), expression, expect(";"), poplex);
  376. }
  377. function maybeCatchBinding(type) {
  378. if (type == "(") return cont(funarg, expect(")"))
  379. }
  380. function expression(type, value) {
  381. return expressionInner(type, value, false);
  382. }
  383. function expressionNoComma(type, value) {
  384. return expressionInner(type, value, true);
  385. }
  386. function parenExpr(type) {
  387. if (type != "(") return pass()
  388. return cont(pushlex(")"), expression, expect(")"), poplex)
  389. }
  390. function expressionInner(type, value, noComma) {
  391. if (cx.state.fatArrowAt == cx.stream.start) {
  392. var body = noComma ? arrowBodyNoComma : arrowBody;
  393. if (type == "(") return cont(pushcontext, pushlex(")"), commasep(funarg, ")"), poplex, expect("=>"), body, popcontext);
  394. else if (type == "variable") return pass(pushcontext, pattern, expect("=>"), body, popcontext);
  395. }
  396. var maybeop = noComma ? maybeoperatorNoComma : maybeoperatorComma;
  397. if (atomicTypes.hasOwnProperty(type)) return cont(maybeop);
  398. if (type == "function") return cont(functiondef, maybeop);
  399. if (type == "class" || (isTS && value == "interface")) { cx.marked = "keyword"; return cont(pushlex("form"), classExpression, poplex); }
  400. if (type == "keyword c" || type == "async") return cont(noComma ? expressionNoComma : expression);
  401. if (type == "(") return cont(pushlex(")"), maybeexpression, expect(")"), poplex, maybeop);
  402. if (type == "operator" || type == "spread") return cont(noComma ? expressionNoComma : expression);
  403. if (type == "[") return cont(pushlex("]"), arrayLiteral, poplex, maybeop);
  404. if (type == "{") return contCommasep(objprop, "}", null, maybeop);
  405. if (type == "quasi") return pass(quasi, maybeop);
  406. if (type == "new") return cont(maybeTarget(noComma));
  407. if (type == "import") return cont(expression);
  408. return cont();
  409. }
  410. function maybeexpression(type) {
  411. if (type.match(/[;\}\)\],]/)) return pass();
  412. return pass(expression);
  413. }
  414. function maybeoperatorComma(type, value) {
  415. if (type == ",") return cont(expression);
  416. return maybeoperatorNoComma(type, value, false);
  417. }
  418. function maybeoperatorNoComma(type, value, noComma) {
  419. var me = noComma == false ? maybeoperatorComma : maybeoperatorNoComma;
  420. var expr = noComma == false ? expression : expressionNoComma;
  421. if (type == "=>") return cont(pushcontext, noComma ? arrowBodyNoComma : arrowBody, popcontext);
  422. if (type == "operator") {
  423. if (/\+\+|--/.test(value) || isTS && value == "!") return cont(me);
  424. if (isTS && value == "<" && cx.stream.match(/^([^>]|<.*?>)*>\s*\(/, false))
  425. return cont(pushlex(">"), commasep(typeexpr, ">"), poplex, me);
  426. if (value == "?") return cont(expression, expect(":"), expr);
  427. return cont(expr);
  428. }
  429. if (type == "quasi") { return pass(quasi, me); }
  430. if (type == ";") return;
  431. if (type == "(") return contCommasep(expressionNoComma, ")", "call", me);
  432. if (type == ".") return cont(property, me);
  433. if (type == "[") return cont(pushlex("]"), maybeexpression, expect("]"), poplex, me);
  434. if (isTS && value == "as") { cx.marked = "keyword"; return cont(typeexpr, me) }
  435. if (type == "regexp") {
  436. cx.state.lastType = cx.marked = "operator"
  437. cx.stream.backUp(cx.stream.pos - cx.stream.start - 1)
  438. return cont(expr)
  439. }
  440. }
  441. function quasi(type, value) {
  442. if (type != "quasi") return pass();
  443. if (value.slice(value.length - 2) != "${") return cont(quasi);
  444. return cont(expression, continueQuasi);
  445. }
  446. function continueQuasi(type) {
  447. if (type == "}") {
  448. cx.marked = "string-2";
  449. cx.state.tokenize = tokenQuasi;
  450. return cont(quasi);
  451. }
  452. }
  453. function arrowBody(type) {
  454. findFatArrow(cx.stream, cx.state);
  455. return pass(type == "{" ? statement : expression);
  456. }
  457. function arrowBodyNoComma(type) {
  458. findFatArrow(cx.stream, cx.state);
  459. return pass(type == "{" ? statement : expressionNoComma);
  460. }
  461. function maybeTarget(noComma) {
  462. return function(type) {
  463. if (type == ".") return cont(noComma ? targetNoComma : target);
  464. else if (type == "variable" && isTS) return cont(maybeTypeArgs, noComma ? maybeoperatorNoComma : maybeoperatorComma)
  465. else return pass(noComma ? expressionNoComma : expression);
  466. };
  467. }
  468. function target(_, value) {
  469. if (value == "target") { cx.marked = "keyword"; return cont(maybeoperatorComma); }
  470. }
  471. function targetNoComma(_, value) {
  472. if (value == "target") { cx.marked = "keyword"; return cont(maybeoperatorNoComma); }
  473. }
  474. function maybelabel(type) {
  475. if (type == ":") return cont(poplex, statement);
  476. return pass(maybeoperatorComma, expect(";"), poplex);
  477. }
  478. function property(type) {
  479. if (type == "variable") {cx.marked = "property"; return cont();}
  480. }
  481. function objprop(type, value) {
  482. if (type == "async") {
  483. cx.marked = "property";
  484. return cont(objprop);
  485. } else if (type == "variable" || cx.style == "keyword") {
  486. cx.marked = "property";
  487. if (value == "get" || value == "set") return cont(getterSetter);
  488. var m // Work around fat-arrow-detection complication for detecting typescript typed arrow params
  489. if (isTS && cx.state.fatArrowAt == cx.stream.start && (m = cx.stream.match(/^\s*:\s*/, false)))
  490. cx.state.fatArrowAt = cx.stream.pos + m[0].length
  491. return cont(afterprop);
  492. } else if (type == "number" || type == "string") {
  493. cx.marked = jsonldMode ? "property" : (cx.style + " property");
  494. return cont(afterprop);
  495. } else if (type == "jsonld-keyword") {
  496. return cont(afterprop);
  497. } else if (isTS && isModifier(value)) {
  498. cx.marked = "keyword"
  499. return cont(objprop)
  500. } else if (type == "[") {
  501. return cont(expression, maybetype, expect("]"), afterprop);
  502. } else if (type == "spread") {
  503. return cont(expressionNoComma, afterprop);
  504. } else if (value == "*") {
  505. cx.marked = "keyword";
  506. return cont(objprop);
  507. } else if (type == ":") {
  508. return pass(afterprop)
  509. }
  510. }
  511. function getterSetter(type) {
  512. if (type != "variable") return pass(afterprop);
  513. cx.marked = "property";
  514. return cont(functiondef);
  515. }
  516. function afterprop(type) {
  517. if (type == ":") return cont(expressionNoComma);
  518. if (type == "(") return pass(functiondef);
  519. }
  520. function commasep(what, end, sep) {
  521. function proceed(type, value) {
  522. if (sep ? sep.indexOf(type) > -1 : type == ",") {
  523. var lex = cx.state.lexical;
  524. if (lex.info == "call") lex.pos = (lex.pos || 0) + 1;
  525. return cont(function(type, value) {
  526. if (type == end || value == end) return pass()
  527. return pass(what)
  528. }, proceed);
  529. }
  530. if (type == end || value == end) return cont();
  531. if (sep && sep.indexOf(";") > -1) return pass(what)
  532. return cont(expect(end));
  533. }
  534. return function(type, value) {
  535. if (type == end || value == end) return cont();
  536. return pass(what, proceed);
  537. };
  538. }
  539. function contCommasep(what, end, info) {
  540. for (var i = 3; i < arguments.length; i++)
  541. cx.cc.push(arguments[i]);
  542. return cont(pushlex(end, info), commasep(what, end), poplex);
  543. }
  544. function block(type) {
  545. if (type == "}") return cont();
  546. return pass(statement, block);
  547. }
  548. function maybetype(type, value) {
  549. if (isTS) {
  550. if (type == ":") return cont(typeexpr);
  551. if (value == "?") return cont(maybetype);
  552. }
  553. }
  554. function maybetypeOrIn(type, value) {
  555. if (isTS && (type == ":" || value == "in")) return cont(typeexpr)
  556. }
  557. function mayberettype(type) {
  558. if (isTS && type == ":") {
  559. if (cx.stream.match(/^\s*\w+\s+is\b/, false)) return cont(expression, isKW, typeexpr)
  560. else return cont(typeexpr)
  561. }
  562. }
  563. function isKW(_, value) {
  564. if (value == "is") {
  565. cx.marked = "keyword"
  566. return cont()
  567. }
  568. }
  569. function typeexpr(type, value) {
  570. if (value == "keyof" || value == "typeof" || value == "infer") {
  571. cx.marked = "keyword"
  572. return cont(value == "typeof" ? expressionNoComma : typeexpr)
  573. }
  574. if (type == "variable" || value == "void") {
  575. cx.marked = "type"
  576. return cont(afterType)
  577. }
  578. if (value == "|" || value == "&") return cont(typeexpr)
  579. if (type == "string" || type == "number" || type == "atom") return cont(afterType);
  580. if (type == "[") return cont(pushlex("]"), commasep(typeexpr, "]", ","), poplex, afterType)
  581. if (type == "{") return cont(pushlex("}"), commasep(typeprop, "}", ",;"), poplex, afterType)
  582. if (type == "(") return cont(commasep(typearg, ")"), maybeReturnType, afterType)
  583. if (type == "<") return cont(commasep(typeexpr, ">"), typeexpr)
  584. }
  585. function maybeReturnType(type) {
  586. if (type == "=>") return cont(typeexpr)
  587. }
  588. function typeprop(type, value) {
  589. if (type == "variable" || cx.style == "keyword") {
  590. cx.marked = "property"
  591. return cont(typeprop)
  592. } else if (value == "?" || type == "number" || type == "string") {
  593. return cont(typeprop)
  594. } else if (type == ":") {
  595. return cont(typeexpr)
  596. } else if (type == "[") {
  597. return cont(expect("variable"), maybetypeOrIn, expect("]"), typeprop)
  598. } else if (type == "(") {
  599. return pass(functiondecl, typeprop)
  600. }
  601. }
  602. function typearg(type, value) {
  603. if (type == "variable" && cx.stream.match(/^\s*[?:]/, false) || value == "?") return cont(typearg)
  604. if (type == ":") return cont(typeexpr)
  605. if (type == "spread") return cont(typearg)
  606. return pass(typeexpr)
  607. }
  608. function afterType(type, value) {
  609. if (value == "<") return cont(pushlex(">"), commasep(typeexpr, ">"), poplex, afterType)
  610. if (value == "|" || type == "." || value == "&") return cont(typeexpr)
  611. if (type == "[") return cont(typeexpr, expect("]"), afterType)
  612. if (value == "extends" || value == "implements") { cx.marked = "keyword"; return cont(typeexpr) }
  613. if (value == "?") return cont(typeexpr, expect(":"), typeexpr)
  614. }
  615. function maybeTypeArgs(_, value) {
  616. if (value == "<") return cont(pushlex(">"), commasep(typeexpr, ">"), poplex, afterType)
  617. }
  618. function typeparam() {
  619. return pass(typeexpr, maybeTypeDefault)
  620. }
  621. function maybeTypeDefault(_, value) {
  622. if (value == "=") return cont(typeexpr)
  623. }
  624. function vardef(_, value) {
  625. if (value == "enum") {cx.marked = "keyword"; return cont(enumdef)}
  626. return pass(pattern, maybetype, maybeAssign, vardefCont);
  627. }
  628. function pattern(type, value) {
  629. if (isTS && isModifier(value)) { cx.marked = "keyword"; return cont(pattern) }
  630. if (type == "variable") { register(value); return cont(); }
  631. if (type == "spread") return cont(pattern);
  632. if (type == "[") return contCommasep(eltpattern, "]");
  633. if (type == "{") return contCommasep(proppattern, "}");
  634. }
  635. function proppattern(type, value) {
  636. if (type == "variable" && !cx.stream.match(/^\s*:/, false)) {
  637. register(value);
  638. return cont(maybeAssign);
  639. }
  640. if (type == "variable") cx.marked = "property";
  641. if (type == "spread") return cont(pattern);
  642. if (type == "}") return pass();
  643. if (type == "[") return cont(expression, expect(']'), expect(':'), proppattern);
  644. return cont(expect(":"), pattern, maybeAssign);
  645. }
  646. function eltpattern() {
  647. return pass(pattern, maybeAssign)
  648. }
  649. function maybeAssign(_type, value) {
  650. if (value == "=") return cont(expressionNoComma);
  651. }
  652. function vardefCont(type) {
  653. if (type == ",") return cont(vardef);
  654. }
  655. function maybeelse(type, value) {
  656. if (type == "keyword b" && value == "else") return cont(pushlex("form", "else"), statement, poplex);
  657. }
  658. function forspec(type, value) {
  659. if (value == "await") return cont(forspec);
  660. if (type == "(") return cont(pushlex(")"), forspec1, poplex);
  661. }
  662. function forspec1(type) {
  663. if (type == "var") return cont(vardef, forspec2);
  664. if (type == "variable") return cont(forspec2);
  665. return pass(forspec2)
  666. }
  667. function forspec2(type, value) {
  668. if (type == ")") return cont()
  669. if (type == ";") return cont(forspec2)
  670. if (value == "in" || value == "of") { cx.marked = "keyword"; return cont(expression, forspec2) }
  671. return pass(expression, forspec2)
  672. }
  673. function functiondef(type, value) {
  674. if (value == "*") {cx.marked = "keyword"; return cont(functiondef);}
  675. if (type == "variable") {register(value); return cont(functiondef);}
  676. if (type == "(") return cont(pushcontext, pushlex(")"), commasep(funarg, ")"), poplex, mayberettype, statement, popcontext);
  677. if (isTS && value == "<") return cont(pushlex(">"), commasep(typeparam, ">"), poplex, functiondef)
  678. }
  679. function functiondecl(type, value) {
  680. if (value == "*") {cx.marked = "keyword"; return cont(functiondecl);}
  681. if (type == "variable") {register(value); return cont(functiondecl);}
  682. if (type == "(") return cont(pushcontext, pushlex(")"), commasep(funarg, ")"), poplex, mayberettype, popcontext);
  683. if (isTS && value == "<") return cont(pushlex(">"), commasep(typeparam, ">"), poplex, functiondecl)
  684. }
  685. function typename(type, value) {
  686. if (type == "keyword" || type == "variable") {
  687. cx.marked = "type"
  688. return cont(typename)
  689. } else if (value == "<") {
  690. return cont(pushlex(">"), commasep(typeparam, ">"), poplex)
  691. }
  692. }
  693. function funarg(type, value) {
  694. if (value == "@") cont(expression, funarg)
  695. if (type == "spread") return cont(funarg);
  696. if (isTS && isModifier(value)) { cx.marked = "keyword"; return cont(funarg); }
  697. if (isTS && type == "this") return cont(maybetype, maybeAssign)
  698. return pass(pattern, maybetype, maybeAssign);
  699. }
  700. function classExpression(type, value) {
  701. // Class expressions may have an optional name.
  702. if (type == "variable") return className(type, value);
  703. return classNameAfter(type, value);
  704. }
  705. function className(type, value) {
  706. if (type == "variable") {register(value); return cont(classNameAfter);}
  707. }
  708. function classNameAfter(type, value) {
  709. if (value == "<") return cont(pushlex(">"), commasep(typeparam, ">"), poplex, classNameAfter)
  710. if (value == "extends" || value == "implements" || (isTS && type == ",")) {
  711. if (value == "implements") cx.marked = "keyword";
  712. return cont(isTS ? typeexpr : expression, classNameAfter);
  713. }
  714. if (type == "{") return cont(pushlex("}"), classBody, poplex);
  715. }
  716. function classBody(type, value) {
  717. if (type == "async" ||
  718. (type == "variable" &&
  719. (value == "static" || value == "get" || value == "set" || (isTS && isModifier(value))) &&
  720. cx.stream.match(/^\s+[\w$\xa1-\uffff]/, false))) {
  721. cx.marked = "keyword";
  722. return cont(classBody);
  723. }
  724. if (type == "variable" || cx.style == "keyword") {
  725. cx.marked = "property";
  726. return cont(isTS ? classfield : functiondef, classBody);
  727. }
  728. if (type == "number" || type == "string") return cont(isTS ? classfield : functiondef, classBody);
  729. if (type == "[")
  730. return cont(expression, maybetype, expect("]"), isTS ? classfield : functiondef, classBody)
  731. if (value == "*") {
  732. cx.marked = "keyword";
  733. return cont(classBody);
  734. }
  735. if (isTS && type == "(") return pass(functiondecl, classBody)
  736. if (type == ";" || type == ",") return cont(classBody);
  737. if (type == "}") return cont();
  738. if (value == "@") return cont(expression, classBody)
  739. }
  740. function classfield(type, value) {
  741. if (value == "?") return cont(classfield)
  742. if (type == ":") return cont(typeexpr, maybeAssign)
  743. if (value == "=") return cont(expressionNoComma)
  744. var context = cx.state.lexical.prev, isInterface = context && context.info == "interface"
  745. return pass(isInterface ? functiondecl : functiondef)
  746. }
  747. function afterExport(type, value) {
  748. if (value == "*") { cx.marked = "keyword"; return cont(maybeFrom, expect(";")); }
  749. if (value == "default") { cx.marked = "keyword"; return cont(expression, expect(";")); }
  750. if (type == "{") return cont(commasep(exportField, "}"), maybeFrom, expect(";"));
  751. return pass(statement);
  752. }
  753. function exportField(type, value) {
  754. if (value == "as") { cx.marked = "keyword"; return cont(expect("variable")); }
  755. if (type == "variable") return pass(expressionNoComma, exportField);
  756. }
  757. function afterImport(type) {
  758. if (type == "string") return cont();
  759. if (type == "(") return pass(expression);
  760. return pass(importSpec, maybeMoreImports, maybeFrom);
  761. }
  762. function importSpec(type, value) {
  763. if (type == "{") return contCommasep(importSpec, "}");
  764. if (type == "variable") register(value);
  765. if (value == "*") cx.marked = "keyword";
  766. return cont(maybeAs);
  767. }
  768. function maybeMoreImports(type) {
  769. if (type == ",") return cont(importSpec, maybeMoreImports)
  770. }
  771. function maybeAs(_type, value) {
  772. if (value == "as") { cx.marked = "keyword"; return cont(importSpec); }
  773. }
  774. function maybeFrom(_type, value) {
  775. if (value == "from") { cx.marked = "keyword"; return cont(expression); }
  776. }
  777. function arrayLiteral(type) {
  778. if (type == "]") return cont();
  779. return pass(commasep(expressionNoComma, "]"));
  780. }
  781. function enumdef() {
  782. return pass(pushlex("form"), pattern, expect("{"), pushlex("}"), commasep(enummember, "}"), poplex, poplex)
  783. }
  784. function enummember() {
  785. return pass(pattern, maybeAssign);
  786. }
  787. function isContinuedStatement(state, textAfter) {
  788. return state.lastType == "operator" || state.lastType == "," ||
  789. isOperatorChar.test(textAfter.charAt(0)) ||
  790. /[,.]/.test(textAfter.charAt(0));
  791. }
  792. function expressionAllowed(stream, state, backUp) {
  793. return state.tokenize == tokenBase &&
  794. /^(?:operator|sof|keyword [bcd]|case|new|export|default|spread|[\[{}\(,;:]|=>)$/.test(state.lastType) ||
  795. (state.lastType == "quasi" && /\{\s*$/.test(stream.string.slice(0, stream.pos - (backUp || 0))))
  796. }
  797. // Interface
  798. return {
  799. startState: function(basecolumn) {
  800. var state = {
  801. tokenize: tokenBase,
  802. lastType: "sof",
  803. cc: [],
  804. lexical: new JSLexical((basecolumn || 0) - indentUnit, 0, "block", false),
  805. localVars: parserConfig.localVars,
  806. context: parserConfig.localVars && new Context(null, null, false),
  807. indented: basecolumn || 0
  808. };
  809. if (parserConfig.globalVars && typeof parserConfig.globalVars == "object")
  810. state.globalVars = parserConfig.globalVars;
  811. return state;
  812. },
  813. token: function(stream, state) {
  814. if (stream.sol()) {
  815. if (!state.lexical.hasOwnProperty("align"))
  816. state.lexical.align = false;
  817. state.indented = stream.indentation();
  818. findFatArrow(stream, state);
  819. }
  820. if (state.tokenize != tokenComment && stream.eatSpace()) return null;
  821. var style = state.tokenize(stream, state);
  822. if (type == "comment") return style;
  823. state.lastType = type == "operator" && (content == "++" || content == "--") ? "incdec" : type;
  824. return parseJS(state, style, type, content, stream);
  825. },
  826. indent: function(state, textAfter) {
  827. if (state.tokenize == tokenComment) return CodeMirror.Pass;
  828. if (state.tokenize != tokenBase) return 0;
  829. var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical, top
  830. // Kludge to prevent 'maybelse' from blocking lexical scope pops
  831. if (!/^\s*else\b/.test(textAfter)) for (var i = state.cc.length - 1; i >= 0; --i) {
  832. var c = state.cc[i];
  833. if (c == poplex) lexical = lexical.prev;
  834. else if (c != maybeelse) break;
  835. }
  836. while ((lexical.type == "stat" || lexical.type == "form") &&
  837. (firstChar == "}" || ((top = state.cc[state.cc.length - 1]) &&
  838. (top == maybeoperatorComma || top == maybeoperatorNoComma) &&
  839. !/^[,\.=+\-*:?[\(]/.test(textAfter))))
  840. lexical = lexical.prev;
  841. if (statementIndent && lexical.type == ")" && lexical.prev.type == "stat")
  842. lexical = lexical.prev;
  843. var type = lexical.type, closing = firstChar == type;
  844. if (type == "vardef") return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? lexical.info.length + 1 : 0);
  845. else if (type == "form" && firstChar == "{") return lexical.indented;
  846. else if (type == "form") return lexical.indented + indentUnit;
  847. else if (type == "stat")
  848. return lexical.indented + (isContinuedStatement(state, textAfter) ? statementIndent || indentUnit : 0);
  849. else if (lexical.info == "switch" && !closing && parserConfig.doubleIndentSwitch != false)
  850. return lexical.indented + (/^(?:case|default)\b/.test(textAfter) ? indentUnit : 2 * indentUnit);
  851. else if (lexical.align) return lexical.column + (closing ? 0 : 1);
  852. else return lexical.indented + (closing ? 0 : indentUnit);
  853. },
  854. electricInput: /^\s*(?:case .*?:|default:|\{|\})$/,
  855. blockCommentStart: jsonMode ? null : "/*",
  856. blockCommentEnd: jsonMode ? null : "*/",
  857. blockCommentContinue: jsonMode ? null : " * ",
  858. lineComment: jsonMode ? null : "//",
  859. fold: "brace",
  860. closeBrackets: "()[]{}''\"\"``",
  861. helperType: jsonMode ? "json" : "javascript",
  862. jsonldMode: jsonldMode,
  863. jsonMode: jsonMode,
  864. expressionAllowed: expressionAllowed,
  865. skipExpression: function(state) {
  866. var top = state.cc[state.cc.length - 1]
  867. if (top == expression || top == expressionNoComma) state.cc.pop()
  868. }
  869. };
  870. });
  871. CodeMirror.registerHelper("wordChars", "javascript", /[\w$]/);
  872. CodeMirror.defineMIME("text/javascript", "javascript");
  873. CodeMirror.defineMIME("text/ecmascript", "javascript");
  874. CodeMirror.defineMIME("application/javascript", "javascript");
  875. CodeMirror.defineMIME("application/x-javascript", "javascript");
  876. CodeMirror.defineMIME("application/ecmascript", "javascript");
  877. CodeMirror.defineMIME("application/json", {name: "javascript", json: true});
  878. CodeMirror.defineMIME("application/x-json", {name: "javascript", json: true});
  879. CodeMirror.defineMIME("application/ld+json", {name: "javascript", jsonld: true});
  880. CodeMirror.defineMIME("text/typescript", { name: "javascript", typescript: true });
  881. CodeMirror.defineMIME("application/typescript", { name: "javascript", typescript: true });
  882. });