tokenizer-mixin.js
5.26 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'use strict';
const Mixin = require('../../utils/mixin');
const Tokenizer = require('../../tokenizer');
const PositionTrackingPreprocessorMixin = require('../position-tracking/preprocessor-mixin');
class LocationInfoTokenizerMixin extends Mixin {
constructor(tokenizer) {
super(tokenizer);
this.tokenizer = tokenizer;
this.posTracker = Mixin.install(tokenizer.preprocessor, PositionTrackingPreprocessorMixin);
this.currentAttrLocation = null;
this.ctLoc = null;
}
_getCurrentLocation() {
return {
startLine: this.posTracker.line,
startCol: this.posTracker.col,
startOffset: this.posTracker.offset,
endLine: -1,
endCol: -1,
endOffset: -1
};
}
_attachCurrentAttrLocationInfo() {
this.currentAttrLocation.endLine = this.posTracker.line;
this.currentAttrLocation.endCol = this.posTracker.col;
this.currentAttrLocation.endOffset = this.posTracker.offset;
const currentToken = this.tokenizer.currentToken;
const currentAttr = this.tokenizer.currentAttr;
if (!currentToken.location.attrs) {
currentToken.location.attrs = Object.create(null);
}
currentToken.location.attrs[currentAttr.name] = this.currentAttrLocation;
}
_getOverriddenMethods(mxn, orig) {
const methods = {
_createStartTagToken() {
orig._createStartTagToken.call(this);
this.currentToken.location = mxn.ctLoc;
},
_createEndTagToken() {
orig._createEndTagToken.call(this);
this.currentToken.location = mxn.ctLoc;
},
_createCommentToken() {
orig._createCommentToken.call(this);
this.currentToken.location = mxn.ctLoc;
},
_createDoctypeToken(initialName) {
orig._createDoctypeToken.call(this, initialName);
this.currentToken.location = mxn.ctLoc;
},
_createCharacterToken(type, ch) {
orig._createCharacterToken.call(this, type, ch);
this.currentCharacterToken.location = mxn.ctLoc;
},
_createEOFToken() {
orig._createEOFToken.call(this);
this.currentToken.location = mxn._getCurrentLocation();
},
_createAttr(attrNameFirstCh) {
orig._createAttr.call(this, attrNameFirstCh);
mxn.currentAttrLocation = mxn._getCurrentLocation();
},
_leaveAttrName(toState) {
orig._leaveAttrName.call(this, toState);
mxn._attachCurrentAttrLocationInfo();
},
_leaveAttrValue(toState) {
orig._leaveAttrValue.call(this, toState);
mxn._attachCurrentAttrLocationInfo();
},
_emitCurrentToken() {
const ctLoc = this.currentToken.location;
//NOTE: if we have pending character token make it's end location equal to the
//current token's start location.
if (this.currentCharacterToken) {
this.currentCharacterToken.location.endLine = ctLoc.startLine;
this.currentCharacterToken.location.endCol = ctLoc.startCol;
this.currentCharacterToken.location.endOffset = ctLoc.startOffset;
}
if (this.currentToken.type === Tokenizer.EOF_TOKEN) {
ctLoc.endLine = ctLoc.startLine;
ctLoc.endCol = ctLoc.startCol;
ctLoc.endOffset = ctLoc.startOffset;
} else {
ctLoc.endLine = mxn.posTracker.line;
ctLoc.endCol = mxn.posTracker.col + 1;
ctLoc.endOffset = mxn.posTracker.offset + 1;
}
orig._emitCurrentToken.call(this);
},
_emitCurrentCharacterToken() {
const ctLoc = this.currentCharacterToken && this.currentCharacterToken.location;
//NOTE: if we have character token and it's location wasn't set in the _emitCurrentToken(),
//then set it's location at the current preprocessor position.
//We don't need to increment preprocessor position, since character token
//emission is always forced by the start of the next character token here.
//So, we already have advanced position.
if (ctLoc && ctLoc.endOffset === -1) {
ctLoc.endLine = mxn.posTracker.line;
ctLoc.endCol = mxn.posTracker.col;
ctLoc.endOffset = mxn.posTracker.offset;
}
orig._emitCurrentCharacterToken.call(this);
}
};
//NOTE: patch initial states for each mode to obtain token start position
Object.keys(Tokenizer.MODE).forEach(modeName => {
const state = Tokenizer.MODE[modeName];
methods[state] = function(cp) {
mxn.ctLoc = mxn._getCurrentLocation();
orig[state].call(this, cp);
};
});
return methods;
}
}
module.exports = LocationInfoTokenizerMixin;