DC Team 的 Laravel 基底框架、資料庫描述、開發規範、環境佈署、版控規範

javascript.js 24KB

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