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 SyntaxNodeRemarks
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
| Name | Description |
|---|---|
| ContainsAnnotations | Gets a value indicating whether this node or anything below it carries an annotation. |
| ContainsDiagnostics | Gets a value indicating whether this node or anything below it carries a diagnostic. |
| ContainsSkippedText | Gets a value indicating whether this node or anything below it came from text the parser could not use. |
| FullSpan | Gets the range this node covers, including the trivia at its outer edges. |
| IsMissing | Gets a value indicating whether the parser inserted this node rather than reading it from the text. |
| Parent | Gets the node this one is a child of, or null when it is the root. |
| RawKind | Gets the language-specific kind of this node as a plain integer. |
| Span | Gets the range this node covers, excluding the trivia at its outer edges. |
| SpanStart | Gets the character offset of the start of this node, excluding its leading trivia. |
| SyntaxTree | Gets the tree this node belongs to, or null when it was built rather than parsed. |
ContainsAnnotations
SyntaxNode.csContainsDiagnostics
SyntaxNode.csContainsSkippedText
SyntaxNode.csFullSpan
SyntaxNode.csIsMissing
SyntaxNode.csParent
SyntaxNode.csGets the node this one is a child of, or null when it is the root.
public SyntaxNode? Parent { get; }Property value
RawKind
SyntaxNode.csSpan
SyntaxNode.csSpanStart
SyntaxNode.csSyntaxTree
SyntaxNode.csGets the tree this node belongs to, or null when it was built rather than parsed.
public SyntaxTree? SyntaxTree { get; }Property value
Methods
| Name | Description |
|---|---|
| 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.csGets the nodes this one sits inside, closest first.
public IEnumerable<SyntaxNode> Ancestors()Returns
AncestorsAndSelf()
SyntaxNode.csGets this node and the nodes it sits inside, closest first.
public IEnumerable<SyntaxNode> AncestorsAndSelf()Returns
ChildNodes()
SyntaxNode.csGets the child nodes of this node, skipping the tokens among them.
public IEnumerable<SyntaxNode> ChildNodes()Returns
ChildNodesAndTokens()
SyntaxNode.csGets the children of this node, with the lists among them flattened away.
public ChildSyntaxList ChildNodesAndTokens()Returns
ChildThatContainsPosition(int)
SyntaxNode.csGets the child whose full span contains position.
public SyntaxNodeOrToken ChildThatContainsPosition(int position)ChildTokens()
SyntaxNode.csGets the child tokens of this node, skipping the nodes among them.
public IEnumerable<SyntaxToken> ChildTokens()Returns
Contains(SyntaxNode?)
SyntaxNode.csDetermines whether node is this node or sits below it.
public bool Contains(SyntaxNode? node)DescendantNodes overloads
DescendantNodes(TextSpan, Func<SyntaxNode, bool>?)
SyntaxNode.csGets every node below this one that touches span, parents before children.
public IEnumerable<SyntaxNode> DescendantNodes(TextSpan span, Func<SyntaxNode, bool>? descendIntoChildren = null)DescendantNodes(Func<SyntaxNode, bool>?)
SyntaxNode.csGets every node below this one, parents before children.
public IEnumerable<SyntaxNode> DescendantNodes(Func<SyntaxNode, bool>? descendIntoChildren = null)Parameters
descendIntoChildrenFunc<SyntaxNode, bool>?- Decides whether to look inside a node. Everything is looked inside when omitted.
Returns
DescendantNodesAndSelf overloads
DescendantNodesAndSelf(TextSpan, Func<SyntaxNode, bool>?)
SyntaxNode.csGets this node and every node below it that touches span, parents before children.
public IEnumerable<SyntaxNode> DescendantNodesAndSelf(TextSpan span, Func<SyntaxNode, bool>? descendIntoChildren = null)DescendantNodesAndSelf(Func<SyntaxNode, bool>?)
SyntaxNode.csGets this node and every node below it, parents before children.
public IEnumerable<SyntaxNode> DescendantNodesAndSelf(Func<SyntaxNode, bool>? descendIntoChildren = null)DescendantNodesAndTokens overloads
DescendantNodesAndTokens(TextSpan, Func<SyntaxNode, bool>?)
SyntaxNode.csGets every node and token below this one that touches span, in source order.
public IEnumerable<SyntaxNodeOrToken> DescendantNodesAndTokens(TextSpan span, Func<SyntaxNode, bool>? descendIntoChildren = null)DescendantNodesAndTokens(Func<SyntaxNode, bool>?)
SyntaxNode.csGets every node and token below this one, in source order.
public IEnumerable<SyntaxNodeOrToken> DescendantNodesAndTokens(Func<SyntaxNode, bool>? descendIntoChildren = null)DescendantTokens overloads
DescendantTokens(TextSpan, Func<SyntaxNode, bool>?)
SyntaxNode.csGets every token below this one that touches span, in source order.
public IEnumerable<SyntaxToken> DescendantTokens(TextSpan span, Func<SyntaxNode, bool>? descendIntoChildren = null)DescendantTokens(Func<SyntaxNode, bool>?)
SyntaxNode.csGets every token below this one, in source order.
public IEnumerable<SyntaxToken> DescendantTokens(Func<SyntaxNode, bool>? descendIntoChildren = null)DescendantTrivia overloads
DescendantTrivia(TextSpan, Func<SyntaxNode, bool>?)
SyntaxNode.csGets every trivia below this node that touches span, in source order.
public IEnumerable<SyntaxTrivia> DescendantTrivia(TextSpan span, Func<SyntaxNode, bool>? descendIntoChildren = null)DescendantTrivia(Func<SyntaxNode, bool>?)
SyntaxNode.csGets every trivia below this node, in source order.
public IEnumerable<SyntaxTrivia> DescendantTrivia(Func<SyntaxNode, bool>? descendIntoChildren = null)FindNode(TextSpan, bool)
SyntaxNode.csFinds the smallest node whose full span covers span.
public SyntaxNode FindNode(TextSpan span, bool getInnermostNodeForTie = false)Parameters
spanTextSpan- A range inside this node.
getInnermostNodeForTiebool- When several nested nodes cover exactly the same range, whether to return the innermost rather than the outermost.
Returns
Exceptions
ArgumentOutOfRangeExceptionspanis not inside this node.
FindToken(int)
SyntaxNode.csFinds the token at position.
public SyntaxToken FindToken(int position)Parameters
positionint- A position inside this node, or its very end.
Returns
Exceptions
ArgumentOutOfRangeExceptionpositionis outside this node.
FindTrivia(int)
SyntaxNode.csFinds the trivia at position, or nothing when the position falls on a token's own text.
public SyntaxTrivia FindTrivia(int position)FirstAncestorOrSelf<TNode>(Func<TNode, bool>?)
SyntaxNode.csFinds the closest node of type TNode, starting with this one.
public TNode? FirstAncestorOrSelf<TNode>(Func<TNode, bool>? predicate = null) where TNode : SyntaxNodeGetAnnotatedNodes overloads
GetAnnotatedNodes(SyntaxAnnotation)
SyntaxNode.csGets the nodes at or below this one carrying annotation.
public IEnumerable<SyntaxNode> GetAnnotatedNodes(SyntaxAnnotation annotation)GetAnnotatedNodes(string)
SyntaxNode.csGets the nodes at or below this one carrying an annotation of the given kind.
public IEnumerable<SyntaxNode> GetAnnotatedNodes(string annotationKind)GetAnnotatedNodesAndTokens overloads
GetAnnotatedNodesAndTokens(SyntaxAnnotation)
SyntaxNode.csGets everything at or below this node carrying annotation.
public IEnumerable<SyntaxNodeOrToken> GetAnnotatedNodesAndTokens(SyntaxAnnotation annotation)Parameters
annotationSyntaxAnnotation
Returns
IEnumerable<SyntaxNodeOrToken>
Exceptions
ArgumentNullExceptionannotationisnull.
GetAnnotatedNodesAndTokens(string)
SyntaxNode.csGets everything at or below this node carrying an annotation of the given kind.
public IEnumerable<SyntaxNodeOrToken> GetAnnotatedNodesAndTokens(string annotationKind)Parameters
annotationKindstring
Returns
IEnumerable<SyntaxNodeOrToken>
Exceptions
ArgumentNullExceptionannotationKindisnull.
GetAnnotatedTokens overloads
GetAnnotatedTokens(SyntaxAnnotation)
SyntaxNode.csGets the tokens below this node carrying annotation.
public IEnumerable<SyntaxToken> GetAnnotatedTokens(SyntaxAnnotation annotation)GetAnnotatedTokens(string)
SyntaxNode.csGets the tokens below this node carrying an annotation of the given kind.
public IEnumerable<SyntaxToken> GetAnnotatedTokens(string annotationKind)GetAnnotatedTrivia overloads
GetAnnotatedTrivia(SyntaxAnnotation)
SyntaxNode.csGets the trivia below this node carrying annotation.
public IEnumerable<SyntaxTrivia> GetAnnotatedTrivia(SyntaxAnnotation annotation)GetAnnotatedTrivia(string)
SyntaxNode.csGets the trivia below this node carrying an annotation of the given kind.
public IEnumerable<SyntaxTrivia> GetAnnotatedTrivia(string annotationKind)GetAnnotations overloads
GetAnnotations()
SyntaxNode.csGets all the annotations of this node itself.
public IEnumerable<SyntaxAnnotation> GetAnnotations()GetAnnotations(string)
SyntaxNode.csGets the annotations of this node itself, of the given kind.
public IEnumerable<SyntaxAnnotation> GetAnnotations(string annotationKind)GetDiagnostics()
SyntaxNode.csGets the diagnostics at or below this node.
public IEnumerable<Diagnostic> GetDiagnostics()Returns
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.csReturns the first token below this node, or SyntaxToken.None when it has none.
public SyntaxToken GetFirstToken()Returns
GetLastToken()
SyntaxNode.csReturns the last token below this node, or SyntaxToken.None when it has none.
public SyntaxToken GetLastToken()Returns
GetLeadingTrivia()
SyntaxNode.csGets the trivia in front of this node, which is the leading trivia of its first token.
public SyntaxTriviaList GetLeadingTrivia()Returns
GetRed<T>(T?, int)
SyntaxNode.csReturns the child in slot, creating it once and caching it in field.
protected T? GetRed<T>(ref T? field, int slot) where T : SyntaxNodeGetRedAtZero<T>(T?)
SyntaxNode.csReturns the child in slot 0, creating it once and caching it in field.
protected T? GetRedAtZero<T>(ref T? field) where T : SyntaxNodeType parameters
T
Parameters
fieldT?
Returns
T?
GetTrailingTrivia()
SyntaxNode.csGets the trivia after this node, which is the trailing trivia of its last token.
public SyntaxTriviaList GetTrailingTrivia()Returns
HasAnnotation(SyntaxAnnotation?)
SyntaxNode.csDetermines whether this node itself carries annotation.
public bool HasAnnotation(SyntaxAnnotation? annotation)HasAnnotations(string)
SyntaxNode.csIsEquivalentTo(SyntaxNode?)
SyntaxNode.csDetermines whether the two nodes have the same structure and text, ignoring their positions.
public bool IsEquivalentTo(SyntaxNode? other)IsIncrementallyIdenticalTo(SyntaxNode?)
SyntaxNode.csDetermines 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)ToFullString()
SyntaxNode.csToString()
SyntaxNode.csWriteTo(TextWriter)
SyntaxNode.csWrites the text of this node, including the trivia at its outer edges.
public void WriteTo(TextWriter writer)