SyntaxNode Class

A node in a syntax tree: a position, a parent, and a view onto the shared immutable node that holds the structure.

public abstract class SyntaxNode

Remarks

Nodes are created on demand as a tree is walked, and each one is created at most once per tree, so reference identity is stable: asking a node for the same child twice returns the same instance.

This type cannot be derived from outside the assemblies that make up the language framework. Every syntax tree is produced by a parser that knows how to build the underlying immutable nodes.

Properties

NameDescription
ContainsAnnotationsGets a value indicating whether this node or anything below it carries an annotation.
ContainsDiagnosticsGets a value indicating whether this node or anything below it carries a diagnostic.
ContainsSkippedTextGets a value indicating whether this node or anything below it came from text the parser could not use.
FullSpanGets the range this node covers, including the trivia at its outer edges.
IsMissingGets a value indicating whether the parser inserted this node rather than reading it from the text.
ParentGets the node this one is a child of, or null when it is the root.
RawKindGets the language-specific kind of this node as a plain integer.
SpanGets the range this node covers, excluding the trivia at its outer edges.
SpanStartGets the character offset of the start of this node, excluding its leading trivia.
SyntaxTreeGets the tree this node belongs to, or null when it was built rather than parsed.

ContainsAnnotations

SyntaxNode.cs

Gets a value indicating whether this node or anything below it carries an annotation.

public bool ContainsAnnotations { get; }

Property value

bool

ContainsDiagnostics

SyntaxNode.cs

Gets a value indicating whether this node or anything below it carries a diagnostic.

public bool ContainsDiagnostics { get; }

Property value

bool

ContainsSkippedText

SyntaxNode.cs

Gets a value indicating whether this node or anything below it came from text the parser could not use.

public bool ContainsSkippedText { get; }

Property value

bool

Gets the range this node covers, including the trivia at its outer edges.

public TextSpan FullSpan { get; }

Property value

TextSpan

IsMissing

SyntaxNode.cs

Gets a value indicating whether the parser inserted this node rather than reading it from the text.

public bool IsMissing { get; }

Property value

bool

Gets the node this one is a child of, or null when it is the root.

public SyntaxNode? Parent { get; }

Property value

SyntaxNode?

Gets the language-specific kind of this node as a plain integer.

public int RawKind { get; }

Property value

int

Remarks

Each language exposes this as its own SyntaxKind enum through a Kind() method.

Gets the range this node covers, excluding the trivia at its outer edges.

public TextSpan Span { get; }

Property value

TextSpan

SpanStart

SyntaxNode.cs

Gets the character offset of the start of this node, excluding its leading trivia.

public int SpanStart { get; }

Property value

int

SyntaxTree

SyntaxNode.cs

Gets the tree this node belongs to, or null when it was built rather than parsed.

public SyntaxTree? SyntaxTree { get; }

Property value

SyntaxTree?

Methods

