|
![]()
|
Introduction:CookScript is a simple Java based script parser for a simple language which it defines. CookScript language is command based and is hierarchical. Its grammar can be best described by the following example: menubar () // command group { // C++ style line comment /* or C style block comments are supported */ menu ("File") { // semi colon is optional between commands item ("open"); // white space or a comma can be used to separate two parameters checkboxitem ("test" true); item ("exit") } // semi colon can be also used after a command group to signal an empty group menu ("empty"); } Here Now, the question is really, how easy (or how hard) it is to use CookScript? If we are using XML parsers such as SAX, programmers concentrate much of their efforts on parsing. If we use the DOM approach, then we would spend too much time and efforts traversing the DOM tree. If we use getters/setters approach, we often run into the the trouble of knowing which data is known/unknown. The most straightforward approach is a function call with all its parameters in place. And this is the objective of CookScript, as the following example illustrates. public class Foo
{
public void parseMenuBar (CookScript parser) throws ParseException
{
// parse commands within group "menubar"
parser.parseGroup ("menubar");
}
public void parseMenu (CookScript parser, String menuName) throws ParseException
{
// parse commands within group "menu"
parser.parseGroup ("menu");
}
public void parseItem (CookScript parser, String itemName)
{
}
public void parseCheckBoxItem (CookScript parser, String itemName, boolean checked)
{
}
}
The code above basically is a list of the command handlers with parameters in
place. Except the extra
Now, imagine how many lines of code you would have to write in order to do the similar thing in XML. CookScript has its short-comings, such as that users need to know exact which position correspond to which parameter, but users have to know which parameters can to passed anyways. The main advantages of CookScript is that it is very fast to write a simple parser in this language and its very easy to extend functionalities. It also allows additional commands to be added dynamically, ideal for plugins and configuration file parsing. |