KirillOsenkov / RoslynQuoter

Roslyn tool that for a given C# program shows syntax tree API calls to construct its syntax tree
http://roslynquoter.azurewebsites.net
Apache License 2.0
918 stars 118 forks source link

Add option to parse input as C# script #36

Closed svick closed 3 years ago

svick commented 6 years ago

Right now, Roslyn Quoter always parses the input as a regular C# file. It would be nice if it also supported parsing the input as a script file (i.e. SourceCodeKind.Script).

For example, parsing f(); currently produces:

CompilationUnit()
.WithMembers(
    SingletonList<MemberDeclarationSyntax>(
        MethodDeclaration(
            PredefinedType(
                MissingToken(SyntaxKind.VoidKeyword)),
            Identifier("f"))
        .WithSemicolonToken(
            Token(SyntaxKind.SemicolonToken))))
.NormalizeWhitespace();

I think there should be an option where it instead produces something like:

CompilationUnit()
.WithMembers(
    SingletonList<MemberDeclarationSyntax>(
        GlobalStatement(
            ExpressionStatement(
                InvocationExpression(
                    IdentifierName("f"))))))
.NormalizeWhitespace();
bernd5 commented 3 years ago

This can be closed - it works now as expected

svick commented 3 years ago

@bernd5 I'm not sure that's the case. Quoter now understands C# 9.0 top-level statements, but those are subtly different from C# scripts. For example, if you parse the code int i; as "regular" C# code, then that's considered to be a global statement containing a local declaration statement and this is what Quoter shows. But if you parse it as a script, then it's treated as a field declaration statement instead.

On the other hand, the C# script dialect never became popular, so there may not be much need to do this.

bernd5 commented 3 years ago

Ah, right - I created a PR...