progress
This commit is contained in:
parent
16e76d6b31
commit
484dbfc9d9
529 changed files with 113694 additions and 0 deletions
111
AspClassic.Parser/Tree.cs
Normal file
111
AspClassic.Parser/Tree.cs
Normal file
|
@ -0,0 +1,111 @@
|
|||
#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)
|
||||
{
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue