Files
stgsharp/test/StgSharpDebug/TestRegexGenerate.cs
T

66 lines
2.1 KiB
C#

// -----------------------------------------------------------------------------
// file="TestRegexGenerate"
// Project: Nitload
// Copyright (c) Nitload.
// SPDX-License-Identifier: MIT
// -----------------------------------------------------------------------------
using Nitload.RegularAnalysis.Abstraction;
using Nitload.RegularAnalysis.Text;
using System;
using System.Collections.Generic;
using System.Runtime.ExceptionServices;
using static Nitload.RegularAnalysis.Text.TextRegexSourceGen;
namespace Nitload
{
internal static class TestRegexGenerate
{
public static int Run(params string[] args)
{
if (args.Length > 1) {
throw new ArgumentException("Expected at most one regex pattern.", nameof(args));
}
if (args.Length == 1)
{
Print(args[0]);
return 0;
}
while (true)
{
Console.Write("regex> ");
string? pattern = Console.ReadLine();
if (pattern is null or ":exit") {
return 0;
}
Print(pattern);
}
}
private static void Print(string pattern)
{
TextRegexSource analysis = RegexAnalyzer.Analyze(pattern);
if (analysis.AnalyseException is not null) {
ExceptionDispatchInfo.Throw(analysis.AnalyseException);
}
SequenceEmitter<string> body = new();
List<SequenceEmitter<string>> functions = [];
SourceGenContext context = new();
ExitLabel exit = context.RequestExitLabel(ExitMode.Goto);
GenerateMethodSource(analysis.Ast.Root, body, functions, context, exit);
foreach (string line in body.ToList()) {
Console.WriteLine(line);
}
foreach (SequenceEmitter<string> function in functions)
{
foreach (string line in function.ToList()) {
Console.WriteLine(line);
}
}
}
}
}