TomlSyntaxTree Class

A parsed TOML document.

public sealed class TomlSyntaxTree : SyntaxTree

Remarks

Parsing never throws and never gives up: whatever the text says, the tree reproduces it exactly, and anything wrong with it is reported through TomlSyntaxTree.GetDiagnostics -- from a missing bracket to a key defined twice.

What breaks the grammar is carried by the nodes, and is what SyntaxNode.ContainsDiagnostics tells. Whether a key or a table is defined twice, or a value extended after the fact, depends on the whole document instead, so the tree works it out from its root the first time it is asked. A node carries none of these, and neither does a node that is not part of a tree.

A tree made from a root with TomlSyntaxTree.Create or TomlSyntaxTree.WithRoot reports the diagnostics of its text instead, which it parses again the first time it is asked: an edit can build nodes that do not read back as themselves, such as a comment that hides the bracket after it, and the options of the tree can be those of another version than the nodes were parsed with.

Example

var tree = TomlSyntaxTree.ParseText("[server]\nport = 8080\n");
var port = (TomlIntegerSyntax)tree.GetRoot().Tables.Single().Properties.Single().Value;
Assert.Equal(8080, port.Value);
Inheritance
SyntaxTree TomlSyntaxTree

Properties

NameDescription
FilePath
OptionsGets the options the text was parsed with.
public override string? FilePath { get; }

Property value

string?

No documentation is available.

Gets the options the text was parsed with.

public TomlParseOptions Options { get; }

Property value

TomlParseOptions

Methods

NameDescription
Create(TomlDocumentSyntax, TomlParseOptions?, string?)Creates a tree over root, taking its text from the root itself.
Create(TomlDocumentSyntax, string?)Creates a tree over root, taking its text from the root itself.
GetChanges(TomlSyntaxTree)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, including the keys and tables it defines twice.
GetDiagnostics(SyntaxToken)Gets the diagnostics on token and the trivia around it.
GetDiagnostics(SyntaxTrivia)Gets the diagnostics on trivia.
GetRoot()Gets the root of the tree.
GetText()
IsEquivalentTo(TomlSyntaxTree?)Determines whether the two trees have the same structure and text.
ParseText(SourceText, TomlParseOptions?, string?)Parses text.
ParseText(SourceText, string?)Parses text as the latest version of TOML.
ParseText(string, TomlParseOptions?, string?)Parses text.
ParseText(string, string?)Parses text as the latest version of TOML.
WithChangedText(SourceText)Returns a tree over newText, parsed with the same options.
WithChanges(TextChange[])Returns a tree over this text with changes applied.
WithChanges(IEnumerable<TextChange>)Returns a tree over this text with changes applied.
WithRoot(TomlDocumentSyntax)Returns a tree whose root is root.

Create overloads

Create(TomlDocumentSyntax, TomlParseOptions?, string?)

TomlSyntaxTree.cs

Creates a tree over root, taking its text from the root itself.

public static TomlSyntaxTree Create(TomlDocumentSyntax root, TomlParseOptions? options, string? path = null)
Parameters
root TomlDocumentSyntax
options TomlParseOptions?
path string?
Returns

TomlSyntaxTree

Exceptions
ArgumentNullException
root is null.
Remarks

root becomes the root of the tree when it is not part of another one yet. The nodes are kept, but the diagnostics are those of the text: it is parsed again with options the first time they are asked for, so that they are right even when an edit built nodes that do not read back as themselves, or when the nodes were parsed as another version of TOML.

Create(TomlDocumentSyntax, string?)

TomlSyntaxTree.cs

Creates a tree over root, taking its text from the root itself.

public static TomlSyntaxTree Create(TomlDocumentSyntax root, string? path = null)
Parameters
root TomlDocumentSyntax
path string?
Returns

TomlSyntaxTree

Exceptions
ArgumentNullException
root is null.
Remarks

root becomes the root of the tree when it is not part of another one yet. The nodes are kept, but the diagnostics are those of the text: it is parsed again the first time they are asked for, so that they are right even when an edit built nodes that do not read back as themselves.

GetChanges(TomlSyntaxTree)

TomlSyntaxTree.cs

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

public IReadOnlyList<TextChange> GetChanges(TomlSyntaxTree oldTree)

