-
-
Notifications
You must be signed in to change notification settings - Fork 723
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reworked the syntax rewriter and introduced a syntax navigator. (#5078)
- Loading branch information
1 parent
b61e97b
commit a8add2f
Showing
52 changed files
with
2,815 additions
and
1,543 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
223 changes: 155 additions & 68 deletions
223
src/HotChocolate/Language/src/Language.Utf8/Properties/LangUtf8Resources.Designer.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/HotChocolate/Language/src/Language.Visitors/Contracts/INavigatorContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace HotChocolate.Language.Visitors; | ||
|
||
/// <summary> | ||
/// A visitor context that contains a syntax navigator. | ||
/// </summary> | ||
public interface INavigatorContext : ISyntaxVisitorContext | ||
{ | ||
/// <summary> | ||
/// Gets the associated <see cref="ISyntaxNavigator" /> from the current context. | ||
/// </summary> | ||
ISyntaxNavigator Navigator { get; } | ||
} | ||
|
||
/// <summary> | ||
/// A base implementation of the visitor context that contains a syntax navigator. | ||
/// </summary> | ||
public class NavigatorContext : INavigatorContext | ||
{ | ||
/// <inheritdoc cref="INavigatorContext.Navigator"/> | ||
public ISyntaxNavigator Navigator { get; } = new DefaultSyntaxNavigator(); | ||
} |
94 changes: 94 additions & 0 deletions
94
src/HotChocolate/Language/src/Language.Visitors/Contracts/ISyntaxNavigator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace HotChocolate.Language.Visitors; | ||
|
||
/// <summary> | ||
/// The syntax navigator keeps track of the syntax path that has been traversed and | ||
/// allows to access the nodes in the path in a streamlined way. | ||
/// </summary> | ||
public interface ISyntaxNavigator | ||
{ | ||
/// <summary> | ||
/// Returns the immediate parent of the current Syntax Node | ||
/// </summary> | ||
ISyntaxNode? Parent { get; } | ||
|
||
/// <summary> | ||
/// Returns the count of items in the current path. | ||
/// </summary> | ||
int Count { get; } | ||
|
||
/// <summary> | ||
/// Adds a syntax node to the Syntax Navigator to record the parent | ||
/// of the Syntax Node being visited. | ||
/// </summary> | ||
/// <param name="node">The parent syntax node to be added to the Syntax Navigator</param> | ||
void Push(ISyntaxNode node); | ||
|
||
/// <summary> | ||
/// Removes the current parent node from the Syntax Navigator. | ||
/// </summary> | ||
/// <returns>The removed parent node.</returns> | ||
/// <exception cref="InvalidOperationException">Navigator is empty.</exception> | ||
ISyntaxNode Pop(); | ||
|
||
/// <summary> | ||
/// Returns the current parent node from the Syntax Navigator without removing it. | ||
/// </summary> | ||
/// <returns>The removed parent node.</returns> | ||
/// <exception cref="InvalidOperationException">Navigator is empty.</exception> | ||
ISyntaxNode Peek(); | ||
|
||
/// <summary> | ||
/// Returns the current parent node from the Syntax Navigator without removing it. | ||
/// </summary> | ||
/// <param name="count">The ancestor level.</param> | ||
/// <returns>The removed parent node.</returns> | ||
/// <exception cref="InvalidOperationException">Navigator is empty.</exception> | ||
ISyntaxNode Peek(int count); | ||
|
||
/// <summary> | ||
/// Attempts to remove the current parent node from the Syntax Navigator. | ||
/// </summary> | ||
/// <param name="node">The removed parent node.</param> | ||
/// <returns>True when a syntax node was successfully removed from the Syntax Navigator. | ||
/// False when a syntax node was not removed.</returns> | ||
bool TryPop([NotNullWhen(true)] out ISyntaxNode? node); | ||
|
||
/// <summary> | ||
/// Attempts to return the current parent node from the Syntax Navigator without removing it. | ||
/// </summary> | ||
/// <param name="node">The removed parent node.</param> | ||
/// <returns>True when a syntax node was successfully removed from the Syntax Navigator. | ||
/// False when a syntax node was not removed.</returns> | ||
bool TryPeek([NotNullWhen(true)] out ISyntaxNode? node); | ||
|
||
/// <summary> | ||
/// Returns the first ancestor of the provided <see cref="TNode" /> type. | ||
/// </summary> | ||
/// <typeparam name="TNode">The type of syntax node to be returned.</typeparam> | ||
/// <returns>The matching first ancestor or null if no match is found.</returns> | ||
TNode? GetAncestor<TNode>() | ||
where TNode : ISyntaxNode; | ||
|
||
/// <summary> | ||
/// Returns all ancestors of the provided <see cref="TNode" /> type. | ||
/// </summary> | ||
/// <typeparam name="TNode">The type of syntax nodes to be returned.</typeparam> | ||
/// <returns>A collection of Syntax Nodes of type <see cref="TNode" /></returns> | ||
IEnumerable<TNode> GetAncestors<TNode>() | ||
where TNode : ISyntaxNode; | ||
|
||
/// <summary> | ||
/// Creates a Schema Coordinate from the current path. | ||
/// </summary> | ||
/// <returns></returns> | ||
/// <exception cref="InvalidOperationException"> | ||
/// If the path does not allow to create a Schema Coordinate. | ||
/// For instance, if traversing an executable document it is not possible to create a | ||
/// Schema Coordinate. | ||
/// </exception> | ||
SchemaCoordinateNode CreateCoordinate(); | ||
} |
21 changes: 21 additions & 0 deletions
21
src/HotChocolate/Language/src/Language.Visitors/Contracts/ISyntaxRewriter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace HotChocolate.Language.Visitors; | ||
|
||
/// <summary> | ||
/// Represents a syntax rewriter. A syntax rewriter is a visitor that creates a new syntax tree | ||
/// from the passed in syntax tree. | ||
/// </summary> | ||
/// <typeparam name="TContext"> | ||
/// The context type. | ||
/// </typeparam> | ||
public interface ISyntaxRewriter<in TContext> where TContext : ISyntaxVisitorContext | ||
{ | ||
/// <summary> | ||
/// Rewrite the syntax node. | ||
/// </summary> | ||
/// <param name="node">The syntax node that shall be rewritten.</param> | ||
/// <param name="context">The visitor context.</param> | ||
/// <returns> | ||
/// Returns the rewritten <see cref="ISyntaxNode"/>. | ||
/// </returns> | ||
ISyntaxNode Rewrite(ISyntaxNode node, TContext context); | ||
} |
8 changes: 0 additions & 8 deletions
8
src/HotChocolate/Language/src/Language.Visitors/Contracts/ISyntaxVisitor.cs
This file was deleted.
Oops, something went wrong.
7 changes: 2 additions & 5 deletions
7
src/HotChocolate/Language/src/Language.Visitors/Contracts/ISyntaxVisitor~1.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,6 @@ | ||
namespace HotChocolate.Language.Visitors; | ||
|
||
public interface ISyntaxVisitor<TContext> | ||
where TContext : ISyntaxVisitorContext | ||
public interface ISyntaxVisitor<in TContext> where TContext : ISyntaxVisitorContext | ||
{ | ||
ISyntaxVisitorAction Visit( | ||
ISyntaxNode node, | ||
TContext context); | ||
ISyntaxVisitorAction Visit(ISyntaxNode node, TContext context); | ||
} |
Oops, something went wrong.