本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
6.9KB

  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://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 = exports.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, context) {
  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. this.context = context
  28. };
  29. StringStream.prototype = {
  30. eol: function() {return this.pos >= this.string.length;},
  31. sol: function() {return this.pos == this.lineStart;},
  32. peek: function() {return this.string.charAt(this.pos) || undefined;},
  33. next: function() {
  34. if (this.pos < this.string.length)
  35. return this.string.charAt(this.pos++);
  36. },
  37. eat: function(match) {
  38. var ch = this.string.charAt(this.pos);
  39. if (typeof match == "string") var ok = ch == match;
  40. else var ok = ch && (match.test ? match.test(ch) : match(ch));
  41. if (ok) {++this.pos; return ch;}
  42. },
  43. eatWhile: function(match) {
  44. var start = this.pos;
  45. while (this.eat(match)){}
  46. return this.pos > start;
  47. },
  48. eatSpace: function() {
  49. var start = this.pos;
  50. while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) ++this.pos;
  51. return this.pos > start;
  52. },
  53. skipToEnd: function() {this.pos = this.string.length;},
  54. skipTo: function(ch) {
  55. var found = this.string.indexOf(ch, this.pos);
  56. if (found > -1) {this.pos = found; return true;}
  57. },
  58. backUp: function(n) {this.pos -= n;},
  59. column: function() {
  60. if (this.lastColumnPos < this.start) {
  61. this.lastColumnValue = countColumn(this.string, this.start, this.tabSize, this.lastColumnPos, this.lastColumnValue);
  62. this.lastColumnPos = this.start;
  63. }
  64. return this.lastColumnValue - (this.lineStart ? countColumn(this.string, this.lineStart, this.tabSize) : 0);
  65. },
  66. indentation: function() {
  67. return countColumn(this.string, null, this.tabSize) -
  68. (this.lineStart ? countColumn(this.string, this.lineStart, this.tabSize) : 0);
  69. },
  70. match: function(pattern, consume, caseInsensitive) {
  71. if (typeof pattern == "string") {
  72. var cased = function(str) {return caseInsensitive ? str.toLowerCase() : str;};
  73. var substr = this.string.substr(this.pos, pattern.length);
  74. if (cased(substr) == cased(pattern)) {
  75. if (consume !== false) this.pos += pattern.length;
  76. return true;
  77. }
  78. } else {
  79. var match = this.string.slice(this.pos).match(pattern);
  80. if (match && match.index > 0) return null;
  81. if (match && consume !== false) this.pos += match[0].length;
  82. return match;
  83. }
  84. },
  85. current: function(){return this.string.slice(this.start, this.pos);},
  86. hideFirstChars: function(n, inner) {
  87. this.lineStart += n;
  88. try { return inner(); }
  89. finally { this.lineStart -= n; }
  90. },
  91. lookAhead: function(n) {
  92. var line = this.context.line + n
  93. return line >= this.context.lines.length ? null : this.context.lines[line]
  94. }
  95. };
  96. exports.StringStream = StringStream;
  97. exports.startState = function(mode, a1, a2) {
  98. return mode.startState ? mode.startState(a1, a2) : true;
  99. };
  100. var modes = exports.modes = {}, mimeModes = exports.mimeModes = {};
  101. exports.defineMode = function(name, mode) {
  102. if (arguments.length > 2)
  103. mode.dependencies = Array.prototype.slice.call(arguments, 2);
  104. modes[name] = mode;
  105. };
  106. exports.defineMIME = function(mime, spec) { mimeModes[mime] = spec; };
  107. exports.defineMode("null", function() {
  108. return {token: function(stream) {stream.skipToEnd();}};
  109. });
  110. exports.defineMIME("text/plain", "null");
  111. exports.resolveMode = function(spec) {
  112. if (typeof spec == "string" && mimeModes.hasOwnProperty(spec)) {
  113. spec = mimeModes[spec];
  114. } else if (spec && typeof spec.name == "string" && mimeModes.hasOwnProperty(spec.name)) {
  115. spec = mimeModes[spec.name];
  116. }
  117. if (typeof spec == "string") return {name: spec};
  118. else return spec || {name: "null"};
  119. };
  120. function copyObj(obj, target, overwrite) {
  121. if (!target) target = {};
  122. for (var prop in obj)
  123. if (obj.hasOwnProperty(prop) && (overwrite !== false || !target.hasOwnProperty(prop)))
  124. target[prop] = obj[prop];
  125. return target;
  126. }
  127. // This can be used to attach properties to mode objects from
  128. // outside the actual mode definition.
  129. var modeExtensions = exports.modeExtensions = {};
  130. exports.extendMode = function(mode, properties) {
  131. var exts = modeExtensions.hasOwnProperty(mode) ? modeExtensions[mode] : (modeExtensions[mode] = {});
  132. copyObj(properties, exts);
  133. };
  134. exports.getMode = function(options, spec) {
  135. var spec = exports.resolveMode(spec);
  136. var mfactory = modes[spec.name];
  137. if (!mfactory) return exports.getMode(options, "text/plain");
  138. var modeObj = mfactory(options, spec);
  139. if (modeExtensions.hasOwnProperty(spec.name)) {
  140. var exts = modeExtensions[spec.name];
  141. for (var prop in exts) {
  142. if (!exts.hasOwnProperty(prop)) continue;
  143. if (modeObj.hasOwnProperty(prop)) modeObj["_" + prop] = modeObj[prop];
  144. modeObj[prop] = exts[prop];
  145. }
  146. }
  147. modeObj.name = spec.name;
  148. if (spec.helperType) modeObj.helperType = spec.helperType;
  149. if (spec.modeProps) for (var prop in spec.modeProps)
  150. modeObj[prop] = spec.modeProps[prop];
  151. return modeObj;
  152. };
  153. exports.innerMode = function(mode, state) {
  154. var info;
  155. while (mode.innerMode) {
  156. info = mode.innerMode(state);
  157. if (!info || info.mode == mode) break;
  158. state = info.state;
  159. mode = info.mode;
  160. }
  161. return info || {mode: mode, state: state};
  162. }
  163. exports.registerHelper = exports.registerGlobalHelper = Math.min;
  164. exports.runMode = function(string, modespec, callback, options) {
  165. var mode = exports.getMode({indentUnit: 2}, modespec);
  166. var lines = splitLines(string), state = (options && options.state) || exports.startState(mode);
  167. var context = {lines: lines, line: 0}
  168. for (var i = 0, e = lines.length; i < e; ++i, ++context.line) {
  169. if (i) callback("\n");
  170. var stream = new exports.StringStream(lines[i], 4, context);
  171. if (!stream.string && mode.blankLine) mode.blankLine(state);
  172. while (!stream.eol()) {
  173. var style = mode.token(stream, state);
  174. callback(stream.current(), style, i, stream.start, state);
  175. stream.start = stream.pos;
  176. }
  177. }
  178. };
  179. require.cache[require.resolve("../../lib/codemirror")] = require.cache[require.resolve("./runmode.node")];
  180. require.cache[require.resolve("../../addon/runmode/runmode")] = require.cache[require.resolve("./runmode.node")];
上海开阖软件有限公司 沪ICP备12045867号-1