Geertjan's Blog

  • January 24, 2012

Syntax Coloring for Clojure

Geertjan Wielenga
Product Manager

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 == token.id().ordinal()) { //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.

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.