plaxtony
Version:
Static code analysis of SC2 Galaxy Script
117 lines • 4.2 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CatalogStore = void 0;
const common_1 = require("../common");
const dtypes = require("./dtypes");
class CatalogStore {
constructor() {
this.documentMap = new Map();
}
remove(uri) {
const catDoc = this.documentMap.get(uri);
if (!catDoc)
return;
this.documentMap.delete(uri);
}
update(doc, archive) {
this.remove(doc.uri);
const catDoc = {
uri: doc.uri,
declarations: [],
};
parseCatalog(doc, (decl) => {
let declarations = catDoc.declarations[decl.family];
if (typeof declarations === 'undefined') {
catDoc.declarations[decl.family] = declarations = new Map();
}
declarations.set(decl.id, decl);
});
this.documentMap.set(doc.uri, catDoc);
}
*findEntry(family) {
for (const catDoc of this.documentMap.values()) {
const declarations = catDoc.declarations[family];
if (typeof declarations === 'undefined')
continue;
yield declarations.values();
}
}
*findEntryByName(family, id) {
for (const catDoc of this.documentMap.values()) {
const declarations = catDoc.declarations[family];
if (typeof declarations === 'undefined')
continue;
const decl = declarations.get(id);
if (decl) {
yield decl;
}
}
}
get docCount() {
return this.documentMap.size;
}
}
__decorate([
common_1.logIt()
], CatalogStore.prototype, "update", null);
exports.CatalogStore = CatalogStore;
const reDataElement = /\s*<C([A-Z][A-Za-z0-9]+)\s([^>]+)\/?>/;
const reAttrs = /([\w-]+)\s?=\s?"([^"]+)"/g;
const reSubwordSeparator = /(?=[A-Z])/;
function parseCatalog(tdoc, onDeclaration) {
for (let i = 0; i < tdoc.lineCount; i++) {
const content = tdoc.getText({ start: { line: i, character: 0 }, end: { line: i, character: 1024 } });
const matchedElement = content.match(reDataElement);
if (!matchedElement)
continue;
let family;
const kindList = matchedElement[1].split(reSubwordSeparator);
while (1) {
family = dtypes.S2DataCatalogDomain[kindList.join('')];
if (typeof family === 'number')
break;
if (kindList.length <= 1)
break;
kindList.pop();
}
if (!family)
continue;
const declaration = {
family: family,
id: '',
ctype: matchedElement[1],
uri: tdoc.uri,
position: {
line: i,
character: matchedElement[0].length - matchedElement[1].length - matchedElement[2].length
},
};
let matchedAttr;
while (matchedAttr = reAttrs.exec(matchedElement[2])) {
switch (matchedAttr[1]) {
case 'id':
{
declaration.id = matchedAttr[2];
break;
}
case 'parent':
case 'default':
{
// (<any>entry)[matchedAttr[1]] = matchedAttr[2];
break;
}
}
}
reAttrs.lastIndex = 0;
if (declaration.id.length <= 0)
continue;
onDeclaration(declaration);
}
}
//# sourceMappingURL=datacatalog.js.map