javascript.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. // TODO actually recognize syntax of TypeScript constructs
  2. CodeMirror.defineMode("javascript", function(config, parserConfig) {
  3. var indentUnit = config.indentUnit;
  4. var jsonMode = parserConfig.json;
  5. var isTS = parserConfig.typescript;
  6. // Tokenizer
  7. var keywords = function(){
  8. function kw(type) {return {type: type, style: "keyword"};}
  9. var A = kw("keyword a"), B = kw("keyword b"), C = kw("keyword c");
  10. var operator = kw("operator"), atom = {type: "atom", style: "atom"};
  11. var jsKeywords = {
  12. "if": A, "while": A, "with": A, "else": B, "do": B, "try": B, "finally": B,
  13. "return": C, "break": C, "continue": C, "new": C, "delete": C, "throw": C,
  14. "var": kw("var"), "const": kw("var"), "let": kw("var"),
  15. "function": kw("function"), "catch": kw("catch"),
  16. "for": kw("for"), "switch": kw("switch"), "case": kw("case"), "default": kw("default"),
  17. "in": operator, "typeof": operator, "instanceof": operator,
  18. "true": atom, "false": atom, "null": atom, "undefined": atom, "NaN": atom, "Infinity": atom,
  19. "this": kw("this")
  20. };
  21. // Extend the 'normal' keywords with the TypeScript language extensions
  22. if (isTS) {
  23. var type = {type: "variable", style: "variable-3"};
  24. var tsKeywords = {
  25. // object-like things
  26. "interface": kw("interface"),
  27. "class": kw("class"),
  28. "extends": kw("extends"),
  29. "constructor": kw("constructor"),
  30. // scope modifiers
  31. "public": kw("public"),
  32. "private": kw("private"),
  33. "protected": kw("protected"),
  34. "static": kw("static"),
  35. "super": kw("super"),
  36. // types
  37. "string": type, "number": type, "bool": type, "any": type
  38. };
  39. for (var attr in tsKeywords) {
  40. jsKeywords[attr] = tsKeywords[attr];
  41. }
  42. }
  43. return jsKeywords;
  44. }();
  45. var isOperatorChar = /[+\-*&%=<>!?|~^]/;
  46. function chain(stream, state, f) {
  47. state.tokenize = f;
  48. return f(stream, state);
  49. }
  50. function nextUntilUnescaped(stream, end) {
  51. var escaped = false, next;
  52. while ((next = stream.next()) != null) {
  53. if (next == end && !escaped)
  54. return false;
  55. escaped = !escaped && next == "\\";
  56. }
  57. return escaped;
  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 jsTokenBase(stream, state) {
  67. var ch = stream.next();
  68. if (ch == '"' || ch == "'")
  69. return chain(stream, state, jsTokenString(ch));
  70. else if (/[\[\]{}\(\),;\:\.]/.test(ch))
  71. return ret(ch);
  72. else if (ch == "0" && stream.eat(/x/i)) {
  73. stream.eatWhile(/[\da-f]/i);
  74. return ret("number", "number");
  75. }
  76. else if (/\d/.test(ch) || ch == "-" && stream.eat(/\d/)) {
  77. stream.match(/^\d*(?:\.\d*)?(?:[eE][+\-]?\d+)?/);
  78. return ret("number", "number");
  79. }
  80. else if (ch == "/") {
  81. if (stream.eat("*")) {
  82. return chain(stream, state, jsTokenComment);
  83. }
  84. else if (stream.eat("/")) {
  85. stream.skipToEnd();
  86. return ret("comment", "comment");
  87. }
  88. else if (state.lastType == "operator" || state.lastType == "keyword c" ||
  89. /^[\[{}\(,;:]$/.test(state.lastType)) {
  90. nextUntilUnescaped(stream, "/");
  91. stream.eatWhile(/[gimy]/); // 'y' is "sticky" option in Mozilla
  92. return ret("regexp", "string-2");
  93. }
  94. else {
  95. stream.eatWhile(isOperatorChar);
  96. return ret("operator", null, stream.current());
  97. }
  98. }
  99. else if (ch == "#") {
  100. stream.skipToEnd();
  101. return ret("error", "error");
  102. }
  103. else if (isOperatorChar.test(ch)) {
  104. stream.eatWhile(isOperatorChar);
  105. return ret("operator", null, stream.current());
  106. }
  107. else {
  108. stream.eatWhile(/[\w\$_]/);
  109. var word = stream.current(), known = keywords.propertyIsEnumerable(word) && keywords[word];
  110. return (known && state.lastType != ".") ? ret(known.type, known.style, word) :
  111. ret("variable", "variable", word);
  112. }
  113. }
  114. function jsTokenString(quote) {
  115. return function(stream, state) {
  116. if (!nextUntilUnescaped(stream, quote))
  117. state.tokenize = jsTokenBase;
  118. return ret("string", "string");
  119. };
  120. }
  121. function jsTokenComment(stream, state) {
  122. var maybeEnd = false, ch;
  123. while (ch = stream.next()) {
  124. if (ch == "/" && maybeEnd) {
  125. state.tokenize = jsTokenBase;
  126. break;
  127. }
  128. maybeEnd = (ch == "*");
  129. }
  130. return ret("comment", "comment");
  131. }
  132. // Parser
  133. var atomicTypes = {"atom": true, "number": true, "variable": true, "string": true, "regexp": true, "this": true};
  134. function JSLexical(indented, column, type, align, prev, info) {
  135. this.indented = indented;
  136. this.column = column;
  137. this.type = type;
  138. this.prev = prev;
  139. this.info = info;
  140. if (align != null) this.align = align;
  141. }
  142. function inScope(state, varname) {
  143. for (var v = state.localVars; v; v = v.next)
  144. if (v.name == varname) return true;
  145. }
  146. function parseJS(state, style, type, content, stream) {
  147. var cc = state.cc;
  148. // Communicate our context to the combinators.
  149. // (Less wasteful than consing up a hundred closures on every call.)
  150. cx.state = state; cx.stream = stream; cx.marked = null, cx.cc = cc;
  151. if (!state.lexical.hasOwnProperty("align"))
  152. state.lexical.align = true;
  153. while(true) {
  154. var combinator = cc.length ? cc.pop() : jsonMode ? expression : statement;
  155. if (combinator(type, content)) {
  156. while(cc.length && cc[cc.length - 1].lex)
  157. cc.pop()();
  158. if (cx.marked) return cx.marked;
  159. if (type == "variable" && inScope(state, content)) return "variable-2";
  160. return style;
  161. }
  162. }
  163. }
  164. // Combinator utils
  165. var cx = {state: null, column: null, marked: null, cc: null};
  166. function pass() {
  167. for (var i = arguments.length - 1; i >= 0; i--) cx.cc.push(arguments[i]);
  168. }
  169. function cont() {
  170. pass.apply(null, arguments);
  171. return true;
  172. }
  173. function register(varname) {
  174. function inList(list) {
  175. for (var v = list; v; v = v.next)
  176. if (v.name == varname) return true;
  177. return false;
  178. }
  179. var state = cx.state;
  180. if (state.context) {
  181. cx.marked = "def";
  182. if (inList(state.localVars)) return;
  183. state.localVars = {name: varname, next: state.localVars};
  184. } else {
  185. if (inList(state.globalVars)) return;
  186. state.globalVars = {name: varname, next: state.globalVars};
  187. }
  188. }
  189. // Combinators
  190. var defaultVars = {name: "this", next: {name: "arguments"}};
  191. function pushcontext() {
  192. cx.state.context = {prev: cx.state.context, vars: cx.state.localVars};
  193. cx.state.localVars = defaultVars;
  194. }
  195. function popcontext() {
  196. cx.state.localVars = cx.state.context.vars;
  197. cx.state.context = cx.state.context.prev;
  198. }
  199. function pushlex(type, info) {
  200. var result = function() {
  201. var state = cx.state;
  202. state.lexical = new JSLexical(state.indented, cx.stream.column(), type, null, state.lexical, info);
  203. };
  204. result.lex = true;
  205. return result;
  206. }
  207. function poplex() {
  208. var state = cx.state;
  209. if (state.lexical.prev) {
  210. if (state.lexical.type == ")")
  211. state.indented = state.lexical.indented;
  212. state.lexical = state.lexical.prev;
  213. }
  214. }
  215. poplex.lex = true;
  216. function expect(wanted) {
  217. return function(type) {
  218. if (type == wanted) return cont();
  219. else if (wanted == ";") return pass();
  220. else return cont(arguments.callee);
  221. };
  222. }
  223. function statement(type) {
  224. if (type == "var") return cont(pushlex("vardef"), vardef1, expect(";"), poplex);
  225. if (type == "keyword a") return cont(pushlex("form"), expression, statement, poplex);
  226. if (type == "keyword b") return cont(pushlex("form"), statement, poplex);
  227. if (type == "{") return cont(pushlex("}"), block, poplex);
  228. if (type == ";") return cont();
  229. if (type == "function") return cont(functiondef);
  230. if (type == "for") return cont(pushlex("form"), expect("("), pushlex(")"), forspec1, expect(")"),
  231. poplex, statement, poplex);
  232. if (type == "variable") return cont(pushlex("stat"), maybelabel);
  233. if (type == "switch") return cont(pushlex("form"), expression, pushlex("}", "switch"), expect("{"),
  234. block, poplex, poplex);
  235. if (type == "case") return cont(expression, expect(":"));
  236. if (type == "default") return cont(expect(":"));
  237. if (type == "catch") return cont(pushlex("form"), pushcontext, expect("("), funarg, expect(")"),
  238. statement, poplex, popcontext);
  239. return pass(pushlex("stat"), expression, expect(";"), poplex);
  240. }
  241. function expression(type) {
  242. return expressionInner(type, maybeoperatorComma);
  243. }
  244. function expressionNoComma(type) {
  245. return expressionInner(type, maybeoperatorNoComma);
  246. }
  247. function expressionInner(type, maybeop) {
  248. if (atomicTypes.hasOwnProperty(type)) return cont(maybeop);
  249. if (type == "function") return cont(functiondef);
  250. if (type == "keyword c") return cont(maybeexpression);
  251. if (type == "(") return cont(pushlex(")"), maybeexpression, expect(")"), poplex, maybeop);
  252. if (type == "operator") return cont(expression);
  253. if (type == "[") return cont(pushlex("]"), commasep(expressionNoComma, "]"), poplex, maybeop);
  254. if (type == "{") return cont(pushlex("}"), commasep(objprop, "}"), poplex, maybeop);
  255. return cont();
  256. }
  257. function maybeexpression(type) {
  258. if (type.match(/[;\}\)\],]/)) return pass();
  259. return pass(expression);
  260. }
  261. function maybeoperatorComma(type, value) {
  262. if (type == ",") return cont(expression);
  263. return maybeoperatorNoComma(type, value, maybeoperatorComma);
  264. }
  265. function maybeoperatorNoComma(type, value, me) {
  266. if (!me) me = maybeoperatorNoComma;
  267. if (type == "operator") {
  268. if (/\+\+|--/.test(value)) return cont(me);
  269. if (value == "?") return cont(expression, expect(":"), expression);
  270. return cont(expression);
  271. }
  272. if (type == ";") return;
  273. if (type == "(") return cont(pushlex(")", "call"), commasep(expressionNoComma, ")"), poplex, me);
  274. if (type == ".") return cont(property, me);
  275. if (type == "[") return cont(pushlex("]"), expression, expect("]"), poplex, me);
  276. }
  277. function maybelabel(type) {
  278. if (type == ":") return cont(poplex, statement);
  279. return pass(maybeoperatorComma, expect(";"), poplex);
  280. }
  281. function property(type) {
  282. if (type == "variable") {cx.marked = "property"; return cont();}
  283. }
  284. function objprop(type, value) {
  285. if (type == "variable") {
  286. cx.marked = "property";
  287. if (value == "get" || value == "set") return cont(getterSetter);
  288. } else if (type == "number" || type == "string") {
  289. cx.marked = type + " property";
  290. }
  291. if (atomicTypes.hasOwnProperty(type)) return cont(expect(":"), expressionNoComma);
  292. }
  293. function getterSetter(type) {
  294. if (type == ":") return cont(expression);
  295. if (type != "variable") return cont(expect(":"), expression);
  296. cx.marked = "property";
  297. return cont(functiondef);
  298. }
  299. function commasep(what, end) {
  300. function proceed(type) {
  301. if (type == ",") {
  302. var lex = cx.state.lexical;
  303. if (lex.info == "call") lex.pos = (lex.pos || 0) + 1;
  304. return cont(what, proceed);
  305. }
  306. if (type == end) return cont();
  307. return cont(expect(end));
  308. }
  309. return function(type) {
  310. if (type == end) return cont();
  311. else return pass(what, proceed);
  312. };
  313. }
  314. function block(type) {
  315. if (type == "}") return cont();
  316. return pass(statement, block);
  317. }
  318. function maybetype(type) {
  319. if (type == ":") return cont(typedef);
  320. return pass();
  321. }
  322. function typedef(type) {
  323. if (type == "variable"){cx.marked = "variable-3"; return cont();}
  324. return pass();
  325. }
  326. function vardef1(type, value) {
  327. if (type == "variable") {
  328. register(value);
  329. return isTS ? cont(maybetype, vardef2) : cont(vardef2);
  330. }
  331. return pass();
  332. }
  333. function vardef2(type, value) {
  334. if (value == "=") return cont(expressionNoComma, vardef2);
  335. if (type == ",") return cont(vardef1);
  336. }
  337. function forspec1(type) {
  338. if (type == "var") return cont(vardef1, expect(";"), forspec2);
  339. if (type == ";") return cont(forspec2);
  340. if (type == "variable") return cont(formaybein);
  341. return pass(expression, expect(";"), forspec2);
  342. }
  343. function formaybein(_type, value) {
  344. if (value == "in") return cont(expression);
  345. return cont(maybeoperatorComma, forspec2);
  346. }
  347. function forspec2(type, value) {
  348. if (type == ";") return cont(forspec3);
  349. if (value == "in") return cont(expression);
  350. return pass(expression, expect(";"), forspec3);
  351. }
  352. function forspec3(type) {
  353. if (type != ")") cont(expression);
  354. }
  355. function functiondef(type, value) {
  356. if (type == "variable") {register(value); return cont(functiondef);}
  357. if (type == "(") return cont(pushlex(")"), pushcontext, commasep(funarg, ")"), poplex, statement, popcontext);
  358. }
  359. function funarg(type, value) {
  360. if (type == "variable") {register(value); return isTS ? cont(maybetype) : cont();}
  361. }
  362. // Interface
  363. return {
  364. startState: function(basecolumn) {
  365. return {
  366. tokenize: jsTokenBase,
  367. lastType: null,
  368. cc: [],
  369. lexical: new JSLexical((basecolumn || 0) - indentUnit, 0, "block", false),
  370. localVars: parserConfig.localVars,
  371. globalVars: parserConfig.globalVars,
  372. context: parserConfig.localVars && {vars: parserConfig.localVars},
  373. indented: 0
  374. };
  375. },
  376. token: function(stream, state) {
  377. if (stream.sol()) {
  378. if (!state.lexical.hasOwnProperty("align"))
  379. state.lexical.align = false;
  380. state.indented = stream.indentation();
  381. }
  382. if (state.tokenize != jsTokenComment && stream.eatSpace()) return null;
  383. var style = state.tokenize(stream, state);
  384. if (type == "comment") return style;
  385. state.lastType = type;
  386. return parseJS(state, style, type, content, stream);
  387. },
  388. indent: function(state, textAfter) {
  389. if (state.tokenize == jsTokenComment) return CodeMirror.Pass;
  390. if (state.tokenize != jsTokenBase) return 0;
  391. var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical;
  392. if (lexical.type == "stat" && firstChar == "}") lexical = lexical.prev;
  393. var type = lexical.type, closing = firstChar == type;
  394. if (parserConfig.statementIndent != null) {
  395. if (type == ")" && lexical.prev && lexical.prev.type == "stat") lexical = lexical.prev;
  396. if (lexical.type == "stat") return lexical.indented + parserConfig.statementIndent;
  397. }
  398. if (type == "vardef") return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? 4 : 0);
  399. else if (type == "form" && firstChar == "{") return lexical.indented;
  400. else if (type == "form") return lexical.indented + indentUnit;
  401. else if (type == "stat")
  402. return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? indentUnit : 0);
  403. else if (lexical.info == "switch" && !closing)
  404. return lexical.indented + (/^(?:case|default)\b/.test(textAfter) ? indentUnit : 2 * indentUnit);
  405. else if (lexical.align) return lexical.column + (closing ? 0 : 1);
  406. else return lexical.indented + (closing ? 0 : indentUnit);
  407. },
  408. electricChars: ":{}",
  409. jsonMode: jsonMode
  410. };
  411. });
  412. CodeMirror.defineMIME("text/javascript", "javascript");
  413. CodeMirror.defineMIME("text/ecmascript", "javascript");
  414. CodeMirror.defineMIME("application/javascript", "javascript");
  415. CodeMirror.defineMIME("application/ecmascript", "javascript");
  416. CodeMirror.defineMIME("application/json", {name: "javascript", json: true});
  417. CodeMirror.defineMIME("text/typescript", { name: "javascript", typescript: true });
  418. CodeMirror.defineMIME("application/typescript", { name: "javascript", typescript: true });