NameDescription
Ancestors()Gets the nodes this one sits inside, closest first.
AncestorsAndSelf()Gets this node and the nodes it sits inside, closest first.
ChildNodes()Gets the child nodes of this node, skipping the tokens among them.
ChildNodesAndTokens()Gets the children of this node, with the lists among them flattened away.
ChildThatContainsPosition(int)Gets the child whose full span contains position.
ChildTokens()Gets the child tokens of this node, skipping the nodes among them.
Contains(SyntaxNode?)Determines whether node is this node or sits below it.
DescendantNodes(TextSpan, Func<SyntaxNode, bool>?)Gets every node below this one that touches span, parents before children.
DescendantNodes(Func<SyntaxNode, bool>?)Gets every node below this one, parents before children.
DescendantNodesAndSelf(TextSpan, Func<SyntaxNode, bool>?)Gets this node and every node below it that touches span, parents before children.
DescendantNodesAndSelf(Func<SyntaxNode, bool>?)Gets this node and every node below it, parents before children.
DescendantNodesAndTokens(TextSpan, Func<SyntaxNode, bool>?)Gets every node and token below this one that touches span, in source order.
DescendantNodesAndTokens(Func<SyntaxNode, bool>?)Gets every node and token below this one, in source order.
DescendantTokens(TextSpan, Func<SyntaxNode, bool>?)Gets every token below this one that touches span, in source order.
DescendantTokens(Func<SyntaxNode, bool>?)Gets every token below this one, in source order.
DescendantTrivia(TextSpan, Func<SyntaxNode, bool>?)Gets every trivia below this node that touches span, in source order.
DescendantTrivia(Func<SyntaxNode, bool>?)Gets every trivia below this node, in source order.
FindNode(TextSpan, bool)Finds the smallest node whose full span covers span.
FindToken(int)Finds the token at position.
FindTrivia(int)Finds the trivia at position, or nothing when the position falls on a token's own text.
FirstAncestorOrSelf<TNode>(Func<TNode, bool>?)Finds the closest node of type TNode, starting with this one.
GetAnnotatedNodes(SyntaxAnnotation)Gets the nodes at or below this one carrying annotation.
GetAnnotatedNodes(string)Gets the nodes at or below this one carrying an annotation of the given kind.
GetAnnotatedNodesAndTokens(SyntaxAnnotation)Gets everything at or below this node carrying annotation.
GetAnnotatedNodesAndTokens(string)Gets everything at or below this node carrying an annotation of the given kind.
GetAnnotatedTokens(SyntaxAnnotation)Gets the tokens below this node carrying annotation.
GetAnnotatedTokens(string)Gets the tokens below this node carrying an annotation of the given kind.
GetAnnotatedTrivia(SyntaxAnnotation)Gets the trivia below this node carrying annotation.
GetAnnotatedTrivia(string)Gets the trivia below this node carrying an annotation of the given kind.
GetAnnotations()Gets all the annotations of this node itself.
GetAnnotations(string)Gets the annotations of this node itself, of the given kind.
GetDiagnostics()Gets the diagnostics at or below this node.
GetFirstToken()Returns the first token below this node, or SyntaxToken.None when it has none.
GetLastToken()Returns the last token below this node, or SyntaxToken.None when it has none.
GetLeadingTrivia()Gets the trivia in front of this node, which is the leading trivia of its first token.
GetRed<T>(T?, int)Returns the child in slot, creating it once and caching it in field.
GetRedAtZero<T>(T?)Returns the child in slot 0, creating it once and caching it in field.
GetTrailingTrivia()Gets the trivia after this node, which is the trailing trivia of its last token.
HasAnnotation(SyntaxAnnotation?)Determines whether this node itself carries annotation.
HasAnnotations(string)Determines whether this node itself carries an annotation of the given kind.
IsEquivalentTo(SyntaxNode?)Determines whether the two nodes have the same structure and text, ignoring their positions.
IsIncrementallyIdenticalTo(SyntaxNode?)Determines whether the two nodes are backed by the very same immutable node, which happens when one tree was derived from the other and this part of it was left untouched.
ToFullString()Returns the text of this node, including the trivia at its outer edges.
ToString()Returns the text of this node, excluding the trivia at its outer edges.
WriteTo(TextWriter)Writes the text of this node, including the trivia at its outer edges.

Ancestors()

SyntaxNode.cs

Gets the nodes this one sits inside, closest first.

public IEnumerable<SyntaxNode> Ancestors()

AncestorsAndSelf()

SyntaxNode.cs

Gets this node and the nodes it sits inside, closest first.

public IEnumerable<SyntaxNode> AncestorsAndSelf()

ChildNodes()

SyntaxNode.cs

Gets the child nodes of this node, skipping the tokens among them.

public IEnumerable<SyntaxNode> ChildNodes()

ChildNodesAndTokens()

SyntaxNode.cs

Gets the children of this node, with the lists among them flattened away.

public ChildSyntaxList ChildNodesAndTokens()

ChildThatContainsPosition(int)

SyntaxNode.cs

Gets the child whose full span contains position.

public SyntaxNodeOrToken ChildThatContainsPosition(int position)

Parameters

position int

Returns

SyntaxNodeOrToken

Exceptions

ArgumentOutOfRangeException
position is outside this node.

ChildTokens()

SyntaxNode.cs

Gets the child tokens of this node, skipping the nodes among them.

public IEnumerable<SyntaxToken> ChildTokens()

Contains(SyntaxNode?)

SyntaxNode.cs

Determines whether node is this node or sits below it.

public bool Contains(SyntaxNode? node)

