Establish a common format for all grammars for easier consumption #3816
Replies: 11 comments 18 replies
-
There is a formatter already in place, here: https://github.com/antlr/Antlr4Formatter |
Beta Was this translation helpful? Give feedback.
-
It seems not to be used very often and I cannot find any formatting rules, but ok, never mind. |
Beta Was this translation helpful? Give feedback.
-
Well... hold on. You're right that it's not universally used. Additionally it's a Java command-line app which is frankly less convenient than VS Code. I know if there was a VS Code one, I would use it and I'm sure others would too. So, perhaps we agree on a formatting standard, and deprecate the old Java one in favor of the more modern and more convenient one you've written? |
Beta Was this translation helpful? Give feedback.
-
You can use any of the grammars I wrote for examples of "standard" formatting, via Antlr4Formatter. Here's the 1st one in the tree, alphabetically https://github.com/antlr/grammars-v4/blob/master/acme/acme.g4 |
Beta Was this translation helpful? Give feedback.
-
Yes, let's reformat the .g4's. Can the reformat tool be available as a command-line tool as well as through the VSCode IDE? A command-line tool can be used to batch reformat grammar files, and used to verify that the .g4 is formatted according to the standard. There are other coding standards for the .g4's which should be considered (naming, use of string literals in parser rules, useless parentheses, unused symbols, undefined lexer symbols, ...). |
Beta Was this translation helpful? Give feedback.
-
I created the formatter tool and tested it with the acme grammar from Tom. The PR is here. @teverett please review the diff and let me know if you want changes. I defined the formatting options in the grammar to stay as close as possible to your original grammar, with 2 notable differences:
The repository for the tool is antlr-format. The tool works already when debugging from VS Code. I still have to finish the node package, but that shouldn't take long. If you guys want to play with it (maybe test different options) then clone the repo, open it in VS Code and select the "Debug main index file" launch config. The path to the grammar is hard coded in the config, so you have to change that for your own testing. Don't forget to |
Beta Was this translation helpful? Give feedback.
-
@mike-lischke I prefer the formatting of your formatter. It genuinely increases readability. I'm honestly ambivalent about 3 spaces or 4, as long as its not tabs :) :) |
Beta Was this translation helpful? Give feedback.
-
I suggest not changing formatting for all grammars since the resulting PR will be very big and hard for review. Also, a lot of changed lines spoil |
Beta Was this translation helpful? Give feedback.
-
A review should be done via automated means. We need a thorough, precise, and efficient review. I would expect that the parse trees of the before and after grammar reformat to be identical (removing off-channel or skip). I don't know what the formatter does with comments and actions. I can't get it working (my system doesn't like the command for this step https://github.com/mike-lischke/antlr-format/blob/0dd5c6a2ebc86e882dfda5093dd4bca01ee09dcd/package.json#L22 and when tried at a bash shell fails to do a dynamic load of 'events' package). |
Beta Was this translation helpful? Give feedback.
-
FYI: I just released the first version of the The formatter can automatically add the used rules as comments to a grammar, so it's easy for me to re-run the formatting process (takes less than 2 seconds for all 516 grammars). Though it's a one time step. Once options are in a grammar, no new ones are added anymore. @kaby76 For usage in a CI you can used the included terminal command (a binary Node.js command). I hope usage becomes clear when you read the documentation. If not, just ask. About the parse tree check: I have not implemented/used that idea in the tool repo, because it would require to generate parsers for all grammars, one before and one after formatting. That would have gone way beyond what is acceptable for such a small tool. Once ANTLRng (the TS port) is available I can do all that in memory and may implement that additional check. |
Beta Was this translation helpful? Give feedback.
-
Hi folks! Sorry, I'm against the idea of any automatic commits, particularly the commits that fix formatting. They spoil git history, git blame (it becomes hard to track original author of the certain line of code), and in fact they are out of control. We can check formatting during the reviewing process. Can be cancel this activity? Also, actually I don't support the idea of the unified formatting for all grammars, it's not very significan. And it can be fixed manually or during reviewing process. We have different authors and different licenses for grammars, I don't expect the same formatting for all grammars. |
Beta Was this translation helpful? Give feedback.
-
May I suggest to establish a consistent formatting in the entire grammar repository? There's a wild mix of indentation (usually 3 and 4 spaces or tabs), alignments and so on.
I have written a formatter class for my ANTLR4 VS Code extension, which is highly configurable, so it's easy to adjust it to rules that we can agree upon here.
I can offer a reformat of the entire grammar repo and create a command line (Node.js) tool everyone can use to apply the format to a grammar file (for future pull requests).
Is there any interest for this?
Beta Was this translation helpful? Give feedback.
All reactions