javascript.js 36 KB

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