UNPKG

@lcap/nasl-parser

Version:

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

18 lines (14 loc) 526 B
import { Parser, Grammar } from "nearley"; import grammar from "./nasl"; import { toNaslAST } from "./toAST/to-nasl-ast"; export function parse(code: string): any { const parser = new Parser(Grammar.fromCompiled(grammar)); parser.feed(code); if (parser.results?.length === 0) { throw new Error('Failed to parse'); } if (parser.results?.length > 1) { throw new Error('Grammar may be ambiguous. Choose the first result.'); } return toNaslAST(parser.results?.[0]?.nasl).toJSON(); }