Parameters

oldTree TomlSyntaxTree

Returns

IReadOnlyList<TextChange>

Exceptions

ArgumentNullException
oldTree is null.

GetDiagnostics overloads

GetDiagnostics()

TomlSyntaxTree.cs

Gets every diagnostic in the tree, in source order.

public override IReadOnlyList<Diagnostic> GetDiagnostics()

GetDiagnostics(SyntaxNode)

TomlSyntaxTree.cs

Gets the diagnostics at or below node, including the keys and tables it defines twice.

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

IEnumerable<Diagnostic>

Exceptions
ArgumentNullException
node is null.

GetDiagnostics(SyntaxToken)

TomlSyntaxTree.cs

Gets the diagnostics on token and the trivia around it.

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

IEnumerable<Diagnostic>

GetDiagnostics(SyntaxTrivia)

TomlSyntaxTree.cs

Gets the diagnostics on trivia.

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

IEnumerable<Diagnostic>

Gets the root of the tree.

public TomlDocumentSyntax GetRoot()
public override SourceText GetText()

Returns

SourceText

No documentation is available.

IsEquivalentTo(TomlSyntaxTree?)

TomlSyntaxTree.cs

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

public bool IsEquivalentTo(TomlSyntaxTree? other)

Parameters

other TomlSyntaxTree?

Returns

bool

ParseText overloads

ParseText(SourceText, TomlParseOptions?, string?)

TomlSyntaxTree.cs

Parses text.

public static TomlSyntaxTree ParseText(SourceText text, TomlParseOptions? options, string? path = null)
Parameters
text SourceText
options TomlParseOptions?
path string?
Returns

TomlSyntaxTree

Exceptions
ArgumentNullException
text is null.

ParseText(SourceText, string?)

TomlSyntaxTree.cs

Parses text as the latest version of TOML.

public static TomlSyntaxTree ParseText(SourceText text, string? path = null)
Parameters
text SourceText
path string?
Returns

TomlSyntaxTree

Exceptions
ArgumentNullException
text is null.

ParseText(string, TomlParseOptions?, string?)

TomlSyntaxTree.cs

Parses text.

public static TomlSyntaxTree ParseText(string text, TomlParseOptions? options, string? path = null)
Parameters
text string
The TOML document to read.
options TomlParseOptions?
How to read it, or null for TomlParseOptions.Default.
path string?
Where the text came from, for the diagnostics to refer to.
Returns

TomlSyntaxTree

Exceptions
ArgumentNullException
text is null.

ParseText(string, string?)

TomlSyntaxTree.cs

Parses text as the latest version of TOML.

public static TomlSyntaxTree ParseText(string text, string? path = null)
Parameters
text string
The TOML document to read.
path string?
Where the text came from, for the diagnostics to refer to.
Returns

TomlSyntaxTree

Exceptions
ArgumentNullException
text is null.

WithChangedText(SourceText)

TomlSyntaxTree.cs

Returns a tree over newText, parsed with the same options.

public TomlSyntaxTree WithChangedText(SourceText newText)

Parameters

newText SourceText

Returns

TomlSyntaxTree

Exceptions

ArgumentNullException
newText is null.

WithChanges overloads

WithChanges(TextChange[])

TomlSyntaxTree.cs

Returns a tree over this text with changes applied.

public TomlSyntaxTree WithChanges(params TextChange[] changes)
Parameters
changes TextChange[]
Returns

TomlSyntaxTree

Exceptions
ArgumentNullException
changes is null.

WithChanges(IEnumerable<TextChange>)

TomlSyntaxTree.cs

Returns a tree over this text with changes applied.

public TomlSyntaxTree WithChanges(IEnumerable<TextChange> changes)
Parameters
changes IEnumerable<TextChange>
Returns

TomlSyntaxTree

Exceptions
ArgumentNullException
changes is null.

WithRoot(TomlDocumentSyntax)

TomlSyntaxTree.cs

Returns a tree whose root is root.

public TomlSyntaxTree WithRoot(TomlDocumentSyntax root)

Parameters

root TomlDocumentSyntax

Returns

TomlSyntaxTree

Exceptions

ArgumentNullException
root is null.