javascript.js 37 KB

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