Parameters

node SyntaxNode?

Returns

bool

DescendantNodes overloads

DescendantNodes(TextSpan, Func<SyntaxNode, bool>?)

SyntaxNode.cs

Gets every node below this one that touches span, parents before children.

public IEnumerable<SyntaxNode> DescendantNodes(TextSpan span, Func<SyntaxNode, bool>? descendIntoChildren = null)
Parameters
span TextSpan
descendIntoChildren Func<SyntaxNode, bool>?
Returns

IEnumerable<SyntaxNode>

DescendantNodes(Func<SyntaxNode, bool>?)

SyntaxNode.cs

Gets every node below this one, parents before children.

public IEnumerable<SyntaxNode> DescendantNodes(Func<SyntaxNode, bool>? descendIntoChildren = null)
Parameters
descendIntoChildren Func<SyntaxNode, bool>?
Decides whether to look inside a node. Everything is looked inside when omitted.
Returns

IEnumerable<SyntaxNode>

DescendantNodesAndSelf overloads

DescendantNodesAndSelf(TextSpan, Func<SyntaxNode, bool>?)

SyntaxNode.cs

Gets this node and every node below it that touches span, parents before children.

public IEnumerable<SyntaxNode> DescendantNodesAndSelf(TextSpan span, Func<SyntaxNode, bool>? descendIntoChildren = null)
Parameters
span TextSpan
descendIntoChildren Func<SyntaxNode, bool>?
Returns

IEnumerable<SyntaxNode>

DescendantNodesAndSelf(Func<SyntaxNode, bool>?)

SyntaxNode.cs

Gets this node and every node below it, parents before children.

public IEnumerable<SyntaxNode> DescendantNodesAndSelf(Func<SyntaxNode, bool>? descendIntoChildren = null)
Parameters
descendIntoChildren Func<SyntaxNode, bool>?
Returns

IEnumerable<SyntaxNode>

DescendantNodesAndTokens overloads

DescendantNodesAndTokens(TextSpan, Func<SyntaxNode, bool>?)

SyntaxNode.cs

Gets every node and token below this one that touches span, in source order.

public IEnumerable<SyntaxNodeOrToken> DescendantNodesAndTokens(TextSpan span, Func<SyntaxNode, bool>? descendIntoChildren = null)
Parameters
span TextSpan
descendIntoChildren Func<SyntaxNode, bool>?
Returns

IEnumerable<SyntaxNodeOrToken>

DescendantNodesAndTokens(Func<SyntaxNode, bool>?)

SyntaxNode.cs

Gets every node and token below this one, in source order.

public IEnumerable<SyntaxNodeOrToken> DescendantNodesAndTokens(Func<SyntaxNode, bool>? descendIntoChildren = null)
Parameters
descendIntoChildren Func<SyntaxNode, bool>?
Returns

IEnumerable<SyntaxNodeOrToken>

DescendantTokens overloads

DescendantTokens(TextSpan, Func<SyntaxNode, bool>?)

SyntaxNode.cs

Gets every token below this one that touches span, in source order.

public IEnumerable<SyntaxToken> DescendantTokens(TextSpan span, Func<SyntaxNode, bool>? descendIntoChildren = null)
Parameters
span TextSpan
descendIntoChildren Func<SyntaxNode, bool>?
Returns

IEnumerable<SyntaxToken>

DescendantTokens(Func<SyntaxNode, bool>?)

SyntaxNode.cs

Gets every token below this one, in source order.

public IEnumerable<SyntaxToken> DescendantTokens(Func<SyntaxNode, bool>? descendIntoChildren = null)
Parameters
descendIntoChildren Func<SyntaxNode, bool>?
Returns

IEnumerable<SyntaxToken>

DescendantTrivia overloads

DescendantTrivia(TextSpan, Func<SyntaxNode, bool>?)

SyntaxNode.cs

Gets every trivia below this node that touches span, in source order.

public IEnumerable<SyntaxTrivia> DescendantTrivia(TextSpan span, Func<SyntaxNode, bool>? descendIntoChildren = null)
Parameters
span TextSpan
descendIntoChildren Func<SyntaxNode, bool>?
Returns

IEnumerable<SyntaxTrivia>

DescendantTrivia(Func<SyntaxNode, bool>?)

