@@ -24,7 +24,7 @@ The parser from this library is used in [GraphQL for .NET](https://github.com/gr
2424
2525Preview versions of this package are available on [ GitHub Packages] ( https://github.com/orgs/graphql-dotnet/packages?repo_name=parser ) .
2626
27- ## Lexer
27+ ## 1. Lexer
2828
2929Generates token based on input text. Lexer takes advantage of ` ReadOnlyMemory<char> ` and in most cases
3030does not allocate memory on the managed heap at all.
@@ -38,7 +38,7 @@ var token = Lexer.Lex("\"str\"");
3838Lex method always returns the first token it finds. In this case case the result would look like following.
3939![ lexer example] ( assets/lexer-example.png )
4040
41- ## Parser
41+ ## 2. Parser
4242
4343Parses provided GraphQL expression into AST (abstract syntax tree). Parser also takes advantage of
4444` ReadOnlyMemory<char> ` but still allocates memory for AST.
@@ -61,51 +61,60 @@ By default `ParserOptions.Ignore` is `IgnoreOptions.IgnoreComments` to improve p
6161If you don't need information about tokens locations in the source document, then use ` IgnoreOptions.IgnoreCommentsAndLocations ` .
6262This will maximize the saving of memory allocated in the managed heap for AST.
6363
64- ### Example of json representation of the resulting AST
64+ ## 3. INodeVisitor
6565
66- ``` json
66+ ` INodeVisitor ` provides API to traverse AST of the parsed GraphQL document.
67+ Default implementation of this interface is ` DefaultNodeVisitor ` that
68+ traverses all AST nodes of the provided one. You can inherit from it and
69+ implement your own AST processing algorithm.
70+
71+ For printing SDL from AST, you can use ` SDLWriter<TContext> ` visitor.
72+ This is a highly optimized visitor for asynchronous non-blocking SDL output
73+ into provided ` TextWriter ` . In the majority of cases it does not allocate
74+ memory in the managed heap at all.
75+
76+ You can also find a ` StructureWriter<TContext> ` visitor that prints AST
77+ into the provided ` TextWriter ` as a hierarchy of node types. It can be useful
78+ when debugging for better understanding the AST structure.
79+ Consider GraphQL document
80+
81+ ``` graphql
82+ query a { name age }
83+ ```
84+
85+ After ` StructureWriter ` processing the output text will be
86+
87+ ```
88+ Document
89+ OperationDefinition
90+ Name [a]
91+ SelectionSet
92+ Field
93+ Name [name]
94+ Field
95+ Name [age]
96+ ```
97+
98+ ### Usage
99+
100+ ``` c#
101+ public class Context : IWriteContext
67102{
68- "Definitions" : [{
69- "Directives" : [],
70- "Kind" : 2 ,
71- "Name" : null ,
72- "Operation" : 0 ,
73- "SelectionSet" : {
74- "Kind" : 5 ,
75- "Selections" : [{
76- "Alias" : null ,
77- "Arguments" : [],
78- "Directives" : [],
79- "Kind" : 6 ,
80- "Name" : {
81- "Kind" : 0 ,
82- "Value" : " field" ,
83- "Location" : {
84- "End" : 50 ,
85- "Start" : 31
86- }
87- },
88- "SelectionSet" : null ,
89- "Location" : {
90- "End" : 50 ,
91- "Start" : 31
92- }
93- }],
94- "Location" : {
95- "End" : 50 ,
96- "Start" : 13
97- }
98- },
99- "VariableDefinitions" : null ,
100- "Location" : {
101- "End" : 50 ,
102- "Start" : 13
103- }
104- }],
105- "Kind" : 1 ,
106- "Location" : {
107- "End" : 50 ,
108- "Start" : 13
109- }
103+ public TextWriter Writer { get ; set ; } = new StringWriter ();
104+
105+ public Stack <AST .ASTNode > Parents { get ; set ; } = new Stack <AST .ASTNode >();
106+
107+ public CancellationToken CancellationToken { get ; set ; }
108+ }
109+
110+ public static void Parse (string text )
111+ {
112+ var document = Parser .Parse (text );
113+
114+ var context = new Context ();
115+ var visitor = new SDLWriter <Context >()
116+ await visitor .Visit (document , context );
117+ var rendered = context .Writer .ToString ();
118+ Console .WriteLine (rendered );
110119}
111120```
0 commit comments