-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathSGParser.ts
More file actions
371 lines (334 loc) · 11.7 KB
/
SGParser.ts
File metadata and controls
371 lines (334 loc) · 11.7 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import type { AttributeCstNode, ContentCstNode, DocumentCstNode, ElementCstNode } from '@xml-tools/parser';
import * as parser from '@xml-tools/parser';
import { DiagnosticMessages } from '../DiagnosticMessages';
import type { Diagnostic, Range } from 'vscode-languageserver';
import util from '../util';
import { SGAst, SGProlog, SGChildren, SGComponent, SGField, SGFunction, SGInterface, SGNode, SGScript } from './SGTypes';
import type { SGTag, SGToken, SGAttribute, SGReferences } from './SGTypes';
import { isSGComponent } from '../astUtils/xml';
export default class SGParser {
/**
* The AST of the XML document, not including the inline scripts
*/
public ast: SGAst = new SGAst();
public tokens: IToken[];
/**
* The list of diagnostics found during the parse process
*/
public diagnostics = [] as Diagnostic[];
private pkgPath: string;
private _references: SGReferences;
/**
* These are initially extracted during parse-time, but will also be dynamically regenerated if need be.
*
* If a plugin modifies the AST, then the plugin should call SGAst#invalidateReferences() to force this object to refresh
*/
get references(): SGReferences {
if (this._references === undefined) {
this.findReferences();
}
return this._references;
}
/**
* Invalidates (clears) the references collection. This should be called anytime the AST has been manipulated.
*/
invalidateReferences() {
this._references = undefined;
}
/**
* Walk the AST to extract references to useful bits of information
*/
private findReferences() {
this._references = emptySGReferences();
const { component } = this.ast;
if (!component) {
return;
}
const nameAttr = component.getAttribute('name');
if (nameAttr?.value) {
this._references.name = nameAttr.value;
}
const extendsAttr = component.getAttribute('extends');
if (extendsAttr?.value) {
this._references.extends = extendsAttr.value;
}
for (const script of component.scripts) {
const uriAttr = script.getAttribute('uri');
if (uriAttr) {
const uri = uriAttr.value.text;
this._references.scriptTagImports.push({
filePathRange: uriAttr.value.range,
text: uri,
pkgPath: util.getPkgPathFromTarget(this.pkgPath, uri)
});
}
}
}
public parse(pkgPath: string, fileContents: string) {
this.pkgPath = pkgPath;
this.diagnostics = [];
const { cst, tokenVector, lexErrors, parseErrors } = parser.parse(fileContents);
this.tokens = tokenVector;
if (lexErrors.length) {
for (const err of lexErrors) {
this.diagnostics.push({
...DiagnosticMessages.xmlGenericParseError(`Syntax error: ${err.message}`),
range: util.createRange(
err.line - 1,
err.column,
err.line - 1,
err.column + err.length
)
});
}
}
if (parseErrors.length) {
const err = parseErrors[0];
const token = err.token;
this.diagnostics.push({
...DiagnosticMessages.xmlGenericParseError(`Syntax error: ${err.message}`),
range: !isNaN(token.startLine) ? rangeFromTokens(token) : util.createRange(0, 0, 0, Number.MAX_VALUE)
});
}
const { prolog, root } = buildAST(cst as any, this.diagnostics);
if (!root) {
const token1 = tokenVector[0];
const token2 = tokenVector[1];
//whitespace before the prolog isn't allowed by the parser
if (
token1?.image.trim().length === 0 &&
token2?.image.trim() === '<?xml'
) {
this.diagnostics.push({
...DiagnosticMessages.xmlGenericParseError('Syntax error: whitespace found before the XML prolog'),
range: rangeFromTokens(token1)
});
}
}
if (isSGComponent(root)) {
this.ast = new SGAst(prolog, root, root);
} else {
if (root) {
//error: not a component
this.diagnostics.push({
...DiagnosticMessages.xmlUnexpectedTag(root.tag.text),
range: root.tag.range
});
}
this.ast = new SGAst(prolog, root);
}
}
}
function buildAST(cst: DocumentCstNode, diagnostics: Diagnostic[]) {
const { prolog: cstProlog, element } = cst.children;
let prolog: SGProlog;
if (cstProlog?.[0]) {
const ctx = cstProlog[0].children;
prolog = new SGProlog(
mapToken(ctx.XMLDeclOpen[0]),
mapAttributes(ctx.attribute),
rangeFromTokens(ctx.XMLDeclOpen[0], ctx.SPECIAL_CLOSE[0])
);
}
let root: SGTag;
if (element.length > 0 && element[0]?.children?.Name) {
root = mapElement(element[0], diagnostics);
}
return {
prolog: prolog,
root: root
};
}
//not exposed by @xml-tools/parser
interface IToken {
image: string;
startOffset: number;
startLine?: number;
startColumn?: number;
endOffset?: number;
endLine?: number;
endColumn?: number;
}
function mapElement({ children }: ElementCstNode, diagnostics: Diagnostic[]): SGTag {
const nameToken = children.Name[0];
let range: Range;
const selfClosing = !!children.SLASH_CLOSE;
if (selfClosing) {
const scToken = children.SLASH_CLOSE[0];
range = rangeFromTokens(nameToken, scToken);
} else {
const endToken = children.END?.[0];
range = rangeFromTokens(nameToken, endToken);
}
const name = mapToken(nameToken);
const attributes = mapAttributes(children.attribute);
const content = children.content?.[0];
switch (name.text) {
case 'component':
const componentContent = mapElements(content, ['interface', 'script', 'children', 'customization'], diagnostics);
return new SGComponent(children, name, attributes, componentContent, range);
case 'interface':
const interfaceContent = mapElements(content, ['field', 'function'], diagnostics);
return new SGInterface(name, interfaceContent, range);
case 'field':
if (hasElements(content)) {
reportUnexpectedChildren(name, diagnostics);
}
return new SGField(name, attributes, range);
case 'function':
if (hasElements(content)) {
reportUnexpectedChildren(name, diagnostics);
}
return new SGFunction(name, attributes, range);
case 'script':
if (hasElements(content)) {
reportUnexpectedChildren(name, diagnostics);
}
const cdata = getCdata(content);
return new SGScript(name, attributes, cdata, range);
case 'children':
const childrenContent = mapNodes(content);
return new SGChildren(name, childrenContent, range);
default:
const nodeContent = mapNodes(content);
return new SGNode(name, attributes, nodeContent, range);
}
}
function reportUnexpectedChildren(name: SGToken, diagnostics: Diagnostic[]) {
diagnostics.push({
...DiagnosticMessages.xmlUnexpectedChildren(name.text),
range: name.range
});
}
function mapNode({ children }: ElementCstNode): SGNode {
const nameToken = children.Name[0];
let range: Range;
const selfClosing = !!children.SLASH_CLOSE;
if (selfClosing) {
const scToken = children.SLASH_CLOSE[0];
range = rangeFromTokens(nameToken, scToken);
} else {
const endToken = children.END?.[0];
range = rangeFromTokens(nameToken, endToken);
}
const name = mapToken(nameToken);
const attributes = mapAttributes(children.attribute);
const content = children.content?.[0];
const nodeContent = mapNodes(content);
return new SGNode(name, attributes, nodeContent, range);
}
function mapElements(content: ContentCstNode, allow: string[], diagnostics: Diagnostic[]): SGTag[] {
if (!content) {
return [];
}
const { element } = content.children;
const tags: SGTag[] = [];
if (element) {
for (const entry of element) {
const name = entry.children.Name?.[0];
if (name?.image) {
if (allow.includes(name.image)) {
tags.push(mapElement(entry, diagnostics));
} else {
//unexpected tag
diagnostics.push({
...DiagnosticMessages.xmlUnexpectedTag(name.image),
range: rangeFromTokens(name)
});
}
} else {
//bad xml syntax...
}
}
}
return tags;
}
function mapNodes(content: ContentCstNode): SGNode[] {
if (!content) {
return [];
}
const { element } = content.children;
return element?.map(element => mapNode(element));
}
function hasElements(content: ContentCstNode): boolean {
return !!content?.children.element?.length;
}
function getCdata(content: ContentCstNode): SGToken {
if (!content) {
return undefined;
}
const { CData } = content.children;
return mapToken(CData?.[0]);
}
function mapToken(token: IToken, unquote = false): SGToken {
if (!token) {
return undefined;
}
return {
text: unquote ? stripQuotes(token.image) : token.image,
range: unquote ? rangeFromTokenValue(token) : rangeFromTokens(token)
};
}
function mapAttributes(attributes: AttributeCstNode[]): SGAttribute[] {
return attributes?.map(({ children }) => {
const key = children.Name[0];
const value = children.STRING?.[0];
let openQuote: SGToken;
let closeQuote: SGToken;
//capture the leading and trailing quote tokens
const match = /^(["']).*?(["'])$/.exec(value?.image);
if (match) {
const range = rangeFromTokenValue(value);
openQuote = {
text: match[1],
range: util.createRange(range.start.line, range.start.character, range.start.line, range.start.character + 1)
};
closeQuote = {
text: match[1],
range: util.createRange(range.end.line, range.end.character - 1, range.end.line, range.end.character)
};
}
return {
key: mapToken(key),
openQuote: openQuote,
value: mapToken(value, true),
closeQuote: closeQuote,
range: rangeFromTokens(key, value)
};
}) || [];
}
//make range from `start` to `end` tokens
function rangeFromTokens(start: IToken, end?: IToken) {
if (!end) {
end = start;
}
return util.createRange(
start.startLine - 1,
start.startColumn - 1,
end.endLine - 1,
end.endColumn
);
}
//make range not including quotes
export function rangeFromTokenValue(token: IToken) {
if (!token) {
return undefined;
}
return util.createRange(
token.startLine - 1,
token.startColumn,
token.endLine - 1,
token.endColumn - 1
);
}
function stripQuotes(value: string) {
if (value?.length > 1) {
return value.substr(1, value.length - 2);
}
return '';
}
function emptySGReferences(): SGReferences {
return {
scriptTagImports: []
};
}