Jordi R. Cardona X3DV Module Suite developer. Netbeans Community Partner. Hiperia3D News administrator. WBandD Co-administrator. Koinup Media Partner ---------------------------------------------- Homepage: http://news.hiperia3d.com/ e-mail: info@hiperia3d.com Jordi has posted 3 posts at DZone. You can read more from them at their website. View Full User Profile

Quick Start: Creating Language Tools In NetBeans IDE

04.14.2008
| 30146 views |
  • submit to reddit
NetBeans IDE is one of the main free Java editors in the market. In fact, it can be used to program in many other computer languages, like C/C++, Ajax, Javascript. NetBeans IDE can be extended by adding modules that add new features. So... you can have a programming editor customized to your needs.

This tutorial can be of interest for all those who want to create a module that adds support for a new language inside NetBeans IDE.

Original article: http://hiperia3d.blogspot.com/2008/04/netbeans-tutorial.html (the original article has coloring that makes reading easier. If you have some dificulty reading it here, you can go there).

The process of creating the first of the modules that compose my X3DV Module Suite was similar to the one described here.

We will learn how to make a module that has these features:

  • Syntax highlighting that is specific for the language we will define.
  • Brace completion and auto-indentation.
  • Icons for the new files of that language.
  • To be able to create new files written in our new language.
  • Template for the new files written in that language.
Just download the last version of NetBeans IDE (download NetBeans IDE 6.1 Beta). Then, follow all the steps described here. This tutorial is valid for the 6.1 version of NetBeans IDE, which has many improvements that make module building easier.

This tutorial takes a fictional language called Foo Language as a sample. I suggest to follow the tutorial as it is, and later, adapt it to your needs. This is not a highly technical tutorial, but a quickstart guide that can be very easy for newcomers. 

First Steps With Your Module

Create a new NetBeans project:


Choose NetBeans Modules in Categories and Module in Projects:


Fill in your project Name, locate the directory where its project folder will be placed, and mark it as a Standalone Module.


Fill the info for your module. You just have to fill the two first text boxes: change the Code Name Base to what is appropriate for your module and choose a Display Name.


The project will be created. Now we will add the basic: support files for the new file type.

New File Type Support

Over the project name, right click and select New/File Type:


In the dialog that appears, you must enter some data so the NetbBeans IDE recognizes the new file type.
In MIME Type you must enter text/x- usually followed by the main extension of the file type.
Why does it say text/x-? As you may note, what you enter is not the true MIME type. For the IDE, all the extensions we create are text files.

In Extension(s) you must enter them separated with spaces. In our example, there's only one extensions for Foo Files: .foo


In Class Name Prefix, you enter the name of the file type, so all classes generated by the IDE for this module will start with it.

In Icon, you must locate in your hard drive a gif icon of 16x16 pixels. In our example, it's this:


Although in some tutorials they say that jpg and png images can also be used, I found that sometimes they're not displayed. So I recommend using gif images, as they are less error-prone.
The IDE will copy your icon to the project directory.


At this point, a bunch of files will be created by NetBeans IDE and opened in the code editor. Close them all, you don't need to edit them.

Now our module is ready to recognize and create the new file types. But we want the new files to have a default content. So we will edit the generated template. Locate the file named:


Edit it and write the content that you want to be the default when you create a new file.


Locate the XML Layer in the module. The XML Layer file is the soul of our module. It controls most of the things that a module can do.


Locate these lines:

The next step is to create a description of the new supported file type that will be displayed when you want to create it, in the New File Wizard. This description will be stored in a file called "Description.html" (strange... uh?).

Add this line after the one highlighted in blue, modifying it to your project url:

<attr name="templateWizardURL" urlvalue="nbresloc:/org/yourorghere/
foolanguagesupport/Description.html"/>
This line we added describes where the Description of the new file is. Right click over your project and select New/Other. Select Other/HTML file.


Name the file "Description", and leave the rest as it is.


Replace all the contents of the generated file with this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
Creates a foo file that is useful for nothing at all.
</body>
</html>

This is what we will see as a result of these steps once the module is finished and we want to create a Foo file.

Create the Language Support

Now that we have the new files recognized, we will add syntax coloring and other features to our language module.
To be able to support language features, we must do the following: right click over your project and choose "Properties". The properties of your module will be displayed. Click over libraries (on the left) and then the "Add..." button.


In the list, search for the entry called "Generic Languages Framework", and add it.


Now, right click over your project and select New/Other.

In Categories, select "Module Development", and on the right, select "Language Support".


Then enter the MIME Type and Extensions as we did in the first section of our tutorial.


You will see that the XML Layer has changed and now has more things added. Between them, there's a new file that describes our language. That file is called "language.nbs".

