63 lines
2.1 KiB
C#
63 lines
2.1 KiB
C#
// -----------------------------------------------------------------------------
|
|
// file="TestRegexGenerate"
|
|
// Project: StgSharp
|
|
// 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(
|
|
string arg
|
|
)
|
|
{
|
|
Console.WriteLine($"Text regex to be tested: {arg} :\n\n\n");
|
|
Print(arg);
|
|
return 0;
|
|
}
|
|
|
|
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 local_fail = context.RequestExitLabel(ExitKind.MatchFailed);
|
|
ExitLabel backtrack = local_fail;
|
|
ExitLabel end = context.RequestExitLabel(ExitKind.MatchEnd);
|
|
GenerateMethodSource(analysis.Ast.Root, body, functions, context, local_fail, ref backtrack);
|
|
_ = body.AppendLine($"goto {end.GetLabelName()};")
|
|
.AppendLine($"{local_fail.GetLabelName()}: {{}}")
|
|
.AppendLine("// TODO(NGRA): Produce the root failure result here.")
|
|
.AppendLine($"{end.GetLabelName()}: {{}}");
|
|
|
|
foreach (string line in body.ToList()) {
|
|
Console.WriteLine(line);
|
|
}
|
|
foreach (SequenceEmitter<string> function in functions)
|
|
{
|
|
foreach (string line in function.ToList()) {
|
|
Console.WriteLine(line);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|