Plugin with 2 Custom Language Supports

已回答

Baseline: We wrote an Intellij Plugin with Custom Language Support, using file-extension ".asd". Using gradle as build tool, adding a "generateLexer" and "generateParser" task:

Task: Extend the plugin for a different Custom Language. E.g. with extension ".qwe":

Question:
1. is possible to generate 2 Lexers in one module? Note that these Languages are always used together, so it makes sense to let users install 1 plugin for both languages.
2a. If so: how to do that? how does the build.gradle file look like?
2b. if not; We would basically implement 2 plugins separately and let one depend on the other... (we did something like that already, but the old one is not based on gradle and that shall change)

1

Hi,

Yes, it is possible.

You can disable the existing (for consistency) and create new tasks for your languages (I'll use build.gradle.kts syntax):

// required imports:
import org.jetbrains.grammarkit.tasks.GenerateLexerTask
import org.jetbrains.grammarkit.tasks.GenerateParserTask

...

tasks {

  // at first disable the default grammar tasks created by Grammar-Kit plugin:
  generateLexer.configure { enabled = false }
generateParser.configure { enabled = false }

// then create tasks for your languages
task<GenerateLexerTask>("generateAsdLexer") {
      group = "grammarkit"
      source.set("src/main/grammars/asd.flex")
      targetDir.set("src/main/gen/com/example/asd/lexer")
      targetClass.set("AsdLexer")
      purgeOldFiles.set(true)
  }

task<GenerateParserTask>("generateAsdParser") {
      group = "grammarkit"
      source.set("src/main/grammars/asd.bnf")
      targetRoot.set("src/main/gen")
      pathToParser.set("/com/example/asd/parser/AsdParser.java")
      pathToPsiRoot.set("/com/example/asd/psi")
      purgeOldFiles.set(true)
  }

  // do the same for the additional language

  ...

  // and make the grammar tasks the dependency of required tasks, e.g.:

  compileKotlin {
    ...
    dependsOn(
        withType<GenerateLexerTask>(),
        withType<GenerateParserTask>()
    )
}
}

Such a setup will result in having 4 tasks:
- generateAsdLexer and generateXyzLexer
- generateAsdParser and generateXyzParser
 
that will be a part of the build lifecycle (thanks to specified dependsOn()).

1

Hi Neskemiquel,

Could you please clarify the issue? Did you try my solution?

0

Hi Karol Lewandowski,

thanks for your detailed description! Works great!

We used to have a build.gradle (in Groovy) that I migrated to Kotlin now, as I couldn't find anything regarding "org.jetbrains.grammarkit.tasks.GenerateLexerTask" and "org.jetbrains.grammarkit.tasks.GenerateParserTask" for Groovy so far. Not sure if that was really necessary but it wasn't a big deal... :)

0

请先登录再写评论。