Download NetBeans!

20070320 Tuesday March 20, 2007

Schliemannizing Regular Expressions in NetBeans IDE

I've been trying to find tokens to exercise my newly acquired Schliemann skills on. Eventually, after investigating a variety of interesting scripting languages (including one called MUMPs, which would have made a cool subject line for a blog entry: "Schliemann Has Mumps"), I settled on regular expressions, because NetBeans engineer Martin Adamek has a regular expression tester on nbextras.org (Sandip has a regular expression checker there too). In other words, the tokens already exist. (Do you have tokens? Do they need language support features? Please send them along to me and I'll take good care of them...)

So basically, this is as much a porting guide as instructions for creating syntax colors (and more stuff in the future) for regular expressions. Although, if all you're doing is taking tokens from an existing module, and rewriting them the Schliemann way (i.e., regular expressions), then you're not really porting. So little of the original code can be reused, because you need so little code for Schliemann in the first place.

Interestingly, here we're using Schliemann within a JEditorPane, instead of in the NetBeans editor. That's also totally possible, simply by putting the MIME type in the contentType property of the JEditorPane. So, this is what I did:

  1. Set up the module. Create a MIME resolver and NBS file, wizards are available for both. Register the two files in the XML layer. Assign the MIME type to the JEditorPane contentType, in the GUI Builder's Properties window. Install the module.

    Usefully, because nothing you type matches a token (because you have not defined tokens yet), the entire line/s that you enter in the JEditorPane is/are displayed in red, as shown below:

    If part of the line were recognized as a token, that part would not be red, as we shall see later.

  2. Copy your tokens from RegexTokenContext.java. To understand what the tokens are supposed to represent, see RegexSyntax.java

    As you define your tokens, keep on checking your progress, by installing the module again and seeing how much of your code is captured by the token definitions. Here you can see we've made some progress already:

Not quite done yet, clearly, but getting there. And all it took was that I defined the following tokens and colors, partly based on Martin Adamek's regular expression tester on nbextras.org:

TOKEN:string:( "\'" )
TOKEN:identifier:( ["a"-"z"] ["a"-"z" "0"-"9"]* )
TOKEN:operator:( "-" | "+" | "*" | "!" | "=" | "," | ";" ) 
TOKEN:bracket:( "(" | ")" | "<" | ">" | "[" | "]" )
TOKEN:dot:( "." )

COLOR:identifier: {
    foreground_color: "blue";
}
COLOR:operator: {
    foreground_color: "blue";
}
COLOR:bracket: {
    foreground_color: "red";
    font_type: "bold";
}
COLOR:dot: {
    foreground_color: "black";
    font_type: "bold";
}
COLOR:string: {
    foreground_color: "magenta";
    font_type: "bold";
}

Now I basically have the basis for further support for regular expressions, such as a navigator and other cool language features. Tools for regular expressions are going to become pretty important in 6.0, I reckon, because so much of Schliemann depends on working with regular expressions. So, the modules by Martin and Sandip (or derivatives of these) in this area are going to be helpful to have around.

By the way, thanks especially to Alex Kotchnev (plus a few others, such as Jochen from the Groovy mailing list), I now have a full blown functioning Groovy Console integrated in NetBeans IDE (plus, including the menu bar). Thanks to the NetBeans windowing system, I can even have two consoles (or as many as my heart desires) open at the same time:

Hurray for Groovy. Next, I'm going to try and do something with the Groovy Compiler. Or are there other things that should be looked at more urgently, either in relation to Groovy or in relation to Grails?

In other news. "Schliemannizing" is a brand new word. It means: "to radically simplify the implementation of, while extensively improving, something", as in the sentence: "He Schliemannized his NetBeans module, and now there are only two files left, instead of 200, while at the same time 10 new features have been added." When the word starts being used everywhere, from the depths of your local pub ("they Schliemannized my favorite beer, so now I get a hangover after just a single drop"), to the middle of a Larry King interview ("has your life improved now that your breasts have been Schliemannized?") , to the high point of the Queen's Christmas speech ("we will do all we can to Schliemannize our next Christmas speech, but chances are that it will be equally dull and at least three times as long"), remember... you heard it here first.

Mar 20 2007, 12:34:42 PM PDT Permalink