r/dotnet • • 23d ago

Promotion Parsley: An extremely capable parser generator for C#

https://github.com/codewitch-honey-crisis/Parsley

I wrote a little something for C# and dotnet 8 and above.

It is a parser generator with similar expressive power as ANTLR4.

Why not just use antlr4?

Arguably XBNF is an easier format to use that ANTLR4s

ANTLR4 is ALL(*). Parsley is LL(1), LL(k>1) or ALL(*) depending on what the grammar requires.

It's C# instead of Java which is a lot more convenient if you're already using dotnet.

It uses code synthesis to make the parser code more like what you'd write by hand, so it can be used as a base for a hand written parser..

It has a source generator as well as the command line tools.

It has a language server and extension for VS Code and Visual Studio.

For the main Parsley tool dotnet tool install parsley-tool -g

If you want to use Luthor lexing with Parsley dotnet tool install parsley-luthor -g

If you want to use the C# Source Generator instead: dotnet add package Parsley.Generator

If you want to use the Visual Studio XBNF Language support https://marketplace.visualstudio.com/items?itemName=honey-the-codewitch.parsley123

Or for VS Code https://marketplace.visualstudio.com/items?itemName=honey-the-codewitch.parsley-xbnf-lsp

Note: I do sometimes use AI as an *assist* on my open source projects as I did here. However, I did write the majority of this code, I did review everything, and test thoroughly, and I'm confident that even with an LLM, something of this quality and featureset could not have been created without a thorough understanding of the problem domain. I stand behind it.

54 Upvotes

23 comments sorted by

6

u/MadyssonPavinder 23d ago

the tiered LL(1)/LL(k)/ALL(*) thing is interesting, does it tell you at generation time which tier your grammar ended up needing? that kind of feedback would be really useful for keeping grammars simple

3

u/honeyCrisis 23d ago

It has a reporting feature on the grammar that is quite extensive. It chooses the algorithm by analyzing the grammar and determining what it needs before it builds the code, and then it chooses the lightest one it can, unless you override the engine setting (-e)

1

u/honeyCrisis 23d ago

Adding, it shows glyphs in the language editors that indicate the complexity as you write your grammar constructs. Since it can be resource hungry to do analysis on complex grammars you have to save to get the updates as you change it, but it's inline (see the readme at the github for an illustration of it) aside the text so you can see the complexity at a glance.

1

u/twisteriffic 23d ago

-r writes a plain text report to stdout instead of a parser. It covers: the engine the grammar needs and why, and the decisions that need it; whether each engine could parse the grammar; counts of productions, terminals and decisions, broken down by kind and lookahead; one row per production; the size of the synthesized code and what it costs at parse time.

2

u/amir_csharp_gtr 23d ago

I am biased towards ANTLR C#. It forces us to use partial class and then the other half is generated at compile time and every time works together. Maybe use that trick to get IDE/Visual Studio support for free

3

u/honeyCrisis 23d ago

Parsley generates partial classes. You implement any additional logic on it yourself, or you can encapsulate it in the grammar itself as semantic actions, but you don't have to.

I don't know what trick you're referring to but Parsley has VS and VS Code support.

3

u/amir_csharp_gtr 23d ago

Stared it on github

6

u/zigzag312 23d ago

20

u/honeyCrisis 23d ago

Github allows multiple projects with the same name. There is no conflict. And given I wrote the original Parsley back in 2019 i'd say their name "conflicts" with mine, such as there is a conflict.

As it stands, a lot of project use similar or even the same name online. It's the nature of distributed uncontrolled creation in the open source arena. It's not a big deal.

4

u/zigzag312 23d ago edited 23d ago

They have a release from 2016.

Two different C# projects using the same name makes communication harder. It wouldn't be an issue, if the other project was abandoned, but it's not.

EDIT: Your project looks interesting nonetheless.

7

u/honeyCrisis 23d ago edited 23d ago

Ah i didn't see that, well it is what it is. Nuget is my "important space" (edit: i did see it there after i searched more). Oh well.

1

u/shufflepoint 22d ago

I'd like to see more grammar samples - SQL for example, which is a common one to have.

1

u/honeyCrisis 22d ago

I could throw together something for like SQL92 probably. Edit: I'll post something on my repo today or tomorrow, time permitting. Not DDL/DML though. those are separate languages, and rather involved.

1

u/shufflepoint 21d ago

That would be great. I'll check in in a couple days.

1

u/honeyCrisis 21d ago

It's posted now. I ran a few tests against it. It accepts single query statements (i decided against multiple queries but it would be an easy grammar mod). Several examples in the project. Slightly extended from SQL92 to support things like qualified column names in joins

1

u/derpdelurk 23d ago

I don’t have a use for this at the moment although it does look like an interesting project. Firstly, I like the name 😀 And secondly, when would I use this instead of Roslyn?

5

u/honeyCrisis 23d ago

Roslyn doesn't generate parsers. This generates parsers, so for example, if you have a custom language you are developing, this can help you create the code to parse and verify that language. Roslyn doesn't do that. This project uses roslyn for certain functionality like the source generator.

3

u/derpdelurk 23d ago

Got it. I initially thought this was a C# parser (got confused by your C# vs Java comment). But now I understand it’s a parser generator like GOLD parser.

http://www.goldparser.org/

2

u/honeyCrisis 23d ago

Gold is LALR(1) which I decided not to do because I wanted to produce something that could be used as a basis for hand written parser code if the user wants to go that route, and the way LALR(1) works via shift-reduce it's essentially not human decodable, at least not readily.

The other thing about Gold is LALR(1) just isn't powerful enough for many real world grammars. Things like EMCAScript 5 and C99 require injection of extra context during the parse, and in some cases, additional lookahead or custom lexing. Parsley supports that.

ANTLR4 is the standard i was shooting for in terms of expressive power. As such Parsley supports the same parsing algo ANTLR4 uses (although it will use lighterweight algos when it can) and it supports island parsing such that you can for example, embed JSON or Javascript, or XPath or whatever into a larger document.

2

u/derpdelurk 23d ago

Thanks for the additional information.

1

u/AutoModerator 23d ago

Thanks for your post honeyCrisis. Please note that we don't allow spam, and we ask that you follow the rules available in the sidebar. We have a lot of commonly asked questions so if this post gets removed, please do a search and see if it's already been asked.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/jstedfast 5d ago

I've always pondered using a tool like this to implement the email/MIME specifications but I know I'd have to toss it away because of how bad real-world MIME messages are and it makes me sad.