@lcap/nasl-parser
Version:
Take Nasl text to Nasl AST with the help of generalized parsing.
235 lines • 8.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNasl = exports.getCst = exports.createReferenceType = exports.extractIdent = exports.createNamedType = exports.prependNamespace = exports.withNoEnv = exports.createQIdentifier = exports.Visitor = exports.identity = exports.foldR1 = exports.tail = exports.last = exports.head = exports.isBuiltinPrimitiveType = exports.projectIdentifierAST = exports.projectIdentifier = exports.NAMESPACE_SEP_AST = exports.NAMESPACE_SEP = exports.CODEWAVE = exports.PRIMITIVE_TYPES = exports.CONNECTOR = exports.APP_ENUM = exports.APP_ENTITY_AST = exports.APP_STRUCTURE_AST = exports.APP_LOGICS_AST = exports.APP_LOGICS_NL = exports.APP = exports.APIS = exports.NASL_COLLECTION_AST = exports.NASL_LOGGING_AST = exports.NASL_UTIL_AST = exports.NASL_UTIL_NL = exports.NASL_CORE_AST = exports.NASL_CORE_NL = exports.NASL = void 0;
exports.zipWith = zipWith;
exports.foldR = foldR;
exports.isEmpty = isEmpty;
exports.splitByPredicate = splitByPredicate;
exports.composeWithEnv = composeWithEnv;
exports.sequenceWithEnv_ = sequenceWithEnv_;
exports.wellDefined = wellDefined;
exports.SimpleSillyDeepClone = SimpleSillyDeepClone;
exports.TYPE = TYPE;
exports.PackNaslAndCst = PackNaslAndCst;
exports.PackNaslAndCstSeq = PackNaslAndCstSeq;
exports.AutoPackNaslAndCst = AutoPackNaslAndCst;
exports.removeDup = removeDup;
const asserts_1 = require("@lcap/nasl-concepts/asserts");
exports.NASL = 'nasl';
exports.NASL_CORE_NL = 'nasl::core';
exports.NASL_CORE_AST = 'nasl.core';
exports.NASL_UTIL_NL = 'nasl::util';
exports.NASL_UTIL_AST = 'nasl.util';
exports.NASL_LOGGING_AST = 'nasl.logging';
exports.NASL_COLLECTION_AST = 'nasl.collection';
exports.APIS = 'apis';
exports.APP = 'app';
exports.APP_LOGICS_NL = 'app::logics';
exports.APP_LOGICS_AST = 'app.logics';
exports.APP_STRUCTURE_AST = 'app.structures';
exports.APP_ENTITY_AST = 'app.dataSources.defaultDS.entities';
exports.APP_ENUM = 'app.enums';
exports.CONNECTOR = 'connector';
exports.PRIMITIVE_TYPES = ['Boolean', 'String', 'Integer', 'Decimal', 'Date', 'Time', 'DateTime', 'Unit'];
exports.CODEWAVE = 'CodeWave';
exports.NAMESPACE_SEP = '::';
exports.NAMESPACE_SEP_AST = '.';
const projectIdentifier = (id) => (id?.namespace ?? []).concat(id.name).join(exports.NAMESPACE_SEP);
exports.projectIdentifier = projectIdentifier;
const projectIdentifierAST = (id) => (id?.namespace ?? []).concat(id?.name).join(exports.NAMESPACE_SEP_AST);
exports.projectIdentifierAST = projectIdentifierAST;
const isBuiltinPrimitiveType = (name) => exports.PRIMITIVE_TYPES.includes(name);
exports.isBuiltinPrimitiveType = isBuiltinPrimitiveType;
const head = (arr) => {
if (isEmpty(arr)) {
throw new Error('empty array');
}
return arr[0];
};
exports.head = head;
const last = (arr) => {
if (isEmpty(arr)) {
throw new Error('empty array');
}
return arr[arr.length - 1];
};
exports.last = last;
const tail = (arr) => arr.slice(1);
exports.tail = tail;
function zipWith(f, arr1, arr2) {
const res = [];
for (let i = 0; i < arr1.length; i++) {
res.push(f(arr1[i], arr2[i]));
}
return res;
}
function foldR(lam, acc, arr) {
if (isEmpty(arr)) {
return acc;
}
return lam((0, exports.head)(arr), foldR(lam, acc, (0, exports.tail)(arr)));
}
const foldR1 = (lam, morph) => (arr) => {
if (isEmpty(arr)) {
throw new Error('empty array');
}
if (arr.length === 1) {
return morph(arr[0]);
}
return lam((0, exports.head)(arr), (0, exports.foldR1)(lam, morph)((0, exports.tail)(arr)));
};
exports.foldR1 = foldR1;
const identity = (x) => x;
exports.identity = identity;
function isEmpty(arr) {
if (!arr) {
throw new Error('input is null or undefined');
}
return arr.length === 0;
}
function splitByPredicate(arr, pred) {
const sat = [];
const unsat = [];
arr.forEach(e => {
if (pred(e)) {
sat.push(e);
}
else {
unsat.push(e);
}
});
return [sat, unsat];
}
function composeWithEnv(f, g) {
return (env, a) => g(env, f(env, a));
}
function sequenceWithEnv_(f, g) {
return (env, a) => { f(env, a); return g(env, a); };
}
class Visitor {
preOrderVisitAll(anyInfo, obj, f, postWork) {
obj = f(anyInfo, obj);
if (Array.isArray(obj)) {
for (let i = 0; i < obj.length; i++) {
const newElem = this.preOrderVisitAll(anyInfo, obj[i], f, postWork);
obj[i] = newElem;
}
}
else if (obj && typeof obj === 'object') {
for (const prop in obj) {
if (prop === 'parentNode') { // 厉害!
continue;
}
const newProp = this.preOrderVisitAll(anyInfo, obj[prop], f, postWork);
if (newProp !== obj[prop]) {
obj[prop] = newProp;
}
}
}
if (postWork) {
return postWork(anyInfo, obj);
}
else {
return obj;
}
}
}
exports.Visitor = Visitor;
const createQIdentifier = (name, namespace = []) => TYPE('QIdentifier', { namespace, name });
exports.createQIdentifier = createQIdentifier;
const withNoEnv = lam => (env, obj) => lam(obj);
exports.withNoEnv = withNoEnv;
const prependNamespace = (ns, name) => {
if (typeof name === 'string') {
return ns.concat(name).join(exports.NAMESPACE_SEP_AST);
}
else if ('namespace' in name) {
return ns.concat(name.namespace, name.name).join(exports.NAMESPACE_SEP_AST);
}
else {
throw new Error(`prependNamespace: invalid input ${ns.toString()} ${name}`);
}
};
exports.prependNamespace = prependNamespace;
// const createTypeConstructor = (name : string) => createIdentifier(name, []);
const createNamedType = (tyCon, tyVars = [], isConst = false) => {
return TYPE('NamedType', { tyCon, tyVars });
};
exports.createNamedType = createNamedType;
const extractIdent = (me) => (0, asserts_1.isIdentifier)(me.object) ? me.object : (0, exports.extractIdent)(me.object);
exports.extractIdent = extractIdent;
const createReferenceType = (name) => {
return (0, exports.createQIdentifier)(name, []);
};
exports.createReferenceType = createReferenceType;
function wellDefined(node) {
return node !== undefined && node !== null;
}
function SimpleSillyDeepClone(obj) {
return JSON.parse(JSON.stringify(obj));
}
function TYPE(ty, data) {
if (data) {
data['__type'] = ty;
return data;
}
else {
return (d) => {
d[0]['__type'] = ty;
return d[0];
};
}
}
function PackNaslAndCst(nasl, cst) {
return { nasl, cst };
}
function PackNaslAndCstSeq(ls) {
// if (!ls) {
// return undefined;
// }
// if (!Array.isArray(ls)) {
// return ls;
// }
return PackNaslAndCst(ls.map(exports.getNasl), ls.map(exports.getCst));
}
function AutoPackNaslAndCst(obj) {
const res = { nasl: {}, cst: {} };
for (const prop in obj) {
if (Object.hasOwn(obj, prop)) {
res.nasl[prop] = obj[prop]?.nasl;
res.cst[prop] = obj[prop]?.cst;
}
}
return res;
}
// export function AutoLiftNaslAndCst(obj) {
// const res = { nasl: {}, cst: {} };
// for (const prop in obj?.nasl) {
// if (Object.hasOwn(obj?.nasl, prop)) {
// res.nasl[prop] = prop;
// }
// }
// for (const prop in obj?.cst) {
// if (Object.hasOwn(obj?.cst, prop)) {
// res.cst[prop] = prop;
// }
// }
// return res;
// }
const getCst = o => o.cst;
exports.getCst = getCst;
const getNasl = o => o.nasl;
exports.getNasl = getNasl;
function removeDup(list) {
const seen = new Set();
return list.filter(element => {
if (seen.has(element)) {
return false;
}
else {
seen.add(element);
return true;
}
});
}
//# sourceMappingURL=common-util.js.map