AntlrVSIX is an open source Visual Studio 2015 and 2017 extension for Antlr version 4 grammars. The features in this extension are:
Colorized tagging of grammars, including: i. Terminals, which are symbols defined in the LHS of a lexical grammar production rule. Terminals always have the first letter capitalized. These symbols are tagged in lime green. ii. Nonterminals, which are symbols defined in the LHS of a parser grammar production rule. Nonterminals never have the first letter capitalized. These symbols are tagged in purple. iii. Comments, which are either single line (//) or multiple line (/* … */). These are tagged in green. iv. Keywords, such as fragment, grammar, etc. Keywords are tagged in blue. v. Literals, such as [\n\r ], ‘foobar’, etc. These are tagged in red.
“Go to definition”: AntlrVSIX can locate the rule that defines the symbol. Right-click on a symbol, and choose “Go to definition” in the pop-up menu.
- “Find all references”: AntlrVSIX can locate the defining and applied occurrences of a symbol. Right-click on a symbol, and choose “Find all references” in the pop-up menu. Open the “Antlr Find Results” windows and select any occurrence.
- “Replace symbol”: AntlrVSIX can rename the defining and applied occurrences of a symbol. Right-click on a symbol, and choose “Rename Antlr symbol” in the pop-up menu. In the pop-up modal dialog box, enter the new name, then click “OK”. You must manually save the files to make the changes permanent.
- “Reformat”: AntlrVSIX can reformat the entire file using Codebuff, which is a machine-learning format tool (https://github.com/antlr/codebuff http://dl.acm.org/citation.cfm?id=2997383 https://arxiv.org/abs/1606.08866). To reformat you’re grammar, you will need to create a list of Antlr4 grammars, set the environmental variable CORPUS_LOCATION to the directory. AntlrVSIX will read all “.g4” grammars in the directory, the reformat the current document from the formatting discovered in the corpus. You must manually save the file to make the changes permanent.
Open all the grammar files in Visual Studio you wish to search, or add the grammar files to a project/solution. AntlrVSIX will go through all files, parse, and record the occurrences of every symbol.
No advertisements will ever be displayed using this extension.
This extension only works on Antlr4 grammars, and the grammar must be in a file that has the suffix “.g4”.
If adding another token class, like punctuation, make sure to add the classification to: AntlrTokenTagger; AntlrClassifier; ClassificationFormat.cs; OrdinaryClassificationDefinition.cs; AugmentQuickInfoSession; AugmentCompletionSession. If you don’t, you end up with very bizarre errors in the ActivityLog.xml file for Visual Studio that are very hard to track down. I suggest you do a search for “nonterminal” and observe all the locations you may have to modify.
The grammar used is the standard Antlr4 grammar in the examples: https://github.com/antlr/grammars-v4/tree/master/antlr4. There were some modifications to get the parser to work in C#.
The parser is not incremental. The parse does not recover from syntax errors at all. If the input grammar does not parse, there is no tagging.
- If you want to make modifications for yourself, you should reset your
Experimental Hive for Visual Studio. To do that, execute from Cygwin (or a cmd, the
- $ cd ‘/cygdrive/c/Program Files (x86)/Microsoft Visual Studio 14.0/VSSDK/VisualStudioIntegration/Tools/Bin’
- $ ./CreateExpInstance /Reset /VSInstance=14.0 /RootSuffix=Exp
“Go to definition” and “Find all references” are not implemented as a Language Service! As noted in Legacy Language Service Extensibility (https://msdn.microsoft.com/en-us/library/bb165099.aspx) “Legacy language services are implemented as part of a VSPackage, but the newer way to implement language service features is to use MEF extensions.” The alternative approach, a Language Service, is undocumented, and the examples that I could find (PTVS) are bloated and poorly structured. Rather than take weeks, if not months, to understand and implement, I chose a very simple WPF implementation.
The grammar for Antlr that this extension uses may not be the “official” version for Antlr4. Consequently, your grammar may be valid according to the Antlr compiler but not with this extension. Please bear with me while I try to correct the grammar.
- Use Visual Studio 2017 to build the extension.
New in v1.2.4:
The extension is now both VS 2017 and 2015 compatible.
The results windows of Antlr Find All References is now “Antlr Find Results”.
New in v1.2.3:
Color selection through VS Options/Environment/Fonts and Colors. Look for “Antlr …” named items.
Bug fixes with Context Menu entries for AntlrVSIX. AntlrVSIX commands are now only visible when cursor positioned at an Antlr symbol in the grammar. This fixes the segv’s when selecting AntlrVSIX commands in non-Antlr files.
Any questions, email me at ken.domino
Alternative Visual Studio Extensions
- ANTLR Language Support – https://marketplace.visualstudio.com/items?itemName=SamHarwell.ANTLRLanguageSupport
- Antlr4Code – https://marketplace.visualstudio.com/items?itemName=RamonFMendes.Antlr4Code
- Actipro SyntaxEditor for WPF – https://marketplace.visualstudio.com/items?itemName=ActiproSoftware.ActiproSyntaxEditorforWPF
- Syntax Highlighting Pack – https://marketplace.visualstudio.com/items?itemName=MadsKristensen.SyntaxHighlightingPack