CookScript HomePage

 

 

heng' html

 

 

 

 

 

 

 

counter

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 menubar, menu, item, checkboxitem and separator are commands. The hierarchy of the script is self-evident. Basically, that is all there is for the grammar of CookScript.

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 CookScript parser, you can see that the command in the script corresponds to the function in the code. CookScript will automatically match numbers to its appropriet Java parameter types (double, float, int, boolean, or Double, Float, Int, Boolean, etc). To invoke the parser, follow the following steps.

  1. Create the CookScript object. CookScript by comes with a "Simple" grammar, which is defined above. To create a CookScript object that follows that grammar, do

      CookScript parser = CookScript.createSimpleParser (new FileReader ("myInputFile"));
  2. Create an instance of a class that contains function that will handle the commands. In this case, we have class Foo.

      Foo foo = new Foo ();
  3. Register commands.

      // top level command
      parser.addCommand (null, "menubar", foo);
      // menu can appear within menubar
      parser.addCommand ("menubar", "menu", foo);
      // menu can be nested
      parser.addCommand ("menu", "menu", foo);
      // item can appear within menu
      parser.addCommand ("menu", "item", foo);
      // checkboxitem can appear within menu
      parser.addCommand ("menu", "checkboxitem", foo);
  4. Finally, start parsing with

      parser.parse (false);

    The false parameter indicates that the parser should not worry about saving the output from commands.

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.

(c) Copyright 2002-2005 Heng Yuan. All rights reserved.

Valid XHTML 1.0!