feat: 将LR分析表生成到代码中 (#27)

Reviewed-on: PostGuard/Canon#27
This commit is contained in:
2024-04-08 19:46:24 +08:00
parent 5e3ea6303e
commit 1690187c0a
14 changed files with 1125 additions and 22 deletions

View File

@@ -0,0 +1,40 @@
using System.CommandLine;
using Canon.Generator.GrammarGenerator;
namespace Canon.Generator.Extensions;
public static class RootCommandExtension
{
public static void AddGenerateCommand(this RootCommand rootCommand)
{
Command generateCommand = new("generate", "Generate source files.");
Argument<string> filenameArgument = new(name: "filename",
description: "determines the generated file name.",
getDefaultValue: () => "Canon.g.cs");
generateCommand.AddArgument(filenameArgument);
Option<string> namespaceOption = new(name: "--namespace",
description: "determines the namespace of generated code.",
getDefaultValue: () => "Canon.Generator.GrammarGenerator");
generateCommand.AddOption(namespaceOption);
generateCommand.SetHandler(async (context) =>
{
string filename = context.ParseResult.GetValueForArgument(filenameArgument);
FileInfo generatedFile = new(Path.Combine(Environment.CurrentDirectory, filename));
if (generatedFile.Exists)
{
generatedFile.Delete();
}
await using FileStream stream = generatedFile.OpenWrite();
GenerateCommand command = new();
string namespaceValue = context.ParseResult.GetValueForOption(namespaceOption) ?? "Canon.Generator.GrammarGenerator";
await command.GenerateCode(stream, namespaceValue);
});
rootCommand.AddCommand(generateCommand);
}
}