SyntaxTree Class

A parsed document: its text, the tree over it, and the diagnostics the parser produced.

public abstract class SyntaxTree

Remarks

Only a handful of members are left to a language: the text, the root, and how to make a new tree from either. Everything else here is answered from the immutable nodes alone.

Constructors

NameDescription
SyntaxTree()

SyntaxTree()

protected SyntaxTree()

No documentation is available.

Properties

NameDescription
FilePathGets the path the text came from, or null when it did not come from a file.
Length

FilePath

Gets the path the text came from, or null when it did not come from a file.

public abstract string? FilePath { get; }

Property value

string?

public int Length { get; }

Property value

int

No documentation is available.

Methods

NameDescription
GetChangedSpans(SyntaxTree)Gets the ranges of this tree that differ from oldTree.
GetChanges(SyntaxTree)Describes how oldTree would have to change to become this one.
GetDiagnostics()Gets every diagnostic in the tree, in source order.
GetDiagnostics(SyntaxNode)Gets the diagnostics at or below node.
GetDiagnostics(SyntaxToken)Gets the diagnostics on token and the trivia around it.
GetDiagnostics(SyntaxTrivia)Gets the diagnostics on trivia.
GetLineSpan(TextSpan)Converts span to line and character positions.
GetLocation(TextSpan)Pairs span with the text it indexes into.
GetRoot()Gets the root of the tree.
GetRootCore()Gets the root of the tree. A language overrides this and offers its own typed GetRoot.
GetText()Gets the text this tree was parsed from.
IsEquivalentTo(SyntaxTree?)Determines whether the two trees have the same structure and text.
ToString()
WithChangedText(SourceText)Returns a tree over newText.
WithChangedTextCore(SourceText)Returns a tree over newText.
WithRoot(SyntaxNode)Returns a tree whose root is root and whose text is the text of that root.
WithRootCore(SyntaxNode)Returns a tree whose root is root.

GetChangedSpans(SyntaxTree)

SyntaxTree.cs

Gets the ranges of this tree that differ from oldTree.

public virtual IReadOnlyList<TextSpan> GetChangedSpans(SyntaxTree oldTree)

Parameters

oldTree SyntaxTree

Returns

IReadOnlyList<TextSpan>

Exceptions

ArgumentNullException
oldTree is null.

GetChanges(SyntaxTree)

SyntaxTree.cs

Describes how oldTree would have to change to become this one.

public virtual IReadOnlyList<TextChange> GetChanges(SyntaxTree oldTree)

Parameters

oldTree SyntaxTree

Returns

IReadOnlyList<TextChange>

Exceptions

ArgumentNullException
oldTree is null.

GetDiagnostics overloads

GetDiagnostics()

SyntaxTree.cs

Gets every diagnostic in the tree, in source order.

public virtual IReadOnlyList<Diagnostic> GetDiagnostics()
Returns

IReadOnlyList<Diagnostic>

Remarks

A language that keeps its diagnostics in a list of its own rather than on the nodes overrides this, so that a caller holding the tree through this type still sees them.

GetDiagnostics(SyntaxNode)

SyntaxTree.cs

Gets the diagnostics at or below node.

public virtual IEnumerable<Diagnostic> GetDiagnostics(SyntaxNode node)
Parameters
node SyntaxNode
Returns

IEnumerable<Diagnostic>

Exceptions
ArgumentNullException
node is null.

GetDiagnostics(SyntaxToken)

SyntaxTree.cs

Gets the diagnostics on token and the trivia around it.

public virtual IEnumerable<Diagnostic> GetDiagnostics(SyntaxToken token)
Parameters
token SyntaxToken
Returns

IEnumerable<Diagnostic>

GetDiagnostics(SyntaxTrivia)

SyntaxTree.cs

Gets the diagnostics on trivia.

public virtual IEnumerable<Diagnostic> GetDiagnostics(SyntaxTrivia trivia)
Parameters
trivia SyntaxTrivia
Returns

IEnumerable<Diagnostic>

GetLineSpan(TextSpan)

SyntaxTree.cs

Converts span to line and character positions.

public virtual LinePositionSpan GetLineSpan(TextSpan span)

Parameters

span TextSpan

Returns

LinePositionSpan

GetLocation(TextSpan)

SyntaxTree.cs

Pairs span with the text it indexes into.

public virtual Location GetLocation(TextSpan span)

Parameters

span TextSpan

Returns

Location

GetRoot()

SyntaxTree.cs

Gets the root of the tree.

public SyntaxNode GetRoot()

Returns

SyntaxNode

GetRootCore()

Gets the root of the tree. A language overrides this and offers its own typed GetRoot.

protected abstract SyntaxNode GetRootCore()

Returns

SyntaxNode

GetText()

Gets the text this tree was parsed from.

public abstract SourceText GetText()

Returns

SourceText

Remarks

The same instance every time: diagnostics compare the text they point into by reference, so a tree that returned a new one each call would produce diagnostics that never compare equal.

IsEquivalentTo(SyntaxTree?)

SyntaxTree.cs

Determines whether the two trees have the same structure and text.

public virtual bool IsEquivalentTo(SyntaxTree? other)

Parameters

other SyntaxTree?

Returns

bool

ToString()

SyntaxTree.cs
public override string ToString()

Returns

string

No documentation is available.

WithChangedText(SourceText)

SyntaxTree.cs

Returns a tree over newText.

public SyntaxTree WithChangedText(SourceText newText)

Parameters

newText SourceText

Returns

SyntaxTree

Exceptions

ArgumentNullException
newText is null.

WithChangedTextCore(SourceText)

Returns a tree over newText.

protected abstract SyntaxTree WithChangedTextCore(SourceText newText)

Parameters

newText SourceText

Returns

SyntaxTree

WithRoot(SyntaxNode)

SyntaxTree.cs

Returns a tree whose root is root and whose text is the text of that root.

public SyntaxTree WithRoot(SyntaxNode root)

Parameters

root SyntaxNode

Returns

SyntaxTree

Exceptions

ArgumentNullException
root is null.

WithRootCore(SyntaxNode)

Returns a tree whose root is root.

protected abstract SyntaxTree WithRootCore(SyntaxNode root)

Parameters

root SyntaxNode

Returns

SyntaxTree