1
CodeMirror.defineMode("javascript", function(config, parserConfig) {
2
var indentUnit = config.indentUnit;
3
var jsonMode = parserConfig.json;
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"};
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
22
var isOperatorChar = /[+\-*&%=<>!?|]/;
24
function chain(stream, state, f) {
26
return f(stream, state);
29
function nextUntilUnescaped(stream, end) {
30
var escaped = false, next;
31
while ((next = stream.next()) != null) {
32
if (next == end && !escaped)
34
escaped = !escaped && next == "\\";
39
// Used as scratch variables to communicate multiple values without
40
// consing up tons of objects.
42
function ret(tp, style, cont) {
43
type = tp; content = cont;
47
function jsTokenBase(stream, state) {
48
var ch = stream.next();
49
if (ch == '"' || ch == "'")
50
return chain(stream, state, jsTokenString(ch));
51
else if (/[\[\]{}\(\),;\:\.]/.test(ch))
53
else if (ch == "0" && stream.eat(/x/i)) {
54
stream.eatWhile(/[\da-f]/i);
55
return ret("number", "number");
57
else if (/\d/.test(ch)) {
58
stream.match(/^\d*(?:\.\d*)?(?:[eE][+\-]?\d+)?/);
59
return ret("number", "number");
62
if (stream.eat("*")) {
63
return chain(stream, state, jsTokenComment);
65
else if (stream.eat("/")) {
67
return ret("comment", "comment");
69
else if (state.reAllowed) {
70
nextUntilUnescaped(stream, "/");
71
stream.eatWhile(/[gimy]/); // 'y' is "sticky" option in Mozilla
72
return ret("regexp", "string-2");
75
stream.eatWhile(isOperatorChar);
76
return ret("operator", null, stream.current());
81
return ret("error", "error");
83
else if (isOperatorChar.test(ch)) {
84
stream.eatWhile(isOperatorChar);
85
return ret("operator", null, stream.current());
88
stream.eatWhile(/[\w\$_]/);
89
var word = stream.current(), known = keywords.propertyIsEnumerable(word) && keywords[word];
90
return (known && state.kwAllowed) ? ret(known.type, known.style, word) :
91
ret("variable", "variable", word);
95
function jsTokenString(quote) {
96
return function(stream, state) {
97
if (!nextUntilUnescaped(stream, quote))
98
state.tokenize = jsTokenBase;
99
return ret("string", "string");
103
function jsTokenComment(stream, state) {
104
var maybeEnd = false, ch;
105
while (ch = stream.next()) {
106
if (ch == "/" && maybeEnd) {
107
state.tokenize = jsTokenBase;
110
maybeEnd = (ch == "*");
112
return ret("comment", "comment");
117
var atomicTypes = {"atom": true, "number": true, "variable": true, "string": true, "regexp": true};
119
function JSLexical(indented, column, type, align, prev, info) {
120
this.indented = indented;
121
this.column = column;
125
if (align != null) this.align = align;
128
function inScope(state, varname) {
129
for (var v = state.localVars; v; v = v.next)
130
if (v.name == varname) return true;
133
function parseJS(state, style, type, content, stream) {
135
// Communicate our context to the combinators.
136
// (Less wasteful than consing up a hundred closures on every call.)
137
cx.state = state; cx.stream = stream; cx.marked = null, cx.cc = cc;
139
if (!state.lexical.hasOwnProperty("align"))
140
state.lexical.align = true;
143
var combinator = cc.length ? cc.pop() : jsonMode ? expression : statement;
144
if (combinator(type, content)) {
145
while(cc.length && cc[cc.length - 1].lex)
147
if (cx.marked) return cx.marked;
148
if (type == "variable" && inScope(state, content)) return "variable-2";
156
var cx = {state: null, column: null, marked: null, cc: null};
158
for (var i = arguments.length - 1; i >= 0; i--) cx.cc.push(arguments[i]);
161
pass.apply(null, arguments);
164
function register(varname) {
165
var state = cx.state;
168
for (var v = state.localVars; v; v = v.next)
169
if (v.name == varname) return;
170
state.localVars = {name: varname, next: state.localVars};
176
var defaultVars = {name: "this", next: {name: "arguments"}};
177
function pushcontext() {
178
if (!cx.state.context) cx.state.localVars = defaultVars;
179
cx.state.context = {prev: cx.state.context, vars: cx.state.localVars};
181
function popcontext() {
182
cx.state.localVars = cx.state.context.vars;
183
cx.state.context = cx.state.context.prev;
185
function pushlex(type, info) {
186
var result = function() {
187
var state = cx.state;
188
state.lexical = new JSLexical(state.indented, cx.stream.column(), type, null, state.lexical, info)
194
var state = cx.state;
195
if (state.lexical.prev) {
196
if (state.lexical.type == ")")
197
state.indented = state.lexical.indented;
198
state.lexical = state.lexical.prev;
203
function expect(wanted) {
204
return function expecting(type) {
205
if (type == wanted) return cont();
206
else if (wanted == ";") return pass();
207
else return cont(arguments.callee);
211
function statement(type) {
212
if (type == "var") return cont(pushlex("vardef"), vardef1, expect(";"), poplex);
213
if (type == "keyword a") return cont(pushlex("form"), expression, statement, poplex);
214
if (type == "keyword b") return cont(pushlex("form"), statement, poplex);
215
if (type == "{") return cont(pushlex("}"), block, poplex);
216
if (type == ";") return cont();
217
if (type == "function") return cont(functiondef);
218
if (type == "for") return cont(pushlex("form"), expect("("), pushlex(")"), forspec1, expect(")"),
219
poplex, statement, poplex);
220
if (type == "variable") return cont(pushlex("stat"), maybelabel);
221
if (type == "switch") return cont(pushlex("form"), expression, pushlex("}", "switch"), expect("{"),
222
block, poplex, poplex);
223
if (type == "case") return cont(expression, expect(":"));
224
if (type == "default") return cont(expect(":"));
225
if (type == "catch") return cont(pushlex("form"), pushcontext, expect("("), funarg, expect(")"),
226
statement, poplex, popcontext);
227
return pass(pushlex("stat"), expression, expect(";"), poplex);
229
function expression(type) {
230
if (atomicTypes.hasOwnProperty(type)) return cont(maybeoperator);
231
if (type == "function") return cont(functiondef);
232
if (type == "keyword c") return cont(maybeexpression);
233
if (type == "(") return cont(pushlex(")"), maybeexpression, expect(")"), poplex, maybeoperator);
234
if (type == "operator") return cont(expression);
235
if (type == "[") return cont(pushlex("]"), commasep(expression, "]"), poplex, maybeoperator);
236
if (type == "{") return cont(pushlex("}"), commasep(objprop, "}"), poplex, maybeoperator);
239
function maybeexpression(type) {
240
if (type.match(/[;\}\)\],]/)) return pass();
241
return pass(expression);
244
function maybeoperator(type, value) {
245
if (type == "operator" && /\+\+|--/.test(value)) return cont(maybeoperator);
246
if (type == "operator") return cont(expression);
247
if (type == ";") return;
248
if (type == "(") return cont(pushlex(")"), commasep(expression, ")"), poplex, maybeoperator);
249
if (type == ".") return cont(property, maybeoperator);
250
if (type == "[") return cont(pushlex("]"), expression, expect("]"), poplex, maybeoperator);
252
function maybelabel(type) {
253
if (type == ":") return cont(poplex, statement);
254
return pass(maybeoperator, expect(";"), poplex);
256
function property(type) {
257
if (type == "variable") {cx.marked = "property"; return cont();}
259
function objprop(type) {
260
if (type == "variable") cx.marked = "property";
261
if (atomicTypes.hasOwnProperty(type)) return cont(expect(":"), expression);
263
function commasep(what, end) {
264
function proceed(type) {
265
if (type == ",") return cont(what, proceed);
266
if (type == end) return cont();
267
return cont(expect(end));
269
return function commaSeparated(type) {
270
if (type == end) return cont();
271
else return pass(what, proceed);
274
function block(type) {
275
if (type == "}") return cont();
276
return pass(statement, block);
278
function vardef1(type, value) {
279
if (type == "variable"){register(value); return cont(vardef2);}
282
function vardef2(type, value) {
283
if (value == "=") return cont(expression, vardef2);
284
if (type == ",") return cont(vardef1);
286
function forspec1(type) {
287
if (type == "var") return cont(vardef1, forspec2);
288
if (type == ";") return pass(forspec2);
289
if (type == "variable") return cont(formaybein);
290
return pass(forspec2);
292
function formaybein(type, value) {
293
if (value == "in") return cont(expression);
294
return cont(maybeoperator, forspec2);
296
function forspec2(type, value) {
297
if (type == ";") return cont(forspec3);
298
if (value == "in") return cont(expression);
299
return cont(expression, expect(";"), forspec3);
301
function forspec3(type) {
302
if (type != ")") cont(expression);
304
function functiondef(type, value) {
305
if (type == "variable") {register(value); return cont(functiondef);}
306
if (type == "(") return cont(pushlex(")"), pushcontext, commasep(funarg, ")"), poplex, statement, popcontext);
308
function funarg(type, value) {
309
if (type == "variable") {register(value); return cont();}
315
startState: function(basecolumn) {
317
tokenize: jsTokenBase,
321
lexical: new JSLexical((basecolumn || 0) - indentUnit, 0, "block", false),
328
token: function(stream, state) {
330
if (!state.lexical.hasOwnProperty("align"))
331
state.lexical.align = false;
332
state.indented = stream.indentation();
334
if (stream.eatSpace()) return null;
335
var style = state.tokenize(stream, state);
336
if (type == "comment") return style;
337
state.reAllowed = type == "operator" || type == "keyword c" || type.match(/^[\[{}\(,;:]$/);
338
state.kwAllowed = type != '.';
339
return parseJS(state, style, type, content, stream);
342
indent: function(state, textAfter) {
343
if (state.tokenize != jsTokenBase) return 0;
344
var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical,
345
type = lexical.type, closing = firstChar == type;
346
if (type == "vardef") return lexical.indented + 4;
347
else if (type == "form" && firstChar == "{") return lexical.indented;
348
else if (type == "stat" || type == "form") return lexical.indented + indentUnit;
349
else if (lexical.info == "switch" && !closing)
350
return lexical.indented + (/^(?:case|default)\b/.test(textAfter) ? indentUnit : 2 * indentUnit);
351
else if (lexical.align) return lexical.column + (closing ? 0 : 1);
352
else return lexical.indented + (closing ? 0 : indentUnit);
359
CodeMirror.defineMIME("text/javascript", "javascript");
360
CodeMirror.defineMIME("application/json", {name: "javascript", json: true});