As the XML Layer has been modified, the icon of our files may have disappeared, so we need to add something to the XML Layer file to recover it.

Close all the opened files. Locate the file called XML Layer, and open it. Locate the line highlighted in blue in this image, that says

<file name="language.nbs" url="language.nbs">

And replace that entire line with:

<file name="language.nbs" url="language.nbs">
<attr name ="icon" stringvalue=
"org/yourorghere/foolanguagesupport/foo_gif.gif"/>
</file>

Editing The Language File

The default language.nbs file is filled with contents that may be a good start point for a scripting language. For declarative languages like VRML or X3D, or markup languages like HTML or similar, these contents are not useful.

In our example of the Foo Language, we will use a very simple language definition. This way you will understand the basics of defining languages.

So delete all the contents of the file language.nbs, and replace them with this:

# To change this template, choose Tools | Templates
# and open the template in the editor.

# definition of tokens
TOKEN:header:( "# foo language v1.0"
)

TOKEN:line_comment: ( "#"[^ "\n" "\r"]* |
"//"[^ "\n" "\r"]* )

TOKEN:keyword:(
"foo_function" |
"foo_command"
)

TOKEN:field:(
"foo_value"
)

# all that follows is useful for mostly all languages
TOKEN:identifier: ( ["a"-"z" "A"-"Z"]
["a"-"z" "A"-"Z" "0"-"9" "_"]* )
TOKEN:number: (["0"-"9"]*)
TOKEN:operator: (
":" | "*" | "?" | "+" | "-" | "[" | "]" |
"<" | ">" |
"^" | "|" | "{" | "}" | "(" | ")" |
"," | "=" | ";" |
"." | "$"
)
TOKEN:string:(
"\""
(
[^ "\"" "\\" "\r" "\n"] |
("\\" ["r" "n" "t" "\\" "\'" "\""]) |
("\\" "u" ["0"-"9" "a"-"f" "A"-"F"]
["0"-"9" "a"-"f" "A"-"F"]
["0"-"9" "a"-"f" "A"-"F"]
["0"-"9" "a"-"f" "A"-"F"])
)*
"\""
)

TOKEN:string:(
"\'"
(
[^ "\'" "\\" "\r" "\n"] |
("\\" ["r" "n" "t" "\\" "\'" "\""]) |
("\\" "u" ["0"-"9" "a"-"f" "A"-"F"]
["0"-"9" "a"-"f" "A"-"F"]
["0"-"9" "a"-"f" "A"-"F"]
["0"-"9" "a"-"f" "A"-"F"])
)*
"\'"
)

TOKEN:whitespace:( [" " "\t" "\n" "\r"]+ )

# colors
COLOR:header:{
foreground_color:"orange";
background_color:"black";
font_type:"bold";
}

COLOR:line_comment:{
foreground_color:"#969696";
}

COLOR:keyword:{
foreground_color:"red";
font_type:"bold";
}

COLOR:field:{
foreground_color:"#25A613";
font_type:"bold";
}

# parser should ignore whitespaces
SKIP:whitespace

# brace completion
COMPLETE "{:}"
COMPLETE "(:)"
COMPLETE "\":\""
COMPLETE "\':\'"

# brace matching
BRACE "{:}"
BRACE "(:)"

# indentation support
INDENT "{:}"
INDENT "(:)"

Now, create a Foo file, using a plain text editor, and save it with the extension .foo

These will be its contents:

# foo language v1.0

# comment
// another comment

foo_function {

foo_command ( foo_value 1 0 1 );

}
Now let's see the language.nbs file and understand the basic parts.

TOKEN:header:( "# foo language v1.0"
)

TOKEN:keyword:(
"foo_function" |
"foo_command"
)

The Tokens are the words that are part of your language, and you want them colored. They define types of words that have something in common in your language. You group them into a category, that is a token.
The words are between double quotes, and separated by a | sign.

COLOR:header:{
foreground_color:"orange";
background_color:"black";
font_type:"bold";
}

COLOR:line_comment:{
foreground_color:"#969696";
}

This defines the colors used for each token. You can specify more properties for colors, but these are the basic ones. All these properties are very easy to understand by their own names, as you see. The colors can be specified by their names (although it recognizes only a few) or by its number.

# brace completion
COMPLETE "{:}"
What these lines do is that when you type a { sign, the editor automatically will type } after your caret, speeding your work and making it less error-prone.

# brace matching
BRACE "{:}"

# indentation support
INDENT "{:}"

This sentences make that when you place the caret over a brace, the matching brace will be highlighted, and that lines after those signs will be indented.

Final Note

