Path: blob/master/web-gui/buildyourownbotnet/assets/js/codemirror/addon/runmode/runmode.node.js
1293 views
/* Just enough of CodeMirror to run runMode under node.js */12function splitLines(string){ return string.split(/\r?\n|\r/); };34function StringStream(string) {5this.pos = this.start = 0;6this.string = string;7this.lineStart = 0;8}9StringStream.prototype = {10eol: function() {return this.pos >= this.string.length;},11sol: function() {return this.pos == 0;},12peek: function() {return this.string.charAt(this.pos) || null;},13next: function() {14if (this.pos < this.string.length)15return this.string.charAt(this.pos++);16},17eat: function(match) {18var ch = this.string.charAt(this.pos);19if (typeof match == "string") var ok = ch == match;20else var ok = ch && (match.test ? match.test(ch) : match(ch));21if (ok) {++this.pos; return ch;}22},23eatWhile: function(match) {24var start = this.pos;25while (this.eat(match)){}26return this.pos > start;27},28eatSpace: function() {29var start = this.pos;30while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) ++this.pos;31return this.pos > start;32},33skipToEnd: function() {this.pos = this.string.length;},34skipTo: function(ch) {35var found = this.string.indexOf(ch, this.pos);36if (found > -1) {this.pos = found; return true;}37},38backUp: function(n) {this.pos -= n;},39column: function() {return this.start - this.lineStart;},40indentation: function() {return 0;},41match: function(pattern, consume, caseInsensitive) {42if (typeof pattern == "string") {43var cased = function(str) {return caseInsensitive ? str.toLowerCase() : str;};44var substr = this.string.substr(this.pos, pattern.length);45if (cased(substr) == cased(pattern)) {46if (consume !== false) this.pos += pattern.length;47return true;48}49} else {50var match = this.string.slice(this.pos).match(pattern);51if (match && match.index > 0) return null;52if (match && consume !== false) this.pos += match[0].length;53return match;54}55},56current: function(){return this.string.slice(this.start, this.pos);},57hideFirstChars: function(n, inner) {58this.lineStart += n;59try { return inner(); }60finally { this.lineStart -= n; }61}62};63exports.StringStream = StringStream;6465exports.startState = function(mode, a1, a2) {66return mode.startState ? mode.startState(a1, a2) : true;67};6869var modes = exports.modes = {}, mimeModes = exports.mimeModes = {};70exports.defineMode = function(name, mode) {71if (arguments.length > 2) {72mode.dependencies = [];73for (var i = 2; i < arguments.length; ++i) mode.dependencies.push(arguments[i]);74}75modes[name] = mode;76};77exports.defineMIME = function(mime, spec) { mimeModes[mime] = spec; };7879exports.defineMode("null", function() {80return {token: function(stream) {stream.skipToEnd();}};81});82exports.defineMIME("text/plain", "null");8384exports.resolveMode = function(spec) {85if (typeof spec == "string" && mimeModes.hasOwnProperty(spec)) {86spec = mimeModes[spec];87} else if (spec && typeof spec.name == "string" && mimeModes.hasOwnProperty(spec.name)) {88spec = mimeModes[spec.name];89}90if (typeof spec == "string") return {name: spec};91else return spec || {name: "null"};92};93exports.getMode = function(options, spec) {94spec = exports.resolveMode(mimeModes[spec]);95var mfactory = modes[spec.name];96if (!mfactory) throw new Error("Unknown mode: " + spec);97return mfactory(options, spec);98};99exports.registerHelper = exports.registerGlobalHelper = Math.min;100101exports.runMode = function(string, modespec, callback) {102var mode = exports.getMode({indentUnit: 2}, modespec);103var lines = splitLines(string), state = (options && options.state) || exports.startState(mode);104for (var i = 0, e = lines.length; i < e; ++i) {105if (i) callback("\n");106var stream = new exports.StringStream(lines[i]);107while (!stream.eol()) {108var style = mode.token(stream, state);109callback(stream.current(), style, i, stream.start, state);110stream.start = stream.pos;111}112}113};114115116