5

I use an ANTLRv4 grammar to parse my DSL. I would like to create a plugin to support code highlighting and code completion in IntelliJ IDEA for my DSL.

As far as I can tell, IntelliJ uses BNF and Flex formats for parsing.

Is it possible to:

  1. use an ANTLR grammar, or
  2. convert an ANTLR grammar to BNF and Flex ...

... to make an IntelliJ plugin for my DSL?

David J.
  • 31,569
  • 22
  • 122
  • 174
Yuriy Chulovskyy
  • 163
  • 1
  • 10
  • 2
    Try [ANTLR v4 grammar plugin](https://plugins.jetbrains.com/plugin/7358-antlr-v4-grammar-plugin) – Andrey Dec 11 '18 at 11:07
  • If it's java (and not a completely new language), you're best to use the AST / node structure that intellij already constructs. For adding new language support to intellij, I've done this once before, I just used the Flex parser format since it plugs in neatly with the intellij plugin architecture. If you want to use ANTLR then you'd have to find a way to make it work... it seems more complicated to me – vikingsteve Dec 12 '18 at 12:04
  • 1
    [This 2013 post](https://intellij-support.jetbrains.com/hc/en-us/community/posts/206103369-Using-ANTLR-v4-to-lex-parse-custom-file-formats) by Terence Parr, the creator of ANTLR and fellow IntelliJ user, seems to indicate that he dreams of providing the functionality you ask for, but it is not there yet. (Not my area of expertise, so perhaps I’ve confused the issues.) – Basil Bourque Mar 06 '19 at 21:14
  • My response to "Try ANTLR v4 grammar plugin – Andrey Dec 11 '18 at 11:07": that plugin is _not_ designed to generate an IntelliJ plugin from an existing ANTLR grammar. – David J. Dec 22 '20 at 17:59
  • @BasilBourque yes, in my opinion, you read Mr. Parr's blog post accurately. – David J. Dec 22 '20 at 18:02

1 Answers1

3

Updated on 2020-Dec-22

Question #1: Is it possible to directly use an ANTLR grammar to generate a JetBrains plugin for language support?

Yes, to some degree. It looks like the antlr4-intellij-adaptor library will help. I don't have firsthand experience. It describes itself as "A library to support the use of ANTLRv4 grammars for custom languages in IntelliJ-based IDEs plug-in development."

Just for clarification, a different tool, the ANTLRv4 Plugin for IntelliJ is designed to aid in the development of ANTLRv4 grammars. It does not support using an ANTLRv4 grammar to generate a lexer and parser for use by the JetBrains editors (e.g CLion, GoLand, IntelliJ, PyCharm, RubyMine, and so on).


Question #2: Is it possible to convert an ANTLR grammar to some other forms (e.g. BNF and Flex) which can be used to generate a JetBrains plugin for language support?

Yes, but this is a large topic. I would direct you to this Stack Overflow post: Are there tools to convert between ANTLR and other forms of BNF?


Related Tooling: You may find Grammar Kit by Jetbrains useful, it is "An IntelliJ IDEA plugin for language plugin developers."

Recommended Docs: Also, JetBrains provides extensive documentation on how to extend language support for its editors.

David J.
  • 31,569
  • 22
  • 122
  • 174