Checking C# Syntax from the Command Line
Asked Answered
M

3

7

Does anyone know of a way in the Microsoft .NET framework to check the syntax, and only the syntax, of a given C# file?

For a little background, what I am interested in doing is setting up syntastic to check the syntax of .cs files. Out of the box, syntastic uses the Mono C# compiler with its --parse flag to perform this operation but I can find no equivalent in the Microsoft .NET framework.

My first attempt to get this to work was to use csc /target:library /nologo in place of mcs --parse, but the problem is that this is called on a per-file basis. As a result, it reports missing namespaces (which exist in the full project build) instead of only syntactic errors.

Maurits answered 10/12, 2012 at 22:38 Comment(2)
What is the reason for doing this?Enosis
Why not just install mono?Telpher
M
1

I've used NRefactory before from the icsharpcode IDE. It's quick and easy for basic stuff.

see this article: Using NRefactory for analyzing C# code

I use it for creating VB.NET examples from C# examples. The method that does this is really straight-forward and can easily be adapted to your needs:

    private static void ConvertLanguage(TextReader input, TextWriter output, SupportedLanguage language, Action<string> onError)
    {
        using (IParser parser = ParserFactory.CreateParser(language, input))
        {
            parser.Parse();
            var specials = parser.Lexer.SpecialTracker.RetrieveSpecials();
            var result = parser.CompilationUnit;
            //if (parser.Errors.Count > 0)
            //    MessageBox.Show(parser.Errors.ErrorOutput, "Parse errors");

            IOutputAstVisitor outputVisitor;
            if (language == SupportedLanguage.CSharp)
                outputVisitor = new VBNetOutputVisitor();
            else
                outputVisitor = new CSharpOutputVisitor();

            outputVisitor.Options.IndentationChar = ' ';
            outputVisitor.Options.IndentSize = 4;
            outputVisitor.Options.TabSize = 4;

            using (SpecialNodesInserter.Install(specials, outputVisitor))
                result.AcceptVisitor(outputVisitor, null);

            if (outputVisitor.Errors.Count > 0 && onError != null)
                onError(outputVisitor.Errors.ErrorOutput);

            output.Write(outputVisitor.Text);
        }
    }

Note: The preceding code is from an older version and may not compile against the latest version of the NRefactory library.

Mccahill answered 11/12, 2012 at 0:22 Comment(1)
This appears to be the best CLR implementation-independent answer out there.Maurits
S
2

You can do this via the Roslyn CTP. It allows you to parse the .cs file entirely, and walk the full tree, looking for errors.

For details, I recommend downloading the Walkthrough: Getting Started with Syntax Analysis for C#, as it shows you the basic approach to looking at syntax trees in a C# file.

Sneakbox answered 10/12, 2012 at 22:45 Comment(0)
M
1

I've used NRefactory before from the icsharpcode IDE. It's quick and easy for basic stuff.

see this article: Using NRefactory for analyzing C# code

I use it for creating VB.NET examples from C# examples. The method that does this is really straight-forward and can easily be adapted to your needs:

    private static void ConvertLanguage(TextReader input, TextWriter output, SupportedLanguage language, Action<string> onError)
    {
        using (IParser parser = ParserFactory.CreateParser(language, input))
        {
            parser.Parse();
            var specials = parser.Lexer.SpecialTracker.RetrieveSpecials();
            var result = parser.CompilationUnit;
            //if (parser.Errors.Count > 0)
            //    MessageBox.Show(parser.Errors.ErrorOutput, "Parse errors");

            IOutputAstVisitor outputVisitor;
            if (language == SupportedLanguage.CSharp)
                outputVisitor = new VBNetOutputVisitor();
            else
                outputVisitor = new CSharpOutputVisitor();

            outputVisitor.Options.IndentationChar = ' ';
            outputVisitor.Options.IndentSize = 4;
            outputVisitor.Options.TabSize = 4;

            using (SpecialNodesInserter.Install(specials, outputVisitor))
                result.AcceptVisitor(outputVisitor, null);

            if (outputVisitor.Errors.Count > 0 && onError != null)
                onError(outputVisitor.Errors.ErrorOutput);

            output.Write(outputVisitor.Text);
        }
    }

Note: The preceding code is from an older version and may not compile against the latest version of the NRefactory library.

Mccahill answered 11/12, 2012 at 0:22 Comment(1)
This appears to be the best CLR implementation-independent answer out there.Maurits
B
0

I think I may have a solution to your question. If you are trying to check the syntax of you code without being in the debugger you can use an online compiler suck as compilr. If you wish to output the resuts then you can use this amazing api called Html Agility to grab the results off of the online compiler with ease. Hope this helped!

Bipack answered 10/12, 2012 at 22:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.