Skip to content

Commit 00d0225

Browse files
authored
Basic Emitter/Visitor docs (#892)
1 parent cae0233 commit 00d0225

File tree

1 file changed

+37
-0
lines changed
  • documentation/for-contributors/generators/emitter

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Visitor
2+
3+
To Transform our internal symbol layer to C# code a visitor (`CSharpEmitter.Visitor`) is used.
4+
5+
## State
6+
7+
To communicate produced syntax nodes with the levels above the current one the state variables are used.
8+
Therefore its important to enshure that this state is clear at all times. (Using `AssertClearState` & `ClearState`)
9+
10+
## Depth First
11+
12+
In general we try to push symbols down as far as possible to avoid code duplication and increase testable surface area.
13+
The best example of this that instead of creating identifier tokens / identifier name syntax nodes inline (which would be very easy) we visit the identifier, letting the `VisitIdentifier` function create those, and then take what we need, using a pattern like
14+
15+
```cs
16+
VisitIdentifier(structSymbol.Identifier);
17+
if (_syntaxToken is not { } identifierToken)
18+
throw new InvalidOperationException("Some Identifier was not visited correctly");
19+
ClearState();
20+
```
21+
22+
when a token is required, or
23+
24+
```cs
25+
VisitIdentifier(fieldSymbol.Type.Identifier);
26+
if (_syntax is not IdentifierNameSyntax typeIdentifierSyntax)
27+
throw new InvalidOperationException("Some Identifier was not visited correctly");
28+
ClearState();
29+
```
30+
31+
when a syntax node is required.
32+
33+
## Trivia
34+
35+
The visitor is intended to produce "valid" code, that means it can be formatted using .ToFullString and the resulting code is valid and can be parsed back by roslyn.
36+
In addition we try to avoid elastic trivia wherever possible. For performance reasons we don't want to normalize our trivia / syntax internally, but elastic trivia would be thrown away when doing so, therefore we try to avoid generating elastic trivia in the first place.
37+
Integration tests should test for elastic trivia, although their primary purpose is to check the resulting formatted string.

0 commit comments

Comments
 (0)