1
CodeMirror.defineMode("clike", function(config, parserConfig) {
2
var indentUnit = config.indentUnit,
3
keywords = parserConfig.keywords || {},
4
blockKeywords = parserConfig.blockKeywords || {},
5
atoms = parserConfig.atoms || {},
6
hooks = parserConfig.hooks || {},
7
multiLineStrings = parserConfig.multiLineStrings;
8
var isOperatorChar = /[+\-*&%=<>!?|\/]/;
12
function tokenBase(stream, state) {
13
var ch = stream.next();
15
var result = hooks[ch](stream, state);
16
if (result !== false) return result;
18
if (ch == '"' || ch == "'") {
19
state.tokenize = tokenString(ch);
20
return state.tokenize(stream, state);
22
if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
27
stream.eatWhile(/[\w\.]/);
31
if (stream.eat("*")) {
32
state.tokenize = tokenComment;
33
return tokenComment(stream, state);
35
if (stream.eat("/")) {
40
if (isOperatorChar.test(ch)) {
41
stream.eatWhile(isOperatorChar);
44
stream.eatWhile(/[\w\$_]/);
45
var cur = stream.current();
46
if (keywords.propertyIsEnumerable(cur)) {
47
if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement";
50
if (atoms.propertyIsEnumerable(cur)) return "atom";
54
function tokenString(quote) {
55
return function(stream, state) {
56
var escaped = false, next, end = false;
57
while ((next = stream.next()) != null) {
58
if (next == quote && !escaped) {end = true; break;}
59
escaped = !escaped && next == "\\";
61
if (end || !(escaped || multiLineStrings))
62
state.tokenize = null;
67
function tokenComment(stream, state) {
68
var maybeEnd = false, ch;
69
while (ch = stream.next()) {
70
if (ch == "/" && maybeEnd) {
71
state.tokenize = null;
74
maybeEnd = (ch == "*");
79
function Context(indented, column, type, align, prev) {
80
this.indented = indented;
86
function pushContext(state, col, type) {
87
return state.context = new Context(state.indented, col, type, null, state.context);
89
function popContext(state) {
90
var t = state.context.type;
91
if (t == ")" || t == "]" || t == "}")
92
state.indented = state.context.indented;
93
return state.context = state.context.prev;
99
startState: function(basecolumn) {
102
context: new Context((basecolumn || 0) - indentUnit, 0, "top", false),
108
token: function(stream, state) {
109
var ctx = state.context;
111
if (ctx.align == null) ctx.align = false;
112
state.indented = stream.indentation();
113
state.startOfLine = true;
115
if (stream.eatSpace()) return null;
117
var style = (state.tokenize || tokenBase)(stream, state);
118
if (style == "comment" || style == "meta") return style;
119
if (ctx.align == null) ctx.align = true;
121
if ((curPunc == ";" || curPunc == ":") && ctx.type == "statement") popContext(state);
122
else if (curPunc == "{") pushContext(state, stream.column(), "}");
123
else if (curPunc == "[") pushContext(state, stream.column(), "]");
124
else if (curPunc == "(") pushContext(state, stream.column(), ")");
125
else if (curPunc == "}") {
126
while (ctx.type == "statement") ctx = popContext(state);
127
if (ctx.type == "}") ctx = popContext(state);
128
while (ctx.type == "statement") ctx = popContext(state);
130
else if (curPunc == ctx.type) popContext(state);
131
else if (ctx.type == "}" || ctx.type == "top" || (ctx.type == "statement" && curPunc == "newstatement"))
132
pushContext(state, stream.column(), "statement");
133
state.startOfLine = false;
137
indent: function(state, textAfter) {
138
if (state.tokenize != tokenBase && state.tokenize != null) return 0;
139
var ctx = state.context, firstChar = textAfter && textAfter.charAt(0);
140
if (ctx.type == "statement" && firstChar == "}") ctx = ctx.prev;
141
var closing = firstChar == ctx.type;
142
if (ctx.type == "statement") return ctx.indented + (firstChar == "{" ? 0 : indentUnit);
143
else if (ctx.align) return ctx.column + (closing ? 0 : 1);
144
else return ctx.indented + (closing ? 0 : indentUnit);
152
function words(str) {
153
var obj = {}, words = str.split(" ");
154
for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
157
var cKeywords = "auto if break int case long char register continue return default short do sizeof " +
158
"double static else struct entry switch extern typedef float union for unsigned " +
159
"goto while enum void const signed volatile";
161
function cppHook(stream, state) {
162
if (!state.startOfLine) return false;
167
// C#-style strings where "" escapes a quote.
168
function tokenAtString(stream, state) {
170
while ((next = stream.next()) != null) {
171
if (next == '"' && !stream.eat('"')) {
172
state.tokenize = null;
179
CodeMirror.defineMIME("text/x-csrc", {
181
keywords: words(cKeywords),
182
blockKeywords: words("case do else for if switch while struct"),
183
atoms: words("null"),
184
hooks: {"#": cppHook}
186
CodeMirror.defineMIME("text/x-c++src", {
188
keywords: words(cKeywords + " asm dynamic_cast namespace reinterpret_cast try bool explicit new " +
189
"static_cast typeid catch operator template typename class friend private " +
190
"this using const_cast inline public throw virtual delete mutable protected " +
192
blockKeywords: words("catch class do else finally for if struct switch try while"),
193
atoms: words("true false null"),
194
hooks: {"#": cppHook}
196
CodeMirror.defineMIME("text/x-java", {
198
keywords: words("abstract assert boolean break byte case catch char class const continue default " +
199
"do double else enum extends final finally float for goto if implements import " +
200
"instanceof int interface long native new package private protected public " +
201
"return short static strictfp super switch synchronized this throw throws transient " +
202
"try void volatile while"),
203
blockKeywords: words("catch class do else finally for if switch try while"),
204
atoms: words("true false null"),
206
"@": function(stream, state) {
207
stream.eatWhile(/[\w\$_]/);
212
CodeMirror.defineMIME("text/x-csharp", {
214
keywords: words("abstract as base bool break byte case catch char checked class const continue decimal" +
215
" default delegate do double else enum event explicit extern finally fixed float for" +
216
" foreach goto if implicit in int interface internal is lock long namespace new object" +
217
" operator out override params private protected public readonly ref return sbyte sealed short" +
218
" sizeof stackalloc static string struct switch this throw try typeof uint ulong unchecked" +
219
" unsafe ushort using virtual void volatile while add alias ascending descending dynamic from get" +
220
" global group into join let orderby partial remove select set value var yield"),
221
blockKeywords: words("catch class do else finally for foreach if struct switch try while"),
222
atoms: words("true false null"),
224
"@": function(stream, state) {
225
if (stream.eat('"')) {
226
state.tokenize = tokenAtString;
227
return tokenAtString(stream, state);
229
stream.eatWhile(/[\w\$_]/);