UNPKG

@lcap/nasl-parser

Version:

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

65 lines (52 loc) 2.01 kB
import { projectIdentifier, NASL_CORE_AST, isBuiltinPrimitiveType, PackNaslAndCst, TYPE, getCst, getNasl, NAMESPACE_SEP, NAMESPACE_SEP_AST, NASL_COLLECTION_AST, createReferenceType } from './common-util'; import * as nasl from '@lcap/nasl'; export const unionTypeToNaslTypeAnno = (ty1, ty2) => { const naslTy1 : nasl.TypeAnnotation = getNasl(ty1); const unionTys : Array<nasl.TypeAnnotation> = []; 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(getNasl(ty2)); unionCstTys.push(ty2.cst); const nta = new nasl.TypeAnnotation({ typeKind: 'union', typeArguments: unionTys }); return PackNaslAndCst(nta, TYPE('UnionType', { tys: unionCstTys })); } export const fnTypeToNaslTypeAnno = (parTys, retTy) => { const nft = new nasl.TypeAnnotation({ typeKind: 'function', returnType: [retTy.nasl], typeArguments: parTys.nasl }) const cst = TYPE('FnType', { parTys: parTys.nasl, retTy: getCst(retTy) }) return PackNaslAndCst(nft, cst); } // tyCon : qIdentifier export const namedTypeToNaslTypeAnno = (tyCon, tyArgs?) => { const cst = TYPE('NamedType', { tyCon, tyArgs: tyArgs?.cst}); const tyConStr = projectIdentifier(tyCon); if (isBuiltinPrimitiveType(tyConStr)) { const npt = createNaslPrimitiveType(tyConStr === 'Integer' ? 'Long' : tyConStr); return PackNaslAndCst(npt, cst); } const nft = new nasl.TypeAnnotation({ typeKind: 'reference', typeNamespace: '', typeName: tyCon.name, typeArguments: tyArgs?.nasl, }); nft.qName = tyCon; return PackNaslAndCst(nft, cst); } export const createNaslPrimitiveType = (typeName : string) => new nasl.TypeAnnotation({ typeName, typeKind: 'primitive', typeNamespace: NASL_CORE_AST, });