javascript.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. // TODO actually recognize syntax of TypeScript constructs
  2. (function(mod) {
  3. if (typeof exports == "object" && typeof module == "object") // CommonJS
  4. mod(require("../../lib/codemirror"));
  5. else if (typeof define == "function" && define.amd) // AMD
  6. define(["../../lib/codemirror"], mod);
  7. else // Plain browser env
  8. mod(CodeMirror);
  9. })(function(CodeMirror) {
  10. "use strict";
  11. CodeMirror.defineMode("javascript", function(config, parserConfig) {
  12. var indentUnit = config.indentUnit;
  13. var statementIndent = parserConfig.statementIndent;
  14. var jsonldMode = parserConfig.jsonld;
  15. var jsonMode = parserConfig.json || jsonldMode;
  16. var isTS = parserConfig.typescript;
  17. // Tokenizer
  18. var keywords = function(){
  19. function kw(type) {return {type: type, style: "keyword"};}
  20. var A = kw("keyword a"), B = kw("keyword b"), C = kw("keyword c");
  21. var operator = kw("operator"), atom = {type: "atom", style: "atom"};
  22. var jsKeywords = {
  23. "if": kw("if"), "while": A, "with": A, "else": B, "do": B, "try": B, "finally": B,
  24. "return": C, "break": C, "continue": C, "new": C, "delete": C, "throw": C, "debugger": C,
  25. "var": kw("var"), "const": kw("var"), "let": kw("var"),
  26. "function": kw("function"), "catch": kw("catch"),
  27. "for": kw("for"), "switch": kw("switch"), "case": kw("case"), "default": kw("default"),
  28. "in": operator, "typeof": operator, "instanceof": operator,
  29. "true": atom, "false": atom, "null": atom, "undefined": atom, "NaN": atom, "Infinity": atom,
  30. "this": kw("this"), "module": kw("module"), "class": kw("class"), "super": kw("atom"),
  31. "yield": C, "export": kw("export"), "import": kw("import"), "extends": C
  32. };
  33. // Extend the 'normal' keywords with the TypeScript language extensions
  34. if (isTS) {
  35. var type = {type: "variable", style: "variable-3"};
  36. var tsKeywords = {
  37. // object-like things
  38. "interface": kw("interface"),
  39. "extends": kw("extends"),
  40. "constructor": kw("constructor"),
  41. // scope modifiers
  42. "public": kw("public"),
  43. "private": kw("private"),
  44. "protected": kw("protected"),
  45. "static": kw("static"),
  46. // types
  47. "string": type, "number": type, "bool": type, "any": type
  48. };
  49. for (var attr in tsKeywords) {
  50. jsKeywords[attr] = tsKeywords[attr];
  51. }
  52. }
  53. return jsKeywords;
  54. }();
  55. var isOperatorChar = /[+\-*&%=<>!?|~^]/;
  56. var isJsonldKeyword = /^@(context|id|value|language|type|container|list|set|reverse|index|base|vocab|graph)"/;
  57. function readRegexp(stream) {
  58. var escaped = false, next, inSet = false;
  59. while ((next = stream.next()) != null) {
  60. if (!escaped) {
  61. if (next == "/" && !inSet) return;
  62. if (next == "[") inSet = true;
  63. else if (inSet && next == "]") inSet = false;
  64. }
  65. escaped = !escaped && next == "\\";
  66. }
  67. }
  68. // Used as scratch variables to communicate multiple values without
  69. // consing up tons of objects.
  70. var type, content;
  71. function ret(tp, style, cont) {
  72. type = tp; content = cont;
  73. return style;
  74. }
  75. function tokenBase(stream, state) {
  76. var ch = stream.next();
  77. if (ch == '"' || ch == "'") {
  78. state.tokenize = tokenString(ch);
  79. return state.tokenize(stream, state);
  80. } else if (ch == "." && stream.match(/^\d+(?:[eE][+\-]?\d+)?/)) {
  81. return ret("number", "number");
  82. } else if (ch == "." && stream.match("..")) {
  83. return ret("spread", "meta");
  84. } else if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
  85. return ret(ch);
  86. } else if (ch == "=" && stream.eat(">")) {
  87. return ret("=>", "operator");
  88. } else if (ch == "0" && stream.eat(/x/i)) {
  89. stream.eatWhile(/[\da-f]/i);
  90. return ret("number", "number");
  91. } else if (/\d/.test(ch)) {
  92. stream.match(/^\d*(?:\.\d*)?(?:[eE][+\-]?\d+)?/);
  93. return ret("number", "number");
  94. } else if (ch == "/") {
  95. if (stream.eat("*")) {
  96. state.tokenize = tokenComment;
  97. return tokenComment(stream, state);
  98. } else if (stream.eat("/")) {
  99. stream.skipToEnd();
  100. return ret("comment", "comment");
  101. } else if (state.lastType == "operator" || state.lastType == "keyword c" ||
  102. state.lastType == "sof" || /^[\[{}\(,;:]$/.test(state.lastType)) {
  103. readRegexp(stream);
  104. stream.eatWhile(/[gimy]/); // 'y' is "sticky" option in Mozilla
  105. return ret("regexp", "string-2");
  106. } else {
  107. stream.eatWhile(isOperatorChar);
  108. return ret("operator", "operator", stream.current());
  109. }
  110. } else if (ch == "`") {
  111. state.tokenize = tokenQuasi;
  112. return tokenQuasi(stream, state);
  113. } else if (ch == "#") {
  114. stream.skipToEnd();
  115. return ret("error", "error");
  116. } else if (isOperatorChar.test(ch)) {
  117. stream.eatWhile(isOperatorChar);
  118. return ret("operator", "operator", stream.current());
  119. } else {
  120. stream.eatWhile(/[\w\$_]/);
  121. var word = stream.current(), known = keywords.propertyIsEnumerable(word) && keywords[word];
  122. return (known && state.lastType != ".") ? ret(known.type, known.style, word) :
  123. 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. var depth = 0, sawSomething = false;
  176. for (var pos = arrow - 1; pos >= 0; --pos) {
  177. var ch = stream.string.charAt(pos);
  178. var bracket = brackets.indexOf(ch);
  179. if (bracket >= 0 && bracket < 3) {
  180. if (!depth) { ++pos; break; }
  181. if (--depth == 0) break;
  182. } else if (bracket >= 3 && bracket < 6) {
  183. ++depth;
  184. } else if (/[$\w]/.test(ch)) {
  185. sawSomething = true;
  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;
  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 register(varname) {
  239. function inList(list) {
  240. for (var v = list; v; v = v.next)
  241. if (v.name == varname) return true;
  242. return false;
  243. }
  244. var state = cx.state;
  245. if (state.context) {
  246. cx.marked = "def";
  247. if (inList(state.localVars)) return;
  248. state.localVars = {name: varname, next: state.localVars};
  249. } else {
  250. if (inList(state.globalVars)) return;
  251. if (parserConfig.globalVars)
  252. state.globalVars = {name: varname, next: state.globalVars};
  253. }
  254. }
  255. // Combinators
  256. var defaultVars = {name: "this", next: {name: "arguments"}};
  257. function pushcontext() {
  258. cx.state.context = {prev: cx.state.context, vars: cx.state.localVars};
  259. cx.state.localVars = defaultVars;
  260. }
  261. function popcontext() {
  262. cx.state.localVars = cx.state.context.vars;
  263. cx.state.context = cx.state.context.prev;
  264. }
  265. function pushlex(type, info) {
  266. var result = function() {
  267. var state = cx.state, indent = state.indented;
  268. if (state.lexical.type == "stat") indent = state.lexical.indented;
  269. state.lexical = new JSLexical(indent, cx.stream.column(), type, null, state.lexical, info);
  270. };
  271. result.lex = true;
  272. return result;
  273. }
  274. function poplex() {
  275. var state = cx.state;
  276. if (state.lexical.prev) {
  277. if (state.lexical.type == ")")
  278. state.indented = state.lexical.indented;
  279. state.lexical = state.lexical.prev;
  280. }
  281. }
  282. poplex.lex = true;
  283. function expect(wanted) {
  284. function exp(type) {
  285. if (type == wanted) return cont();
  286. else if (wanted == ";") return pass();
  287. else return cont(exp);
  288. };
  289. return exp;
  290. }
  291. function statement(type, value) {
  292. if (type == "var") return cont(pushlex("vardef", value.length), vardef, expect(";"), poplex);
  293. if (type == "keyword a") return cont(pushlex("form"), expression, statement, poplex);
  294. if (type == "keyword b") return cont(pushlex("form"), statement, poplex);
  295. if (type == "{") return cont(pushlex("}"), block, poplex);
  296. if (type == ";") return cont();
  297. if (type == "if") return cont(pushlex("form"), expression, statement, poplex, maybeelse);
  298. if (type == "function") return cont(functiondef);
  299. if (type == "for") return cont(pushlex("form"), forspec, statement, poplex);
  300. if (type == "variable") return cont(pushlex("stat"), maybelabel);
  301. if (type == "switch") return cont(pushlex("form"), expression, pushlex("}", "switch"), expect("{"),
  302. block, poplex, poplex);
  303. if (type == "case") return cont(expression, expect(":"));
  304. if (type == "default") return cont(expect(":"));
  305. if (type == "catch") return cont(pushlex("form"), pushcontext, expect("("), funarg, expect(")"),
  306. statement, poplex, popcontext);
  307. if (type == "module") return cont(pushlex("form"), pushcontext, afterModule, popcontext, poplex);
  308. if (type == "class") return cont(pushlex("form"), className, objlit, poplex);
  309. if (type == "export") return cont(pushlex("form"), afterExport, poplex);
  310. if (type == "import") return cont(pushlex("form"), afterImport, poplex);
  311. return pass(pushlex("stat"), expression, expect(";"), poplex);
  312. }
  313. function expression(type) {
  314. return expressionInner(type, false);
  315. }
  316. function expressionNoComma(type) {
  317. return expressionInner(type, true);
  318. }
  319. function expressionInner(type, noComma) {
  320. if (cx.state.fatArrowAt == cx.stream.start) {
  321. var body = noComma ? arrowBodyNoComma : arrowBody;
  322. if (type == "(") return cont(pushcontext, pushlex(")"), commasep(pattern, ")"), poplex, expect("=>"), body, popcontext);
  323. else if (type == "variable") return pass(pushcontext, pattern, expect("=>"), body, popcontext);
  324. }
  325. var maybeop = noComma ? maybeoperatorNoComma : maybeoperatorComma;
  326. if (atomicTypes.hasOwnProperty(type)) return cont(maybeop);
  327. if (type == "function") return cont(functiondef);
  328. if (type == "keyword c") return cont(noComma ? maybeexpressionNoComma : maybeexpression);
  329. if (type == "(") return cont(pushlex(")"), maybeexpression, comprehension, expect(")"), poplex, maybeop);
  330. if (type == "operator" || type == "spread") return cont(noComma ? expressionNoComma : expression);
  331. if (type == "[") return cont(pushlex("]"), arrayLiteral, poplex, maybeop);
  332. if (type == "{") return contCommasep(objprop, "}", null, maybeop);
  333. return cont();
  334. }
  335. function maybeexpression(type) {
  336. if (type.match(/[;\}\)\],]/)) return pass();
  337. return pass(expression);
  338. }
  339. function maybeexpressionNoComma(type) {
  340. if (type.match(/[;\}\)\],]/)) return pass();
  341. return pass(expressionNoComma);
  342. }
  343. function maybeoperatorComma(type, value) {
  344. if (type == ",") return cont(expression);
  345. return maybeoperatorNoComma(type, value, false);
  346. }
  347. function maybeoperatorNoComma(type, value, noComma) {
  348. var me = noComma == false ? maybeoperatorComma : maybeoperatorNoComma;
  349. var expr = noComma == false ? expression : expressionNoComma;
  350. if (value == "=>") return cont(pushcontext, noComma ? arrowBodyNoComma : arrowBody, popcontext);
  351. if (type == "operator") {
  352. if (/\+\+|--/.test(value)) return cont(me);
  353. if (value == "?") return cont(expression, expect(":"), expr);
  354. return cont(expr);
  355. }
  356. if (type == "quasi") { cx.cc.push(me); return quasi(value); }
  357. if (type == ";") return;
  358. if (type == "(") return contCommasep(expressionNoComma, ")", "call", me);
  359. if (type == ".") return cont(property, me);
  360. if (type == "[") return cont(pushlex("]"), maybeexpression, expect("]"), poplex, me);
  361. }
  362. function quasi(value) {
  363. if (value.slice(value.length - 2) != "${") return cont();
  364. return cont(expression, continueQuasi);
  365. }
  366. function continueQuasi(type) {
  367. if (type == "}") {
  368. cx.marked = "string-2";
  369. cx.state.tokenize = tokenQuasi;
  370. return cont();
  371. }
  372. }
  373. function arrowBody(type) {
  374. findFatArrow(cx.stream, cx.state);
  375. if (type == "{") return pass(statement);
  376. return pass(expression);
  377. }
  378. function arrowBodyNoComma(type) {
  379. findFatArrow(cx.stream, cx.state);
  380. if (type == "{") return pass(statement);
  381. return pass(expressionNoComma);
  382. }
  383. function maybelabel(type) {
  384. if (type == ":") return cont(poplex, statement);
  385. return pass(maybeoperatorComma, expect(";"), poplex);
  386. }
  387. function property(type) {
  388. if (type == "variable") {cx.marked = "property"; return cont();}
  389. }
  390. function objprop(type, value) {
  391. if (type == "variable") {
  392. cx.marked = "property";
  393. if (value == "get" || value == "set") return cont(getterSetter);
  394. } else if (type == "number" || type == "string") {
  395. cx.marked = jsonldMode ? "property" : (type + " property");
  396. } else if (type == "[") {
  397. return cont(expression, expect("]"), afterprop);
  398. }
  399. if (atomicTypes.hasOwnProperty(type)) return cont(afterprop);
  400. }
  401. function getterSetter(type) {
  402. if (type != "variable") return pass(afterprop);
  403. cx.marked = "property";
  404. return cont(functiondef);
  405. }
  406. function afterprop(type) {
  407. if (type == ":") return cont(expressionNoComma);
  408. if (type == "(") return pass(functiondef);
  409. }
  410. function commasep(what, end) {
  411. function proceed(type) {
  412. if (type == ",") {
  413. var lex = cx.state.lexical;
  414. if (lex.info == "call") lex.pos = (lex.pos || 0) + 1;
  415. return cont(what, proceed);
  416. }
  417. if (type == end) return cont();
  418. return cont(expect(end));
  419. }
  420. return function(type) {
  421. if (type == end) return cont();
  422. return pass(what, proceed);
  423. };
  424. }
  425. function contCommasep(what, end, info) {
  426. for (var i = 3; i < arguments.length; i++)
  427. cx.cc.push(arguments[i]);
  428. return cont(pushlex(end, info), commasep(what, end), poplex);
  429. }
  430. function block(type) {
  431. if (type == "}") return cont();
  432. return pass(statement, block);
  433. }
  434. function maybetype(type) {
  435. if (isTS && type == ":") return cont(typedef);
  436. }
  437. function typedef(type) {
  438. if (type == "variable"){cx.marked = "variable-3"; return cont();}
  439. }
  440. function vardef() {
  441. return pass(pattern, maybetype, maybeAssign, vardefCont);
  442. }
  443. function pattern(type, value) {
  444. if (type == "variable") { register(value); return cont(); }
  445. if (type == "[") return contCommasep(pattern, "]");
  446. if (type == "{") return contCommasep(proppattern, "}");
  447. }
  448. function proppattern(type, value) {
  449. if (type == "variable" && !cx.stream.match(/^\s*:/, false)) {
  450. register(value);
  451. return cont(maybeAssign);
  452. }
  453. if (type == "variable") cx.marked = "property";
  454. return cont(expect(":"), pattern, maybeAssign);
  455. }
  456. function maybeAssign(_type, value) {
  457. if (value == "=") return cont(expressionNoComma);
  458. }
  459. function vardefCont(type) {
  460. if (type == ",") return cont(vardef);
  461. }
  462. function maybeelse(type, value) {
  463. if (type == "keyword b" && value == "else") return cont(pushlex("form"), statement, poplex);
  464. }
  465. function forspec(type) {
  466. if (type == "(") return cont(pushlex(")"), forspec1, expect(")"), poplex);
  467. }
  468. function forspec1(type) {
  469. if (type == "var") return cont(vardef, expect(";"), forspec2);
  470. if (type == ";") return cont(forspec2);
  471. if (type == "variable") return cont(formaybeinof);
  472. return pass(expression, expect(";"), forspec2);
  473. }
  474. function formaybeinof(_type, value) {
  475. if (value == "in" || value == "of") { cx.marked = "keyword"; return cont(expression); }
  476. return cont(maybeoperatorComma, forspec2);
  477. }
  478. function forspec2(type, value) {
  479. if (type == ";") return cont(forspec3);
  480. if (value == "in" || value == "of") { cx.marked = "keyword"; return cont(expression); }
  481. return pass(expression, expect(";"), forspec3);
  482. }
  483. function forspec3(type) {
  484. if (type != ")") cont(expression);
  485. }
  486. function functiondef(type, value) {
  487. if (value == "*") {cx.marked = "keyword"; return cont(functiondef);}
  488. if (type == "variable") {register(value); return cont(functiondef);}
  489. if (type == "(") return cont(pushcontext, pushlex(")"), commasep(funarg, ")"), poplex, statement, popcontext);
  490. }
  491. function funarg(type) {
  492. if (type == "spread") return cont(funarg);
  493. return pass(pattern, maybetype);
  494. }
  495. function className(type, value) {
  496. if (type == "variable") {register(value); return cont(classNameAfter);}
  497. }
  498. function classNameAfter(_type, value) {
  499. if (value == "extends") return cont(expression);
  500. }
  501. function objlit(type) {
  502. if (type == "{") return contCommasep(objprop, "}");
  503. }
  504. function afterModule(type, value) {
  505. if (type == "string") return cont(statement);
  506. if (type == "variable") { register(value); return cont(maybeFrom); }
  507. }
  508. function afterExport(_type, value) {
  509. if (value == "*") { cx.marked = "keyword"; return cont(maybeFrom, expect(";")); }
  510. if (value == "default") { cx.marked = "keyword"; return cont(expression, expect(";")); }
  511. return pass(statement);
  512. }
  513. function afterImport(type) {
  514. if (type == "string") return cont();
  515. return pass(importSpec, maybeFrom);
  516. }
  517. function importSpec(type, value) {
  518. if (type == "{") return contCommasep(importSpec, "}");
  519. if (type == "variable") register(value);
  520. return cont();
  521. }
  522. function maybeFrom(_type, value) {
  523. if (value == "from") { cx.marked = "keyword"; return cont(expression); }
  524. }
  525. function arrayLiteral(type) {
  526. if (type == "]") return cont();
  527. return pass(expressionNoComma, maybeArrayComprehension);
  528. }
  529. function maybeArrayComprehension(type) {
  530. if (type == "for") return pass(comprehension, expect("]"));
  531. if (type == ",") return cont(commasep(expressionNoComma, "]"));
  532. return pass(commasep(expressionNoComma, "]"));
  533. }
  534. function comprehension(type) {
  535. if (type == "for") return cont(forspec, comprehension);
  536. if (type == "if") return cont(expression, comprehension);
  537. }
  538. // Interface
  539. return {
  540. startState: function(basecolumn) {
  541. var state = {
  542. tokenize: tokenBase,
  543. lastType: "sof",
  544. cc: [],
  545. lexical: new JSLexical((basecolumn || 0) - indentUnit, 0, "block", false),
  546. localVars: parserConfig.localVars,
  547. context: parserConfig.localVars && {vars: parserConfig.localVars},
  548. indented: 0
  549. };
  550. if (parserConfig.globalVars && typeof parserConfig.globalVars == "object")
  551. state.globalVars = parserConfig.globalVars;
  552. return state;
  553. },
  554. token: function(stream, state) {
  555. if (stream.sol()) {
  556. if (!state.lexical.hasOwnProperty("align"))
  557. state.lexical.align = false;
  558. state.indented = stream.indentation();
  559. findFatArrow(stream, state);
  560. }
  561. if (state.tokenize != tokenComment && stream.eatSpace()) return null;
  562. var style = state.tokenize(stream, state);
  563. if (type == "comment") return style;
  564. state.lastType = type == "operator" && (content == "++" || content == "--") ? "incdec" : type;
  565. return parseJS(state, style, type, content, stream);
  566. },
  567. indent: function(state, textAfter) {
  568. if (state.tokenize == tokenComment) return CodeMirror.Pass;
  569. if (state.tokenize != tokenBase) return 0;
  570. var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical;
  571. // Kludge to prevent 'maybelse' from blocking lexical scope pops
  572. for (var i = state.cc.length - 1; i >= 0; --i) {
  573. var c = state.cc[i];
  574. if (c == poplex) lexical = lexical.prev;
  575. else if (c != maybeelse) break;
  576. }
  577. if (lexical.type == "stat" && firstChar == "}") lexical = lexical.prev;
  578. if (statementIndent && lexical.type == ")" && lexical.prev.type == "stat")
  579. lexical = lexical.prev;
  580. var type = lexical.type, closing = firstChar == type;
  581. if (type == "vardef") return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? lexical.info + 1 : 0);
  582. else if (type == "form" && firstChar == "{") return lexical.indented;
  583. else if (type == "form") return lexical.indented + indentUnit;
  584. else if (type == "stat")
  585. return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? statementIndent || indentUnit : 0);
  586. else if (lexical.info == "switch" && !closing && parserConfig.doubleIndentSwitch != false)
  587. return lexical.indented + (/^(?:case|default)\b/.test(textAfter) ? indentUnit : 2 * indentUnit);
  588. else if (lexical.align) return lexical.column + (closing ? 0 : 1);
  589. else return lexical.indented + (closing ? 0 : indentUnit);
  590. },
  591. electricChars: ":{}",
  592. blockCommentStart: jsonMode ? null : "/*",
  593. blockCommentEnd: jsonMode ? null : "*/",
  594. lineComment: jsonMode ? null : "//",
  595. fold: "brace",
  596. helperType: jsonMode ? "json" : "javascript",
  597. jsonldMode: jsonldMode,
  598. jsonMode: jsonMode
  599. };
  600. });
  601. CodeMirror.defineMIME("text/javascript", "javascript");
  602. CodeMirror.defineMIME("text/ecmascript", "javascript");
  603. CodeMirror.defineMIME("application/javascript", "javascript");
  604. CodeMirror.defineMIME("application/ecmascript", "javascript");
  605. CodeMirror.defineMIME("application/json", {name: "javascript", json: true});
  606. CodeMirror.defineMIME("application/x-json", {name: "javascript", json: true});
  607. CodeMirror.defineMIME("application/ld+json", {name: "javascript", jsonld: true});
  608. CodeMirror.defineMIME("text/typescript", { name: "javascript", typescript: true });
  609. CodeMirror.defineMIME("application/typescript", { name: "javascript", typescript: true });
  610. });