runmode.node.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. // declare global: StringStream
  5. function splitLines(string){ return string.split(/\r?\n|\r/); };
  6. function StringStream(string) {
  7. this.pos = this.start = 0;
  8. this.string = string;
  9. this.lineStart = 0;
  10. }
  11. StringStream.prototype = {
  12. eol: function() {return this.pos >= this.string.length;},
  13. sol: function() {return this.pos == 0;},
  14. peek: function() {return this.string.charAt(this.pos) || null;},
  15. next: function() {
  16. if (this.pos < this.string.length)
  17. return this.string.charAt(this.pos++);
  18. },
  19. eat: function(match) {
  20. var ch = this.string.charAt(this.pos);
  21. if (typeof match == "string") var ok = ch == match;
  22. else var ok = ch && (match.test ? match.test(ch) : match(ch));
  23. if (ok) {++this.pos; return ch;}
  24. },
  25. eatWhile: function(match) {
  26. var start = this.pos;
  27. while (this.eat(match)){}
  28. return this.pos > start;
  29. },
  30. eatSpace: function() {
  31. var start = this.pos;
  32. while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) ++this.pos;
  33. return this.pos > start;
  34. },
  35. skipToEnd: function() {this.pos = this.string.length;},
  36. skipTo: function(ch) {
  37. var found = this.string.indexOf(ch, this.pos);
  38. if (found > -1) {this.pos = found; return true;}
  39. },
  40. backUp: function(n) {this.pos -= n;},
  41. column: function() {return this.start - this.lineStart;},
  42. indentation: function() {return 0;},
  43. match: function(pattern, consume, caseInsensitive) {
  44. if (typeof pattern == "string") {
  45. var cased = function(str) {return caseInsensitive ? str.toLowerCase() : str;};
  46. var substr = this.string.substr(this.pos, pattern.length);
  47. if (cased(substr) == cased(pattern)) {
  48. if (consume !== false) this.pos += pattern.length;
  49. return true;
  50. }
  51. } else {
  52. var match = this.string.slice(this.pos).match(pattern);
  53. if (match && match.index > 0) return null;
  54. if (match && consume !== false) this.pos += match[0].length;
  55. return match;
  56. }
  57. },
  58. current: function(){return this.string.slice(this.start, this.pos);},
  59. hideFirstChars: function(n, inner) {
  60. this.lineStart += n;
  61. try { return inner(); }
  62. finally { this.lineStart -= n; }
  63. }
  64. };
  65. exports.StringStream = StringStream;
  66. exports.startState = function(mode, a1, a2) {
  67. return mode.startState ? mode.startState(a1, a2) : true;
  68. };
  69. var modes = exports.modes = {}, mimeModes = exports.mimeModes = {};
  70. exports.defineMode = function(name, mode) {
  71. if (arguments.length > 2)
  72. mode.dependencies = Array.prototype.slice.call(arguments, 2);
  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. if (!stream.string && mode.blankLine) mode.blankLine(state);
  103. while (!stream.eol()) {
  104. var style = mode.token(stream, state);
  105. callback(stream.current(), style, i, stream.start, state);
  106. stream.start = stream.pos;
  107. }
  108. }
  109. };
  110. require.cache[require.resolve("../../lib/codemirror")] = require.cache[require.resolve("./runmode.node")];