88using Microsoft . AspNetCore . Mvc ;
99using Microsoft . CodeAnalysis ;
1010using Microsoft . CodeAnalysis . CSharp ;
11+ using Microsoft . CodeAnalysis . VisualBasic ;
1112using Microsoft . Extensions . Logging ;
1213
1314namespace DotNetSyntaxTreeVisualizer . Controllers
@@ -23,65 +24,30 @@ public SyntaxTreeController(ILogger<SyntaxTreeController> logger)
2324 _logger = logger ;
2425 }
2526
26- [ HttpPost ]
27+ [ HttpPost ( "CSharp" ) ] // POST: /SyntaxTree/CSharp
2728 public async Task < SyntaxTreeNode > CSharpPost ( CancellationToken cancellationToken )
2829 {
2930 using var reader = new StreamReader ( Request . Body , Encoding . UTF8 ) ;
3031 string body = await reader . ReadToEndAsync ( ) . ConfigureAwait ( false ) ;
31- SyntaxNode root = await CSharpSyntaxTree . ParseText ( body ) . GetRootAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
32- SyntaxTreeNode myRoot = CreateMyOwnTree ( root ) ;
32+ SyntaxTree tree = CSharpSyntaxTree . ParseText ( body ) ;
33+ SyntaxNode root = await tree . GetRootAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
34+ Compilation compilation = CSharpCompilation . Create ( "HelloWorld" , new [ ] { tree } ) ;
35+ SemanticModel model = compilation . GetSemanticModel ( tree ) ;
36+ SyntaxTreeNode myRoot = SyntaxTreeNode . CreateMyOwnTree ( root , model ) ;
3337 return myRoot ;
3438 }
3539
36- private SyntaxTreeNode CreateMyOwnTree ( SyntaxNodeOrToken nodeOrToken )
40+ [ HttpPost ( "VisualBasic" ) ] // POST: /SyntaxTree/VisualBasic
41+ public async Task < SyntaxTreeNode > VisualBasicPost ( CancellationToken cancellationToken )
3742 {
38- var root = new SyntaxTreeNode ( GetSyntaxNodeOrTokenInformation ( nodeOrToken ) ) ;
39- foreach ( SyntaxNodeOrToken child in nodeOrToken . ChildNodesAndTokens ( ) )
40- {
41- root . AddChild ( CreateMyOwnTree ( child ) ) ;
42- }
43- return root ;
44- }
45-
46- private IDictionary < string , string > GetSyntaxNodeOrTokenInformation ( SyntaxNodeOrToken nodeOrToken )
47- {
48- return nodeOrToken . IsNode
49- ? GetSyntaxInformation ( nodeOrToken . AsNode ( ) )
50- : GetSyntaxInformation ( nodeOrToken . AsToken ( ) ) ;
51- }
52-
53- private IDictionary < string , string > GetSyntaxInformation < T > ( T syntax )
54- {
55- var result = new Dictionary < string , string > ( ) ;
56- if ( syntax is SyntaxNode node )
57- {
58- result . Add ( "NodeKind" , node . Kind ( ) . ToString ( ) ) ; // TODO: Kind() here is for C#. Considering fixing that.
59- }
60- else if ( syntax is SyntaxToken token )
61- {
62- result . Add ( "TokenKind" , token . Kind ( ) . ToString ( ) ) ;
63- }
64- else
65- {
66- throw new ArgumentException ( $ "The specified { nameof ( syntax ) } is not a SyntaxNode nor a SyntaxToken.") ;
67- }
68- PropertyInfo [ ] properties = syntax . GetType ( ) . GetProperties ( ) ;
69- foreach ( PropertyInfo info in properties )
70- {
71- // Language isn't important to include in each node.
72- // Parent is redundant. I can already see the parent.
73- // ValueText and Value are the same as Text.
74- // SyntaxTree shows the complete source in each node. That's redundant.
75- // RawKind is just the underlying numeric value of SyntaxKind enum. It's meaningless.
76- if ( info . Name == "Language" || info . Name == "Parent" ||
77- info . Name == "ValueText" || info . Name == "Value" ||
78- info . Name == "SyntaxTree" || info . Name == "RawKind" )
79- {
80- continue ;
81- }
82- result . Add ( info . Name , info . GetValue ( syntax ) ? . ToString ( ) ) ;
83- }
84- return result ;
43+ using var reader = new StreamReader ( Request . Body , Encoding . UTF8 ) ;
44+ string body = await reader . ReadToEndAsync ( ) . ConfigureAwait ( false ) ;
45+ SyntaxTree tree = VisualBasicSyntaxTree . ParseText ( body ) ;
46+ SyntaxNode root = await tree . GetRootAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
47+ Compilation compilation = VisualBasicCompilation . Create ( "HelloWorld" , new [ ] { tree } ) ;
48+ SemanticModel model = compilation . GetSemanticModel ( tree ) ;
49+ SyntaxTreeNode myRoot = SyntaxTreeNode . CreateMyOwnTree ( root , model ) ;
50+ return myRoot ;
8551 }
8652 }
8753}
0 commit comments