preprocessor_mixin.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. 'use strict';
  2. var Mixin = require('../../utils/mixin'),
  3. inherits = require('util').inherits,
  4. UNICODE = require('../../common/unicode');
  5. //Aliases
  6. var $ = UNICODE.CODE_POINTS;
  7. var PositionTrackingPreprocessorMixin = module.exports = function (preprocessor) {
  8. // NOTE: avoid installing tracker twice
  9. if (!preprocessor.__locTracker) {
  10. preprocessor.__locTracker = this;
  11. Mixin.call(this, preprocessor);
  12. this.preprocessor = preprocessor;
  13. this.isEol = false;
  14. this.lineStartPos = 0;
  15. this.droppedBufferSize = 0;
  16. this.col = -1;
  17. this.line = 1;
  18. }
  19. return preprocessor.__locTracker;
  20. };
  21. inherits(PositionTrackingPreprocessorMixin, Mixin);
  22. Object.defineProperty(PositionTrackingPreprocessorMixin.prototype, 'offset', {
  23. get: function () {
  24. return this.droppedBufferSize + this.preprocessor.pos;
  25. }
  26. });
  27. PositionTrackingPreprocessorMixin.prototype._getOverriddenMethods = function (mxn, orig) {
  28. return {
  29. advance: function () {
  30. var cp = orig.advance.call(this);
  31. //NOTE: LF should be in the last column of the line
  32. if (mxn.isEol) {
  33. mxn.isEol = false;
  34. mxn.line++;
  35. mxn.lineStartPos = mxn.offset;
  36. }
  37. if (cp === $.LINE_FEED)
  38. mxn.isEol = true;
  39. mxn.col = mxn.offset - mxn.lineStartPos + 1;
  40. return cp;
  41. },
  42. retreat: function () {
  43. orig.retreat.call(this);
  44. mxn.isEol = false;
  45. mxn.col = mxn.offset - mxn.lineStartPos + 1;
  46. },
  47. dropParsedChunk: function () {
  48. var prevPos = this.pos;
  49. orig.dropParsedChunk.call(this);
  50. mxn.droppedBufferSize += prevPos - this.pos;
  51. }
  52. };
  53. };