javascript.js 15 KB

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