1
0

runmode.node.js 3.4 KB

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