add ir emitter

This commit is contained in:
Youmu Liu
2026-06-16 21:04:51 +08:00
parent 3cfd29f793
commit a7bb3209dd
9 changed files with 711 additions and 148 deletions
@@ -167,6 +167,15 @@ namespace StgSharp.RegularAnalysis.TextRegex
}
/**/
/// <summary>
/// Replace the current node with another node, and update the parent and sibling
/// references accordingly. This is implemented by copying metadata and nearby
/// environment of the given node to the current node, and then update the nearby nodes to
/// point to the current node.
/// </summary>
/// <param name="node">
/// The node to replace the current node with.
/// </param>
internal void ReplaceBy(
RegexAstNode node
)
+64 -79
View File
@@ -38,13 +38,10 @@ namespace StgSharp.RegularAnalysis.TextRegex
Search = 0,
Jump = 1,
Compare = 2,
Check = 3,
Uncheck = 4,
Span = 5,
Define = 6,
Assign = 7,
Back = 8,
Condition = 2,
Count = 3,
Try = 4,
Pop = 5,
}
@@ -53,10 +50,48 @@ namespace StgSharp.RegularAnalysis.TextRegex
internal abstract RegexIRCommand Command { get; }
public static RegexIR Condition(
int success,
int fail
)
{
return new RegexConditionIR(success, fail);
}
public static RegexIR PopLevel(
bool isSuccess
)
{
return new RegexPopIR(isSuccess);
}
public static RegexIR Try(
int count
)
{
return new RegexTryIR(count);
}
internal abstract void Execute();
}
internal sealed class RegexPopIR(
bool isSuccess
) : RegexIR
{
public bool IsSuccess { get; } = isSuccess;
internal override RegexIRCommand Command => RegexIRCommand.Pop;
internal override void Execute()
{
throw new NotImplementedException();
}
}
/// <summary>
/// Searches for a certain string pattern or charset from current position.
/// </summary>
@@ -92,12 +127,12 @@ namespace StgSharp.RegularAnalysis.TextRegex
}
internal sealed class RegexSearchCharIR(
char c
internal sealed class RegexSearchCharSetIR(
string c
) : RegexIR
{
public char target { get; } = c;
public string Target { get; } = c;
internal override RegexIRCommand Command => RegexIRCommand.Search;
@@ -130,23 +165,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
}
internal sealed class RegexBackIR(
int index
) : RegexIR
{
public int Target { get; } = index;
internal override RegexIRCommand Command => RegexIRCommand.Back;
internal override void Execute()
{
throw new NotImplementedException();
}
}
internal sealed class RegexCompareIR(
internal sealed class RegexConditionIR(
int success,
int fail
) : RegexIR
@@ -156,7 +175,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
public int FailCase { get; } = fail;
internal override RegexIRCommand Command => RegexIRCommand.Compare;
internal override RegexIRCommand Command => RegexIRCommand.Condition;
internal override void Execute()
{
@@ -165,38 +184,23 @@ namespace StgSharp.RegularAnalysis.TextRegex
}
internal sealed class RegexCheckIR : RegexIR
{
internal override RegexIRCommand Command => RegexIRCommand.Check;
internal override void Execute()
{
throw new NotImplementedException();
}
}
internal sealed class RegexUncheckIR : RegexIR
{
internal override RegexIRCommand Command => RegexIRCommand.Check;
internal override void Execute()
{
throw new NotImplementedException();
}
}
internal sealed class RegexSpanIR(
string name
internal sealed class RegexCountIR(
int min,
int max,
bool isGreedy,
int operation
) : RegexIR
{
public string Name { get; } = name;
public bool IsGreedy { get; } = isGreedy;
internal override RegexIRCommand Command => RegexIRCommand.Span;
public int Min { get; } = min;
public int Max { get; } = max;
public int OperationIndex { get; } = operation;
internal override RegexIRCommand Command => throw new NotImplementedException();
internal override void Execute()
{
@@ -205,33 +209,14 @@ namespace StgSharp.RegularAnalysis.TextRegex
}
internal sealed class RegexDefineCommand(
string name
internal sealed class RegexTryIR(
int count
) : RegexIR
{
public string Name { get; } = name;
public int CountOfCommand { get; } = count;
internal override RegexIRCommand Command => RegexIRCommand.Define;
internal override void Execute()
{
throw new NotImplementedException();
}
}
internal sealed class RegexAssignCommand<T>(
string name,
T value
) : RegexIR
{
public string Name { get; } = name;
public T Value { get; } = value;
internal override RegexIRCommand Command => RegexIRCommand.Assign;
internal override RegexIRCommand Command => RegexIRCommand.Try;
internal override void Execute()
{
@@ -28,23 +28,72 @@
using StgSharp.RegularAnalysis.Abstraction;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace StgSharp.RegularAnalysis.TextRegex
{
public partial class RegexInterpreter
{
private List<RegexIR> EmptyIR { get; } = [];
internal List<RegexIR> GenerateIR(
AbstractSyntaxTree<RegexAstNode, RegexElementLabel> tree
)
{
using TreeEnumerator<RegexAstNode, RegexElementLabel> enumerator = new(tree);
while (enumerator.MoveNext())
return GenerateIRRecursion(tree.Root, 0);
}
private List<RegexIR> GenerateIRRecursion(
RegexAstNode node,
int depth
)
{
if (depth > 1024) {
throw new OverflowException("Regex parse exceeds maximum recursion depth");
}
int curDepth = depth;
RegexAstNode left = node.Left;
RegexAstNode right = node.Right;
List<RegexIR> leftIR = EmptyIR;
List<RegexIR> rightIR = EmptyIR;
bool isLeftOperator = !RegexAstNode.IsNullOrEmpty(left) &&
(left.Source.Flag & RegexElementLabel.OPERATOR) != 0;
bool isRightOperator = !RegexAstNode.IsNullOrEmpty(right) &&
(right.Source.Flag & RegexElementLabel.OPERATOR) != 0;
Token<RegexElementLabel> token = node.Source;
switch (node.Source.Flag)
{
if (true) { }
case RegexElementLabel.GROUP_BEGIN:
string group_source = token.Value;
bool is_group = group_source.Length == 1;
string group_name = group_source.StartsWith(@"(?<", StringComparison.Ordinal) ?
group_source[3..^2] :
string.Empty;
List<RegexIR> groupIR = [];
rightIR = GenerateIRRecursion(right, curDepth + 1);
groupIR.Add(RegexIR.Try(rightIR.Count));
groupIR.AddRange(rightIR);
groupIR.Add(RegexIR.Condition(2, 1));
groupIR.Add(RegexIR.PopLevel(false));
groupIR.Add(RegexIR.PopLevel(true));
break;
case RegexElementLabel.COUNT:
break;
case RegexElementLabel.CONCAT:
break;
case RegexElementLabel.ALT:
break;
default:
break;
}
return [];
@@ -2,8 +2,8 @@
// -----------------------------------------------------------------------
// file="RegexInterpreter.Optimize"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@@ -42,7 +42,9 @@ namespace StgSharp.RegularAnalysis.TextRegex
public partial class RegexInterpreter
{
private static RegexAstNode FlattenAlt(RegexAstNode root)
private static RegexAstNode FlattenAlt(
RegexAstNode root
)
{
TreeEnumerator<RegexAstNode, RegexElementLabel> enumerator = new(root);
List<RegexAstNode> cases = [];
@@ -67,7 +69,9 @@ namespace StgSharp.RegularAnalysis.TextRegex
return cases[0];
}
private static bool MergeConcat(RegexAstNode root)
private static bool MergeConcat(
RegexAstNode root
)
{
if (root.EqualityTypeConvert != RegexElementLabel.CONCAT) {
return false;
@@ -109,7 +113,8 @@ namespace StgSharp.RegularAnalysis.TextRegex
string str = left.Value;
buffer.Add(str);
length += str.Length;
while (!RegexAstNode.IsNullOrEmpty(right) && right.EqualityTypeConvert == RegexElementLabel.CONCAT)
while (!RegexAstNode.IsNullOrEmpty(right) &&
right.EqualityTypeConvert == RegexElementLabel.CONCAT)
{
left = right.Left;
if (left.EqualityTypeConvert is not RegexElementLabel.UNIT_SPAN and
@@ -132,7 +137,10 @@ namespace StgSharp.RegularAnalysis.TextRegex
buffer.Add(right.Value);
length += right.Value.Length;
}
str = string.Create(length, buffer, static(span, buffer) => {
str = string.Create(length, buffer, static(
span,
buffer
) => {
int offset = 0;
for (int i = 0; i < buffer.Count; i++)
{
@@ -140,17 +148,21 @@ namespace StgSharp.RegularAnalysis.TextRegex
offset += buffer[i].Length;
}
});
root.Left = new RegexAstNode(new Token<RegexElementLabel>(str, left.Source.Line, left.Source.Column,
root.Left = new RegexAstNode(new Token<RegexElementLabel>(str, left.Source.Line,
left.Source.Column,
str.Length == 1 ?
RegexElementLabel.UNIT :
RegexElementLabel.UNIT_SPAN));
return true;
}
private static void RotateConcat(RegexAstNode root)
private static void RotateConcat(
RegexAstNode root
)
{
RegexAstNode left = root.Left;
while (!RegexAstNode.IsNullOrEmpty(left) && left.EqualityTypeConvert == RegexElementLabel.CONCAT)
while (!RegexAstNode.IsNullOrEmpty(left) &&
left.EqualityTypeConvert == RegexElementLabel.CONCAT)
{
/*
* root → root
@@ -193,7 +205,9 @@ namespace StgSharp.RegularAnalysis.TextRegex
///
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static RegexAstNode MergeAlt(List<RegexAstNode> cases)
private static RegexAstNode MergeAlt(
List<RegexAstNode> cases
)
{
UnionAlt union = new(0, cases);
RegexAstNode root = union.AsNormal();
@@ -205,7 +219,10 @@ namespace StgSharp.RegularAnalysis.TextRegex
private const int MaxLevel = 8;
private UnionAlt(int level, RegexAstNode node)
private UnionAlt(
int level,
RegexAstNode node
)
: base(node.Source)
{
Level = level;
@@ -213,7 +230,8 @@ namespace StgSharp.RegularAnalysis.TextRegex
RegexAstNode right = IsNullOrEmpty(node.Right) ?
new RegexAstNode(
new Token<RegexElementLabel>(node.Value, 0, node.Source.Column, RegexElementLabel.UNIT)
new Token<RegexElementLabel>(node.Value, 0, node.Source.Column,
RegexElementLabel.UNIT)
) :
node.Right;
Cases.Add(right);
@@ -224,7 +242,10 @@ namespace StgSharp.RegularAnalysis.TextRegex
UnionEnd = node;
}
public UnionAlt(int level, List<RegexAstNode> c)
public UnionAlt(
int level,
List<RegexAstNode> c
)
: base(c[0].Source)
{
Cases = c;
@@ -251,7 +272,8 @@ namespace StgSharp.RegularAnalysis.TextRegex
{
if ((Cases[i].EqualityTypeConvert & RegexElementLabel.SEQUENCE) != 0)
{
RegexAstNode concat = new(new Token<RegexElementLabel>(string.Empty, 0, Cases[i].Source.Column,
RegexAstNode concat = new(new Token<RegexElementLabel>(string.Empty, 0,
Cases[i].Source.Column,
RegexElementLabel.CONCAT))
{
Left = Cases[i]
@@ -323,13 +345,15 @@ namespace StgSharp.RegularAnalysis.TextRegex
RegexAstNode branch;
if (IsNullOrEmpty(UnionEnd))
{
branch = new RegexAstNode(new Token<RegexElementLabel>(string.Empty, 0, Source.Column,
branch = new RegexAstNode(new Token<RegexElementLabel>(string.Empty, 0,
Source.Column,
RegexElementLabel.ALT));
UnionBegin = branch;
UnionEnd = branch;
} else
{
branch = new RegexAstNode(new Token<RegexElementLabel>(string.Empty, 0, UnionEnd.Source.Column,
branch = new RegexAstNode(new Token<RegexElementLabel>(string.Empty, 0,
UnionEnd.Source.Column,
RegexElementLabel.ALT));
UnionEnd.Right = branch;
branch.Parent = UnionEnd;
@@ -346,17 +370,23 @@ namespace StgSharp.RegularAnalysis.TextRegex
return UnionBegin;
}
private void AddCase(RegexAstNode _case)
private void AddCase(
RegexAstNode _case
)
{
RegexAstNode right = IsNullOrEmpty(_case.Right) ?
new RegexAstNode(
new Token<RegexElementLabel>(_case.Value, 0, _case.Source.Column, RegexElementLabel.UNIT)
new Token<RegexElementLabel>(_case.Value, 0, _case.Source.Column,
RegexElementLabel.UNIT)
) :
_case.Right;
Cases.Add(right);
}
private static bool CanMerge(RegexAstNode origin, RegexAstNode target)
private static bool CanMerge(
RegexAstNode origin,
RegexAstNode target
)
{
// suppose that origin is not a union,
if (target is UnionAlt alt)
@@ -2,8 +2,8 @@
// -----------------------------------------------------------------------
// file="RegexInterpreter"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@@ -58,7 +58,9 @@ namespace StgSharp.RegularAnalysis.TextRegex
}
}
public static RegexInterpreter Analyze(string regex)
public static RegexInterpreter Analyze(
string regex
)
{
RegexInterpreter interpreter = new();
interpreter._source = regex;
@@ -160,8 +162,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
throw new InvalidOperationException("Invalid regular expression syntax.");
}
private void OptimizeTree(
)
private void OptimizeTree()
{
// phase0: scan container and remove no parent nodes
_ = _tree.AllNodes.RemoveWhere(node => node.Parent == null && node != _tree.Root);
@@ -169,7 +170,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
RegexAstNode current;
// phase1: ALT flatten
// phase3: CONCAT normalize
// phase2: CONCAT normalize
int i = 0;
List<RegexAstNode> altNodes = [];
while (enumerator.MoveNext())
@@ -177,30 +178,10 @@ namespace StgSharp.RegularAnalysis.TextRegex
current = enumerator.Current;
if (current.EqualityTypeConvert == RegexElementLabel.ALT)
{
if (current.Left.EqualityTypeConvert == RegexElementLabel.UNIT &&
current.Right.EqualityTypeConvert == RegexElementLabel.UNIT)
{
if (string.IsNullOrEmpty(current.Left.Value))
{
// TODO replace the alt node with a count node
/*
RegexAstNode parent = current.Parent;
parent.Next = new RegexAstNode();
/**/
} else if (string.IsNullOrEmpty(current.Right.Value))
{
// TODO replace the alt node with a count node
} else
{
// TODO replace the alt node with a charset node
}
} else
{
RegexAstNode union = FlattenAlt(current);
altNodes.Add(current);
current.Right = union;
current.Left = RegexAstNode.Empty;
}
RegexAstNode union = FlattenAlt(current);
altNodes.Add(current);
current.Right = union;
current.Left = RegexAstNode.Empty;
} else if (current.EqualityTypeConvert == RegexElementLabel.CONCAT) {
RotateConcat(current);
}
@@ -208,7 +189,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
List<RegexAstNode> cases = [];
// phase2: then ALT union
// phase3: then ALT union
foreach (RegexAstNode item in altNodes)
{
RegexAstNode _case = item.Right;
@@ -237,13 +218,18 @@ namespace StgSharp.RegularAnalysis.TextRegex
}
}
}
// TODO enum AST again , do ALT tail merge and simple ALT front merge
}
private void ProcessOperator(RegexAstNode op)
private void ProcessOperator(
RegexAstNode op
)
{
if (op.EqualityTypeConvert == RegexElementLabel.CONCAT)
{
if (_stack.TryPopOperand(out RegexAstNode? _1) && _stack.TryPopOperand(out RegexAstNode? _2))
if (_stack.TryPopOperand(out RegexAstNode? _1) &&
_stack.TryPopOperand(out RegexAstNode? _2))
{
op.Left = _2;
op.Right = _1;
@@ -269,7 +255,8 @@ namespace StgSharp.RegularAnalysis.TextRegex
}
} else if (op.EqualityTypeConvert == RegexElementLabel.ALT)
{
if (_stack.TryPopOperand(out RegexAstNode? _1) && _stack.TryPopOperand(out RegexAstNode? _2))
if (_stack.TryPopOperand(out RegexAstNode? _1) &&
_stack.TryPopOperand(out RegexAstNode? _2))
{
op.Left = _2;
op.Right = _1;
@@ -148,7 +148,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
}
throw new InvalidOperationException("Unclosed count specifier");
case '*' or '+':
int count = _source[pos + 1] == '?' ? 2 : 1;
int count = pos + 1 >= _source.Length ? 1 : (_source[pos + 1] == '?' ? 2 : 1);
position = pos + count;
return new Token<RegexElementLabel>(_source[(pos)..(pos + count)], 0, pos,
RegexElementLabel.COUNT);
@@ -157,6 +157,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
return new Token<RegexElementLabel>(_source[(pos)..(pos + 1)], 0, pos,
RegexElementLabel.COUNT);
case '.':
position = pos + 1;
return new Token<RegexElementLabel>(_source[(pos)..(pos + 1)], 0, pos,
RegexElementLabel.UNIT);
default:
@@ -287,7 +288,7 @@ namespace StgSharp.RegularAnalysis.TextRegex
}
// Consume "(?<name>"
int consumedLen = (i - start) + 1; // include '>'
int consumedLen = i - start + 1; // include '>'
string text = _source.Substring(start, consumedLen);
position = start + consumedLen;