As promised earlier, this is the first in my series of articles on how to use GSLgen as a code generator.
Before you can do any code generation with GSLgen, you first have to download, compile and setup your environment variables. Currently, the latest version of GSLgen is 3.1, and it is packaged along with a number of other open-source tools from iMatix in a source code only distribution called RealiBase 1.0. The cool thing about RealiBase is that the bits can be compiled and run on both Windows and Linux.
The RealiBase makefile assumes that you have MSVC 6.0 installed on your computer. Dismayed by the blizzard of build time errors when I tried to compile using VS.NET 2003, I simply installed MSVC 6.0 and compiled the sources from a MSVC 6.0 command prompt window. Andy Hopper, having a higher pain tolerance than me, messed around with the build under VS.NET 2002 and got it to build by copying the required libraries from the Platform SDK into the Microsoft Visual Studio .NET\Vc7\lib folder:
uuid.lib
mpr.lib
winmm.lib
wsock32.lib
odbc32.lib
shell32.lib
netapi32.lib
advapi32.lib
comdlg32.lib
gdi32.lib
user32.lib
Once you have the bits built, you need to place gsl.exe on your system path (the build script compiles GSLgen/3.1 into \imatix\bin\gsl.exe), and you’re off to the races.
GSLgen expects two different files as its input. The first file is the file that contains the GSLgen code generation script. The second file is the file that contains your custom language, which must be implemented using XML.
What follows is arguably the simplest possible GSLgen code generation script. It lives in a file called helloworld.gsl, and is simply a C# Hello World application that writes the application name to the system console.
`.define ignorecase=0 using System;
public class App { public static void Main() { Console.WriteLine( “Hello, $(app.name)” ); } }`
We define the application name in an XML custom language file (helloworld.xml) that is used as input to this GSL script is:
<app name="John"/>
If you run the GSLgen interpreter from the command prompt using:
>gsl -script:helloworld.gsl helloworld.xml
you should get the following as output:
`using System;
public class App { public static void Main() { Console.WriteLine( “Hello, John” ); } }`
Next, let’s examine the GSL script. The first line of your script instructs GSLgen to be case-sensitive (which is what you want most, if not all of the time). The rest of the script, with one notable exception, is simply literal text that gets copied to the output.
The $(app.name) construct tells the GSLgen interpreter to evaluate the expression contained within the parentheses and copy the result to standard output. This is what generates “John” in the output . In this case, our expression means “the name attribute of the app element”.[1]
The thing that really appeals to me is GSLgen’s strength as a templating language. As you can see, the syntax of the GSL script is very clean and uncluttered (contrast this with XSLT!). There is a minimum amount of syntax that gets in the way of seeing what is template and what is code.
I’d like to close off this entry by providing you with a couple of tips. First, you can initialize global variables from the command line. The ignorecase variable that we defined earlier in the GSL script is a variable that can be initialized using the following command line:
>gsl -ignorecase:0 -script:helloworld.gsl helloworld.xml
Another option that you might want to consider (but that I wouldn’t necessarily recommend) is the fact that you can have an XML file associated with a GSL file by adding a script attribute to the document element of your XML file. For example, you can get a simpler command line:
>gsl -ignorecase:0 helloworld.xml
By changing your XML file to:
<app script="helloworld.gsl" name="John"/>
This is somewhat similar to the way XSLT works via the <?xml-stylesheet …/> pseudo-element.
There are two drawbacks to using this embedding. The first is that your XML file is usually used as input to more than one GSLgen script. The second (and this is the reason I’m bringing this up at all) is that the GSLgen XML parser doesn’t understand namespaces.
Ideally, I would have implemented this feature using an attribute that would have been scoped to the GSLgen namespace:
<app xmlns:gsl="http://www.imatix.com/gsl/31" gsl:script="helloworld.gsl" name="John"/>
but this simply isn’t possible using the current version of GSLgen.
So that’s Hello, World using GSLgen. Hopefully this whets your appetite to try out this tool. Hopefully folks currently using XSLT to do their code generation will be encouraged by the more straightforward of this tool. In the next installment in this series, I’ll examine the notion of context in GSLgen and how it can be exploited in loops.
[1] You might be tempted to think that you can arbitrarily nest these expressions a’la XPath. Unfortunately, you cannot, and you will quickly find out that your expressions are really limited to (element name.attribute name). Note that these expressions are always evaluated relative to your current context; I will discuss GSL contexts in a future article.
7 archived reader comments
Andy Hopper (2003-04-26)
Hmm, that last remark’s not entirely correct… given the following xml document: Running the following script: .#Spanglish.gsl .echo ->one->two->three->four.spanish will generate the output [some date and time] gsl M: quattro This is a risky thing to do unless you know for a fact that the path exists because GSL will throw an error if any of those path elements don’t exist. However! This is an opportunity for a wrapper function. -Andy
Andy Hopper (2003-04-26)
Snot. I thought Haloscan would entitize my comment. Here’s that magic XML document:
John Lam (2003-04-27)
Thanks for the catch, Andy! What I meant to say (and didn’t) was that you can’t use XPath like syntax using the dot operator (which is what you would intuitively expect) such as one.two.three.four, since you can’t distinguish between elements and attributes in such a scheme. I figured that I would introduce the arrow operator in the next article, but you beat me to the punch
Dave Lee (2003-04-27)
for those wanting to build on OS X, here is a script I put together
!/bin/sh
echo
grep -A 1 '^\*predefines:' /usr/libexec/gcc/darwin/ppc/3.1/specs- D__FreeBSD__ > tmpspecs for i in */boomake; do perl -pi -e ‘s/cp lib/cp -p lib/’ $i done CCOPTS=”-O2 -Wall -specs=pwd/tmpspecs” ./build.shend of script
Dave Lee (2003-04-27)
I guess haloscan ignores tags, so here is the script properly formattedhttp://cherryville.org/build-gslgen.txt to run it do a
sh ./build-gslgen.txtthis script needs to be run in the directory you unpacked the rbase archive.John Lam (2003-04-28)
Whoa, Dave. This might mean that I’m going to have to fight my wife for the TiBook
Jeff Clark (2003-12-02)
John, You’ve done some work with GSLgen and FreeMind. Since the output of FreeMind is an XML document, have you attempted using FreeMind to define the class structure of components and input into GSLgen? Unfortuately, the XML structure is full of “node” nodes instead of real names. Maybe running it through an XSLT could put it into a better format. FreeMind was simple to use and gives a nice structure picture of the classes as you enter items. This thought came about when I was looking at FreeMind after your article about it. I found myself adding more detail to some fixes I was going to make to a program. I ended up creating some structures and flow charting some methods.
Jeff