javascript.js 33 KB

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