1 // TODO actually recognize syntax of TypeScript constructs
3 CodeMirror.defineMode("javascript", function(config, parserConfig) {
4 var indentUnit = config.indentUnit;
5 var statementIndent = parserConfig.statementIndent;
6 var jsonMode = parserConfig.json;
7 var isTS = parserConfig.typescript;
11 var keywords = function(){
12 function kw(type) {return {type: type, style: "keyword"};}
13 var A = kw("keyword a"), B = kw("keyword b"), C = kw("keyword c");
14 var operator = kw("operator"), atom = {type: "atom", style: "atom"};
17 "if": kw("if"), "while": A, "with": A, "else": B, "do": B, "try": B, "finally": B,
18 "return": C, "break": C, "continue": C, "new": C, "delete": C, "throw": C,
19 "var": kw("var"), "const": kw("var"), "let": kw("var"),
20 "function": kw("function"), "catch": kw("catch"),
21 "for": kw("for"), "switch": kw("switch"), "case": kw("case"), "default": kw("default"),
22 "in": operator, "typeof": operator, "instanceof": operator,
23 "true": atom, "false": atom, "null": atom, "undefined": atom, "NaN": atom, "Infinity": atom,
27 // Extend the 'normal' keywords with the TypeScript language extensions
29 var type = {type: "variable", style: "variable-3"};
32 "interface": kw("interface"),
34 "extends": kw("extends"),
35 "constructor": kw("constructor"),
38 "public": kw("public"),
39 "private": kw("private"),
40 "protected": kw("protected"),
41 "static": kw("static"),
46 "string": type, "number": type, "bool": type, "any": type
49 for (var attr in tsKeywords) {
50 jsKeywords[attr] = tsKeywords[attr];
57 var isOperatorChar = /[+\-*&%=<>!?|~^]/;
59 function chain(stream, state, f) {
61 return f(stream, state);
64 function nextUntilUnescaped(stream, end) {
65 var escaped = false, next;
66 while ((next = stream.next()) != null) {
67 if (next == end && !escaped)
69 escaped = !escaped && next == "\\";
74 // Used as scratch variables to communicate multiple values without
75 // consing up tons of objects.
77 function ret(tp, style, cont) {
78 type = tp; content = cont;
82 function jsTokenBase(stream, state) {
83 var ch = stream.next();
84 if (ch == '"' || ch == "'")
85 return chain(stream, state, jsTokenString(ch));
86 else if (/[\[\]{}\(\),;\:\.]/.test(ch))
88 else if (ch == "0" && stream.eat(/x/i)) {
89 stream.eatWhile(/[\da-f]/i);
90 return ret("number", "number");
92 else if (/\d/.test(ch) || ch == "-" && stream.eat(/\d/)) {
93 stream.match(/^\d*(?:\.\d*)?(?:[eE][+\-]?\d+)?/);
94 return ret("number", "number");
97 if (stream.eat("*")) {
98 return chain(stream, state, jsTokenComment);
100 else if (stream.eat("/")) {
102 return ret("comment", "comment");
104 else if (state.lastType == "operator" || state.lastType == "keyword c" ||
105 /^[\[{}\(,;:]$/.test(state.lastType)) {
106 nextUntilUnescaped(stream, "/");
107 stream.eatWhile(/[gimy]/); // 'y' is "sticky" option in Mozilla
108 return ret("regexp", "string-2");
111 stream.eatWhile(isOperatorChar);
112 return ret("operator", null, stream.current());
115 else if (ch == "#") {
117 return ret("error", "error");
119 else if (isOperatorChar.test(ch)) {
120 stream.eatWhile(isOperatorChar);
121 return ret("operator", null, stream.current());
124 stream.eatWhile(/[\w\$_]/);
125 var word = stream.current(), known = keywords.propertyIsEnumerable(word) && keywords[word];
126 return (known && state.lastType != ".") ? ret(known.type, known.style, word) :
127 ret("variable", "variable", word);
131 function jsTokenString(quote) {
132 return function(stream, state) {
133 if (!nextUntilUnescaped(stream, quote))
134 state.tokenize = jsTokenBase;
135 return ret("string", "string");
139 function jsTokenComment(stream, state) {
140 var maybeEnd = false, ch;
141 while (ch = stream.next()) {
142 if (ch == "/" && maybeEnd) {
143 state.tokenize = jsTokenBase;
146 maybeEnd = (ch == "*");
148 return ret("comment", "comment");
153 var atomicTypes = {"atom": true, "number": true, "variable": true, "string": true, "regexp": true, "this": true};
155 function JSLexical(indented, column, type, align, prev, info) {
156 this.indented = indented;
157 this.column = column;
161 if (align != null) this.align = align;
164 function inScope(state, varname) {
165 for (var v = state.localVars; v; v = v.next)
166 if (v.name == varname) return true;
169 function parseJS(state, style, type, content, stream) {
171 // Communicate our context to the combinators.
172 // (Less wasteful than consing up a hundred closures on every call.)
173 cx.state = state; cx.stream = stream; cx.marked = null, cx.cc = cc;
175 if (!state.lexical.hasOwnProperty("align"))
176 state.lexical.align = true;
179 var combinator = cc.length ? cc.pop() : jsonMode ? expression : statement;
180 if (combinator(type, content)) {
181 while(cc.length && cc[cc.length - 1].lex)
183 if (cx.marked) return cx.marked;
184 if (type == "variable" && inScope(state, content)) return "variable-2";
192 var cx = {state: null, column: null, marked: null, cc: null};
194 for (var i = arguments.length - 1; i >= 0; i--) cx.cc.push(arguments[i]);
197 pass.apply(null, arguments);
200 function register(varname) {
201 function inList(list) {
202 for (var v = list; v; v = v.next)
203 if (v.name == varname) return true;
206 var state = cx.state;
209 if (inList(state.localVars)) return;
210 state.localVars = {name: varname, next: state.localVars};
212 if (inList(state.globalVars)) return;
213 state.globalVars = {name: varname, next: state.globalVars};
219 var defaultVars = {name: "this", next: {name: "arguments"}};
220 function pushcontext() {
221 cx.state.context = {prev: cx.state.context, vars: cx.state.localVars};
222 cx.state.localVars = defaultVars;
224 function popcontext() {
225 cx.state.localVars = cx.state.context.vars;
226 cx.state.context = cx.state.context.prev;
228 function pushlex(type, info) {
229 var result = function() {
230 var state = cx.state, indent = state.indented;
231 if (state.lexical.type == "stat") indent = state.lexical.indented;
232 state.lexical = new JSLexical(indent, cx.stream.column(), type, null, state.lexical, info);
238 var state = cx.state;
239 if (state.lexical.prev) {
240 if (state.lexical.type == ")")
241 state.indented = state.lexical.indented;
242 state.lexical = state.lexical.prev;
247 function expect(wanted) {
248 return function(type) {
249 if (type == wanted) return cont();
250 else if (wanted == ";") return pass();
251 else return cont(arguments.callee);
255 function statement(type) {
256 if (type == "var") return cont(pushlex("vardef"), vardef1, expect(";"), poplex);
257 if (type == "keyword a") return cont(pushlex("form"), expression, statement, poplex);
258 if (type == "keyword b") return cont(pushlex("form"), statement, poplex);
259 if (type == "{") return cont(pushlex("}"), block, poplex);
260 if (type == ";") return cont();
261 if (type == "if") return cont(pushlex("form"), expression, statement, poplex, maybeelse);
262 if (type == "function") return cont(functiondef);
263 if (type == "for") return cont(pushlex("form"), expect("("), pushlex(")"), forspec1, expect(")"),
264 poplex, statement, poplex);
265 if (type == "variable") return cont(pushlex("stat"), maybelabel);
266 if (type == "switch") return cont(pushlex("form"), expression, pushlex("}", "switch"), expect("{"),
267 block, poplex, poplex);
268 if (type == "case") return cont(expression, expect(":"));
269 if (type == "default") return cont(expect(":"));
270 if (type == "catch") return cont(pushlex("form"), pushcontext, expect("("), funarg, expect(")"),
271 statement, poplex, popcontext);
272 return pass(pushlex("stat"), expression, expect(";"), poplex);
274 function expression(type) {
275 return expressionInner(type, false);
277 function expressionNoComma(type) {
278 return expressionInner(type, true);
280 function expressionInner(type, noComma) {
281 var maybeop = noComma ? maybeoperatorNoComma : maybeoperatorComma;
282 if (atomicTypes.hasOwnProperty(type)) return cont(maybeop);
283 if (type == "function") return cont(functiondef);
284 if (type == "keyword c") return cont(noComma ? maybeexpressionNoComma : maybeexpression);
285 if (type == "(") return cont(pushlex(")"), maybeexpression, expect(")"), poplex, maybeop);
286 if (type == "operator") return cont(noComma ? expressionNoComma : expression);
287 if (type == "[") return cont(pushlex("]"), commasep(expressionNoComma, "]"), poplex, maybeop);
288 if (type == "{") return cont(pushlex("}"), commasep(objprop, "}"), poplex, maybeop);
291 function maybeexpression(type) {
292 if (type.match(/[;\}\)\],]/)) return pass();
293 return pass(expression);
295 function maybeexpressionNoComma(type) {
296 if (type.match(/[;\}\)\],]/)) return pass();
297 return pass(expressionNoComma);
300 function maybeoperatorComma(type, value) {
301 if (type == ",") return cont(expression);
302 return maybeoperatorNoComma(type, value, false);
304 function maybeoperatorNoComma(type, value, noComma) {
305 var me = noComma == false ? maybeoperatorComma : maybeoperatorNoComma;
306 var expr = noComma == false ? expression : expressionNoComma;
307 if (type == "operator") {
308 if (/\+\+|--/.test(value)) return cont(me);
309 if (value == "?") return cont(expression, expect(":"), expr);
312 if (type == ";") return;
313 if (type == "(") return cont(pushlex(")", "call"), commasep(expressionNoComma, ")"), poplex, me);
314 if (type == ".") return cont(property, me);
315 if (type == "[") return cont(pushlex("]"), maybeexpression, expect("]"), poplex, me);
317 function maybelabel(type) {
318 if (type == ":") return cont(poplex, statement);
319 return pass(maybeoperatorComma, expect(";"), poplex);
321 function property(type) {
322 if (type == "variable") {cx.marked = "property"; return cont();}
324 function objprop(type, value) {
325 if (type == "variable") {
326 cx.marked = "property";
327 if (value == "get" || value == "set") return cont(getterSetter);
328 } else if (type == "number" || type == "string") {
329 cx.marked = type + " property";
331 if (atomicTypes.hasOwnProperty(type)) return cont(expect(":"), expressionNoComma);
333 function getterSetter(type) {
334 if (type == ":") return cont(expression);
335 if (type != "variable") return cont(expect(":"), expression);
336 cx.marked = "property";
337 return cont(functiondef);
339 function commasep(what, end) {
340 function proceed(type) {
342 var lex = cx.state.lexical;
343 if (lex.info == "call") lex.pos = (lex.pos || 0) + 1;
344 return cont(what, proceed);
346 if (type == end) return cont();
347 return cont(expect(end));
349 return function(type) {
350 if (type == end) return cont();
351 else return pass(what, proceed);
354 function block(type) {
355 if (type == "}") return cont();
356 return pass(statement, block);
358 function maybetype(type) {
359 if (type == ":") return cont(typedef);
362 function typedef(type) {
363 if (type == "variable"){cx.marked = "variable-3"; return cont();}
366 function vardef1(type, value) {
367 if (type == "variable") {
369 return isTS ? cont(maybetype, vardef2) : cont(vardef2);
373 function vardef2(type, value) {
374 if (value == "=") return cont(expressionNoComma, vardef2);
375 if (type == ",") return cont(vardef1);
377 function maybeelse(type, value) {
378 if (type == "keyword b" && value == "else") return cont(pushlex("form"), statement, poplex);
380 function forspec1(type) {
381 if (type == "var") return cont(vardef1, expect(";"), forspec2);
382 if (type == ";") return cont(forspec2);
383 if (type == "variable") return cont(formaybein);
384 return pass(expression, expect(";"), forspec2);
386 function formaybein(_type, value) {
387 if (value == "in") return cont(expression);
388 return cont(maybeoperatorComma, forspec2);
390 function forspec2(type, value) {
391 if (type == ";") return cont(forspec3);
392 if (value == "in") return cont(expression);
393 return pass(expression, expect(";"), forspec3);
395 function forspec3(type) {
396 if (type != ")") cont(expression);
398 function functiondef(type, value) {
399 if (type == "variable") {register(value); return cont(functiondef);}
400 if (type == "(") return cont(pushlex(")"), pushcontext, commasep(funarg, ")"), poplex, statement, popcontext);
402 function funarg(type, value) {
403 if (type == "variable") {register(value); return isTS ? cont(maybetype) : cont();}
409 startState: function(basecolumn) {
411 tokenize: jsTokenBase,
414 lexical: new JSLexical((basecolumn || 0) - indentUnit, 0, "block", false),
415 localVars: parserConfig.localVars,
416 globalVars: parserConfig.globalVars,
417 context: parserConfig.localVars && {vars: parserConfig.localVars},
422 token: function(stream, state) {
424 if (!state.lexical.hasOwnProperty("align"))
425 state.lexical.align = false;
426 state.indented = stream.indentation();
428 if (state.tokenize != jsTokenComment && stream.eatSpace()) return null;
429 var style = state.tokenize(stream, state);
430 if (type == "comment") return style;
431 state.lastType = type == "operator" && (content == "++" || content == "--") ? "incdec" : type;
432 return parseJS(state, style, type, content, stream);
435 indent: function(state, textAfter) {
436 if (state.tokenize == jsTokenComment) return CodeMirror.Pass;
437 if (state.tokenize != jsTokenBase) return 0;
438 var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical;
439 // Kludge to prevent 'maybelse' from blocking lexical scope pops
440 for (var i = state.cc.length - 1; i >= 0; --i) {
442 if (c == poplex) lexical = lexical.prev;
443 else if (c != maybeelse || /^else\b/.test(textAfter)) break;
445 if (lexical.type == "stat" && firstChar == "}") lexical = lexical.prev;
446 if (statementIndent && lexical.type == ")" && lexical.prev.type == "stat")
447 lexical = lexical.prev;
448 var type = lexical.type, closing = firstChar == type;
450 if (type == "vardef") return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? 4 : 0);
451 else if (type == "form" && firstChar == "{") return lexical.indented;
452 else if (type == "form") return lexical.indented + indentUnit;
453 else if (type == "stat")
454 return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? statementIndent || indentUnit : 0);
455 else if (lexical.info == "switch" && !closing && parserConfig.doubleIndentSwitch != false)
456 return lexical.indented + (/^(?:case|default)\b/.test(textAfter) ? indentUnit : 2 * indentUnit);
457 else if (lexical.align) return lexical.column + (closing ? 0 : 1);
458 else return lexical.indented + (closing ? 0 : indentUnit);
461 electricChars: ":{}",
462 blockCommentStart: jsonMode ? null : "/*",
463 blockCommentEnd: jsonMode ? null : "*/",
464 lineComment: jsonMode ? null : "//",
467 helperType: jsonMode ? "json" : "javascript",
472 CodeMirror.defineMIME("text/javascript", "javascript");
473 CodeMirror.defineMIME("text/ecmascript", "javascript");
474 CodeMirror.defineMIME("application/javascript", "javascript");
475 CodeMirror.defineMIME("application/ecmascript", "javascript");
476 CodeMirror.defineMIME("application/json", {name: "javascript", json: true});
477 CodeMirror.defineMIME("application/x-json", {name: "javascript", json: true});
478 CodeMirror.defineMIME("text/typescript", { name: "javascript", typescript: true });
479 CodeMirror.defineMIME("application/typescript", { name: "javascript", typescript: true });