-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdiagnostics.ts
218 lines (193 loc) · 8.23 KB
/
diagnostics.ts
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* Copyright (c) 2020 DeltaXML Ltd. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the MIT license
* which accompanies this distribution.
*
* Contributors:
* DeltaXML Ltd. - XPath/XSLT Lexer/Syntax Highlighter
*/
import {CharLevelState, TokenLevelState, Token, XPathLexer} from "./xpLexer";
export class Debug {
public static printResultTokens(tokens: Token[]) {
tokens.forEach(Debug.showTokens);
}
public static printSerializedTokens(testTitle: string, testXpath: string, tokens: Token[]) {
let preamble: string = `
test('${testTitle}', () => {
let l: XPathLexer = new XPathLexer();
let r: Token[] = l.analyse('${testXpath}');
let ts: Token[] = `;
let postamble: string = `
expect (r).toEqual(ts);
});`
let r = tokens.reduce(this.serializeTokens, '');
let result = '[' + r + ']';
console.log(preamble + result + postamble);
}
public static printTokenValues(xpathExpr: string, resultTokens: Token[]) {
let lines = xpathExpr.split(/\r\n|\r|\n/);
console.log('-----------------')
console.log('line count: ' + lines.length);
console.log('token count: ' + resultTokens.length);
for (let i = 0; i < resultTokens.length; i++) {
let t: Token = resultTokens[i];
if (t.tokenType.valueOf() !== TokenLevelState.Whitespace.valueOf()) {
let line = lines[t.line];
let sc = t.startCharacter ? t.startCharacter : 0;
console.log(i + '. ' + line.substr(sc, t.length) + '_');
}
}
}
private static serializeTokens = function(accumulator: any, token: Token|null): any {
if (!token) {
return accumulator;
}
let err = (token.error)? ', error' : '\"\"';
let value = token.value;
let tokenType = 'TokenLevelState.' + Debug.tokenStateToString(token.tokenType);
let charType = 'CharLevelState.' + Debug.charStateToString(token.charType);
let childrenString: string = '';
if (token.children) {
childrenString = ',\nchildren:';
childrenString += '[' + token.children.reduce(Debug.serializeTokens, '') + ']';
}
let objectString =
`
{value: "${value}",
charType: ${charType},
tokenType: ${tokenType + childrenString}
},`;
return accumulator + objectString;
}
public static printMinSerializedTokens(testTitle: string, testXpath: string, tokens: Token[]) {
let preamble: string = `
test(\`${testTitle}\`, () => {
let l: XPathLexer = new XPathLexer();
let rx: Token[] = l.analyse(\`${testXpath}\`, ExitCondition.None, pos);
let r: Token[] = Utilities.minimiseTokens2(rx);
let ts: Token[] = `;
let postamble: string = `
expect (r).toEqual(ts);
});`
let r = tokens.reduce(this.minSerializeTokens, '');
let result = '[' + r + ']';
console.log(preamble + result + postamble);
}
private static minSerializeTokens = function(accumulator: any, token: Token|null): any {
if (!token || XPathLexer.isCharTypeEqual(token, CharLevelState.lWs)) {
return accumulator;
} else {
let err = (token.hasOwnProperty('error'))? "\nerror: true," : '';
let value = token.value;
let tokenType = 'TokenLevelState.' + Debug.tokenStateToString(token.tokenType);
let childrenString: string = '';
if (token.children) {
childrenString = ',\nchildren:';
childrenString += '[' + token.children.reduce(Debug.minSerializeTokens, '') + ']';
}
let objectString = `
{value: \`${value}\`,${err}
tokenType: ${tokenType + childrenString},
line: ${token.line},
length: ${token.length},
startCharacter: ${token.startCharacter}
},`;
return accumulator + objectString;
}
}
private static showTokens = function(token: Token) {
let err = (token.error)? ' error' : '';
let tokenValue = token.value + '';
let charState: string = Debug.charStateToString(token.charType);
console.log(Debug.padString(tokenValue) + Debug.padString(charState) + Debug.tokenStateToString(token.tokenType) + err);
if (token.children) {
console.log('--- children-start---');
token.children.forEach(Debug.showTokens);
console.log('--- children-end ----');
}
}
public static printDebugOutput(cachedRealToken: Token|null, newValue: Token, lineNumber: number, startCharacter: number) {
if (newValue.value !== '') {
let showWhitespace = true;
let cachedRealTokenString: string = cachedRealToken? this.getTokenDebugString(cachedRealToken): '';
let newT: string = this.getTokenDebugString(newValue);
let posString: string = lineNumber + ':' + startCharacter;
let posPadding: string = this.padColumns(newT.length);
let cachedTpadding: string = this.padColumns(cachedRealTokenString.length);
if (newValue.charType === CharLevelState.lWs && !(showWhitespace)) {
// show nothing
} else {
console.log(cachedRealTokenString + cachedTpadding + newT + posPadding + posString);
}
}
}
public static printStateOuput(prevRealToken: Token, currentLabelState: CharLevelState, nextLabelState: CharLevelState, token: string ) {
console.log('============STATE CHANGE ===========================');
let prevType: string;
let prevToken: string = '';
if (prevRealToken === null) {
prevType = 'NULL';
} else {
prevType = Debug.charStateToString(prevRealToken.charType);
prevToken = prevRealToken.value;
}
console.log('prevReal: ' + prevType + '[' + prevToken + ']');
console.log("from: " + Debug.charStateToString(currentLabelState));
console.log("to: " + Debug.charStateToString(nextLabelState)) + "[" + token + "]";
}
public static getTokenDebugString(lrt: Token) {
let prevType: string;
let prevToken: string = '';
if (lrt === null) {
prevType = 'NULL';
}
else {
prevType = this.charStateToString(lrt.charType);
prevToken = lrt.value;
}
let prevTypeLength = (prevType)? prevType.length : 0;
let oldT: string = prevType + this.padParts(prevTypeLength) + prevToken + '_';
return oldT;
}
private static padString(text: string): string {
return text + this.padParts(text.length);
}
private static padStringDots(padLength: number): string {
let padding = '';
for (let i = 0; i < 16 - padLength; i++) {
padding += '.';
}
return padding;
}
private static padColumns(padLength: number): string {
let padding = '';
for (let i = 0; i < 50 - padLength; i++) {
padding += ' ';
}
return padding;
}
private static padParts(padLength: number): string {
let padding = '';
for (let i = 0; i < 16 - padLength; i++) {
padding += ' ';
}
return padding;
}
public static debugHeading() {
let cachedT: string = 'Cached Real Token';
let newT: string = 'New Token';
let oldT: string = 'New Real Token';
let paddingCached: string = this.padColumns(cachedT.length);
let padding: string = this.padColumns(newT.length);
console.log('===============================================================================================================');
console.log(cachedT + paddingCached + newT);
console.log('===============================================================================================================');
}
public static tokenStateToString (resolvedState: TokenLevelState) : string {
return TokenLevelState[resolvedState];
}
public static charStateToString (stringCommentState: CharLevelState|undefined) : string {
return stringCommentState? CharLevelState[stringCommentState]: '';
}
}