Syntax Coloring for Clojure

Someone reading yesterday's blog entry might have thought: "OK. So you're not only able to create syntax coloring for some kind of trivial DSL. Yes, you're also able to do it for a real language, in this case, Ceylon. However, you caught a lucky break in that case, it's unlikely you'll be able to do it again for another real language."

Well, OK, take a look at this, my hypothetically sceptical imaginary friend:

What you see here is syntax coloring for Clojure (via the Clojure.g ANTLR file), created in about 45 minutes (Clojure turns out to have a lot less tokens than Ceylon), together with HTML embedding within Clojure comments. However, note that the embedding only supports syntax coloring, not code completion, not sure if that is possible nor how to do that.

Here's my embedding definition, i.e., this is all, no more or less than this:

@ServiceProvider(service = LanguageProvider.class)
public class HTMLEmbeddingLanguageProvider extends LanguageProvider {

    private Language embeddedLanguage;

    public Language<?> findLanguage(String mimeType) {
	return ClojureTokenId.getLanguage();

    public LanguageEmbedding<?> findLanguageEmbedding(Token<?> token, LanguagePath languagePath, InputAttributes inputAttributes) {
	if (11 == { //11 is the token ordinal in Clojure for comments
	    return LanguageEmbedding.create(embeddedLanguage, 0, 0);
	return null;

    private void initLanguage() {
	embeddedLanguage = MimeLookup.getLookup("text/html").lookup(Language.class);
	if (embeddedLanguage == null) {
	    throw new NullPointerException("Can't find language for embedding");


Soon I'll be working on a tutorial showing how to create syntax coloring for the Movie Query DSL I showed over the last two days.


Post a Comment:
  • HTML Syntax: NOT allowed

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« March 2015