switch root namespace name to NITLOAD
This commit is contained in:
@@ -0,0 +1,238 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
// file="AbstractSyntaxTree"
|
||||
// Project: StgSharp
|
||||
// Copyright (c) Nitload.
|
||||
// SPDX-License-Identifier: MIT
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
using Nitload.Common.Collections;
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
using System.Xml.Linq;
|
||||
using System.Xml.Schema;
|
||||
|
||||
namespace Nitload.RegularAnalysis.Abstraction
|
||||
{
|
||||
public sealed class AbstractSyntaxTree<TNode, TType> where TNode : ISyntaxNode<TNode, TType>
|
||||
where TType : unmanaged
|
||||
{
|
||||
|
||||
public HashSet<TNode> AllNodes => AllToken;
|
||||
|
||||
public int Count => AllToken.Count;
|
||||
|
||||
public TNode Root
|
||||
{
|
||||
get;
|
||||
internal set
|
||||
{
|
||||
if (AllToken.Contains(value)) {
|
||||
field = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private HashSet<TNode> AllToken { get; } = new();
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool AddNode(
|
||||
TNode node
|
||||
)
|
||||
{
|
||||
return AllToken.Add(node);
|
||||
}
|
||||
|
||||
public bool Contains(
|
||||
TNode node
|
||||
)
|
||||
{
|
||||
return AllToken.Contains(node);
|
||||
}
|
||||
|
||||
public IEnumerator<TNode> GetEnumerator()
|
||||
{
|
||||
return new TreeEnumerator<TNode, TType>(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
internal class TreeEnumerator<TNode, TType>(
|
||||
TNode root
|
||||
) : IEnumerator<TNode> where TNode : ISyntaxNode<TNode, TType> where TType : unmanaged
|
||||
{
|
||||
|
||||
private bool _completed;
|
||||
|
||||
private int _state, _target; // 0: not started, 1: on right, 2: ended
|
||||
private readonly Stack<Record> _recycle = [];
|
||||
private readonly Stack<Record> _stack = [];
|
||||
|
||||
// private TNode _lRoot;
|
||||
private TNode _root = root;
|
||||
|
||||
internal TreeEnumerator(
|
||||
AbstractSyntaxTree<TNode, TType> tree
|
||||
)
|
||||
: this(tree.Root) { }
|
||||
|
||||
public int Depth => _stack.Count;
|
||||
|
||||
public TNode Current
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_stack.TryPeek(out Record? r)) {
|
||||
return r.Node;
|
||||
}
|
||||
return TNode.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
if (_completed) {
|
||||
return false;
|
||||
}
|
||||
while (_stack.TryPeek(out Record? cur))
|
||||
{
|
||||
switch (cur.State)
|
||||
{
|
||||
case 0:
|
||||
cur.State++;
|
||||
if (TNode.IsNullOrEmpty(cur.Node.Left))
|
||||
{
|
||||
goto case 1;
|
||||
} else
|
||||
{
|
||||
_stack.Push(GetNewRecord(cur.Node.Left));
|
||||
return true;
|
||||
}
|
||||
case 1:
|
||||
cur.State++;
|
||||
if (TNode.IsNullOrEmpty(cur.Node.Right))
|
||||
{
|
||||
goto case 2;
|
||||
} else
|
||||
{
|
||||
_stack.Push(GetNewRecord(cur.Node.Right));
|
||||
return true;
|
||||
}
|
||||
case 2:
|
||||
_recycle.Push(_stack.Pop());
|
||||
if (TNode.IsNullOrEmpty(cur.Node.Next))
|
||||
{
|
||||
if (_stack.Count == 0)
|
||||
{
|
||||
_completed = true;
|
||||
return false;
|
||||
}
|
||||
} else
|
||||
{
|
||||
_stack.Push(GetNewRecord(cur.Node.Next));
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
_stack.Push(GetNewRecord(_root));
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
_completed = false;
|
||||
_stack.Clear();
|
||||
_state = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Skips the node and its subtree if the current node is the given node. Otherwise, do
|
||||
/// nothing.
|
||||
/// </summary>
|
||||
/// <param name="node">
|
||||
/// The node to skip.
|
||||
/// </param>
|
||||
public void SkipNode(
|
||||
TNode node
|
||||
)
|
||||
{
|
||||
if (_stack.TryPeek(out Record? r) && ReferenceEquals(r.Node, node))
|
||||
{
|
||||
_ = _stack.Pop();
|
||||
_recycle.Push(r);
|
||||
}
|
||||
}
|
||||
|
||||
public void SkipOnce()
|
||||
{
|
||||
if (_stack.TryPeek(out Record? cur))
|
||||
{
|
||||
switch (cur.State)
|
||||
{
|
||||
case 0:
|
||||
cur.State++;
|
||||
break;
|
||||
case 1:
|
||||
cur.State++;
|
||||
break;
|
||||
case 2:
|
||||
if (TNode.IsNullOrEmpty(cur.Node.Next))
|
||||
{
|
||||
_recycle.Push(_stack.Pop());
|
||||
if (_stack.Count == 0)
|
||||
{
|
||||
_completed = true;
|
||||
return;
|
||||
}
|
||||
} else
|
||||
{
|
||||
_stack.Push(GetNewRecord(cur.Node.Next));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Record GetNewRecord(
|
||||
TNode node
|
||||
)
|
||||
{
|
||||
if (_recycle.TryPop(out Record? r))
|
||||
{
|
||||
r.State = 0;
|
||||
r.Node = node;
|
||||
return r;
|
||||
}
|
||||
return new Record(node, 0);
|
||||
}
|
||||
|
||||
object IEnumerator.Current => Current;
|
||||
|
||||
private class Record(
|
||||
TNode node,
|
||||
int state
|
||||
)
|
||||
{
|
||||
|
||||
public int State { get; set; } = state;
|
||||
|
||||
public TNode Node { get; set; } = node;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
// file="CompileDepthState"
|
||||
// Project: StgSharp
|
||||
// Copyright (c) Nitload.
|
||||
// SPDX-License-Identifier: MIT
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Nitload.RegularAnalysis.Abstraction
|
||||
{
|
||||
public interface ICompileDepthState
|
||||
{
|
||||
|
||||
string CurrentState { get; set; }
|
||||
|
||||
}
|
||||
|
||||
public class CompileDepthMark
|
||||
{
|
||||
|
||||
internal CompileDepthMark(
|
||||
int operatorBegin,
|
||||
int operandBegin,
|
||||
int usage
|
||||
)
|
||||
{
|
||||
OperatorBegin = operatorBegin;
|
||||
OperandBegin = operandBegin;
|
||||
Usage = usage;
|
||||
}
|
||||
|
||||
internal CompileDepthMark(
|
||||
int operatorBegin,
|
||||
int operandBegin,
|
||||
int usage,
|
||||
ICompileDepthState stateCache
|
||||
)
|
||||
{
|
||||
OperatorBegin = operatorBegin;
|
||||
OperandBegin = operandBegin;
|
||||
Usage = usage;
|
||||
StateCache = stateCache;
|
||||
}
|
||||
|
||||
public bool IsBeginOfBlock { get; internal set; }
|
||||
|
||||
public ICompileDepthState StateCache { get; internal set; }
|
||||
|
||||
public int OperandBegin { get; internal set; }
|
||||
|
||||
public int OperatorBegin { get; internal set; }
|
||||
|
||||
public int Usage { get; internal set; }
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
// file="CompileStack"
|
||||
// Project: StgSharp
|
||||
// Copyright (c) Nitload.
|
||||
// SPDX-License-Identifier: MIT
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
using Nitload.Common.Collections;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Nitload.RegularAnalysis.Abstraction
|
||||
{
|
||||
public partial class CompileStack<TNode, TLabel> where TNode : ISyntaxNode<TNode, TLabel>
|
||||
where TLabel : unmanaged
|
||||
{
|
||||
|
||||
private readonly RandomAccessibleStack<DepthMark> _depthMarks = [];
|
||||
private readonly RandomAccessibleStack<TNode> _operands = [];
|
||||
private readonly RandomAccessibleStack<TNode> _operators = [];
|
||||
|
||||
public CompileStack()
|
||||
{
|
||||
_depthMarks.Push(new DepthMark(0, 0, 0));
|
||||
}
|
||||
|
||||
public int OperandInDepthCount => _operands.Count - _depthMarks.Peek().OperandPos;
|
||||
|
||||
public int OperatorInDepthCount => _operators.Count - _depthMarks.Peek().OperatorPos;
|
||||
|
||||
public int Depth => _depthMarks.Count;
|
||||
|
||||
private record struct DepthMark(
|
||||
int OperandPos,
|
||||
int OperatorPos,
|
||||
int Mark
|
||||
);
|
||||
|
||||
#region operator
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public TNode PopOperator(
|
||||
)
|
||||
{
|
||||
if (OperatorInDepthCount > 0) {
|
||||
return _operators.Pop();
|
||||
}
|
||||
return TNode.Empty;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void PushOperator(
|
||||
TNode operatorNode
|
||||
)
|
||||
{
|
||||
_operators.Push(operatorNode);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool TryPopOperator(
|
||||
out TNode op
|
||||
)
|
||||
{
|
||||
if (OperatorInDepthCount > 0)
|
||||
{
|
||||
op = _operators.Pop();
|
||||
return true;
|
||||
}
|
||||
op = TNode.Empty;
|
||||
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
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public TNode PopOperand(
|
||||
)
|
||||
{
|
||||
if (OperandInDepthCount > 0) {
|
||||
return _operands.Pop();
|
||||
}
|
||||
return TNode.Empty;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void PushOperand(
|
||||
TNode operatorNode
|
||||
)
|
||||
{
|
||||
_operands.Push(operatorNode);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool TryPopOperand(
|
||||
out TNode op
|
||||
)
|
||||
{
|
||||
if (OperandInDepthCount > 0)
|
||||
{
|
||||
op = _operands.Pop();
|
||||
return true;
|
||||
}
|
||||
op = TNode.Empty;
|
||||
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
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void DecreaseDepth(
|
||||
)
|
||||
{
|
||||
if (Depth > 1)
|
||||
{
|
||||
DepthMark mark = _depthMarks.Pop();
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void IncreaseDepth(
|
||||
int mark
|
||||
)
|
||||
{
|
||||
_depthMarks.Push(new DepthMark(_operands.Count, _operators.Count, mark));
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
// file="ISyntaxNode"
|
||||
// Project: StgSharp
|
||||
// Copyright (c) Nitload.
|
||||
// SPDX-License-Identifier: MIT
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
using Nitload.Common.Collections;
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
|
||||
namespace Nitload.RegularAnalysis.Abstraction
|
||||
{
|
||||
public interface ITrinitySyntaxNode<TNode, TLabel> : ISyntaxNode<TNode, TLabel>
|
||||
where TNode : ISyntaxNode<TNode, TLabel>
|
||||
where TLabel : unmanaged
|
||||
{
|
||||
|
||||
TNode Third { get; set; }
|
||||
|
||||
}
|
||||
|
||||
public interface ISyntaxNode<TNode, TLabel> where TNode : ISyntaxNode<TNode, TLabel>
|
||||
where TLabel : unmanaged
|
||||
{
|
||||
|
||||
ISyntaxPayload<TLabel> Source { get; }
|
||||
|
||||
long NodeFlag { get; }
|
||||
|
||||
TLabel EqualityTypeConvert { get; }
|
||||
|
||||
TNode Previous { get; set; }
|
||||
|
||||
TNode Next { get; set; }
|
||||
|
||||
TNode Left { get; set; }
|
||||
|
||||
TNode Right { get; set; }
|
||||
|
||||
TNode Parent { get; set; }
|
||||
|
||||
static abstract TNode Empty { get; }
|
||||
|
||||
protected internal int EnumState { get; set; }
|
||||
|
||||
void AppendNode(
|
||||
TNode nextToken
|
||||
);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static virtual bool IsNullOrEmpty(
|
||||
TNode node
|
||||
)
|
||||
{
|
||||
return node is null || node.Equals(TNode.Empty);
|
||||
}
|
||||
|
||||
void PrependNode(
|
||||
TNode previousNode
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
// file="ISyntaxPayload"
|
||||
// Project: StgSharp
|
||||
// Copyright (c) Nitload.
|
||||
// SPDX-License-Identifier: MIT
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Nitload.RegularAnalysis.Abstraction
|
||||
{
|
||||
public interface ISyntaxPayload<TLabel> where TLabel : unmanaged
|
||||
{
|
||||
|
||||
int Column { get; }
|
||||
|
||||
int Row { get; }
|
||||
|
||||
string Source { get; }
|
||||
|
||||
TLabel Flag { get; }
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
// file="ITokenParser"
|
||||
// Project: StgSharp
|
||||
// Copyright (c) Nitload.
|
||||
// SPDX-License-Identifier: MIT
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Nitload.RegularAnalysis.Abstraction
|
||||
{
|
||||
public abstract class TokenParser<TIn, TOut> : ITokenReader<TOut> where TIn : unmanaged
|
||||
where TOut : unmanaged
|
||||
{
|
||||
|
||||
public abstract bool IsEmpty { get; }
|
||||
|
||||
protected internal ITokenReader<TIn> Source { get; internal set; }
|
||||
|
||||
public abstract Token<TOut> ReadToken();
|
||||
|
||||
public abstract bool TryReadToken(
|
||||
out Token<TOut> t
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
// file="ITokenReader"
|
||||
// Project: StgSharp
|
||||
// Copyright (c) Nitload.
|
||||
// SPDX-License-Identifier: MIT
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
using System;
|
||||
|
||||
namespace Nitload.RegularAnalysis.Abstraction
|
||||
{
|
||||
public static class TokenReader
|
||||
{
|
||||
|
||||
public static TokenParser<TIn, TOut> Pipe<TIn, TOut>(
|
||||
this ITokenReader<TIn> source,
|
||||
Func<TokenParser<TIn, TOut>> builder
|
||||
) where TIn : unmanaged where TOut : unmanaged
|
||||
{
|
||||
TokenParser<TIn, TOut> parser = builder();
|
||||
parser.Source = source;
|
||||
return parser;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public interface ITokenReader<TLabel> where TLabel : unmanaged
|
||||
{
|
||||
|
||||
bool IsEmpty { get; }
|
||||
|
||||
Token<TLabel> ReadToken();
|
||||
|
||||
bool TryReadToken(
|
||||
out Token<TLabel> t
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,260 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
// file="SequenceEmitter"
|
||||
// Project: StgSharp
|
||||
// Copyright (c) Nitload.
|
||||
// SPDX-License-Identifier: MIT
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
using Nitload.RegularAnalysis.Text;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Nitload.RegularAnalysis.Abstraction
|
||||
{
|
||||
public static class SequenceEmitterExtensions
|
||||
{
|
||||
|
||||
public static SequenceEmitter<string> AppendLine(
|
||||
this SequenceEmitter<string> emitter
|
||||
)
|
||||
{
|
||||
return emitter is null ? null! : emitter.Append(string.Empty);
|
||||
}
|
||||
|
||||
public static SequenceEmitter<string> AppendLine(
|
||||
this SequenceEmitter<string> emitter,
|
||||
string line
|
||||
)
|
||||
{
|
||||
return emitter is null ?
|
||||
null! :
|
||||
emitter.Append(string.IsNullOrEmpty(line) ? string.Empty : line);
|
||||
}
|
||||
|
||||
public static SequenceEmitter<string> AppendLine(
|
||||
this SequenceEmitter<string> emitter,
|
||||
params string[] lines
|
||||
)
|
||||
{
|
||||
if (lines is null || lines.Length == 0) {
|
||||
return emitter;
|
||||
}
|
||||
foreach (string line in lines) {
|
||||
emitter = emitter.AppendLine(line);
|
||||
}
|
||||
return emitter;
|
||||
}
|
||||
|
||||
public static SequenceEmitter<string> AppendLine(
|
||||
this SequenceEmitter<string> emitter,
|
||||
string line,
|
||||
bool condition
|
||||
)
|
||||
{
|
||||
return emitter is null ? null! : condition ? emitter.Append(line) : emitter;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public sealed class SequenceEmitter<T>
|
||||
{
|
||||
|
||||
private bool _isRoot;
|
||||
|
||||
private readonly Node _head;
|
||||
private Node _tail;
|
||||
|
||||
private SequenceEmitter(
|
||||
Node node
|
||||
)
|
||||
{
|
||||
_isRoot = true;
|
||||
_head = node;
|
||||
_tail = _head;
|
||||
}
|
||||
|
||||
public SequenceEmitter()
|
||||
{
|
||||
_isRoot = true;
|
||||
_head = new();
|
||||
_tail = _head;
|
||||
}
|
||||
|
||||
public int Count { get; private set; }
|
||||
|
||||
public SequenceEmitter<T> Append(
|
||||
T unit
|
||||
)
|
||||
{
|
||||
if (!_isRoot) {
|
||||
throw new InvalidOperationException("This emitter has been appended to another emitter. It is now readonly.");
|
||||
}
|
||||
_tail.Add(unit);
|
||||
Count++;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SequenceEmitter<T> Append(
|
||||
SequenceEmitter<T> emitter
|
||||
)
|
||||
{
|
||||
if (emitter == null || emitter.Count == 0) {
|
||||
return this;
|
||||
}
|
||||
if (ReferenceEquals(this, emitter)) {
|
||||
throw new InvalidOperationException("Cannot append emitter to itself.");
|
||||
}
|
||||
|
||||
if (!emitter._isRoot) {
|
||||
throw new InvalidOperationException("The emitter has been appended to another emitter.");
|
||||
}
|
||||
_tail.Next = emitter._head;
|
||||
_tail = emitter._tail;
|
||||
Count += emitter.Count;
|
||||
emitter._isRoot = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SequenceEmitter<T> Clone()
|
||||
{
|
||||
if (!_isRoot) {
|
||||
throw new InvalidOperationException("This emitter has been appended to another emitter. It is now readonly.");
|
||||
}
|
||||
SequenceEmitter<T> clone = new();
|
||||
Node current = _head;
|
||||
return clone;
|
||||
}
|
||||
|
||||
public Enumerator GetEnumerator()
|
||||
{
|
||||
return _isRoot ?
|
||||
new Enumerator(this) :
|
||||
throw new InvalidOperationException("This emitter has been appended to another emitter. You should not get its enumerator.");
|
||||
}
|
||||
|
||||
public List<T> ToList()
|
||||
{
|
||||
List<T> result = new(Count);
|
||||
Node current = _head;
|
||||
while (current != null)
|
||||
{
|
||||
result.AddRange(current.IRList);
|
||||
current = current.Next!;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
#pragma warning disable CA1063
|
||||
#pragma warning disable CA1816
|
||||
public class Enumerator(
|
||||
[NotNull] SequenceEmitter<T> emitter
|
||||
) : IEnumerator<T>
|
||||
{
|
||||
|
||||
private int _index = -1;
|
||||
|
||||
private Node _current = null!;
|
||||
private readonly Node _root = emitter._head;
|
||||
|
||||
public T Current
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_current is null || _index < 0 || _index >= _current.Count) {
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
return _current[_index];
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose() { }
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
_current ??= _root;
|
||||
int index = _index + 1;
|
||||
if (index < _current.Count)
|
||||
{
|
||||
_index = index;
|
||||
return true;
|
||||
}
|
||||
Node current = _current.Next!;
|
||||
if (current is null || ReferenceEquals(current, _root))
|
||||
{
|
||||
_index = -1;
|
||||
return false;
|
||||
}
|
||||
_current = current;
|
||||
_index = -1;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
_current = _root;
|
||||
_index = -1;
|
||||
}
|
||||
|
||||
object IEnumerator.Current => Current!;
|
||||
|
||||
}
|
||||
|
||||
#pragma warning restore CA1063
|
||||
#pragma warning restore CA1816
|
||||
|
||||
private sealed class Node
|
||||
{
|
||||
|
||||
public Node()
|
||||
{
|
||||
IRList = [];
|
||||
}
|
||||
|
||||
public Node(
|
||||
int capacity
|
||||
)
|
||||
{
|
||||
IRList = new(capacity);
|
||||
}
|
||||
|
||||
public T this[
|
||||
int index
|
||||
]
|
||||
{
|
||||
get => IRList[index];
|
||||
set => IRList[index] = value;
|
||||
}
|
||||
|
||||
public Node? Next { get; set; }
|
||||
|
||||
public int Count => IRList.Count;
|
||||
|
||||
public List<T> IRList { get; }
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Add(
|
||||
T ir
|
||||
)
|
||||
{
|
||||
IRList.Add(ir);
|
||||
}
|
||||
|
||||
public void AddRange(
|
||||
IEnumerable<T> irs
|
||||
)
|
||||
{
|
||||
IRList.AddRange(irs);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
// file="Token"
|
||||
// Project: StgSharp
|
||||
// Copyright (c) Nitload.
|
||||
// SPDX-License-Identifier: MIT
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Nitload.RegularAnalysis.Abstraction
|
||||
{
|
||||
public readonly struct Token<TLabel> where TLabel : unmanaged
|
||||
{
|
||||
|
||||
public readonly int Column;
|
||||
public readonly int Line;
|
||||
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,
|
||||
int columnNumber,
|
||||
TLabel flag
|
||||
)
|
||||
{
|
||||
Value = chars;
|
||||
Line = lineNumber;
|
||||
Column = columnNumber;
|
||||
Flag = flag;
|
||||
}
|
||||
|
||||
public static Token<TLabel> Empty { get; } = new Token<TLabel>();
|
||||
|
||||
public override bool Equals(
|
||||
[NotNullWhen(true)] object? obj
|
||||
)
|
||||
{
|
||||
return base.Equals(obj);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Token<TLabel> ReLocate(
|
||||
int line,
|
||||
int column
|
||||
)
|
||||
{
|
||||
return new Token<TLabel>(Value, line, column, Flag);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Token<TNewLabel> Remark<TNewLabel>(
|
||||
TNewLabel label
|
||||
) where TNewLabel : unmanaged
|
||||
{
|
||||
return new Token<TNewLabel>(Value, Line, Column, label);
|
||||
}
|
||||
|
||||
public static bool operator !=(
|
||||
Token<TLabel> left,
|
||||
Token<TLabel> right
|
||||
)
|
||||
{
|
||||
return !left.Value.SequenceEqual(right.Value) ||
|
||||
left.Line != right.Line ||
|
||||
left.Column != right.Column ||
|
||||
!left.Flag.Equals(right.Flag);
|
||||
}
|
||||
|
||||
public static bool operator ==(
|
||||
Token<TLabel> left,
|
||||
Token<TLabel> right
|
||||
)
|
||||
{
|
||||
return left.Value.SequenceEqual(right.Value) &&
|
||||
left.Line == right.Line &&
|
||||
left.Column == right.Column &&
|
||||
left.Flag.Equals(right.Flag);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user