Code Monkey home page Code Monkey logo

Comments (3)

warrenseine avatar warrenseine commented on May 29, 2024

What would it take to support newer versions of C# and even other flavors, i.e. CSharp Scripting?

I try to keep up with C# upgrades but it doesn't current support all of C# 7, so they're still a bunch of things to implement. I don't have the list at the moment.

C# scripting is probably a trivial alternative to C# so I'm pretty sure it could fit in the existing C# grammar of the plugin.

Would the antlr project be the right place to implement the grammar?

No, ANTLR is a parser generator. It doesn't officially provide grammars. The only I'm implementing at the moment is an upgrade of https://github.com/antlr/grammars-v4/tree/master/csharp.

Do you know if other projects can be reused, i.e. Visual Studio already has code formatting and MS has made a lot of stuff open source? Maybe they also have some sort of "grammar" files somewhere?

No they don't. Trust me, I've looked :) C# doesn't have an up-to-date grammar specification.

One alternative is to use the actual Roslyn compiler instead of a custom grammar, and you can find the beginning of an implementation on a branch: https://github.com/warrenseine/prettier-plugin-csharp/tree/roslyn.

General question about grammar vs formatting: I assume the grammar is some kind of ruleset or syntax about what is considered "the language", whereas formatting is only concerned about all whitespaces and linebreaks. It seems that the formatting is defined in src/printer.js. Am I roughly correct?

Yes, you are. Parsing and printer are different and implemented in different files, but they are also tightly coupled. As we don't have an official AST specification, the printer expects a specific AST from the parser, so you can't just drop-in any parser and hope get it formatted. In other words, a lot of language logic exists in the printer.

from prettier-plugin-csharp.

jannikbuschke avatar jannikbuschke commented on May 29, 2024

Thanks for the explanations.

Follow up question regarding C# scripting:

If I understand you correctly allowing to have c# code (and functions) on the root level of a file (i.e. not nested in any namespace, class or function) is totally doable and could be directly supported by this plugin?

C# scripting also has some way of referencing other dlls, to give an example I think the following is something I would want to be supported:

// Reference to an assembly
#r "Newtonsoft.Json.dll"

using System;
using Newtonsoft.Json;

// class without namespace
class Product
{
	public Guid Id{get;set;}
	public string Name{get;set;}
	public bool Enabled{get;set;}
}

// function without namespace and class
void Foo() {

}

// some code without namespace, class and function
var productObj = new Product {
	Id = Guid.NewGuid(),
	Name = "FooBar",
	Enabled = true
};

Console.WriteLine(JsonConvert.SerializeObject(productObj));

from prettier-plugin-csharp.

warrenseine avatar warrenseine commented on May 29, 2024

Yes, I believe it should be easy to implement in the current parser and printer.

Not sure how Iā€™d do it, but two ways I can think of:

  • Add a new compilation_unit that allows all these new rules at the root of the file.
  • Fit new rules into the existing compilation_unit (could be tricky as the printer already makes some assumptions that could break usual C# code).

Not much to do on the printer side I think.

from prettier-plugin-csharp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.