@lcap/nasl-parser
Version:
Take Nasl text to Nasl AST with the help of generalized parsing.
133 lines • 5.46 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.forceToNumber = exports.forceToBoolean = exports.forceToString = void 0;
exports.processAnnotations = processAnnotations;
exports.processLogicAnnotations = processLogicAnnotations;
exports.processEnumAnnotations = processEnumAnnotations;
exports.processStructAnnotations = processStructAnnotations;
const nasl = __importStar(require("./decorated-nasl-ast"));
const asserts_1 = require("@lcap/nasl-concepts/asserts");
const common_util_1 = require("./common-util");
// import { tryAddToNaslDataSource } from './process-annotations-entity';
/**
* 怎么说呢,步子不能迈太大,还是分三步吧。
* 1. 编译器内置一批可用的注解和处理规则,这样 AI 就能用起来了
* 2. 编译器给一套规范和一个入口,用户可用 ts、js 写处理规则,编译器会调用用户的处理规则
* 3. 用户可用 nasl 文本语法写处理规则
*
* 发现一个用构造函数 @column(id = ..., name = ..., required = ...)相比于 @id =..., @name =... @required =... 的好处
* 用构造函数的话,可以为 id、name、required 都设默认值,所以 @column 可以不写这些参数。
* 如果不用构造函数的话,即便要用默认值,@id、@name、@required 也都必须写出来,因为一般来说如果用户不标记,也不能认为有一批默认的标记吧
*/
// processAnnotations -> tryAddToNaslDataSource -> addToSpecificNaslDataSource -> toNaslDataEntity -> processColumnAnnotation
function processAnnotations(cei, nodes, dataSources) {
new common_util_1.Visitor().preOrderVisitAll(cei, nodes, (0, common_util_1.withNoEnv)(processLogicAnnotations));
new common_util_1.Visitor().preOrderVisitAll(cei, nodes, (0, common_util_1.withNoEnv)(processEnumAnnotations));
new common_util_1.Visitor().preOrderVisitAll(cei, nodes, (0, common_util_1.withNoEnv)(processStructAnnotations));
// @ts-ignore
// nodes = new Visitor().preOrderVisitAll(cei, nodes, withNoEnv(tryAddToNaslDataSource(dataSources)));
}
const IDE = 'ide';
const DISPLAY = 'display';
function processLogicAnnotations(nl) {
if (!(0, asserts_1.isLogic)(nl) && !(0, asserts_1.isFunction)(nl) && !(0, asserts_1.isLogicDeclaration)(nl)) {
return nl;
}
const cAnns = nl.annotations;
if (cAnns) {
cAnns.forEach(cAnn => {
cAnn.value.forEach(ann => {
nl[ann.name] = ann.value.value;
});
});
}
return nl;
}
function processEnumAnnotations(ne) {
if (!(0, asserts_1.isEnum)(ne)) {
return ne;
}
const cAnns = ne.annotations;
if (cAnns) {
cAnns.forEach(cAnn => {
cAnn.value.forEach(ann => {
ne[ann.name] = ann.value.value;
});
});
}
ne.enumItems.forEach(processEnumItemAnnotations);
return ne;
function processEnumItemAnnotations(ei) {
const cAnns = ei.annotations;
if (cAnns) {
cAnns.forEach(cAnn => {
cAnn.value.forEach(ann => {
ei[ann.name] = ann.value;
if (ann.name === 'label') {
ei.label = new nasl.StaticString({ value: ann.value.value }); // 厉害啊!
}
});
});
}
}
}
function processStructAnnotations(ns) {
if (!(0, asserts_1.isStructure)(ns)) {
return ns;
}
const cAnns = ns.annotations;
if (cAnns) {
cAnns.forEach(cAnn => {
cAnn.value.forEach(ann => {
ns[ann.name] = ann.value.value;
});
});
}
ns.properties.forEach(processStructPropertyAnnotations);
return ns;
function processStructPropertyAnnotations(sp) {
const cAnns = sp.annotations;
if (cAnns) {
cAnns.forEach(cAnn => {
cAnn.value.forEach(ann => {
if (ann.name === 'label') {
sp.label = ann.value.value; // 厉害啊!
}
});
});
}
}
}
// @ts-ignore
const forceToString = (e) => e.val;
exports.forceToString = forceToString;
// @ts-ignore
const forceToBoolean = (e) => e.val;
exports.forceToBoolean = forceToBoolean;
// @ts-ignore
const forceToNumber = (e) => e.val;
exports.forceToNumber = forceToNumber;
//# sourceMappingURL=process-annotations.js.map