runmode.node.js 4.1 KB

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