javascript.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. var state = cx.state;
  174. if (state.context) {
  175. cx.marked = "def";
  176. for (var v = state.localVars; v; v = v.next)
  177. if (v.name == varname) return;
  178. state.localVars = {name: varname, next: state.localVars};
  179. }
  180. }
  181. // Combinators
  182. var defaultVars = {name: "this", next: {name: "arguments"}};
  183. function pushcontext() {
  184. cx.state.context = {prev: cx.state.context, vars: cx.state.localVars};
  185. cx.state.localVars = defaultVars;
  186. }
  187. function popcontext() {
  188. cx.state.localVars = cx.state.context.vars;
  189. cx.state.context = cx.state.context.prev;
  190. }
  191. function pushlex(type, info) {
  192. var result = function() {
  193. var state = cx.state;
  194. state.lexical = new JSLexical(state.indented, cx.stream.column(), type, null, state.lexical, info);
  195. };
  196. result.lex = true;
  197. return result;
  198. }
  199. function poplex() {
  200. var state = cx.state;
  201. if (state.lexical.prev) {
  202. if (state.lexical.type == ")")
  203. state.indented = state.lexical.indented;
  204. state.lexical = state.lexical.prev;
  205. }
  206. }
  207. poplex.lex = true;
  208. function expect(wanted) {
  209. return function expecting(type) {
  210. if (type == wanted) return cont();
  211. else if (wanted == ";") return pass();
  212. else return cont(arguments.callee);
  213. };
  214. }
  215. function statement(type) {
  216. if (type == "var") return cont(pushlex("vardef"), vardef1, expect(";"), poplex);
  217. if (type == "keyword a") return cont(pushlex("form"), expression, statement, poplex);
  218. if (type == "keyword b") return cont(pushlex("form"), statement, poplex);
  219. if (type == "{") return cont(pushlex("}"), block, poplex);
  220. if (type == ";") return cont();
  221. if (type == "function") return cont(functiondef);
  222. if (type == "for") return cont(pushlex("form"), expect("("), pushlex(")"), forspec1, expect(")"),
  223. poplex, statement, poplex);
  224. if (type == "variable") return cont(pushlex("stat"), maybelabel);
  225. if (type == "switch") return cont(pushlex("form"), expression, pushlex("}", "switch"), expect("{"),
  226. block, poplex, poplex);
  227. if (type == "case") return cont(expression, expect(":"));
  228. if (type == "default") return cont(expect(":"));
  229. if (type == "catch") return cont(pushlex("form"), pushcontext, expect("("), funarg, expect(")"),
  230. statement, poplex, popcontext);
  231. return pass(pushlex("stat"), expression, expect(";"), poplex);
  232. }
  233. function expression(type) {
  234. if (atomicTypes.hasOwnProperty(type)) return cont(maybeoperator);
  235. if (type == "function") return cont(functiondef);
  236. if (type == "keyword c") return cont(maybeexpression);
  237. if (type == "(") return cont(pushlex(")"), maybeexpression, expect(")"), poplex, maybeoperator);
  238. if (type == "operator") return cont(expression);
  239. if (type == "[") return cont(pushlex("]"), commasep(expression, "]"), poplex, maybeoperator);
  240. if (type == "{") return cont(pushlex("}"), commasep(objprop, "}"), poplex, maybeoperator);
  241. return cont();
  242. }
  243. function maybeexpression(type) {
  244. if (type.match(/[;\}\)\],]/)) return pass();
  245. return pass(expression);
  246. }
  247. function maybeoperator(type, value) {
  248. if (type == "operator" && /\+\+|--/.test(value)) return cont(maybeoperator);
  249. if (type == "operator" && value == "?") return cont(expression, expect(":"), expression);
  250. if (type == ";") return;
  251. if (type == "(") return cont(pushlex(")"), commasep(expression, ")"), poplex, maybeoperator);
  252. if (type == ".") return cont(property, maybeoperator);
  253. if (type == "[") return cont(pushlex("]"), expression, expect("]"), poplex, maybeoperator);
  254. }
  255. function maybelabel(type) {
  256. if (type == ":") return cont(poplex, statement);
  257. return pass(maybeoperator, expect(";"), poplex);
  258. }
  259. function property(type) {
  260. if (type == "variable") {cx.marked = "property"; return cont();}
  261. }
  262. function objprop(type) {
  263. if (type == "variable") cx.marked = "property";
  264. if (atomicTypes.hasOwnProperty(type)) return cont(expect(":"), expression);
  265. }
  266. function commasep(what, end) {
  267. function proceed(type) {
  268. if (type == ",") return cont(what, proceed);
  269. if (type == end) return cont();
  270. return cont(expect(end));
  271. }
  272. return function commaSeparated(type) {
  273. if (type == end) return cont();
  274. else return pass(what, proceed);
  275. };
  276. }
  277. function block(type) {
  278. if (type == "}") return cont();
  279. return pass(statement, block);
  280. }
  281. function maybetype(type) {
  282. if (type == ":") return cont(typedef);
  283. return pass();
  284. }
  285. function typedef(type) {
  286. if (type == "variable"){cx.marked = "variable-3"; return cont();}
  287. return pass();
  288. }
  289. function vardef1(type, value) {
  290. if (type == "variable") {
  291. register(value);
  292. return isTS ? cont(maybetype, vardef2) : cont(vardef2);
  293. }
  294. return pass();
  295. }
  296. function vardef2(type, value) {
  297. if (value == "=") return cont(expression, vardef2);
  298. if (type == ",") return cont(vardef1);
  299. }
  300. function forspec1(type) {
  301. if (type == "var") return cont(vardef1, expect(";"), forspec2);
  302. if (type == ";") return cont(forspec2);
  303. if (type == "variable") return cont(formaybein);
  304. return cont(forspec2);
  305. }
  306. function formaybein(_type, value) {
  307. if (value == "in") return cont(expression);
  308. return cont(maybeoperator, forspec2);
  309. }
  310. function forspec2(type, value) {
  311. if (type == ";") return cont(forspec3);
  312. if (value == "in") return cont(expression);
  313. return cont(expression, expect(";"), forspec3);
  314. }
  315. function forspec3(type) {
  316. if (type != ")") cont(expression);
  317. }
  318. function functiondef(type, value) {
  319. if (type == "variable") {register(value); return cont(functiondef);}
  320. if (type == "(") return cont(pushlex(")"), pushcontext, commasep(funarg, ")"), poplex, statement, popcontext);
  321. }
  322. function funarg(type, value) {
  323. if (type == "variable") {register(value); return isTS ? cont(maybetype) : cont();}
  324. }
  325. // Interface
  326. return {
  327. startState: function(basecolumn) {
  328. return {
  329. tokenize: jsTokenBase,
  330. lastType: null,
  331. cc: [],
  332. lexical: new JSLexical((basecolumn || 0) - indentUnit, 0, "block", false),
  333. localVars: parserConfig.localVars,
  334. context: parserConfig.localVars && {vars: parserConfig.localVars},
  335. indented: 0
  336. };
  337. },
  338. token: function(stream, state) {
  339. if (stream.sol()) {
  340. if (!state.lexical.hasOwnProperty("align"))
  341. state.lexical.align = false;
  342. state.indented = stream.indentation();
  343. }
  344. if (stream.eatSpace()) return null;
  345. var style = state.tokenize(stream, state);
  346. if (type == "comment") return style;
  347. state.lastType = type;
  348. return parseJS(state, style, type, content, stream);
  349. },
  350. indent: function(state, textAfter) {
  351. if (state.tokenize == jsTokenComment) return CodeMirror.Pass;
  352. if (state.tokenize != jsTokenBase) return 0;
  353. var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical;
  354. if (lexical.type == "stat" && firstChar == "}") lexical = lexical.prev;
  355. var type = lexical.type, closing = firstChar == type;
  356. if (type == "vardef") return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? 4 : 0);
  357. else if (type == "form" && firstChar == "{") return lexical.indented;
  358. else if (type == "form") return lexical.indented + indentUnit;
  359. else if (type == "stat")
  360. return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? indentUnit : 0);
  361. else if (lexical.info == "switch" && !closing)
  362. return lexical.indented + (/^(?:case|default)\b/.test(textAfter) ? indentUnit : 2 * indentUnit);
  363. else if (lexical.align) return lexical.column + (closing ? 0 : 1);
  364. else return lexical.indented + (closing ? 0 : indentUnit);
  365. },
  366. electricChars: ":{}",
  367. jsonMode: jsonMode
  368. };
  369. });
  370. CodeMirror.defineMIME("text/javascript", "javascript");
  371. CodeMirror.defineMIME("application/json", {name: "javascript", json: true});
  372. CodeMirror.defineMIME("text/typescript", { name: "javascript", typescript: true });
  373. CodeMirror.defineMIME("application/typescript", { name: "javascript", typescript: true });