Fix: merge conflict
[myslice.git] / to-be-integrated / third-party / codemirror-3.15 / addon / runmode / runmode-standalone.js
1 /* Just enough of CodeMirror to run runMode under node.js */
2
3 window.CodeMirror = {};
4
5 function splitLines(string){ return string.split(/\r?\n|\r/); };
6
7 function StringStream(string) {
8   this.pos = this.start = 0;
9   this.string = string;
10 }
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;},
15   next: function() {
16     if (this.pos < this.string.length)
17       return this.string.charAt(this.pos++);
18   },
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;}
24   },
25   eatWhile: function(match) {
26     var start = this.pos;
27     while (this.eat(match)){}
28     return this.pos > start;
29   },
30   eatSpace: function() {
31     var start = this.pos;
32     while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) ++this.pos;
33     return this.pos > start;
34   },
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;}
39   },
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;
49         return true;
50       }
51     } else {
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;
55       return match;
56     }
57   },
58   current: function(){return this.string.slice(this.start, this.pos);}
59 };
60 CodeMirror.StringStream = StringStream;
61
62 CodeMirror.startState = function (mode, a1, a2) {
63   return mode.startState ? mode.startState(a1, a2) : true;
64 };
65
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 || {});
79 };
80
81 CodeMirror.runMode = function (string, modespec, callback, options) {
82   var mode = CodeMirror.getMode({ indentUnit: 2 }, modespec);
83
84   if (callback.nodeType == 1) {
85     var tabSize = (options && options.tabSize) || 4;
86     var node = callback, col = 0;
87     node.innerHTML = "";
88     callback = function (text, style) {
89       if (text == "\n") {
90         node.appendChild(document.createElement("br"));
91         col = 0;
92         return;
93       }
94       var content = "";
95       // replace tabs
96       for (var pos = 0; ;) {
97         var idx = text.indexOf("\t", pos);
98         if (idx == -1) {
99           content += text.slice(pos);
100           col += text.length - pos;
101           break;
102         } else {
103           col += idx - pos;
104           content += text.slice(pos, idx);
105           var size = tabSize - col % tabSize;
106           col += size;
107           for (var i = 0; i < size; ++i) content += " ";
108           pos = idx + 1;
109         }
110       }
111
112       if (style) {
113         var sp = node.appendChild(document.createElement("span"));
114         sp.className = "cm-" + style.replace(/ +/g, " cm-");
115         sp.appendChild(document.createTextNode(content));
116       } else {
117         node.appendChild(document.createTextNode(content));
118       }
119     };
120   }
121
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;
130     }
131   }
132 };