111 lines
2 KiB
C#
111 lines
2 KiB
C#
#define DEBUG
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Diagnostics;
|
|
|
|
namespace AspClassic.Parser;
|
|
|
|
/// <summary>
|
|
/// The root class of all trees.
|
|
/// </summary>
|
|
public abstract class Tree
|
|
{
|
|
private readonly TreeType _Type;
|
|
|
|
private readonly Span _Span;
|
|
|
|
private Tree _Parent;
|
|
|
|
private ReadOnlyCollection<Tree> _Children;
|
|
|
|
/// <summary>
|
|
/// The type of the tree.
|
|
/// </summary>
|
|
public TreeType Type => _Type;
|
|
|
|
/// <summary>
|
|
/// The location of the tree.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// The span ends at the first character beyond the tree
|
|
/// </remarks>
|
|
public Span Span => _Span;
|
|
|
|
/// <summary>
|
|
/// The parent of the tree. Nothing if the root tree.
|
|
/// </summary>
|
|
public Tree Parent => _Parent;
|
|
|
|
/// <summary>
|
|
/// The children of the tree.
|
|
/// </summary>
|
|
public ReadOnlyCollection<Tree> Children
|
|
{
|
|
get
|
|
{
|
|
if (_Children == null)
|
|
{
|
|
List<Tree> ChildList = new List<Tree>();
|
|
GetChildTrees(ChildList);
|
|
_Children = new ReadOnlyCollection<Tree>(ChildList);
|
|
}
|
|
return _Children;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Whether the tree is 'bad'.
|
|
/// </summary>
|
|
public virtual bool IsBad => false;
|
|
|
|
protected Tree(TreeType type, Span span)
|
|
{
|
|
Debug.Assert(type >= TreeType.SyntaxError && type <= TreeType.File);
|
|
_Type = type;
|
|
_Span = span;
|
|
}
|
|
|
|
protected void SetParent(Tree child)
|
|
{
|
|
if (child != null)
|
|
{
|
|
child._Parent = this;
|
|
}
|
|
}
|
|
|
|
protected void SetParents<T>(IList<T> children) where T : Tree
|
|
{
|
|
if (children == null)
|
|
{
|
|
return;
|
|
}
|
|
foreach (Tree Child in children)
|
|
{
|
|
SetParent(Child);
|
|
}
|
|
}
|
|
|
|
protected static void AddChild(IList<Tree> childList, Tree child)
|
|
{
|
|
if (child != null)
|
|
{
|
|
childList.Add(child);
|
|
}
|
|
}
|
|
|
|
protected static void AddChildren<T>(IList<Tree> childList, ReadOnlyCollection<T> children) where T : Tree
|
|
{
|
|
if (children == null)
|
|
{
|
|
return;
|
|
}
|
|
foreach (Tree Child in children)
|
|
{
|
|
childList.Add(Child);
|
|
}
|
|
}
|
|
|
|
protected virtual void GetChildTrees(IList<Tree> childList)
|
|
{
|
|
}
|
|
}
|