public string PrettyPrint(CrawlSyntaxNode node) { _result = new StringBuilder(); _indentation = new StringBuilder(); Visit(node); return(_result.ToString()); }
private void addLowerPart(CrawlSyntaxNode node, int width) { _result.Append(_indentation); //└─┬─────┘ //If this is not the last sibling, a branch continues downwards if (node.Parent?.ChildCount - 1 > node.IndexInParent) { _result.Append("│"); } else { _result.Append(" "); } //If this is a leaf no branch down is needed. if (node.ChildCount == 0) { _result.Append("└──"); } else { _result.Append("└┬─"); } for (int i = 0; i < width - 2; i++) { _result.Append("─"); } _result.Append("┘\n"); }
public CrawlSyntaxNode Translplant(CrawlSyntaxNode replacement) { Stack <int> parrentIndex = new Stack <int>(); int count = 0; GreenCrawlSyntaxNode toInsert = replacement.Green; CrawlSyntaxNode self = this; while (self.Parent != null) { parrentIndex.Push(self.IndexInParent); toInsert = self.Parent.Green.WithReplacedChild(toInsert, self.IndexInParent); self = self.Parent; count++; } CrawlSyntaxNode newRoot = toInsert.CreateRed(null, 0); while (parrentIndex.Count != 0) { newRoot = newRoot.GetChildAt(parrentIndex.Pop()); if (newRoot == null) { throw new Exception(); } } return(newRoot); }
/// <param name="parent">The parent of this node.</param> /// <param name="self">The red counterpart of this node.</param> /// <param name="indexInParent">The index this node has in its parent.</param> protected internal CrawlSyntaxNode(CrawlSyntaxNode parent, GreenCrawlSyntaxNode self, int indexInParent) { Parent = parent; Green = self; //GreenNodes sometimes uses upper bits to encode extra information. Not allowed here // ReSharper disable once BitwiseOperatorOnEnumWithoutFlags Type = self.Type; IndexInParent = indexInParent; }
private void addSingleLine(CrawlSyntaxNode node) { _result.Append(_indentation); //├Foo //If this is root, no branch goes down to it _result.Append(GetParentBranch(node, _slimStrings)); _result.Append(node); _result.Append('\n'); }
private void addMiddlePart(CrawlSyntaxNode node, string text, int width) { _result.Append(_indentation); //└┤foo │ _result.Append(GetParentBranch(node, _fatStrings)); _result.Append(text); for (int i = text.Length; i < width; i++) { _result.Append(' '); } _result.Append("│\n"); }
private void addIndentation(CrawlSyntaxNode node) { //If this is root, add no indentation if (node.Parent == null) { } //If parent is not the last sibling, a branch continues downwards else if (node.Parent.Parent?.ChildCount - 1 > node.Parent.IndexInParent) { _indentation.Append("│ "); } else { _indentation.Append(" "); } }
// checks if the parent inherites from IScope, if it inherities it return the node as an IScope, // if the node do not inherites from IScope we check the parents parent. /// <summary> /// Finds the first scope among this node and its ancestors /// </summary> /// <param name="node">A <see cref="CrawlSyntaxNode"/> to examine for scope information</param> /// <returns></returns> public static IScope FindFirstScope(this CrawlSyntaxNode node) { if (node == null) { return(null); } IScope scope = node as IScope; if (scope != null) { return(scope); } return(FindFirstScope(node.Parent)); }
private string GetParentBranch(CrawlSyntaxNode node, string[] choices) { if (node.Parent == null) { return(choices[0]); } //If this is not the last sibling, a branch continues downwards else if (node.Parent.ChildCount - 1 > node.IndexInParent) { return(choices[1]); } else { return(choices[2]); } }
private void addDetails(CrawlSyntaxNode node, string detail, int width) { string[] prefixpart = _singleLine ? _slimParts : _fatParts; _result.Append(_indentation); _result.Append(GetParentBranch(node, prefixpart)); _result.Append(detail); for (int i = detail.Length; i < width; i++) { _result.Append(' '); } if (!_singleLine) { _result.Append('│'); } _result.Append('\n'); }
private void addUpperPart(CrawlSyntaxNode node, int width) { _result.Append(_indentation); //┌──────────────┐ //If this is root, no branch goes down to it. if (node.Parent == null) { _result.Append(" ┌──"); } else { _result.Append("│┌──"); } for (int i = 0; i < width - 2; i++) { _result.Append("─"); } _result.Append("┐\n"); }
public override void Visit(CrawlSyntaxNode node) { if (node == null) { return; } addIndentation(node); string detail = _details?.Invoke(node); if (_singleLine) { addSingleLine(node); if (detail != null) { addDetails(node, detail, detail.Length); } } else { string nodeString = node.ToString(); int width = Math.Max(Math.Max(nodeString.Length, detail?.Length ?? 0), 2); addUpperPart(node, width); addMiddlePart(node, nodeString, width); if (detail != null) { addDetails(node, detail, width); } addLowerPart(node, width); } base.Visit(node); removeIndentation(); }
internal override CrawlSyntaxNode CreateRed(CrawlSyntaxNode parent, int indexInParent) { return(new ListNode <T>(parent, this, indexInParent)); }
/// <summary> /// Create new red representation of node with specified parent. /// </summary> internal abstract CrawlSyntaxNode CreateRed(CrawlSyntaxNode parent, int indexInParent);
internal CrawlSyntaxTree(CrawlSyntaxNode node, string name) { RootNode = node; CompilationUnitName = name; }
public ListNode(CrawlSyntaxNode parent, GreenCrawlSyntaxNode self, int indexInParent) : base(parent, self, indexInParent) { _childNodes = new T[self.ChildCount]; }
protected static GreenCrawlSyntaxNode ExtractGreenNode(CrawlSyntaxNode node) => node?.Green;