Sin descripción
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

javascript.js 30KB

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