regex interpreter now can build partial correct AST (i forget groups), but in bad shape. building an optimizer.
This commit is contained in:
@@ -29,6 +29,7 @@ using StgSharp.Collections;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Xml.Schema;
|
||||
|
||||
@@ -38,6 +39,8 @@ namespace StgSharp.RegularAnalysis.Abstraction
|
||||
where TType : unmanaged
|
||||
{
|
||||
|
||||
private HashSet<TNode> _allToken = new();
|
||||
|
||||
private TNode _root;
|
||||
|
||||
public int Count
|
||||
@@ -45,17 +48,31 @@ namespace StgSharp.RegularAnalysis.Abstraction
|
||||
get { return _allToken.Count; }
|
||||
}
|
||||
|
||||
public TNode Root => _root;
|
||||
public TNode Root
|
||||
{
|
||||
get;
|
||||
internal set
|
||||
{
|
||||
if (_allToken.Contains(value)) {
|
||||
field = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool AddNode(
|
||||
TNode node
|
||||
)
|
||||
{
|
||||
return _allToken.Add(node);
|
||||
}
|
||||
|
||||
public bool Contains(
|
||||
TNode node
|
||||
)
|
||||
{
|
||||
return _allToken<TLabel>.Contains(node);
|
||||
return _allToken.Contains(node);
|
||||
}
|
||||
|
||||
private List<TNode> _allToken<TLabel> =
|
||||
new();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ namespace StgSharp.RegularAnalysis.Abstraction
|
||||
|
||||
public CompileStack()
|
||||
{
|
||||
_depthMarks.Push(new DepthMark(0, 0));
|
||||
_depthMarks.Push(new DepthMark(0, 0, 0));
|
||||
}
|
||||
|
||||
public int OperandInDepthCount
|
||||
@@ -59,7 +59,7 @@ namespace StgSharp.RegularAnalysis.Abstraction
|
||||
public int OperatorInDepthCount
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => _operands.Count - _depthMarks.Peek().OperandPos;
|
||||
get => _operators.Count - _depthMarks.Peek().OperandPos;
|
||||
}
|
||||
|
||||
public int Depth
|
||||
@@ -108,6 +108,20 @@ namespace StgSharp.RegularAnalysis.Abstraction
|
||||
return false;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool TryPeekOperator(
|
||||
out TNode op
|
||||
)
|
||||
{
|
||||
if (OperatorInDepthCount > 0)
|
||||
{
|
||||
op = _operators.Peek();
|
||||
return true;
|
||||
}
|
||||
op = TNode.Empty;
|
||||
return false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region operand
|
||||
@@ -117,7 +131,7 @@ namespace StgSharp.RegularAnalysis.Abstraction
|
||||
)
|
||||
{
|
||||
if (OperatorInDepthCount > 0) {
|
||||
return _operators.Pop();
|
||||
return _operands.Pop();
|
||||
}
|
||||
return TNode.Empty;
|
||||
}
|
||||
@@ -127,7 +141,7 @@ namespace StgSharp.RegularAnalysis.Abstraction
|
||||
TNode operatorNode
|
||||
)
|
||||
{
|
||||
_operators.Push(operatorNode);
|
||||
_operands.Push(operatorNode);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
@@ -144,6 +158,20 @@ namespace StgSharp.RegularAnalysis.Abstraction
|
||||
return false;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool TryPeekOperand(
|
||||
out TNode op
|
||||
)
|
||||
{
|
||||
if (OperandInDepthCount > 0)
|
||||
{
|
||||
op = _operands.Peek();
|
||||
return true;
|
||||
}
|
||||
op = TNode.Empty;
|
||||
return false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region depth
|
||||
|
||||
@@ -45,6 +45,14 @@ namespace StgSharp.RegularAnalysis.Abstraction
|
||||
public readonly string Value;
|
||||
public readonly TLabel Flag;
|
||||
|
||||
public Token()
|
||||
{
|
||||
Column = 0;
|
||||
Line = 0;
|
||||
Value = string.Empty;
|
||||
Flag = default;
|
||||
}
|
||||
|
||||
public Token(
|
||||
string chars,
|
||||
int lineNumber,
|
||||
|
||||
Reference in New Issue
Block a user