2
0

runmode.node.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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) { modes[name] = mode; };
  60. exports.defineMIME = function(mime, spec) { mimeModes[mime] = spec; };
  61. exports.getMode = function(options, spec) {
  62. if (typeof spec == "string" && mimeModes.hasOwnProperty(spec))
  63. spec = mimeModes[spec];
  64. if (typeof spec == "string")
  65. var mname = spec, config = {};
  66. else if (spec != null)
  67. var mname = spec.name, config = spec;
  68. var mfactory = modes[mname];
  69. if (!mfactory) throw new Error("Unknown mode: " + spec);
  70. return mfactory(options, config || {});
  71. };
  72. exports.runMode = function(string, modespec, callback) {
  73. var mode = exports.getMode({indentUnit: 2}, modespec);
  74. var lines = splitLines(string), state = exports.startState(mode);
  75. for (var i = 0, e = lines.length; i < e; ++i) {
  76. if (i) callback("\n");
  77. var stream = new exports.StringStream(lines[i]);
  78. while (!stream.eol()) {
  79. var style = mode.token(stream, state);
  80. callback(stream.current(), style, i, stream.start);
  81. stream.start = stream.pos;
  82. }
  83. }
  84. };