javascript.js 33 KB

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