Skip to content

Commit

Permalink
Reworked the syntax rewriter and introduced a syntax navigator. (#5078)
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-psaltis committed May 27, 2022
1 parent b61e97b commit a8add2f
Show file tree
Hide file tree
Showing 52 changed files with 2,815 additions and 1,543 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using HotChocolate.Execution.Processing;
using HotChocolate.Language;
using HotChocolate.Language.Visitors;
using HotChocolate.Resolvers;
using HotChocolate.Types;
using HotChocolate.Types.Pagination;
Expand Down Expand Up @@ -114,8 +115,7 @@ private IReadOnlyList<ISelectionNode> CollectSelection(SelectionOptimizerContext
{
foreach (ISelectionNode? nodeField in edgeSubFieldNode.SelectionSet.Selections)
{
selections.Add(
CloneSelectionSetVisitor.Default.CloneSelectionNode(nodeField));
selections.Add(_cloneSelectionSetRewriter.Rewrite(nodeField));
}
}
}
Expand All @@ -131,7 +131,7 @@ private IReadOnlyList<ISelectionNode> CollectSelection(SelectionOptimizerContext
{
foreach (ISelectionNode? nodeField in itemSelection.SelectionSet!.Selections)
{
selections.Add(CloneSelectionSetVisitor.Default.CloneSelectionNode(nodeField));
selections.Add(_cloneSelectionSetRewriter.Rewrite(nodeField));
}
}
}
Expand All @@ -145,27 +145,14 @@ private IReadOnlyList<ISelectionNode> CollectSelection(SelectionOptimizerContext
{
foreach (ISelectionNode? nodeField in nodeSelection.SelectionSet!.Selections)
{
selections.Add(CloneSelectionSetVisitor.Default.CloneSelectionNode(nodeField));
selections.Add(_cloneSelectionSetRewriter.Rewrite(nodeField));
}
}
}

private sealed class CloneSelectionSetVisitor : QuerySyntaxRewriter<object>
{
private static readonly object _context = new();

protected override SelectionSetNode RewriteSelectionSet(
SelectionSetNode node,
object context)
{
return new(base.RewriteSelectionSet(node, context).Selections);
}

public ISelectionNode CloneSelectionNode(ISelectionNode selection)
{
return RewriteSelection(selection, _context);
}

public static readonly CloneSelectionSetVisitor Default = new();
}
private static readonly ISyntaxRewriter<ISyntaxVisitorContext> _cloneSelectionSetRewriter =
SyntaxRewriter.Create(
n => n.Kind is SyntaxKind.SelectionSet
? new SelectionSetNode(((SelectionSetNode)n).Selections)
: n);
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,22 +1,121 @@
<?xml version="1.0" encoding="utf-8"?>

<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">

<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>1.3</value>
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="GraphQLData_Empty" xml:space="preserve">
<value>The graphQLData cannot be empty.</value>
Expand All @@ -30,9 +129,6 @@
<data name="Parser_InvalidToken" xml:space="preserve">
<value>Expected a `{0}`-token, but found a `{1}`-token.</value>
</data>
<data name="QuerySyntaxRewriter_NotSupported" xml:space="preserve">
<value>The given syntax node is not supported by this rewriter.</value>
</data>
<data name="ThrowHelper_Reader_InvalidToken" xml:space="preserve">
<value>Expected a `{0}`-token.</value>
</data>
Expand Down Expand Up @@ -99,4 +195,4 @@
<data name="ThrowHelper_UnexpectedToken" xml:space="preserve">
<value>Unexpected token found `{0}` while expecting a scalar value.</value>
</data>
</root>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,27 @@ public static class Syntax
Utf8GraphQLReader reader) =>
new Utf8GraphQLParser(reader).ParseObjectTypeDefinition();

/// <summary>
/// Parses a GraphQL object type definitions e.g. type Foo { bar: String }
/// </summary>
public static DirectiveDefinitionNode ParseDirectiveDefinition(
string sourceText) =>
Parse(sourceText, parser => parser.ParseDirectiveDefinition());

/// <summary>
/// Parses a GraphQL object type definitions e.g. type Foo { bar: String }
/// </summary>
public static DirectiveDefinitionNode ParseDirectiveDefinition(
ReadOnlySpan<byte> sourceText) =>
Parse(sourceText, parser => parser.ParseDirectiveDefinition());

/// <summary>
/// Parses a GraphQL object type definitions e.g. type Foo { bar: String }
/// </summary>
public static DirectiveDefinitionNode ParseDirectiveDefinition(
Utf8GraphQLReader reader) =>
new Utf8GraphQLParser(reader).ParseDirectiveDefinition();

/// <summary>
/// Parses a GraphQL field selection string e.g. field(arg: "abc")
/// </summary>
Expand Down
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();
}
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();
}
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);
}

This file was deleted.

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);
}

0 comments on commit a8add2f

Please sign in to comment.