1 /* Just enough of CodeMirror to run runMode under node.js */
3 window.CodeMirror = {};
5 function splitLines(string){ return string.split(/\r?\n|\r/); };
7 function StringStream(string) {
8 this.pos = this.start = 0;
11 StringStream.prototype = {
12 eol: function() {return this.pos >= this.string.length;},
13 sol: function() {return this.pos == 0;},
14 peek: function() {return this.string.charAt(this.pos) || null;},
16 if (this.pos < this.string.length)
17 return this.string.charAt(this.pos++);
19 eat: function(match) {
20 var ch = this.string.charAt(this.pos);
21 if (typeof match == "string") var ok = ch == match;
22 else var ok = ch && (match.test ? match.test(ch) : match(ch));
23 if (ok) {++this.pos; return ch;}
25 eatWhile: function(match) {
27 while (this.eat(match)){}
28 return this.pos > start;
30 eatSpace: function() {
32 while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) ++this.pos;
33 return this.pos > start;
35 skipToEnd: function() {this.pos = this.string.length;},
36 skipTo: function(ch) {
37 var found = this.string.indexOf(ch, this.pos);
38 if (found > -1) {this.pos = found; return true;}
40 backUp: function(n) {this.pos -= n;},
41 column: function() {return this.start;},
42 indentation: function() {return 0;},
43 match: function(pattern, consume, caseInsensitive) {
44 if (typeof pattern == "string") {
45 var cased = function(str) {return caseInsensitive ? str.toLowerCase() : str;};
46 var substr = this.string.substr(this.pos, pattern.length);
47 if (cased(substr) == cased(pattern)) {
48 if (consume !== false) this.pos += pattern.length;
52 var match = this.string.slice(this.pos).match(pattern);
53 if (match && match.index > 0) return null;
54 if (match && consume !== false) this.pos += match[0].length;
58 current: function(){return this.string.slice(this.start, this.pos);}
60 CodeMirror.StringStream = StringStream;
62 CodeMirror.startState = function (mode, a1, a2) {
63 return mode.startState ? mode.startState(a1, a2) : true;
66 var modes = CodeMirror.modes = {}, mimeModes = CodeMirror.mimeModes = {};
67 CodeMirror.defineMode = function (name, mode) { modes[name] = mode; };
68 CodeMirror.defineMIME = function (mime, spec) { mimeModes[mime] = spec; };
69 CodeMirror.getMode = function (options, spec) {
70 if (typeof spec == "string" && mimeModes.hasOwnProperty(spec))
71 spec = mimeModes[spec];
72 if (typeof spec == "string")
73 var mname = spec, config = {};
74 else if (spec != null)
75 var mname = spec.name, config = spec;
76 var mfactory = modes[mname];
77 if (!mfactory) throw new Error("Unknown mode: " + spec);
78 return mfactory(options, config || {});
81 CodeMirror.runMode = function (string, modespec, callback, options) {
82 var mode = CodeMirror.getMode({ indentUnit: 2 }, modespec);
84 if (callback.nodeType == 1) {
85 var tabSize = (options && options.tabSize) || 4;
86 var node = callback, col = 0;
88 callback = function (text, style) {
90 node.appendChild(document.createElement("br"));
96 for (var pos = 0; ;) {
97 var idx = text.indexOf("\t", pos);
99 content += text.slice(pos);
100 col += text.length - pos;
104 content += text.slice(pos, idx);
105 var size = tabSize - col % tabSize;
107 for (var i = 0; i < size; ++i) content += " ";
113 var sp = node.appendChild(document.createElement("span"));
114 sp.className = "cm-" + style.replace(/ +/g, " cm-");
115 sp.appendChild(document.createTextNode(content));
117 node.appendChild(document.createTextNode(content));
122 var lines = splitLines(string), state = CodeMirror.startState(mode);
123 for (var i = 0, e = lines.length; i < e; ++i) {
124 if (i) callback("\n");
125 var stream = new CodeMirror.StringStream(lines[i]);
126 while (!stream.eol()) {
127 var style = mode.token(stream, state);
128 callback(stream.current(), style, i, stream.start);
129 stream.start = stream.pos;