UNPKG

@lcap/nasl-parser

Version:

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

63 lines (52 loc) 1.68 kB
import { Parser, Grammar } from "nearley"; import grammar from "../../ts/nasl"; const namedArgsStr =` logic LCAPPermission() {} logic LCAPGetPermissionResult() {} logic LCAPLoadPermissionManagementTableView(filter: LCAPPermission, page: Integer, size: Integer) => result { result = LCAPGetPermissionResult( permission=item.lCAPPermission, roleList=ListTransform(MapGet(perAndRoleMap, item.lCAPPermission.name), { item1 => item1.lCAPRole }) ); } ` const callWithLambdaStr1 = ` logic fn1() { fn1(x = { e4 => e4 }) fn1({e5 => e5}) } ` const callWithLambdaStr2 = ` logic fn1() { fn1(x = 3, { e1 => e1 }) fn1(x = 3) { e2 => e2() } fn1() { e3 => e3() } } ` const callWithLambdaStr3 = ` logic fn1() { fn1(x = 3,y,z = 2) // 有问题的 } ` describe('Nasl call syntax', () => { test('named arguments', async () => { const parser = new Parser(Grammar.fromCompiled(grammar)); parser.feed(namedArgsStr); expect(parser.results.length).toBe(1); }); test('call with lambda1', async () => { const parser = new Parser(Grammar.fromCompiled(grammar)); parser.feed(callWithLambdaStr1); expect(parser.results.length).toBe(1); }); test('call with lambda2', async () => { const parser = new Parser(Grammar.fromCompiled(grammar)); parser.feed(callWithLambdaStr2); expect(parser.results.length).toBe(1); }); test('call with lambda3', async () => { const parser = new Parser(Grammar.fromCompiled(grammar)); parser.feed(callWithLambdaStr3); expect(parser.results.length).toBe(1); }); });