quick guide for new language support in netbeans

Original web: http://netbeans.dzone.com/tips/quickstart-guide-language-supp  在netbeans下写新的语言支持插件

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 [1] (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 [2] 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 [3]). 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:  

[4]
Choose NetBeans Modules in Categories and Module in Projects:  

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

[6]
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.  

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

[8]  

New File Type Support

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

[9]
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  

[10]
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 16×16 pixels. In our example, it’s this:  

[11]
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.  

[12]
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.  

[13]
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.  

[14]
Locate these lines:  

[15]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.  

[16]
Name the file “Description”, and leave the rest as it is.  

[17]
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.  

[18]

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.  

[19]
In the list, search for the entry called “Generic Languages Framework”, and add it.  

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

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

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

[22]
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”>
[23]
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 [24]  

© 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 [1]   Source URL: http://netbeans.dzone.com/tips/quickstart-guide-language-supp

About daveti

Interested in kernel hacking, compilers, machine learning and guitars.
This entry was posted in IDE_Make and tagged , , . Bookmark the permalink.

3 Responses to quick guide for new language support in netbeans

  1. I have found some great ideas on this site and I will bookmark it.

  2. I love the layout of your blog! Totally creativ

  3. How come what as a result little, i’m unable to find out anything at all.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.