SyntaxNode.cs

Gets every trivia below this node, in source order.

public IEnumerable<SyntaxTrivia> DescendantTrivia(Func<SyntaxNode, bool>? descendIntoChildren = null)
Parameters
descendIntoChildren Func<SyntaxNode, bool>?
Returns

IEnumerable<SyntaxTrivia>

FindNode(TextSpan, bool)

SyntaxNode.cs

Finds the smallest node whose full span covers span.

public SyntaxNode FindNode(TextSpan span, bool getInnermostNodeForTie = false)

Parameters

span TextSpan
A range inside this node.
getInnermostNodeForTie bool
When several nested nodes cover exactly the same range, whether to return the innermost rather than the outermost.

Returns

SyntaxNode

Exceptions

ArgumentOutOfRangeException
span is not inside this node.

FindToken(int)

SyntaxNode.cs

Finds the token at position.

public SyntaxToken FindToken(int position)

Parameters

position int
A position inside this node, or its very end.

Returns

SyntaxToken

Exceptions

ArgumentOutOfRangeException
position is outside this node.

FindTrivia(int)

SyntaxNode.cs

Finds the trivia at position, or nothing when the position falls on a token's own text.

public SyntaxTrivia FindTrivia(int position)

Parameters

position int

Returns

SyntaxTrivia

FirstAncestorOrSelf<TNode>(Func<TNode, bool>?)

SyntaxNode.cs

Finds the closest node of type TNode, starting with this one.

public TNode? FirstAncestorOrSelf<TNode>(Func<TNode, bool>? predicate = null) where TNode : SyntaxNode

Type parameters

TNode

Parameters

predicate Func<TNode, bool>?

Returns

TNode?

GetAnnotatedNodes overloads

GetAnnotatedNodes(SyntaxAnnotation)

SyntaxNode.cs

Gets the nodes at or below this one carrying annotation.

public IEnumerable<SyntaxNode> GetAnnotatedNodes(SyntaxAnnotation annotation)
Parameters
annotation SyntaxAnnotation
Returns

IEnumerable<SyntaxNode>

GetAnnotatedNodes(string)

SyntaxNode.cs

Gets the nodes at or below this one carrying an annotation of the given kind.

public IEnumerable<SyntaxNode> GetAnnotatedNodes(string annotationKind)
Parameters
annotationKind string
Returns

IEnumerable<SyntaxNode>

GetAnnotatedNodesAndTokens overloads

GetAnnotatedNodesAndTokens(SyntaxAnnotation)

SyntaxNode.cs

Gets everything at or below this node carrying annotation.

public IEnumerable<SyntaxNodeOrToken> GetAnnotatedNodesAndTokens(SyntaxAnnotation annotation)
Parameters
annotation SyntaxAnnotation
Returns

IEnumerable<SyntaxNodeOrToken>

Exceptions
ArgumentNullException
annotation is null.

GetAnnotatedNodesAndTokens(string)

SyntaxNode.cs

Gets everything at or below this node carrying an annotation of the given kind.

public IEnumerable<SyntaxNodeOrToken> GetAnnotatedNodesAndTokens(string annotationKind)
Parameters
annotationKind string
Returns

IEnumerable<SyntaxNodeOrToken>

Exceptions
ArgumentNullException
annotationKind is null.

GetAnnotatedTokens overloads

GetAnnotatedTokens(SyntaxAnnotation)

SyntaxNode.cs

Gets the tokens below this node carrying annotation.

public IEnumerable<SyntaxToken> GetAnnotatedTokens(SyntaxAnnotation annotation)
Parameters
annotation SyntaxAnnotation
Returns

IEnumerable<SyntaxToken>

GetAnnotatedTokens(string)

SyntaxNode.cs

Gets the tokens below this node carrying an annotation of the given kind.

public IEnumerable<SyntaxToken> GetAnnotatedTokens(string annotationKind)
Parameters
annotationKind string
Returns

IEnumerable<SyntaxToken>

GetAnnotatedTrivia overloads

GetAnnotatedTrivia(SyntaxAnnotation)

SyntaxNode.cs

Gets the trivia below this node carrying annotation.

