Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reworked the syntax rewriter and introduced a syntax navigator. #5078

Merged
merged 28 commits into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
8512379
Add initial implementation for navigation rewriter with rewriters ref…
matt-psaltis May 18, 2022
03a85f6
Remove coordinates used basic naming in tests
matt-psaltis May 18, 2022
538d861
Merge branch 'main' into mp/language-rewriters-primitives
michaelstaib May 18, 2022
0be3934
Feedback fixes
matt-psaltis May 19, 2022
ce327d7
Update src/HotChocolate/Language/src/Language.Rewriters/Contracts/INa…
michaelstaib May 19, 2022
261d418
Update src/HotChocolate/Language/src/Language.Rewriters/Contracts/ISy…
michaelstaib May 19, 2022
d48cf1a
Update src/HotChocolate/Language/src/Language.Rewriters/DefaultSyntax…
michaelstaib May 19, 2022
885d65e
Update src/HotChocolate/Language/src/Language.Rewriters/DefaultSyntax…
michaelstaib May 19, 2022
ce1f47f
Adds TryPop
matt-psaltis May 19, 2022
001c9dc
Fixes GetParent
matt-psaltis May 19, 2022
6c17565
Changes parent to property
matt-psaltis May 19, 2022
00b1c41
Update src/HotChocolate/Language/src/Language.Rewriters/Contracts/ISy…
michaelstaib May 19, 2022
0861b5e
Fixes Pop signature
matt-psaltis May 19, 2022
8534272
Merge branch 'main' into mp/language-rewriters-primitives
michaelstaib May 22, 2022
cbdad60
stated cleanup
michaelstaib May 22, 2022
7add32a
edits
michaelstaib May 24, 2022
236b3be
edits
michaelstaib May 24, 2022
a19bd89
edits
michaelstaib May 27, 2022
11d5663
edits
michaelstaib May 27, 2022
285df51
edits
michaelstaib May 27, 2022
cbc3ae5
edits
michaelstaib May 27, 2022
b8aadea
edits
michaelstaib May 27, 2022
c257728
edits
michaelstaib May 27, 2022
767eb32
edits
michaelstaib May 27, 2022
78a2139
edits
michaelstaib May 27, 2022
0f915f8
edits
michaelstaib May 27, 2022
58a902a
Fixed snapshot
michaelstaib May 27, 2022
7a1c77d
Merge branch 'main' into mp/language-rewriters-primitives
michaelstaib May 27, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace HotChocolate.Language.Rewriters;

/// <summary>
/// A rewriter context that contains a syntax navigator.
/// </summary>
public interface INavigatorContext
michaelstaib marked this conversation as resolved.
Show resolved Hide resolved
{
/// <summary>
/// Gets the associated <see cref="ISyntaxNavigator" /> from the current context.
/// </summary>
ISyntaxNavigator Navigator { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace HotChocolate.Language.Rewriters;

/// <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
matt-psaltis marked this conversation as resolved.
Show resolved Hide resolved
michaelstaib marked this conversation as resolved.
Show resolved Hide resolved
{
/// <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>
/// <param name="node">The removed parent node.</param>
void Pop(out ISyntaxNode node);
matt-psaltis marked this conversation as resolved.
Show resolved Hide resolved

/// <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>
/// 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>
/// Returns the immediate parent of the current Syntax Node
/// </summary>
ISyntaxNode? Parent { get; }
}
michaelstaib marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using static HotChocolate.Language.Rewriters.LangRewritersResources;

namespace HotChocolate.Language.Rewriters;

/// <summary>
/// Represents the default implementation of <see cref="ISyntaxNavigator" />
/// </summary>
public class DefaultSyntaxNavigator : ISyntaxNavigator
matt-psaltis marked this conversation as resolved.
Show resolved Hide resolved
michaelstaib marked this conversation as resolved.
Show resolved Hide resolved
{
private readonly List<ISyntaxNode> _ancestors = new();

/// <inheritdoc cref="ISyntaxNavigator.Push"/>
public void Push(ISyntaxNode node) => _ancestors.Add(node);

/// <inheritdoc cref="ISyntaxNavigator.Pop"/>
public void Pop(out ISyntaxNode node)
{
if (!TryPop(out node!))
{
throw new InvalidOperationException(DefaultSyntaxNavigator_NoAncestors);
matt-psaltis marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// <inheritdoc cref="ISyntaxNavigator.TryPop"/>
public bool TryPop([NotNullWhen(true)] out ISyntaxNode? node)
{
michaelstaib marked this conversation as resolved.
Show resolved Hide resolved
if (_ancestors.Count == 0)
{
node = default;
return false;
}

node = _ancestors[_ancestors.Count - 1];
_ancestors.RemoveAt(_ancestors.Count - 1);
return true;
}

/// <inheritdoc cref="ISyntaxNavigator.GetAncestor{TNode}"/>
public TNode? GetAncestor<TNode>()
where TNode : ISyntaxNode
{
for (var i = _ancestors.Count - 1; i >= 0; i--)
{
if (_ancestors[i] is TNode typedNode)
{
return typedNode;
}
}

return default;
}

/// <inheritdoc cref="ISyntaxNavigator.GetAncestors{TNode}"/>
public IEnumerable<TNode> GetAncestors<TNode>()
where TNode : ISyntaxNode
{
for (var i = _ancestors.Count - 1; i >= 0; i--)
{
if (_ancestors[i] is TNode typedNode)
{
yield return typedNode;
}
}
}

/// <inheritdoc cref="ISyntaxNavigator.Parent"/>
public ISyntaxNode? Parent
{
get
{
if (_ancestors.Count == 0)
{
return null;
}

return _ancestors[_ancestors.Count - 1];
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;

namespace HotChocolate.Language;
namespace HotChocolate.Language.Rewriters;

public class DirectiveQuerySyntaxRewriter
: QuerySyntaxRewriter<DirectiveNode>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace HotChocolate.Language;
namespace HotChocolate.Language.Rewriters;

public static class DocumentRewriterExtensions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<PackageId>HotChocolate.Language.Rewriters</PackageId>
<AssemblyName>HotChocolate.Language.Rewriters</AssemblyName>
<RootNamespace>HotChocolate.Language.Rewriters</RootNamespace>
<Description>This package contains Syntax Rewrite primitives of Hot Chocolate.</Description>
</PropertyGroup>

<ItemGroup>
<None Remove="HotChocolate.Language.Rewriters.csproj.DotSettings" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Language.SyntaxTree\HotChocolate.Language.SyntaxTree.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Update="Properties\LangRewritersResources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>LangRewritersResources.resx</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Properties\LangRewritersResources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>LangRewritersResources.Designer.cs</LastGenOutput>
<CustomToolNamespace>HotChocolate.Language.Rewriters</CustomToolNamespace>
</EmbeddedResource>
</ItemGroup>

</Project>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.