RegexSyntaxTree Class

A parsed regular expression.

public sealed class RegexSyntaxTree : SyntaxTree

Remarks

Parsing never throws. Whatever the pattern says, the tree reproduces it exactly, and anything wrong with it is reported through RegexSyntaxTree.GetDiagnostics.

Inheritance
SyntaxTree RegexSyntaxTree

Properties

NameDescription
CapturesGets the capture groups the pattern declares, in the order the engine numbers them.
DialectGets the dialect the pattern was parsed as.
FilePath
Options
PatternOptionsGets the options in effect at the start of the pattern, including any read from a literal's flags.

Gets the capture groups the pattern declares, in the order the engine numbers them.

public IReadOnlyList<RegexCaptureInfo> Captures { get; }

Gets the dialect the pattern was parsed as.

public RegexDialect Dialect { get; }

Property value

RegexDialect

public override string? FilePath { get; }

Property value

string?

No documentation is available.

public RegexParseOptions Options { get; }

Property value

RegexParseOptions

No documentation is available.

PatternOptions

RegexSyntaxTree.cs

Gets the options in effect at the start of the pattern, including any read from a literal's flags.

public RegexPatternOptions PatternOptions { get; }

Property value

RegexPatternOptions

Methods

NameDescription
GetChanges(SyntaxTree)Returns the edit that turns oldTree's text into this tree's text. The common prefix and suffix are trimmed, so an edit in the middle of a pattern reports only the part that actually differs.
GetDiagnostics()Gets everything the parser had to report, in source order.
GetRoot()Gets the root of the tree.
GetText()
IsEquivalentTo(RegexSyntaxTree?)Compares this tree with other structurally, ignoring extended-mode whitespace and comments. Two patterns parsed as different dialects, or with different options, are never equivalent.
ParseJavaScriptLiteral(string)Parses a JavaScript regular-expression literal such as /a+/giu, reading the flag letters into RegexSyntaxTree.PatternOptions and keeping the delimiters on the root so the literal round-trips.
ParseText(string, RegexDialect)Parses pattern. Never throws; problems are reported as diagnostics.
ParseText(string, RegexParseOptions)Parses pattern. Never throws; problems are reported as diagnostics.
WithChanges(TextChange[])Returns a tree over this pattern with changes applied.
WithChanges(IEnumerable<TextChange>)Returns a tree over this pattern with changes applied.

GetChanges(SyntaxTree)

RegexSyntaxTree.cs

Returns the edit that turns oldTree's text into this tree's text. The common prefix and suffix are trimmed, so an edit in the middle of a pattern reports only the part that actually differs.

public override IReadOnlyList<TextChange> GetChanges(SyntaxTree oldTree)

Parameters

oldTree SyntaxTree

Returns

IReadOnlyList<TextChange>

Exceptions

ArgumentNullException
oldTree is null.

Remarks

This compares the two texts rather than the two trees. Every edit to a pattern reparses it, so the trees never share nodes and a structural comparison would report the whole pattern as replaced.

GetDiagnostics()

RegexSyntaxTree.cs

Gets everything the parser had to report, in source order.

public override IReadOnlyList<Diagnostic> GetDiagnostics()

Gets the root of the tree.

public RegexPatternSyntax GetRoot()
public override SourceText GetText()

Returns

SourceText

No documentation is available.

IsEquivalentTo(RegexSyntaxTree?)

RegexSyntaxTree.cs

Compares this tree with other structurally, ignoring extended-mode whitespace and comments. Two patterns parsed as different dialects, or with different options, are never equivalent.

public bool IsEquivalentTo(RegexSyntaxTree? other)

Parameters

other RegexSyntaxTree?

Returns

bool

Remarks

Identical text is a shortcut only when the options match as well. The same characters read with and without RegexPatternOptions.IgnorePatternWhitespace are different trees -- a space is a term in one and trivia in the other -- so comparing the text alone would call them equivalent.

ParseJavaScriptLiteral(string)

RegexSyntaxTree.cs

Parses a JavaScript regular-expression literal such as /a+/giu, reading the flag letters into RegexSyntaxTree.PatternOptions and keeping the delimiters on the root so the literal round-trips.

public static RegexSyntaxTree ParseJavaScriptLiteral(string literal)

Parameters

literal string

Returns

RegexSyntaxTree

Remarks

The dialect is always RegexDialect.JavaScript: only that dialect has literals. Text that follows the flags is reported as REGEX0204 and kept as skipped text.

ParseText overloads

ParseText(string, RegexDialect)

RegexSyntaxTree.cs

Parses pattern. Never throws; problems are reported as diagnostics.

public static RegexSyntaxTree ParseText(string pattern, RegexDialect dialect)
Parameters
pattern string
dialect RegexDialect
Returns

RegexSyntaxTree

Exceptions
ArgumentNullException
dialect is null.

ParseText(string, RegexParseOptions)

RegexSyntaxTree.cs

Parses pattern. Never throws; problems are reported as diagnostics.

public static RegexSyntaxTree ParseText(string pattern, RegexParseOptions options)
Parameters
pattern string
options RegexParseOptions
Returns

RegexSyntaxTree

Exceptions
ArgumentNullException
options is null.

WithChanges overloads

WithChanges(TextChange[])

RegexSyntaxTree.cs

Returns a tree over this pattern with changes applied.

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

RegexSyntaxTree

Exceptions
ArgumentNullException
changes is null.

WithChanges(IEnumerable<TextChange>)

RegexSyntaxTree.cs

Returns a tree over this pattern with changes applied.

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

RegexSyntaxTree

Exceptions
ArgumentNullException
changes is null.