UNPKG

@lcap/nasl-parser

Version:

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

119 lines (97 loc) 2.85 kB
import { Parser, Grammar } from "nearley"; import grammar from "../../ts/nasl"; import { toNaslAST } from "../../ts/toAST/to-nasl-ast"; const enumDef1 =` enum ProductStatus { @(label = '上架') case Listing; @(label = '下架') case DeListing; } ` const enumDef2 = ` @( label = "商品状态", description = "表示商品状态的枚举", ) enum ProductStatus { @(label = '上架') case Listing; @(label = '下架') case DeListing; }` const enumDef3 = ` enum COLOR { @(label="红") case 0; @(label="YELLOW") case 1; case 2; } ` const enumQRef =` logic f() { let x : app::enums::ProductStatus = app::enums::ProductStatus::Listing; let y = app::enums::ProductStatus::DeListing; } ` const enumCustomNamespaceRef = ` namespace MyNamespace { enum ProductStatus { @(label = '上架') case Listing; @(label = '下架') case DeListing; } } logic f() { let x : MyNamespace::ProductStatus = MyNamespace::ProductStatus::Listing; let y = MyNamespace::ProductStatus::DeListing; }` const enumRef =` namespace MyNamespace { enum ProductStatus { @(label = '上架') case Listing; @(label = '下架') case DeListing; } } using MyNamespace; logic f() { let x : ProductStatus = ProductStatus::Listing; let y = ProductStatus::DeListing; } ` describe('Enum syntax...', () => { test('Enum type definition', async () => { const parser = new Parser(Grammar.fromCompiled(grammar)); parser.feed(enumDef1); expect(() => toNaslAST(parser.results[0].nasl)).not.toThrow(); }); test('Enum type definition', async () => { const parser = new Parser(Grammar.fromCompiled(grammar)); parser.feed(enumDef2); expect(() => toNaslAST(parser.results[0].nasl)).not.toThrow(); }); test('Enum type definition', async () => { const parser = new Parser(Grammar.fromCompiled(grammar)); parser.feed(enumDef3); expect(() => toNaslAST(parser.results[0].nasl)).not.toThrow(); }); test('Enum constructor qualified reference', async () => { const parser = new Parser(Grammar.fromCompiled(grammar)); parser.feed(enumQRef); expect(() => toNaslAST(parser.results[0].nasl)).not.toThrow(); }); test('Enum constructor custom namespace reference', async () => { const parser = new Parser(Grammar.fromCompiled(grammar)); parser.feed(enumCustomNamespaceRef); expect(() => toNaslAST(parser.results[0].nasl)).not.toThrow(); }); test('Enum constructor (unqualified) reference', async () => { const parser = new Parser(Grammar.fromCompiled(grammar)); parser.feed(enumRef); expect(() => toNaslAST(parser.results[0].nasl)).not.toThrow(); }); });