2
0

javascript.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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": kw("if"), "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 == "if") return cont(pushlex("form"), expression, statement, poplex, maybeelse(cx.state.indented));
  230. if (type == "function") return cont(functiondef);
  231. if (type == "for") return cont(pushlex("form"), expect("("), pushlex(")"), forspec1, expect(")"),
  232. poplex, statement, poplex);
  233. if (type == "variable") return cont(pushlex("stat"), maybelabel);
  234. if (type == "switch") return cont(pushlex("form"), expression, pushlex("}", "switch"), expect("{"),
  235. block, poplex, poplex);
  236. if (type == "case") return cont(expression, expect(":"));
  237. if (type == "default") return cont(expect(":"));
  238. if (type == "catch") return cont(pushlex("form"), pushcontext, expect("("), funarg, expect(")"),
  239. statement, poplex, popcontext);
  240. return pass(pushlex("stat"), expression, expect(";"), poplex);
  241. }
  242. function expression(type) {
  243. return expressionInner(type, maybeoperatorComma);
  244. }
  245. function expressionNoComma(type) {
  246. return expressionInner(type, maybeoperatorNoComma);
  247. }
  248. function expressionInner(type, maybeop) {
  249. if (atomicTypes.hasOwnProperty(type)) return cont(maybeop);
  250. if (type == "function") return cont(functiondef);
  251. if (type == "keyword c") return cont(maybeexpression);
  252. if (type == "(") return cont(pushlex(")"), maybeexpression, expect(")"), poplex, maybeop);
  253. if (type == "operator") return cont(expression);
  254. if (type == "[") return cont(pushlex("]"), commasep(expressionNoComma, "]"), poplex, maybeop);
  255. if (type == "{") return cont(pushlex("}"), commasep(objprop, "}"), poplex, maybeop);
  256. return cont();
  257. }
  258. function maybeexpression(type) {
  259. if (type.match(/[;\}\)\],]/)) return pass();
  260. return pass(expression);
  261. }
  262. function maybeoperatorComma(type, value) {
  263. if (type == ",") return pass();
  264. return maybeoperatorNoComma(type, value, maybeoperatorComma);
  265. }
  266. function maybeoperatorNoComma(type, value, me) {
  267. if (!me) me = maybeoperatorNoComma;
  268. if (type == "operator") {
  269. if (/\+\+|--/.test(value)) return cont(me);
  270. if (value == "?") return cont(expression, expect(":"), expression);
  271. return cont(expression);
  272. }
  273. if (type == ";") return;
  274. if (type == "(") return cont(pushlex(")", "call"), commasep(expressionNoComma, ")"), poplex, me);
  275. if (type == ".") return cont(property, me);
  276. if (type == "[") return cont(pushlex("]"), expression, expect("]"), poplex, me);
  277. }
  278. function maybelabel(type) {
  279. if (type == ":") return cont(poplex, statement);
  280. return pass(maybeoperatorComma, expect(";"), poplex);
  281. }
  282. function property(type) {
  283. if (type == "variable") {cx.marked = "property"; return cont();}
  284. }
  285. function objprop(type, value) {
  286. if (type == "variable") {
  287. cx.marked = "property";
  288. if (value == "get" || value == "set") return cont(getterSetter);
  289. } else if (type == "number" || type == "string") {
  290. cx.marked = type + " property";
  291. }
  292. if (atomicTypes.hasOwnProperty(type)) return cont(expect(":"), expressionNoComma);
  293. }
  294. function getterSetter(type) {
  295. if (type == ":") return cont(expression);
  296. if (type != "variable") return cont(expect(":"), expression);
  297. cx.marked = "property";
  298. return cont(functiondef);
  299. }
  300. function commasep(what, end) {
  301. function proceed(type) {
  302. if (type == ",") {
  303. var lex = cx.state.lexical;
  304. if (lex.info == "call") lex.pos = (lex.pos || 0) + 1;
  305. return cont(what, proceed);
  306. }
  307. if (type == end) return cont();
  308. return cont(expect(end));
  309. }
  310. return function(type) {
  311. if (type == end) return cont();
  312. else return pass(what, proceed);
  313. };
  314. }
  315. function block(type) {
  316. if (type == "}") return cont();
  317. return pass(statement, block);
  318. }
  319. function maybetype(type) {
  320. if (type == ":") return cont(typedef);
  321. return pass();
  322. }
  323. function typedef(type) {
  324. if (type == "variable"){cx.marked = "variable-3"; return cont();}
  325. return pass();
  326. }
  327. function vardef1(type, value) {
  328. if (type == "variable") {
  329. register(value);
  330. return isTS ? cont(maybetype, vardef2) : cont(vardef2);
  331. }
  332. return pass();
  333. }
  334. function vardef2(type, value) {
  335. if (value == "=") return cont(expressionNoComma, vardef2);
  336. if (type == ",") return cont(vardef1);
  337. }
  338. function maybeelse(indent) {
  339. return function(type, value) {
  340. if (type == "keyword b" && value == "else") {
  341. cx.state.lexical = new JSLexical(indent, 0, "form", null, cx.state.lexical);
  342. return cont(statement, poplex);
  343. }
  344. return pass();
  345. };
  346. }
  347. function forspec1(type) {
  348. if (type == "var") return cont(vardef1, expect(";"), forspec2);
  349. if (type == ";") return cont(forspec2);
  350. if (type == "variable") return cont(formaybein);
  351. return pass(expression, expect(";"), forspec2);
  352. }
  353. function formaybein(_type, value) {
  354. if (value == "in") return cont(expression);
  355. return cont(maybeoperatorComma, forspec2);
  356. }
  357. function forspec2(type, value) {
  358. if (type == ";") return cont(forspec3);
  359. if (value == "in") return cont(expression);
  360. return pass(expression, expect(";"), forspec3);
  361. }
  362. function forspec3(type) {
  363. if (type != ")") cont(expression);
  364. }
  365. function functiondef(type, value) {
  366. if (type == "variable") {register(value); return cont(functiondef);}
  367. if (type == "(") return cont(pushlex(")"), pushcontext, commasep(funarg, ")"), poplex, statement, popcontext);
  368. }
  369. function funarg(type, value) {
  370. if (type == "variable") {register(value); return isTS ? cont(maybetype) : cont();}
  371. }
  372. // Interface
  373. return {
  374. startState: function(basecolumn) {
  375. return {
  376. tokenize: jsTokenBase,
  377. lastType: null,
  378. cc: [],
  379. lexical: new JSLexical((basecolumn || 0) - indentUnit, 0, "block", false),
  380. localVars: parserConfig.localVars,
  381. globalVars: parserConfig.globalVars,
  382. context: parserConfig.localVars && {vars: parserConfig.localVars},
  383. indented: 0
  384. };
  385. },
  386. token: function(stream, state) {
  387. if (stream.sol()) {
  388. if (!state.lexical.hasOwnProperty("align"))
  389. state.lexical.align = false;
  390. state.indented = stream.indentation();
  391. }
  392. if (state.tokenize != jsTokenComment && stream.eatSpace()) return null;
  393. var style = state.tokenize(stream, state);
  394. if (type == "comment") return style;
  395. state.lastType = type == "operator" && (content == "++" || content == "--") ? "incdec" : type;
  396. return parseJS(state, style, type, content, stream);
  397. },
  398. indent: function(state, textAfter) {
  399. if (state.tokenize == jsTokenComment) return CodeMirror.Pass;
  400. if (state.tokenize != jsTokenBase) return 0;
  401. var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical;
  402. if (lexical.type == "stat" && firstChar == "}") lexical = lexical.prev;
  403. var type = lexical.type, closing = firstChar == type;
  404. if (parserConfig.statementIndent != null) {
  405. if (type == ")" && lexical.prev && lexical.prev.type == "stat") lexical = lexical.prev;
  406. if (lexical.type == "stat") return lexical.indented + parserConfig.statementIndent;
  407. }
  408. if (type == "vardef") return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? 4 : 0);
  409. else if (type == "form" && firstChar == "{") return lexical.indented;
  410. else if (type == "form") return lexical.indented + indentUnit;
  411. else if (type == "stat")
  412. return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? indentUnit : 0);
  413. else if (lexical.info == "switch" && !closing)
  414. return lexical.indented + (/^(?:case|default)\b/.test(textAfter) ? indentUnit : 2 * indentUnit);
  415. else if (lexical.align) return lexical.column + (closing ? 0 : 1);
  416. else return lexical.indented + (closing ? 0 : indentUnit);
  417. },
  418. electricChars: ":{}",
  419. blockCommentStart: jsonMode ? null : "/*",
  420. blockCommentEnd: jsonMode ? null : "*/",
  421. lineComment: jsonMode ? null : "//",
  422. jsonMode: jsonMode
  423. };
  424. });
  425. CodeMirror.defineMIME("text/javascript", "javascript");
  426. CodeMirror.defineMIME("text/ecmascript", "javascript");
  427. CodeMirror.defineMIME("application/javascript", "javascript");
  428. CodeMirror.defineMIME("application/ecmascript", "javascript");
  429. CodeMirror.defineMIME("application/json", {name: "javascript", json: true});
  430. CodeMirror.defineMIME("application/x-json", {name: "javascript", json: true});
  431. CodeMirror.defineMIME("text/typescript", { name: "javascript", typescript: true });
  432. CodeMirror.defineMIME("application/typescript", { name: "javascript", typescript: true });