@lcap/nasl-parser
Version:
Take Nasl text to Nasl AST with the help of generalized parsing.
84 lines • 3.54 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createNaslPrimitiveType = exports.namedTypeToNaslTypeAnno = exports.fnTypeToNaslTypeAnno = exports.unionTypeToNaslTypeAnno = void 0;
const common_util_1 = require("./common-util");
const nasl = __importStar(require("@lcap/nasl"));
const unionTypeToNaslTypeAnno = (ty1, ty2) => {
const naslTy1 = (0, common_util_1.getNasl)(ty1);
const unionTys = [];
const unionCstTys = [];
if (nasl.isUnion(naslTy1)) {
unionTys.push(...naslTy1.typeArguments);
unionCstTys.push(...ty1.cst.tys);
}
else {
unionTys.push(naslTy1);
unionCstTys.push(ty1.cst);
}
unionTys.push((0, common_util_1.getNasl)(ty2));
unionCstTys.push(ty2.cst);
const nta = new nasl.TypeAnnotation({
typeKind: 'union',
typeArguments: unionTys
});
return (0, common_util_1.PackNaslAndCst)(nta, (0, common_util_1.TYPE)('UnionType', { tys: unionCstTys }));
};
exports.unionTypeToNaslTypeAnno = unionTypeToNaslTypeAnno;
const fnTypeToNaslTypeAnno = (parTys, retTy) => {
const nft = new nasl.TypeAnnotation({
typeKind: 'function',
returnType: [retTy.nasl],
typeArguments: parTys.nasl
});
const cst = (0, common_util_1.TYPE)('FnType', { parTys: parTys.nasl, retTy: (0, common_util_1.getCst)(retTy) });
return (0, common_util_1.PackNaslAndCst)(nft, cst);
};
exports.fnTypeToNaslTypeAnno = fnTypeToNaslTypeAnno;
// tyCon : qIdentifier
const namedTypeToNaslTypeAnno = (tyCon, tyArgs) => {
const cst = (0, common_util_1.TYPE)('NamedType', { tyCon, tyArgs: tyArgs?.cst });
const tyConStr = (0, common_util_1.projectIdentifier)(tyCon);
if ((0, common_util_1.isBuiltinPrimitiveType)(tyConStr)) {
const npt = (0, exports.createNaslPrimitiveType)(tyConStr === 'Integer' ? 'Long' : tyConStr);
return (0, common_util_1.PackNaslAndCst)(npt, cst);
}
const nft = new nasl.TypeAnnotation({
typeKind: 'reference',
typeNamespace: '',
typeName: tyCon.name,
typeArguments: tyArgs?.nasl,
});
nft.qName = tyCon;
return (0, common_util_1.PackNaslAndCst)(nft, cst);
};
exports.namedTypeToNaslTypeAnno = namedTypeToNaslTypeAnno;
const createNaslPrimitiveType = (typeName) => new nasl.TypeAnnotation({
typeName,
typeKind: 'primitive',
typeNamespace: common_util_1.NASL_CORE_AST,
});
exports.createNaslPrimitiveType = createNaslPrimitiveType;
//# sourceMappingURL=to-nasl-type.js.map