add generator if ''UNIT''s in regex

This commit is contained in:
Youmu Liu
2026-08-19 18:58:32 +08:00
parent eb60422fa3
commit 843eb6b237
5 changed files with 74 additions and 49 deletions
@@ -24,7 +24,6 @@ namespace StgSharp.RegularAnalysis.Text
internal TextRegexSource(
string pattern,
AbstractSyntaxTree<RegexAstNode, RegexElementLabel> ast,
RegexInfo info,
Exception? analyseException = null
)
@@ -32,7 +31,6 @@ namespace StgSharp.RegularAnalysis.Text
Pattern = pattern;
Ast = ast;
AnalyseException = analyseException;
Info = info;
}
public Exception? AnalyseException { get; set; }
@@ -41,8 +39,6 @@ namespace StgSharp.RegularAnalysis.Text
internal AbstractSyntaxTree<RegexAstNode, RegexElementLabel> Ast { get; }
internal RegexInfo Info { get; }
}
public static partial class RegexAnalyzer
@@ -56,16 +52,15 @@ namespace StgSharp.RegularAnalysis.Text
// RegexAnalyzer interpreter = new();
// interpreter._source = regex;
AbstractSyntaxTree<RegexAstNode, RegexElementLabel> tree;
RegexInfo info = new();
try
{
tree = GenerateAST(regex);
OptimizeTree(tree);
return new TextRegexSource(regex, tree, info, null);
return new TextRegexSource(regex, tree, null);
}
catch (Exception ex)
{
return new TextRegexSource(regex, null, info, ex);
return new TextRegexSource(regex, null, ex);
}
/*
// List<RegexIR> base_ir;
@@ -94,15 +89,4 @@ namespace StgSharp.RegularAnalysis.Text
}
}
internal struct RegexInfo
{
public int RegionCount { get; set; }
public int MinPredictLength { get; set; }
public int SingleLineResultCount { get; set; }
}
}
@@ -218,7 +218,7 @@ namespace StgSharp.RegularAnalysis.Text
if (last < first) {
throw new InvalidCastException($"Invalid character range: {first}-{last}");
}
rules.Add(new(content[first..last].ToString(), RegexCharSetType.Range, true));
rules.Add(new($"{first}{last}", RegexCharSetType.Range, true));
} else
{
_ = singles.Add(first);
@@ -29,17 +29,36 @@ namespace StgSharp.RegularAnalysis.Text
)
{
RegexAstNode root = ast;
switch (root.Label & (RegexElementLabel.VAST_OPERATOR))
RegexElementLabel op = root.Label & (RegexElementLabel.VAST_OPERATOR);
if (op != 0)
{
case RegexElementLabel.CONCAT:
GenerateConcat(root, sc, func_list);
break;
case RegexElementLabel.COUNT:
break;
case RegexElementLabel.GROUP_BEGIN:
break;
case RegexElementLabel.ALT:
break;
switch (op)
{
case RegexElementLabel.CONCAT:
GenerateConcat(root, sc, func_list);
break;
case RegexElementLabel.COUNT:
GenerateCount(root, sc, func_list);
break;
case RegexElementLabel.GROUP_BEGIN:
GenerateGroup(root, sc, func_list);
break;
case RegexElementLabel.ALT:
GenerateAlt(root, sc, func_list);
break;
}
} else
{
op = root.Label & (RegexElementLabel.SEQUENCE);
switch (op)
{
case RegexElementLabel.UNIT:
break;
case RegexElementLabel.UNIT_SET:
break;
case RegexElementLabel.UNIT_SPAN:
break;
}
}
}
@@ -80,7 +99,46 @@ namespace StgSharp.RegularAnalysis.Text
in List<SequenceEmitter<string>> func_list
)
{
RegexAstNode left = node.Left;
RegexAstNode right = node.Right;
RegexAstNode current = left;
while (RegexAstNode.IsNullOrEmpty(current))
{
GenerateMethodSource(current, sc, func_list);
current = current.Next;
}
current = right;
while (RegexAstNode.IsNullOrEmpty(current))
{
GenerateMethodSource(current, sc, func_list);
current = current.Next;
}
}
private static void GenerateGroup(
RegexAstNode node,
[NotNull] in SequenceEmitter<string> sc,
in List<SequenceEmitter<string>> func_list
)
{
RegexAstNode left = node.Left;
RegexAstNode right = node.Right;
RegexGroupPayload config = node.PayloadAs<RegexGroupPayload>();
if (left is not null)
{
GenerateMethodSource(left, sc, func_list);
} else
if (right is not null) {
GenerateMethodSource(right, sc, func_list);
}
string group_name = config.Name;
throw new NotImplementedException();
}
private static void GenerateConcat(
@@ -146,7 +146,7 @@ namespace StgSharp.RegularAnalysis.Text
result.AddDiagnostic(CompileFailDiag, null, analyze_result.AnalyseException.Message);
} else
{
SequenceEmitter<string> method_source = GenerateSource(analyze_result);
SequenceEmitter<string> method_source = /**/ new()/*GenerateSource(analyze_result)/**/;
result.GeneratedSource = method_source;
}
+2 -19
View File
@@ -35,7 +35,7 @@ namespace StgSharp
{
foreach (string regex in regexSource)
{
TextRegexSource interpreterSource = RegexAnalyzer.Analyze(regex, true, true);
TextRegexSource interpreterSource = RegexAnalyzer.Analyze(regex);
AbstractSyntaxTree<RegexAstNode, RegexElementLabel> tree = interpreterSource.Ast;
Console.WriteLine($"result of \"{regex}\"");
TreeEnumerator<RegexAstNode, RegexElementLabel> enumerator = new(tree);
@@ -43,25 +43,8 @@ namespace StgSharp
{
RegexAstNode node = enumerator.Current;
Console.Write(new string('|', enumerator.Depth));
Console.WriteLine($"\t{node.Source.Value}\t{node.Source.Flag}");
Console.WriteLine($"\t{node.Value}\t{node.Source.Flag}");
}
/*
Console.WriteLine("-----IR-----");
List<RegexIR> ir = interpreterSource.BaseIR;
for (int i = 0; i < ir.Count; i++) {
Console.WriteLine(ir[i].AsCommand(i));
}
/**/
/**/
Console.WriteLine("-----Optimized once-----");
List<RegexIR> opt_ir = interpreterSource.IRs;
if (opt_ir is not null)
{
for (int i = 0; i < opt_ir.Count; i++) {
Console.WriteLine(opt_ir[i].AsCommand(i));
}
}
/**/
Console.WriteLine(interpreterSource.AnalyseException?.ToString() ?? "No error");
RegexInfo info = interpreterSource.Info;
Console.WriteLine("-----Regex Info-----");