本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

931 lines
37KB

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