Daily update on 20260209-005019 by Nitload Youmu. Additional Message : Start NGRA, AST build success, working on optimizing.

Signed-off-by: Nitload <YoumuLiu_NSI@whv0t.onmicrosoft.com>
This commit is contained in:
Nitload
2026-02-09 00:50:19 +08:00
parent ed7972f1da
commit 909bf50b9d
6 changed files with 168 additions and 119 deletions
@@ -1,9 +1,9 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="RegexInterpreter"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. 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
@@ -29,6 +29,7 @@ using StgSharp.RegularAnalysis.Abstraction;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
@@ -39,19 +40,29 @@ namespace StgSharp.RegularAnalysis.TextRegex
{
private AbstractSyntaxTree<RegexAstNode, RegexElementLabel> _tree = new();
private CompileStack<RegexAstNode, RegexElementLabel> _stack = new();
private Exception _interpreteException;
private string _source;
public static AbstractSyntaxTree<RegexAstNode, RegexElementLabel> Interpret(
string regex
)
private RegexInterpreter() { }
public static RegexInterpreter Analyze(string regex)
{
RegexInterpreter interpreter = new RegexInterpreter();
interpreter._source = regex;
interpreter.Analyze();
interpreter.OptimizeTree();
return interpreter._tree;
bool isSuccess;
try
{
interpreter.Analyze();
interpreter.OptimizeTree();
isSuccess = true;
}
catch (Exception ex)
{
interpreter._interpreteException = ex;
isSuccess = false;
}
return interpreter;
}
private void Analyze()
@@ -144,34 +155,33 @@ namespace StgSharp.RegularAnalysis.TextRegex
private void OptimizeTree(
)
{
// phase0P: ALT flatten
int opCount = 0;
do
{
opCount = 1;
} while (opCount != 0);
// phase1: ALT merge
opCount = 0;
do
{
opCount = 1;
} while (opCount != 0);
// phase2: CONCAT merge
// phase3: scan container and remove no parent nodes
// phase0: scan container and remove no parent nodes
_tree.AllNodes.RemoveWhere(node => node.Parent == null && node != _tree.Root);
// phase1: ALT flatten
int opCount = 0, cycle = 0;
do
{
cycle++;
} while (opCount != 0 && cycle < 16);
// phase2: ALT merge
opCount = 0;
cycle = 0;
do
{
opCount = 1;
} while (opCount != 0);
// phase3: CONCAT merge
}
private void ProcessOperator(
RegexAstNode op
)
private void ProcessOperator(RegexAstNode op)
{
Console.WriteLine($"Processing a {op.Source.Flag} operator");
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;
@@ -197,8 +207,7 @@ 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 = _1;
op.Right = _2;