runmode.node.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. /* Just enough of CodeMirror to run runMode under node.js */
  4. function splitLines(string){return string.split(/\r\n?|\n/);};
  5. // Counts the column offset in a string, taking tabs into account.
  6. // Used mostly to find indentation.
  7. var countColumn = function(string, end, tabSize, startIndex, startValue) {
  8. if (end == null) {
  9. end = string.search(/[^\s\u00a0]/);
  10. if (end == -1) end = string.length;
  11. }
  12. for (var i = startIndex || 0, n = startValue || 0;;) {
  13. var nextTab = string.indexOf("\t", i);
  14. if (nextTab < 0 || nextTab >= end)
  15. return n + (end - i);
  16. n += nextTab - i;
  17. n += tabSize - (n % tabSize);
  18. i = nextTab + 1;
  19. }
  20. };
  21. function StringStream(string, tabSize) {
  22. this.pos = this.start = 0;
  23. this.string = string;
  24. this.tabSize = tabSize || 8;
  25. this.lastColumnPos = this.lastColumnValue = 0;
  26. this.lineStart = 0;
  27. };
  28. StringStream.prototype = {
  29. eol: function() {return this.pos >= this.string.length;},
  30. sol: function() {return this.pos == this.lineStart;},
  31. peek: function() {return this.string.charAt(this.pos) || undefined;},
  32. next: function() {
  33. if (this.pos < this.string.length)
  34. return this.string.charAt(this.pos++);
  35. },
  36. eat: function(match) {
  37. var ch = this.string.charAt(this.pos);
  38. if (typeof match == "string") var ok = ch == match;
  39. else var ok = ch && (match.test ? match.test(ch) : match(ch));
  40. if (ok) {++this.pos; return ch;}
  41. },
  42. eatWhile: function(match) {
  43. var start = this.pos;
  44. while (this.eat(match)){}
  45. return this.pos > start;
  46. },
  47. eatSpace: function() {
  48. var start = this.pos;
  49. while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) ++this.pos;
  50. return this.pos > start;
  51. },
  52. skipToEnd: function() {this.pos = this.string.length;},
  53. skipTo: function(ch) {
  54. var found = this.string.indexOf(ch, this.pos);
  55. if (found > -1) {this.pos = found; return true;}
  56. },
  57. backUp: function(n) {this.pos -= n;},
  58. column: function() {
  59. if (this.lastColumnPos < this.start) {
  60. this.lastColumnValue = countColumn(this.string, this.start, this.tabSize, this.lastColumnPos, this.lastColumnValue);
  61. this.lastColumnPos = this.start;
  62. }
  63. return this.lastColumnValue - (this.lineStart ? countColumn(this.string, this.lineStart, this.tabSize) : 0);
  64. },
  65. indentation: function() {
  66. return countColumn(this.string, null, this.tabSize) -
  67. (this.lineStart ? countColumn(this.string, this.lineStart, this.tabSize) : 0);
  68. },
  69. match: function(pattern, consume, caseInsensitive) {
  70. if (typeof pattern == "string") {
  71. var cased = function(str) {return caseInsensitive ? str.toLowerCase() : str;};
  72. var substr = this.string.substr(this.pos, pattern.length);
  73. if (cased(substr) == cased(pattern)) {
  74. if (consume !== false) this.pos += pattern.length;
  75. return true;
  76. }
  77. } else {
  78. var match = this.string.slice(this.pos).match(pattern);
  79. if (match && match.index > 0) return null;
  80. if (match && consume !== false) this.pos += match[0].length;
  81. return match;
  82. }
  83. },
  84. current: function(){return this.string.slice(this.start, this.pos);},
  85. hideFirstChars: function(n, inner) {
  86. this.lineStart += n;
  87. try { return inner(); }
  88. finally { this.lineStart -= n; }
  89. }
  90. };
  91. exports.StringStream = StringStream;
  92. exports.startState = function(mode, a1, a2) {
  93. return mode.startState ? mode.startState(a1, a2) : true;
  94. };
  95. var modes = exports.modes = {}, mimeModes = exports.mimeModes = {};
  96. exports.defineMode = function(name, mode) {
  97. if (arguments.length > 2)
  98. mode.dependencies = Array.prototype.slice.call(arguments, 2);
  99. modes[name] = mode;
  100. };
  101. exports.defineMIME = function(mime, spec) { mimeModes[mime] = spec; };
  102. exports.defineMode("null", function() {
  103. return {token: function(stream) {stream.skipToEnd();}};
  104. });
  105. exports.defineMIME("text/plain", "null");
  106. exports.resolveMode = function(spec) {
  107. if (typeof spec == "string" && mimeModes.hasOwnProperty(spec)) {
  108. spec = mimeModes[spec];
  109. } else if (spec && typeof spec.name == "string" && mimeModes.hasOwnProperty(spec.name)) {
  110. spec = mimeModes[spec.name];
  111. }
  112. if (typeof spec == "string") return {name: spec};
  113. else return spec || {name: "null"};
  114. };
  115. function copyObj(obj, target, overwrite) {
  116. if (!target) target = {};
  117. for (var prop in obj)
  118. if (obj.hasOwnProperty(prop) && (overwrite !== false || !target.hasOwnProperty(prop)))
  119. target[prop] = obj[prop];
  120. return target;
  121. }
  122. // This can be used to attach properties to mode objects from
  123. // outside the actual mode definition.
  124. var modeExtensions = exports.modeExtensions = {};
  125. exports.extendMode = function(mode, properties) {
  126. var exts = modeExtensions.hasOwnProperty(mode) ? modeExtensions[mode] : (modeExtensions[mode] = {});
  127. copyObj(properties, exts);
  128. };
  129. exports.getMode = function(options, spec) {
  130. var spec = exports.resolveMode(spec);
  131. var mfactory = modes[spec.name];
  132. if (!mfactory) return exports.getMode(options, "text/plain");
  133. var modeObj = mfactory(options, spec);
  134. if (modeExtensions.hasOwnProperty(spec.name)) {
  135. var exts = modeExtensions[spec.name];
  136. for (var prop in exts) {
  137. if (!exts.hasOwnProperty(prop)) continue;
  138. if (modeObj.hasOwnProperty(prop)) modeObj["_" + prop] = modeObj[prop];
  139. modeObj[prop] = exts[prop];
  140. }
  141. }
  142. modeObj.name = spec.name;
  143. if (spec.helperType) modeObj.helperType = spec.helperType;
  144. if (spec.modeProps) for (var prop in spec.modeProps)
  145. modeObj[prop] = spec.modeProps[prop];
  146. return modeObj;
  147. };
  148. exports.registerHelper = exports.registerGlobalHelper = Math.min;
  149. exports.runMode = function(string, modespec, callback, options) {
  150. var mode = exports.getMode({indentUnit: 2}, modespec);
  151. var lines = splitLines(string), state = (options && options.state) || exports.startState(mode);
  152. for (var i = 0, e = lines.length; i < e; ++i) {
  153. if (i) callback("\n");
  154. var stream = new exports.StringStream(lines[i]);
  155. if (!stream.string && mode.blankLine) mode.blankLine(state);
  156. while (!stream.eol()) {
  157. var style = mode.token(stream, state);
  158. callback(stream.current(), style, i, stream.start, state);
  159. stream.start = stream.pos;
  160. }
  161. }
  162. };
  163. require.cache[require.resolve("../../lib/codemirror")] = require.cache[require.resolve("./runmode.node")];
  164. require.cache[require.resolve("../../addon/runmode/runmode")] = require.cache[require.resolve("./runmode.node")];