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