javascript.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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")) { cx.marked = "keyword"; return cont(pushlex("form"), className, poplex); }
  340. if (type == "variable") {
  341. if (isTS && value == "declare") {
  342. cx.marked = "keyword"
  343. return cont(statement)
  344. } else if (isTS && (value == "module" || value == "enum" || value == "type") && cx.stream.match(/^\s*\w/, false)) {
  345. cx.marked = "keyword"
  346. if (value == "enum") return cont(enumdef);
  347. else if (value == "type") return cont(typeexpr, expect("operator"), typeexpr, expect(";"));
  348. else return cont(pushlex("form"), pattern, expect("{"), pushlex("}"), block, poplex, poplex)
  349. } else if (isTS && value == "namespace") {
  350. cx.marked = "keyword"
  351. return cont(pushlex("form"), expression, block, poplex)
  352. } else if (isTS && value == "abstract") {
  353. cx.marked = "keyword"
  354. return cont(statement)
  355. } else {
  356. return cont(pushlex("stat"), maybelabel);
  357. }
  358. }
  359. if (type == "switch") return cont(pushlex("form"), parenExpr, expect("{"), pushlex("}", "switch"), pushblockcontext,
  360. block, poplex, poplex, popcontext);
  361. if (type == "case") return cont(expression, expect(":"));
  362. if (type == "default") return cont(expect(":"));
  363. if (type == "catch") return cont(pushlex("form"), pushcontext, maybeCatchBinding, statement, poplex, popcontext);
  364. if (type == "export") return cont(pushlex("stat"), afterExport, poplex);
  365. if (type == "import") return cont(pushlex("stat"), afterImport, poplex);
  366. if (type == "async") return cont(statement)
  367. if (value == "@") return cont(expression, statement)
  368. return pass(pushlex("stat"), expression, expect(";"), poplex);
  369. }
  370. function maybeCatchBinding(type) {
  371. if (type == "(") return cont(funarg, expect(")"))
  372. }
  373. function expression(type, value) {
  374. return expressionInner(type, value, false);
  375. }
  376. function expressionNoComma(type, value) {
  377. return expressionInner(type, value, true);
  378. }
  379. function parenExpr(type) {
  380. if (type != "(") return pass()
  381. return cont(pushlex(")"), expression, expect(")"), poplex)
  382. }
  383. function expressionInner(type, value, noComma) {
  384. if (cx.state.fatArrowAt == cx.stream.start) {
  385. var body = noComma ? arrowBodyNoComma : arrowBody;
  386. if (type == "(") return cont(pushcontext, pushlex(")"), commasep(funarg, ")"), poplex, expect("=>"), body, popcontext);
  387. else if (type == "variable") return pass(pushcontext, pattern, expect("=>"), body, popcontext);
  388. }
  389. var maybeop = noComma ? maybeoperatorNoComma : maybeoperatorComma;
  390. if (atomicTypes.hasOwnProperty(type)) return cont(maybeop);
  391. if (type == "function") return cont(functiondef, maybeop);
  392. if (type == "class" || (isTS && value == "interface")) { cx.marked = "keyword"; return cont(pushlex("form"), classExpression, poplex); }
  393. if (type == "keyword c" || type == "async") return cont(noComma ? expressionNoComma : expression);
  394. if (type == "(") return cont(pushlex(")"), maybeexpression, expect(")"), poplex, maybeop);
  395. if (type == "operator" || type == "spread") return cont(noComma ? expressionNoComma : expression);
  396. if (type == "[") return cont(pushlex("]"), arrayLiteral, poplex, maybeop);
  397. if (type == "{") return contCommasep(objprop, "}", null, maybeop);
  398. if (type == "quasi") return pass(quasi, maybeop);
  399. if (type == "new") return cont(maybeTarget(noComma));
  400. if (type == "import") return cont(expression);
  401. return cont();
  402. }
  403. function maybeexpression(type) {
  404. if (type.match(/[;\}\)\],]/)) return pass();
  405. return pass(expression);
  406. }
  407. function maybeoperatorComma(type, value) {
  408. if (type == ",") return cont(expression);
  409. return maybeoperatorNoComma(type, value, false);
  410. }
  411. function maybeoperatorNoComma(type, value, noComma) {
  412. var me = noComma == false ? maybeoperatorComma : maybeoperatorNoComma;
  413. var expr = noComma == false ? expression : expressionNoComma;
  414. if (type == "=>") return cont(pushcontext, noComma ? arrowBodyNoComma : arrowBody, popcontext);
  415. if (type == "operator") {
  416. if (/\+\+|--/.test(value) || isTS && value == "!") return cont(me);
  417. if (isTS && value == "<" && cx.stream.match(/^([^>]|<.*?>)*>\s*\(/, false))
  418. return cont(pushlex(">"), commasep(typeexpr, ">"), poplex, me);
  419. if (value == "?") return cont(expression, expect(":"), expr);
  420. return cont(expr);
  421. }
  422. if (type == "quasi") { return pass(quasi, me); }
  423. if (type == ";") return;
  424. if (type == "(") return contCommasep(expressionNoComma, ")", "call", me);
  425. if (type == ".") return cont(property, me);
  426. if (type == "[") return cont(pushlex("]"), maybeexpression, expect("]"), poplex, me);
  427. if (isTS && value == "as") { cx.marked = "keyword"; return cont(typeexpr, me) }
  428. if (type == "regexp") {
  429. cx.state.lastType = cx.marked = "operator"
  430. cx.stream.backUp(cx.stream.pos - cx.stream.start - 1)
  431. return cont(expr)
  432. }
  433. }
  434. function quasi(type, value) {
  435. if (type != "quasi") return pass();
  436. if (value.slice(value.length - 2) != "${") return cont(quasi);
  437. return cont(expression, continueQuasi);
  438. }
  439. function continueQuasi(type) {
  440. if (type == "}") {
  441. cx.marked = "string-2";
  442. cx.state.tokenize = tokenQuasi;
  443. return cont(quasi);
  444. }
  445. }
  446. function arrowBody(type) {
  447. findFatArrow(cx.stream, cx.state);
  448. return pass(type == "{" ? statement : expression);
  449. }
  450. function arrowBodyNoComma(type) {
  451. findFatArrow(cx.stream, cx.state);
  452. return pass(type == "{" ? statement : expressionNoComma);
  453. }
  454. function maybeTarget(noComma) {
  455. return function(type) {
  456. if (type == ".") return cont(noComma ? targetNoComma : target);
  457. else if (type == "variable" && isTS) return cont(maybeTypeArgs, noComma ? maybeoperatorNoComma : maybeoperatorComma)
  458. else return pass(noComma ? expressionNoComma : expression);
  459. };
  460. }
  461. function target(_, value) {
  462. if (value == "target") { cx.marked = "keyword"; return cont(maybeoperatorComma); }
  463. }
  464. function targetNoComma(_, value) {
  465. if (value == "target") { cx.marked = "keyword"; return cont(maybeoperatorNoComma); }
  466. }
  467. function maybelabel(type) {
  468. if (type == ":") return cont(poplex, statement);
  469. return pass(maybeoperatorComma, expect(";"), poplex);
  470. }
  471. function property(type) {
  472. if (type == "variable") {cx.marked = "property"; return cont();}
  473. }
  474. function objprop(type, value) {
  475. if (type == "async") {
  476. cx.marked = "property";
  477. return cont(objprop);
  478. } else if (type == "variable" || cx.style == "keyword") {
  479. cx.marked = "property";
  480. if (value == "get" || value == "set") return cont(getterSetter);
  481. var m // Work around fat-arrow-detection complication for detecting typescript typed arrow params
  482. if (isTS && cx.state.fatArrowAt == cx.stream.start && (m = cx.stream.match(/^\s*:\s*/, false)))
  483. cx.state.fatArrowAt = cx.stream.pos + m[0].length
  484. return cont(afterprop);
  485. } else if (type == "number" || type == "string") {
  486. cx.marked = jsonldMode ? "property" : (cx.style + " property");
  487. return cont(afterprop);
  488. } else if (type == "jsonld-keyword") {
  489. return cont(afterprop);
  490. } else if (isTS && isModifier(value)) {
  491. cx.marked = "keyword"
  492. return cont(objprop)
  493. } else if (type == "[") {
  494. return cont(expression, maybetype, expect("]"), afterprop);
  495. } else if (type == "spread") {
  496. return cont(expressionNoComma, afterprop);
  497. } else if (value == "*") {
  498. cx.marked = "keyword";
  499. return cont(objprop);
  500. } else if (type == ":") {
  501. return pass(afterprop)
  502. }
  503. }
  504. function getterSetter(type) {
  505. if (type != "variable") return pass(afterprop);
  506. cx.marked = "property";
  507. return cont(functiondef);
  508. }
  509. function afterprop(type) {
  510. if (type == ":") return cont(expressionNoComma);
  511. if (type == "(") return pass(functiondef);
  512. }
  513. function commasep(what, end, sep) {
  514. function proceed(type, value) {
  515. if (sep ? sep.indexOf(type) > -1 : type == ",") {
  516. var lex = cx.state.lexical;
  517. if (lex.info == "call") lex.pos = (lex.pos || 0) + 1;
  518. return cont(function(type, value) {
  519. if (type == end || value == end) return pass()
  520. return pass(what)
  521. }, proceed);
  522. }
  523. if (type == end || value == end) return cont();
  524. return cont(expect(end));
  525. }
  526. return function(type, value) {
  527. if (type == end || value == end) return cont();
  528. return pass(what, proceed);
  529. };
  530. }
  531. function contCommasep(what, end, info) {
  532. for (var i = 3; i < arguments.length; i++)
  533. cx.cc.push(arguments[i]);
  534. return cont(pushlex(end, info), commasep(what, end), poplex);
  535. }
  536. function block(type) {
  537. if (type == "}") return cont();
  538. return pass(statement, block);
  539. }
  540. function maybetype(type, value) {
  541. if (isTS) {
  542. if (type == ":") return cont(typeexpr);
  543. if (value == "?") return cont(maybetype);
  544. }
  545. }
  546. function mayberettype(type) {
  547. if (isTS && type == ":") {
  548. if (cx.stream.match(/^\s*\w+\s+is\b/, false)) return cont(expression, isKW, typeexpr)
  549. else return cont(typeexpr)
  550. }
  551. }
  552. function isKW(_, value) {
  553. if (value == "is") {
  554. cx.marked = "keyword"
  555. return cont()
  556. }
  557. }
  558. function typeexpr(type, value) {
  559. if (value == "keyof" || value == "typeof") {
  560. cx.marked = "keyword"
  561. return cont(value == "keyof" ? typeexpr : expressionNoComma)
  562. }
  563. if (type == "variable" || value == "void") {
  564. cx.marked = "type"
  565. return cont(afterType)
  566. }
  567. if (type == "string" || type == "number" || type == "atom") return cont(afterType);
  568. if (type == "[") return cont(pushlex("]"), commasep(typeexpr, "]", ","), poplex, afterType)
  569. if (type == "{") return cont(pushlex("}"), commasep(typeprop, "}", ",;"), poplex, afterType)
  570. if (type == "(") return cont(commasep(typearg, ")"), maybeReturnType)
  571. if (type == "<") return cont(commasep(typeexpr, ">"), typeexpr)
  572. }
  573. function maybeReturnType(type) {
  574. if (type == "=>") return cont(typeexpr)
  575. }
  576. function typeprop(type, value) {
  577. if (type == "variable" || cx.style == "keyword") {
  578. cx.marked = "property"
  579. return cont(typeprop)
  580. } else if (value == "?") {
  581. return cont(typeprop)
  582. } else if (type == ":") {
  583. return cont(typeexpr)
  584. } else if (type == "[") {
  585. return cont(expression, maybetype, expect("]"), typeprop)
  586. }
  587. }
  588. function typearg(type, value) {
  589. if (type == "variable" && cx.stream.match(/^\s*[?:]/, false) || value == "?") return cont(typearg)
  590. if (type == ":") return cont(typeexpr)
  591. return pass(typeexpr)
  592. }
  593. function afterType(type, value) {
  594. if (value == "<") return cont(pushlex(">"), commasep(typeexpr, ">"), poplex, afterType)
  595. if (value == "|" || type == "." || value == "&") return cont(typeexpr)
  596. if (type == "[") return cont(expect("]"), afterType)
  597. if (value == "extends" || value == "implements") { cx.marked = "keyword"; return cont(typeexpr) }
  598. }
  599. function maybeTypeArgs(_, value) {
  600. if (value == "<") return cont(pushlex(">"), commasep(typeexpr, ">"), poplex, afterType)
  601. }
  602. function typeparam() {
  603. return pass(typeexpr, maybeTypeDefault)
  604. }
  605. function maybeTypeDefault(_, value) {
  606. if (value == "=") return cont(typeexpr)
  607. }
  608. function vardef(_, value) {
  609. if (value == "enum") {cx.marked = "keyword"; return cont(enumdef)}
  610. return pass(pattern, maybetype, maybeAssign, vardefCont);
  611. }
  612. function pattern(type, value) {
  613. if (isTS && isModifier(value)) { cx.marked = "keyword"; return cont(pattern) }
  614. if (type == "variable") { register(value); return cont(); }
  615. if (type == "spread") return cont(pattern);
  616. if (type == "[") return contCommasep(eltpattern, "]");
  617. if (type == "{") return contCommasep(proppattern, "}");
  618. }
  619. function proppattern(type, value) {
  620. if (type == "variable" && !cx.stream.match(/^\s*:/, false)) {
  621. register(value);
  622. return cont(maybeAssign);
  623. }
  624. if (type == "variable") cx.marked = "property";
  625. if (type == "spread") return cont(pattern);
  626. if (type == "}") return pass();
  627. return cont(expect(":"), pattern, maybeAssign);
  628. }
  629. function eltpattern() {
  630. return pass(pattern, maybeAssign)
  631. }
  632. function maybeAssign(_type, value) {
  633. if (value == "=") return cont(expressionNoComma);
  634. }
  635. function vardefCont(type) {
  636. if (type == ",") return cont(vardef);
  637. }
  638. function maybeelse(type, value) {
  639. if (type == "keyword b" && value == "else") return cont(pushlex("form", "else"), statement, poplex);
  640. }
  641. function forspec(type, value) {
  642. if (value == "await") return cont(forspec);
  643. if (type == "(") return cont(pushlex(")"), forspec1, expect(")"), poplex);
  644. }
  645. function forspec1(type) {
  646. if (type == "var") return cont(vardef, expect(";"), forspec2);
  647. if (type == ";") return cont(forspec2);
  648. if (type == "variable") return cont(formaybeinof);
  649. return pass(expression, expect(";"), forspec2);
  650. }
  651. function formaybeinof(_type, value) {
  652. if (value == "in" || value == "of") { cx.marked = "keyword"; return cont(expression); }
  653. return cont(maybeoperatorComma, forspec2);
  654. }
  655. function forspec2(type, value) {
  656. if (type == ";") return cont(forspec3);
  657. if (value == "in" || value == "of") { cx.marked = "keyword"; return cont(expression); }
  658. return pass(expression, expect(";"), forspec3);
  659. }
  660. function forspec3(type) {
  661. if (type != ")") cont(expression);
  662. }
  663. function functiondef(type, value) {
  664. if (value == "*") {cx.marked = "keyword"; return cont(functiondef);}
  665. if (type == "variable") {register(value); return cont(functiondef);}
  666. if (type == "(") return cont(pushcontext, pushlex(")"), commasep(funarg, ")"), poplex, mayberettype, statement, popcontext);
  667. if (isTS && value == "<") return cont(pushlex(">"), commasep(typeparam, ">"), poplex, functiondef)
  668. }
  669. function funarg(type, value) {
  670. if (value == "@") cont(expression, funarg)
  671. if (type == "spread") return cont(funarg);
  672. if (isTS && isModifier(value)) { cx.marked = "keyword"; return cont(funarg); }
  673. return pass(pattern, maybetype, maybeAssign);
  674. }
  675. function classExpression(type, value) {
  676. // Class expressions may have an optional name.
  677. if (type == "variable") return className(type, value);
  678. return classNameAfter(type, value);
  679. }
  680. function className(type, value) {
  681. if (type == "variable") {register(value); return cont(classNameAfter);}
  682. }
  683. function classNameAfter(type, value) {
  684. if (value == "<") return cont(pushlex(">"), commasep(typeparam, ">"), poplex, classNameAfter)
  685. if (value == "extends" || value == "implements" || (isTS && type == ",")) {
  686. if (value == "implements") cx.marked = "keyword";
  687. return cont(isTS ? typeexpr : expression, classNameAfter);
  688. }
  689. if (type == "{") return cont(pushlex("}"), classBody, poplex);
  690. }
  691. function classBody(type, value) {
  692. if (type == "async" ||
  693. (type == "variable" &&
  694. (value == "static" || value == "get" || value == "set" || (isTS && isModifier(value))) &&
  695. cx.stream.match(/^\s+[\w$\xa1-\uffff]/, false))) {
  696. cx.marked = "keyword";
  697. return cont(classBody);
  698. }
  699. if (type == "variable" || cx.style == "keyword") {
  700. cx.marked = "property";
  701. return cont(isTS ? classfield : functiondef, classBody);
  702. }
  703. if (type == "[")
  704. return cont(expression, maybetype, expect("]"), isTS ? classfield : functiondef, classBody)
  705. if (value == "*") {
  706. cx.marked = "keyword";
  707. return cont(classBody);
  708. }
  709. if (type == ";") return cont(classBody);
  710. if (type == "}") return cont();
  711. if (value == "@") return cont(expression, classBody)
  712. }
  713. function classfield(type, value) {
  714. if (value == "?") return cont(classfield)
  715. if (type == ":") return cont(typeexpr, maybeAssign)
  716. if (value == "=") return cont(expressionNoComma)
  717. return pass(functiondef)
  718. }
  719. function afterExport(type, value) {
  720. if (value == "*") { cx.marked = "keyword"; return cont(maybeFrom, expect(";")); }
  721. if (value == "default") { cx.marked = "keyword"; return cont(expression, expect(";")); }
  722. if (type == "{") return cont(commasep(exportField, "}"), maybeFrom, expect(";"));
  723. return pass(statement);
  724. }
  725. function exportField(type, value) {
  726. if (value == "as") { cx.marked = "keyword"; return cont(expect("variable")); }
  727. if (type == "variable") return pass(expressionNoComma, exportField);
  728. }
  729. function afterImport(type) {
  730. if (type == "string") return cont();
  731. if (type == "(") return pass(expression);
  732. return pass(importSpec, maybeMoreImports, maybeFrom);
  733. }
  734. function importSpec(type, value) {
  735. if (type == "{") return contCommasep(importSpec, "}");
  736. if (type == "variable") register(value);
  737. if (value == "*") cx.marked = "keyword";
  738. return cont(maybeAs);
  739. }
  740. function maybeMoreImports(type) {
  741. if (type == ",") return cont(importSpec, maybeMoreImports)
  742. }
  743. function maybeAs(_type, value) {
  744. if (value == "as") { cx.marked = "keyword"; return cont(importSpec); }
  745. }
  746. function maybeFrom(_type, value) {
  747. if (value == "from") { cx.marked = "keyword"; return cont(expression); }
  748. }
  749. function arrayLiteral(type) {
  750. if (type == "]") return cont();
  751. return pass(commasep(expressionNoComma, "]"));
  752. }
  753. function enumdef() {
  754. return pass(pushlex("form"), pattern, expect("{"), pushlex("}"), commasep(enummember, "}"), poplex, poplex)
  755. }
  756. function enummember() {
  757. return pass(pattern, maybeAssign);
  758. }
  759. function isContinuedStatement(state, textAfter) {
  760. return state.lastType == "operator" || state.lastType == "," ||
  761. isOperatorChar.test(textAfter.charAt(0)) ||
  762. /[,.]/.test(textAfter.charAt(0));
  763. }
  764. function expressionAllowed(stream, state, backUp) {
  765. return state.tokenize == tokenBase &&
  766. /^(?:operator|sof|keyword [bcd]|case|new|export|default|spread|[\[{}\(,;:]|=>)$/.test(state.lastType) ||
  767. (state.lastType == "quasi" && /\{\s*$/.test(stream.string.slice(0, stream.pos - (backUp || 0))))
  768. }
  769. // Interface
  770. return {
  771. startState: function(basecolumn) {
  772. var state = {
  773. tokenize: tokenBase,
  774. lastType: "sof",
  775. cc: [],
  776. lexical: new JSLexical((basecolumn || 0) - indentUnit, 0, "block", false),
  777. localVars: parserConfig.localVars,
  778. context: parserConfig.localVars && new Context(null, null, false),
  779. indented: basecolumn || 0
  780. };
  781. if (parserConfig.globalVars && typeof parserConfig.globalVars == "object")
  782. state.globalVars = parserConfig.globalVars;
  783. return state;
  784. },
  785. token: function(stream, state) {
  786. if (stream.sol()) {
  787. if (!state.lexical.hasOwnProperty("align"))
  788. state.lexical.align = false;
  789. state.indented = stream.indentation();
  790. findFatArrow(stream, state);
  791. }
  792. if (state.tokenize != tokenComment && stream.eatSpace()) return null;
  793. var style = state.tokenize(stream, state);
  794. if (type == "comment") return style;
  795. state.lastType = type == "operator" && (content == "++" || content == "--") ? "incdec" : type;
  796. return parseJS(state, style, type, content, stream);
  797. },
  798. indent: function(state, textAfter) {
  799. if (state.tokenize == tokenComment) return CodeMirror.Pass;
  800. if (state.tokenize != tokenBase) return 0;
  801. var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical, top
  802. // Kludge to prevent 'maybelse' from blocking lexical scope pops
  803. if (!/^\s*else\b/.test(textAfter)) for (var i = state.cc.length - 1; i >= 0; --i) {
  804. var c = state.cc[i];
  805. if (c == poplex) lexical = lexical.prev;
  806. else if (c != maybeelse) break;
  807. }
  808. while ((lexical.type == "stat" || lexical.type == "form") &&
  809. (firstChar == "}" || ((top = state.cc[state.cc.length - 1]) &&
  810. (top == maybeoperatorComma || top == maybeoperatorNoComma) &&
  811. !/^[,\.=+\-*:?[\(]/.test(textAfter))))
  812. lexical = lexical.prev;
  813. if (statementIndent && lexical.type == ")" && lexical.prev.type == "stat")
  814. lexical = lexical.prev;
  815. var type = lexical.type, closing = firstChar == type;
  816. if (type == "vardef") return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? lexical.info.length + 1 : 0);
  817. else if (type == "form" && firstChar == "{") return lexical.indented;
  818. else if (type == "form") return lexical.indented + indentUnit;
  819. else if (type == "stat")
  820. return lexical.indented + (isContinuedStatement(state, textAfter) ? statementIndent || indentUnit : 0);
  821. else if (lexical.info == "switch" && !closing && parserConfig.doubleIndentSwitch != false)
  822. return lexical.indented + (/^(?:case|default)\b/.test(textAfter) ? indentUnit : 2 * indentUnit);
  823. else if (lexical.align) return lexical.column + (closing ? 0 : 1);
  824. else return lexical.indented + (closing ? 0 : indentUnit);
  825. },
  826. electricInput: /^\s*(?:case .*?:|default:|\{|\})$/,
  827. blockCommentStart: jsonMode ? null : "/*",
  828. blockCommentEnd: jsonMode ? null : "*/",
  829. blockCommentContinue: jsonMode ? null : " * ",
  830. lineComment: jsonMode ? null : "//",
  831. fold: "brace",
  832. closeBrackets: "()[]{}''\"\"``",
  833. helperType: jsonMode ? "json" : "javascript",
  834. jsonldMode: jsonldMode,
  835. jsonMode: jsonMode,
  836. expressionAllowed: expressionAllowed,
  837. skipExpression: function(state) {
  838. var top = state.cc[state.cc.length - 1]
  839. if (top == expression || top == expressionNoComma) state.cc.pop()
  840. }
  841. };
  842. });
  843. CodeMirror.registerHelper("wordChars", "javascript", /[\w$]/);
  844. CodeMirror.defineMIME("text/javascript", "javascript");
  845. CodeMirror.defineMIME("text/ecmascript", "javascript");
  846. CodeMirror.defineMIME("application/javascript", "javascript");
  847. CodeMirror.defineMIME("application/x-javascript", "javascript");
  848. CodeMirror.defineMIME("application/ecmascript", "javascript");
  849. CodeMirror.defineMIME("application/json", {name: "javascript", json: true});
  850. CodeMirror.defineMIME("application/x-json", {name: "javascript", json: true});
  851. CodeMirror.defineMIME("application/ld+json", {name: "javascript", jsonld: true});
  852. CodeMirror.defineMIME("text/typescript", { name: "javascript", typescript: true });
  853. CodeMirror.defineMIME("application/typescript", { name: "javascript", typescript: true });
  854. });