UNPKG

@lcap/nasl-parser

Version:

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

39 lines (32 loc) 881 B
import { Parser, Grammar } from "nearley"; import grammar from "../../ts/nasl"; const testMatchExpr =` logic testMatchExpr(x : Integer) { let x : Integer = match (x) { A<X> | B<Y> => x() CC<ZZ> => 1 + 2 _ => true && false } } ` const testMatchStmt = ` logic testMatchStmt(x : Integer) { match (x) { A<X> | B<Y> => { x() } CC<ZZ> => { x = 2 } _ => { x = f(flag = true) } }; } ` describe('Test Match', () => { test('Match Expression', async () => { const parser = new Parser(Grammar.fromCompiled(grammar)); parser.feed(testMatchExpr); expect(parser.results.length).toBe(1); }); test('Match Statement', async () => { const parser = new Parser(Grammar.fromCompiled(grammar)); parser.feed(testMatchStmt); expect(parser.results.length).toBe(1); }); });