@lcap/nasl-parser
Version:
Take Nasl text to Nasl AST with the help of generalized parsing.
120 lines (96 loc) • 2.65 kB
text/typescript
import { Parser, Grammar } from "nearley";
import grammar from "../../ts/nasl";
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(parser.results.length).toBe(1);
});
test('Enum type definition', async () => {
const parser = new Parser(Grammar.fromCompiled(grammar));
parser.feed(enumDef2);
expect(parser.results.length).toBe(1);
});
test('Enum type definition', async () => {
const parser = new Parser(Grammar.fromCompiled(grammar));
parser.feed(enumDef3);
expect(parser.results.length).toBe(1);
});
test('Enum constructor qualified reference', async () => {
const parser = new Parser(Grammar.fromCompiled(grammar));
parser.feed(enumQRef);
expect(parser.results.length).toBe(1);
});
test('Enum constructor custom namespace reference', async () => {
const parser = new Parser(Grammar.fromCompiled(grammar));
parser.feed(enumCustomNamespaceRef);
expect(parser.results.length).toBe(1);
});
test('Enum constructor (unqualified) reference', async () => {
const parser = new Parser(Grammar.fromCompiled(grammar));
parser.feed(enumRef);
expect(parser.results.length).toBe(1);
});
});