phrebejk@460: CodeMirror.defineMode("clike", function(config, parserConfig) { phrebejk@460: var indentUnit = config.indentUnit, phrebejk@460: statementIndentUnit = parserConfig.statementIndentUnit || indentUnit, phrebejk@460: keywords = parserConfig.keywords || {}, phrebejk@460: builtin = parserConfig.builtin || {}, phrebejk@460: blockKeywords = parserConfig.blockKeywords || {}, phrebejk@460: atoms = parserConfig.atoms || {}, phrebejk@460: hooks = parserConfig.hooks || {}, phrebejk@460: multiLineStrings = parserConfig.multiLineStrings; phrebejk@460: var isOperatorChar = /[+\-*&%=<>!?|\/]/; phrebejk@460: phrebejk@460: var curPunc; phrebejk@460: phrebejk@460: function tokenBase(stream, state) { phrebejk@460: var ch = stream.next(); phrebejk@460: if (hooks[ch]) { phrebejk@460: var result = hooks[ch](stream, state); phrebejk@460: if (result !== false) return result; phrebejk@460: } phrebejk@460: if (ch == '"' || ch == "'") { phrebejk@460: state.tokenize = tokenString(ch); phrebejk@460: return state.tokenize(stream, state); phrebejk@460: } phrebejk@460: if (/[\[\]{}\(\),;\:\.]/.test(ch)) { phrebejk@460: curPunc = ch; phrebejk@460: return null; phrebejk@460: } phrebejk@460: if (/\d/.test(ch)) { phrebejk@460: stream.eatWhile(/[\w\.]/); phrebejk@460: return "number"; phrebejk@460: } phrebejk@460: if (ch == "/") { phrebejk@460: if (stream.eat("*")) { phrebejk@460: state.tokenize = tokenComment; phrebejk@460: return tokenComment(stream, state); phrebejk@460: } phrebejk@460: if (stream.eat("/")) { phrebejk@460: stream.skipToEnd(); phrebejk@460: return "comment"; phrebejk@460: } phrebejk@460: } phrebejk@460: if (isOperatorChar.test(ch)) { phrebejk@460: stream.eatWhile(isOperatorChar); phrebejk@460: return "operator"; phrebejk@460: } phrebejk@460: stream.eatWhile(/[\w\$_]/); phrebejk@460: var cur = stream.current(); phrebejk@460: if (keywords.propertyIsEnumerable(cur)) { phrebejk@460: if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement"; phrebejk@460: return "keyword"; phrebejk@460: } phrebejk@460: if (builtin.propertyIsEnumerable(cur)) { phrebejk@460: if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement"; phrebejk@460: return "builtin"; phrebejk@460: } phrebejk@460: if (atoms.propertyIsEnumerable(cur)) return "atom"; phrebejk@460: return "variable"; phrebejk@460: } phrebejk@460: phrebejk@460: function tokenString(quote) { phrebejk@460: return function(stream, state) { phrebejk@460: var escaped = false, next, end = false; phrebejk@460: while ((next = stream.next()) != null) { phrebejk@460: if (next == quote && !escaped) {end = true; break;} phrebejk@460: escaped = !escaped && next == "\\"; phrebejk@460: } phrebejk@460: if (end || !(escaped || multiLineStrings)) phrebejk@460: state.tokenize = null; phrebejk@460: return "string"; phrebejk@460: }; phrebejk@460: } phrebejk@460: phrebejk@460: function tokenComment(stream, state) { phrebejk@460: var maybeEnd = false, ch; phrebejk@460: while (ch = stream.next()) { phrebejk@460: if (ch == "/" && maybeEnd) { phrebejk@460: state.tokenize = null; phrebejk@460: break; phrebejk@460: } phrebejk@460: maybeEnd = (ch == "*"); phrebejk@460: } phrebejk@460: return "comment"; phrebejk@460: } phrebejk@460: phrebejk@460: function Context(indented, column, type, align, prev) { phrebejk@460: this.indented = indented; phrebejk@460: this.column = column; phrebejk@460: this.type = type; phrebejk@460: this.align = align; phrebejk@460: this.prev = prev; phrebejk@460: } phrebejk@460: function pushContext(state, col, type) { phrebejk@460: var indent = state.indented; phrebejk@460: if (state.context && state.context.type == "statement") phrebejk@460: indent = state.context.indented; phrebejk@460: return state.context = new Context(indent, col, type, null, state.context); phrebejk@460: } phrebejk@460: function popContext(state) { phrebejk@460: var t = state.context.type; phrebejk@460: if (t == ")" || t == "]" || t == "}") phrebejk@460: state.indented = state.context.indented; phrebejk@460: return state.context = state.context.prev; phrebejk@460: } phrebejk@460: phrebejk@460: // Interface phrebejk@460: phrebejk@460: return { phrebejk@460: startState: function(basecolumn) { phrebejk@460: return { phrebejk@460: tokenize: null, phrebejk@460: context: new Context((basecolumn || 0) - indentUnit, 0, "top", false), phrebejk@460: indented: 0, phrebejk@460: startOfLine: true phrebejk@460: }; phrebejk@460: }, phrebejk@460: phrebejk@460: token: function(stream, state) { phrebejk@460: var ctx = state.context; phrebejk@460: if (stream.sol()) { phrebejk@460: if (ctx.align == null) ctx.align = false; phrebejk@460: state.indented = stream.indentation(); phrebejk@460: state.startOfLine = true; phrebejk@460: } phrebejk@460: if (stream.eatSpace()) return null; phrebejk@460: curPunc = null; phrebejk@460: var style = (state.tokenize || tokenBase)(stream, state); phrebejk@460: if (style == "comment" || style == "meta") return style; phrebejk@460: if (ctx.align == null) ctx.align = true; phrebejk@460: phrebejk@460: if ((curPunc == ";" || curPunc == ":" || curPunc == ",") && ctx.type == "statement") popContext(state); phrebejk@460: else if (curPunc == "{") pushContext(state, stream.column(), "}"); phrebejk@460: else if (curPunc == "[") pushContext(state, stream.column(), "]"); phrebejk@460: else if (curPunc == "(") pushContext(state, stream.column(), ")"); phrebejk@460: else if (curPunc == "}") { phrebejk@460: while (ctx.type == "statement") ctx = popContext(state); phrebejk@460: if (ctx.type == "}") ctx = popContext(state); phrebejk@460: while (ctx.type == "statement") ctx = popContext(state); phrebejk@460: } phrebejk@460: else if (curPunc == ctx.type) popContext(state); phrebejk@460: else if (((ctx.type == "}" || ctx.type == "top") && curPunc != ';') || (ctx.type == "statement" && curPunc == "newstatement")) phrebejk@460: pushContext(state, stream.column(), "statement"); phrebejk@460: state.startOfLine = false; phrebejk@460: return style; phrebejk@460: }, phrebejk@460: phrebejk@460: indent: function(state, textAfter) { phrebejk@460: if (state.tokenize != tokenBase && state.tokenize != null) return CodeMirror.Pass; phrebejk@460: var ctx = state.context, firstChar = textAfter && textAfter.charAt(0); phrebejk@460: if (ctx.type == "statement" && firstChar == "}") ctx = ctx.prev; phrebejk@460: var closing = firstChar == ctx.type; phrebejk@460: if (ctx.type == "statement") return ctx.indented + (firstChar == "{" ? 0 : statementIndentUnit); phrebejk@460: else if (ctx.align) return ctx.column + (closing ? 0 : 1); phrebejk@460: else return ctx.indented + (closing ? 0 : indentUnit); phrebejk@460: }, phrebejk@460: phrebejk@460: electricChars: "{}" phrebejk@460: }; phrebejk@460: }); phrebejk@460: phrebejk@460: (function() { phrebejk@460: function words(str) { phrebejk@460: var obj = {}, words = str.split(" "); phrebejk@460: for (var i = 0; i < words.length; ++i) obj[words[i]] = true; phrebejk@460: return obj; phrebejk@460: } phrebejk@460: var cKeywords = "auto if break int case long char register continue return default short do sizeof " + phrebejk@460: "double static else struct entry switch extern typedef float union for unsigned " + phrebejk@460: "goto while enum void const signed volatile"; phrebejk@460: phrebejk@460: function cppHook(stream, state) { phrebejk@460: if (!state.startOfLine) return false; phrebejk@460: for (;;) { phrebejk@460: if (stream.skipTo("\\")) { phrebejk@460: stream.next(); phrebejk@460: if (stream.eol()) { phrebejk@460: state.tokenize = cppHook; phrebejk@460: break; phrebejk@460: } phrebejk@460: } else { phrebejk@460: stream.skipToEnd(); phrebejk@460: state.tokenize = null; phrebejk@460: break; phrebejk@460: } phrebejk@460: } phrebejk@460: return "meta"; phrebejk@460: } phrebejk@460: phrebejk@460: // C#-style strings where "" escapes a quote. phrebejk@460: function tokenAtString(stream, state) { phrebejk@460: var next; phrebejk@460: while ((next = stream.next()) != null) { phrebejk@460: if (next == '"' && !stream.eat('"')) { phrebejk@460: state.tokenize = null; phrebejk@460: break; phrebejk@460: } phrebejk@460: } phrebejk@460: return "string"; phrebejk@460: } phrebejk@460: phrebejk@460: function mimes(ms, mode) { phrebejk@460: for (var i = 0; i < ms.length; ++i) CodeMirror.defineMIME(ms[i], mode); phrebejk@460: } phrebejk@460: phrebejk@460: mimes(["text/x-csrc", "text/x-c", "text/x-chdr"], { phrebejk@460: name: "clike", phrebejk@460: keywords: words(cKeywords), phrebejk@460: blockKeywords: words("case do else for if switch while struct"), phrebejk@460: atoms: words("null"), phrebejk@460: hooks: {"#": cppHook} phrebejk@460: }); phrebejk@460: mimes(["text/x-c++src", "text/x-c++hdr"], { phrebejk@460: name: "clike", phrebejk@460: keywords: words(cKeywords + " asm dynamic_cast namespace reinterpret_cast try bool explicit new " + phrebejk@460: "static_cast typeid catch operator template typename class friend private " + phrebejk@460: "this using const_cast inline public throw virtual delete mutable protected " + phrebejk@460: "wchar_t"), phrebejk@460: blockKeywords: words("catch class do else finally for if struct switch try while"), phrebejk@460: atoms: words("true false null"), phrebejk@460: hooks: {"#": cppHook} phrebejk@460: }); phrebejk@460: CodeMirror.defineMIME("text/x-java", { phrebejk@460: name: "clike", phrebejk@460: keywords: words("abstract assert boolean break byte case catch char class const continue default " + phrebejk@460: "do double else enum extends final finally float for goto if implements import " + phrebejk@460: "instanceof int interface long native new package private protected public " + phrebejk@460: "return short static strictfp super switch synchronized this throw throws transient " + phrebejk@460: "try void volatile while"), phrebejk@460: blockKeywords: words("catch class do else finally for if switch try while"), phrebejk@460: atoms: words("true false null"), phrebejk@460: hooks: { phrebejk@460: "@": function(stream) { phrebejk@460: stream.eatWhile(/[\w\$_]/); phrebejk@460: return "meta"; phrebejk@460: } phrebejk@460: } phrebejk@460: }); phrebejk@460: CodeMirror.defineMIME("text/x-csharp", { phrebejk@460: name: "clike", phrebejk@460: keywords: words("abstract as base break case catch checked class const continue" + phrebejk@460: " default delegate do else enum event explicit extern finally fixed for" + phrebejk@460: " foreach goto if implicit in interface internal is lock namespace new" + phrebejk@460: " operator out override params private protected public readonly ref return sealed" + phrebejk@460: " sizeof stackalloc static struct switch this throw try typeof unchecked" + phrebejk@460: " unsafe using virtual void volatile while add alias ascending descending dynamic from get" + phrebejk@460: " global group into join let orderby partial remove select set value var yield"), phrebejk@460: blockKeywords: words("catch class do else finally for foreach if struct switch try while"), phrebejk@460: builtin: words("Boolean Byte Char DateTime DateTimeOffset Decimal Double" + phrebejk@460: " Guid Int16 Int32 Int64 Object SByte Single String TimeSpan UInt16 UInt32" + phrebejk@460: " UInt64 bool byte char decimal double short int long object" + phrebejk@460: " sbyte float string ushort uint ulong"), phrebejk@460: atoms: words("true false null"), phrebejk@460: hooks: { phrebejk@460: "@": function(stream, state) { phrebejk@460: if (stream.eat('"')) { phrebejk@460: state.tokenize = tokenAtString; phrebejk@460: return tokenAtString(stream, state); phrebejk@460: } phrebejk@460: stream.eatWhile(/[\w\$_]/); phrebejk@460: return "meta"; phrebejk@460: } phrebejk@460: } phrebejk@460: }); phrebejk@460: CodeMirror.defineMIME("text/x-scala", { phrebejk@460: name: "clike", phrebejk@460: keywords: words( phrebejk@460: phrebejk@460: /* scala */ phrebejk@460: "abstract case catch class def do else extends false final finally for forSome if " + phrebejk@460: "implicit import lazy match new null object override package private protected return " + phrebejk@460: "sealed super this throw trait try trye type val var while with yield _ : = => <- <: " + phrebejk@460: "<% >: # @ " + phrebejk@460: phrebejk@460: /* package scala */ phrebejk@460: "assert assume require print println printf readLine readBoolean readByte readShort " + phrebejk@460: "readChar readInt readLong readFloat readDouble " + phrebejk@460: phrebejk@460: "AnyVal App Application Array BufferedIterator BigDecimal BigInt Char Console Either " + phrebejk@460: "Enumeration Equiv Error Exception Fractional Function IndexedSeq Integral Iterable " + phrebejk@460: "Iterator List Map Numeric Nil NotNull Option Ordered Ordering PartialFunction PartialOrdering " + phrebejk@460: "Product Proxy Range Responder Seq Serializable Set Specializable Stream StringBuilder " + phrebejk@460: "StringContext Symbol Throwable Traversable TraversableOnce Tuple Unit Vector :: #:: " + phrebejk@460: phrebejk@460: /* package java.lang */ phrebejk@460: "Boolean Byte Character CharSequence Class ClassLoader Cloneable Comparable " + phrebejk@460: "Compiler Double Exception Float Integer Long Math Number Object Package Pair Process " + phrebejk@460: "Runtime Runnable SecurityManager Short StackTraceElement StrictMath String " + phrebejk@460: "StringBuffer System Thread ThreadGroup ThreadLocal Throwable Triple Void" phrebejk@460: phrebejk@460: phrebejk@460: ), phrebejk@460: blockKeywords: words("catch class do else finally for forSome if match switch try while"), phrebejk@460: atoms: words("true false null"), phrebejk@460: hooks: { phrebejk@460: "@": function(stream) { phrebejk@460: stream.eatWhile(/[\w\$_]/); phrebejk@460: return "meta"; phrebejk@460: } phrebejk@460: } phrebejk@460: }); phrebejk@460: }());