UNPKG

@lcap/nasl-parser

Version:

Take Nasl text to Nasl AST with the help of generalized parsing.

191 lines 8.03 kB
"use strict"; 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.toNaslValueParam = exports.toNaslSwitchCase = exports.toNaslSwitch = exports.toNaslEnd = exports.toNaslAbort = exports.toNaslForeach = exports.toNaslWhile = exports.toNaslElseIf = exports.toNaslIf = void 0; exports.toNaslSingleAssign = toNaslSingleAssign; exports.toNaslBatchAsgn = toNaslBatchAsgn; const common_util_1 = require("./common-util"); const nasl = __importStar(require("@lcap/nasl")); const asserts_1 = require("@lcap/nasl-concepts/asserts"); function toNaslSingleAssign(lhs, rhs) { const na = new nasl.Assignment({ left: lhs.nasl, right: rhs.nasl }); return (0, common_util_1.PackNaslAndCst)(na, (0, common_util_1.TYPE)('SingleAssign', { lhs: lhs.cst, rhs: rhs.cst })); } function toNaslBatchAsgn(asgns) { const packed = (0, common_util_1.PackNaslAndCstSeq)(asgns); const lefts = packed.nasl.map((asgn) => asgn.left); const rights = packed.nasl.map((asgn) => asgn.right); const repSel = (0, common_util_1.extractIdent)(lefts?.[0]); const selMems = new nasl.SelectMembers({ expression: repSel, members: lefts, hideMembers: false }); // 无能为力,认怂 const rSelMems = rights.map((r) => { // if (isMemberExpression(r)) { // throw new Error('批量赋值的单个赋值的右侧暂不支持多层复杂'); // } return new nasl.SelectMembers({ expression: (0, asserts_1.isMemberExpression)(r) ? (0, common_util_1.extractIdent)(r) : r, members: (0, asserts_1.isMemberExpression)(r) ? [r] : [], // 暂不支持多层 hideMembers: false }); }); // 无能为力,认怂 const lines = rights.map((r, idx) => new nasl.AssignmentLine({ leftIndex: [0, idx], rightIndex: (0, asserts_1.isMemberExpression)(r) ? [idx, 0] : [idx], })); const batchAsgn = new nasl.BatchAssignment({ left: selMems, rights: rSelMems, assignmentLines: lines }); return (0, common_util_1.PackNaslAndCst)(batchAsgn, (0, common_util_1.TYPE)('BatchAssign', { asgns: packed.cst })); } const toNaslIf = (test, consequent, elseifs, finalElse) => { if (elseifs && elseifs.nasl && !(0, common_util_1.isEmpty)(elseifs.nasl)) { const switchCase = new nasl.SwitchStatement({ cases: [new nasl.SwitchCase({ test: test.nasl, consequent: consequent.nasl })] }); switchCase.cases.push(elseifs.nasl.map(ei => new nasl.SwitchCase({ test: ei.test, consequent: ei.consequent }))); switchCase.cases.push(new nasl.SwitchCase({ test: null, consequent: finalElse?.nasl })); return (0, common_util_1.PackNaslAndCst)(switchCase, (0, common_util_1.TYPE)('If', { test: test.cst, consequent: consequent.cst, elseifs: elseifs.cst, finalElse: finalElse?.cst })); } else { const ni = new nasl.IfStatement({ test: test.nasl, consequent: consequent.nasl, alternate: finalElse?.nasl }); return (0, common_util_1.PackNaslAndCst)(ni, (0, common_util_1.TYPE)('If', { test: test.cst, consequent: consequent.cst, finalElse: finalElse?.cst })); } }; exports.toNaslIf = toNaslIf; const toNaslElseIf = (test, stmts) => (0, common_util_1.PackNaslAndCst)({ test: test.nasl, consequent: stmts.nasl }, (0, common_util_1.TYPE)('ElseIf', { test: test.cst, consequent: stmts.cst })); exports.toNaslElseIf = toNaslElseIf; const toNaslWhile = (test, body) => { const nw = new nasl.WhileStatement({ test: test.nasl, body: body.nasl }); return (0, common_util_1.PackNaslAndCst)(nw, (0, common_util_1.TYPE)('While', { test: test.cst, body: body.cst })); }; exports.toNaslWhile = toNaslWhile; const toNaslForeach = (cond, stmts) => { const nl = new nasl.ForEachStatement({ item: new nasl.Param({ name: cond.nasl.item }), index: new nasl.Param({ name: cond.nasl.idx }), each: cond.nasl.colle, body: stmts.nasl }); if (cond.nasl.start) { nl.start = cond.nasl.start; } if (cond.nasl.end) { nl.end = cond.nasl.end; } const cst = { item: cond.cst.item, colle: cond.cst.colle, idx: cond.cst.idx, start: cond.cst.start, end: cond.cst.end, body: stmts.cst }; return (0, common_util_1.PackNaslAndCst)(nl, (0, common_util_1.TYPE)('Foreach', cst)); }; exports.toNaslForeach = toNaslForeach; const toNaslAbort = (_) => (0, common_util_1.PackNaslAndCst)(new nasl.Abort(), (0, common_util_1.TYPE)('Abort', {})); exports.toNaslAbort = toNaslAbort; const toNaslEnd = (_) => { const ne = new nasl.End(); ne.label = '结束'; // 不写 label 还真就不加了,🐂 return (0, common_util_1.PackNaslAndCst)(ne, (0, common_util_1.TYPE)('End', {})); }; exports.toNaslEnd = toNaslEnd; const toNaslSwitch = (cases) => { const ns = new nasl.SwitchStatement({ cases: cases.nasl.norm.concat(cases.nasl.dft) }); return (0, common_util_1.PackNaslAndCst)(ns, (0, common_util_1.TYPE)('Switch', { cases: cases.cst.norm?.concat(cases.cst.dft) })); }; exports.toNaslSwitch = toNaslSwitch; const toNaslSwitchCase = (stmts, expr) => { const nsc = new nasl.SwitchCase({ test: expr?.nasl, consequent: stmts.nasl }); return (0, common_util_1.PackNaslAndCst)(nsc, (0, common_util_1.TYPE)('SwitchCase', { test: expr?.cst, consequent: stmts.cst })); }; exports.toNaslSwitchCase = toNaslSwitchCase; exports.toNaslValueParam = undefined; // export function toNaslValueParam(e: LamParam | Param | QIdentifier | string): nasl.Param { // if (isParam(e)) { // const np = new nasl.Param({ // name: e.name, // typeAnnotation: toNaslTypeAnno(e.ty), // defaultValue: e.val ? toNaslDefaultValue(e.val.expr) : undefined, // defaultExpression: e.val ? dispatchToNaslExpr(e.val.expr) : null // }); // if (e.anns) { // // @ts-ignore // np.annotations = e.anns; // } // return np; // } // if (isLamParam(e)) { // const np = new nasl.Param({ // name: e.name, // typeAnnotation: e.ty ? toNaslTypeAnno(e.ty) : null, // defaultValue: null, // defaultExpression: null // }); // return np; // } // if (isQIdentifier(e)) { // const np = new nasl.Param({ // name: e.name, // typeAnnotation: null, // defaultValue: null, // }); // return np; // } // if (typeof e === 'string') { // return new nasl.Param({ // name: e, // typeAnnotation: null, // defaultValue: null // }); // } // throw new Error(`toNaslValueParam: ${e}`); // } //# sourceMappingURL=to-nasl-statement.js.map