Now you know all that is needed to create the basic support for a new file type and language syntax highlighting.
You can add anything you like to your module, that you think is important for you and that could make your work easier.

There's much more than can be done with NetBeans IDE. I invite just to test it, join its huge community of users, and experience it by yourself.

-Jordi R. Cardona-
X3D/VRML Worldbuilder
Java Programmer
X3DV Module Suite Developer.
Hiperia3D News

© 2008 by Jordi R. Cardona. The images and text of this post were added by the author to dzone. The author has granted dzone.com with exclusivity to use these images and text for the only purpose to spread this article. If you want to promote this tutorial, you can link to the original one at: http://hiperia3d.blogspot.com/2008/04/netbeans-tutorial.html

 

0
Your rating: None
Published at DZone with permission of its author, Jordi R Cardona.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Anuradha Gunasekara replied on Wed, 2008/04/16 - 5:47am

simply superb guide

Jordi R Cardona replied on Wed, 2008/04/16 - 8:22am in response to: Anuradha Gunasekara

Thank you Anuradha!

Mark Hull replied on Fri, 2008/05/09 - 12:52pm

Great quick start. I've referred to it about a hundred times already. Well, maybe six, but still... Nice work and thanks.

Jordi R Cardona replied on Sat, 2008/05/10 - 2:32am in response to: Mark Hull

Thank you very much. I tried to keep it simple, to be a real quick start.

Michael Bedward replied on Wed, 2008/07/02 - 2:58am

Thanks for this article Jordi.  As others have said its clear, concise and really informative - what more could you want :)  I'm using it as a head start on how to build an SDL (Simple Declarative Language) editor for NB and it's just what I needed.

Geertjan Wielenga replied on Wed, 2008/07/02 - 3:06am

Jordi R Cardona replied on Wed, 2008/07/02 - 11:47am

Thanks mxb, it's good that you found it useful. While more languages are added, better for Netbeans.

 Hi Geertjan, thanks for those link. I specially like the 2 last ones. I need to go deeper on the grammar definition. Please post any beginner or advanced info on this that you have. Thank you.

Eddy Jhon replied on Thu, 2009/06/11 - 5:32am

This is really a great stuff for sharing.keep it up .Thanks for sharing. Adison High School

Jordi R Cardona replied on Thu, 2009/06/25 - 12:44am

Thanks

Vidhyadharan De... replied on Thu, 2011/10/27 - 1:27pm

Hi  Geertjan/Jordi,

 I am using Netbeans 7.0, I saw the Generic Languages Framework is deprecated. what is the other options to use this framework instead. 

 

Thanks,

vidhya

Matt Coleman replied on Tue, 2011/11/15 - 12:53am

 awesome quickstart guide

 

web designer buffalo

Ryan Fitz replied on Tue, 2011/11/15 - 1:26am

this program is cool amish rifle t-shirt

Mateo Gomez replied on Fri, 2012/02/03 - 12:41am

great coding you guys...very good chicken burrito recipe

Carla Brian replied on Fri, 2012/04/06 - 9:15am

NetBeans IDE provides a rich set of features and a more productive work environment than other IDEs. You don't waste valuable development time searching for plug-ins. - Marla Ahlgrimm

Matt Coleman replied on Tue, 2012/06/19 - 2:01am in response to: Jordi R Cardona

I agree Jordi..it's really user-friendly

graphic designer buffalo

Danish12 Ali replied on Fri, 2013/07/26 - 3:37pm

 I found an example from some time ago, by Tim, and have been playing with it to get used to this new development. The result is as follows: Not trust  no flame e cigarette

Danish12 Ali replied on Thu, 2013/08/15 - 2:35am

I would also motivate just about every person to save this web page for any favorite assistance to assist posted the appearance. See Inside of Members Area (NO BS)

Danish12 Ali replied on Thu, 2013/08/15 - 4:37am

 Easily, the article is actually the best topic on this registry related issue. I fit in with your conclusions and will eagerly look forward to your next updates. Just saying thanks will not just be sufficient, for the fantasti c lucidity in your writing. I will instantly grab your rss feed to stay informed of any updates  Fat Loss Factor Review

Danish12 Ali replied on Wed, 2013/08/21 - 5:01am

 Ambre Energy Ltd., one of the companies seeking to export coal through the Northwest, laid out a clear answer in response to a lawsuit filed by a business partner last year:  author miles dawson

Leonor Buckner replied on Thu, 2013/10/24 - 4:38pm

I really like visiting your site because I think your posts are amazing. Thanks a lot for sharing them.epson 2030 

Aisin Gioro replied on Fri, 2014/02/07 - 11:23pm

I love doing things that are not done by other people or less done by other people.

rolex-orologi 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.