public IEnumerable<SyntaxTrivia> GetAnnotatedTrivia(SyntaxAnnotation annotation)
Parameters
annotation SyntaxAnnotation
Returns

IEnumerable<SyntaxTrivia>

Exceptions
ArgumentNullException
annotation is null.

GetAnnotatedTrivia(string)

SyntaxNode.cs

Gets the trivia below this node carrying an annotation of the given kind.

public IEnumerable<SyntaxTrivia> GetAnnotatedTrivia(string annotationKind)
Parameters
annotationKind string
Returns

IEnumerable<SyntaxTrivia>

Exceptions
ArgumentNullException
annotationKind is null.

GetAnnotations overloads

GetAnnotations()

SyntaxNode.cs

Gets all the annotations of this node itself.

public IEnumerable<SyntaxAnnotation> GetAnnotations()

GetAnnotations(string)

SyntaxNode.cs

Gets the annotations of this node itself, of the given kind.

public IEnumerable<SyntaxAnnotation> GetAnnotations(string annotationKind)
Parameters
annotationKind string
Returns

IEnumerable<SyntaxAnnotation>

Exceptions
ArgumentNullException
annotationKind is null.

GetDiagnostics()

SyntaxNode.cs

Gets the diagnostics at or below this node.

public IEnumerable<Diagnostic> GetDiagnostics()

Returns

IEnumerable<Diagnostic>

Remarks

A node that is part of a tree asks the tree, which may know of diagnostics the nodes do not carry, such as a key defined twice in a document.

GetFirstToken()

SyntaxNode.cs

Returns the first token below this node, or SyntaxToken.None when it has none.

public SyntaxToken GetFirstToken()

GetLastToken()

SyntaxNode.cs

Returns the last token below this node, or SyntaxToken.None when it has none.

public SyntaxToken GetLastToken()

GetLeadingTrivia()

SyntaxNode.cs

Gets the trivia in front of this node, which is the leading trivia of its first token.

public SyntaxTriviaList GetLeadingTrivia()

GetRed<T>(T?, int)

SyntaxNode.cs

Returns the child in slot, creating it once and caching it in field.

protected T? GetRed<T>(ref T? field, int slot) where T : SyntaxNode

Type parameters

T

Parameters

field T?
slot int

Returns

T?

GetRedAtZero<T>(T?)

SyntaxNode.cs

Returns the child in slot 0, creating it once and caching it in field.

protected T? GetRedAtZero<T>(ref T? field) where T : SyntaxNode

Type parameters

T

Parameters

field T?

Returns

T?

GetTrailingTrivia()

SyntaxNode.cs

Gets the trivia after this node, which is the trailing trivia of its last token.

public SyntaxTriviaList GetTrailingTrivia()

HasAnnotation(SyntaxAnnotation?)

SyntaxNode.cs

Determines whether this node itself carries annotation.

public bool HasAnnotation(SyntaxAnnotation? annotation)

Parameters

annotation SyntaxAnnotation?

Returns

bool

HasAnnotations(string)

SyntaxNode.cs

Determines whether this node itself carries an annotation of the given kind.

public bool HasAnnotations(string annotationKind)

Parameters

annotationKind string

Returns

bool

IsEquivalentTo(SyntaxNode?)

SyntaxNode.cs

Determines whether the two nodes have the same structure and text, ignoring their positions.

public bool IsEquivalentTo(SyntaxNode? other)

Parameters

other SyntaxNode?

Returns

bool

IsIncrementallyIdenticalTo(SyntaxNode?)

SyntaxNode.cs

Determines whether the two nodes are backed by the very same immutable node, which happens when one tree was derived from the other and this part of it was left untouched.

public bool IsIncrementallyIdenticalTo(SyntaxNode? other)

Parameters

other SyntaxNode?

Returns

bool

ToFullString()

SyntaxNode.cs

Returns the text of this node, including the trivia at its outer edges.

public virtual string ToFullString()

Returns

string

ToString()

SyntaxNode.cs

Returns the text of this node, excluding the trivia at its outer edges.

public override string ToString()

Returns

string

WriteTo(TextWriter)

SyntaxNode.cs

Writes the text of this node, including the trivia at its outer edges.

public void WriteTo(TextWriter writer)

Parameters

writer TextWriter

Exceptions

ArgumentNullException
writer is null.