John Lam 1997 — 2026

GSLgen, context and loops

In the first article in this series, I talked about the basics of acquiring, compiling and running GSLgen. In this article, I’ll introduce GSLgen’s notion of context, and how it can be used within loops.

Let’s consider how we can use GSLgen to generate some C# classes. Here’s an XML file that contains the simplest possible definition of two classes:

<classes> <class name="Person"/> <class name="Place"/> </classes>

Here’s a GSL script that iterates over the XML file:

`using System; .for classes.class

public class $(name) { } .endfor`

And this is what it generates:

`using System;

public class Person { }

public class Place { }`

Let’s take a closer look at the GSL script. It showcases the for … endfor construct which is commonly used to iterate over a set of XML elements. The for statement’s expression, classes.class, defines what XML elements to iterate over. Within the body of the for … endfor statement is a template. That template’s context changes each time through the loop to point to the current XML element. The $(name) expression retrieves the value of the name attribute of the current XML node and outputs the value to the output stream.

Next, let’s consider a more complex XML file that contains some nested elements that we want to iterate over:

<classes> <class name="Person"> <field name="Name" type="string"/> <field name="Age" type="int"/> </class> <class name="Place"> <field name="Address" type="string"/> <field name="Zip" type="string"/> </class> </classes>

We can nest our for … endfor loops like so:

`using System; .for classes.class

public class $(name) { .for field private $(type) $(name)_; .endfor } .endfor`

Which results in the generation of the following C# code:

`using System;

public class Person { private string Name_; private int Age_; }

public class Place { private string Address_; private string Zip_; }`

As you can see, the nested for … endfor loop inherits the context of its parent for … endfor loop. This lets us write very simple code that can iterate over nested elements using a very natural syntax.

7 archived reader comments

Andy Hopper (2003-05-07)

I’ve found that it’s a Good Idea® to fully qualify names in GSL. The reason for this is that, given an unqualified attribute name, the GSL intepreter will climb up the context stack looking for a parent context that does have that attribute. Only after it searches all of the contexts does it report that it is undefined. So, given the following XML: The following GSL script: .for Country . for State .# The colon at the end of the eval statement tells GSL to preserve case $(name . endfor State .endfor Country Will result in the following output: 2003/05/07 11:28:08: gsl I: Processing states.xml… Georgia Washington USA The simplest way to fully qualify names in the local context is to prefix the name with the ‘0’ scope, like so: 0.name If we modify our script to include the prefix, we’ll now get an “Undefined Identifier” error. The way to fix this is to either check to see if the property is defined using the ‘defined’ function, or to modify the evaluation string to substitute something for undefined values: .for Country . for State $(0.name?”” . endfor State .endfor Country This will result in the following output: 2003/05/07 11:27:10: gsl I: Processing states.xml… Georgia Washington

Andy Hopper (2003-05-07)

Grr. Haloscan can be terribly irritating. Replace all ‘s with :)’s

Jonx (2003-05-07)

Do you know XGOF ? http://nmatrix.com.ar/XGoF/Documentation/Overview.aspx

John Lam (2003-05-07)

Thanks for the tip, Andy! I’m surprised that I haven’t run into that one before, but I suspect I was just lucky

David Pickett (2003-05-07)

Anyone got a pointer to a good GSLgen-style code generator that handles Unicode?

David Pickett (2003-05-07)

Ah…it looks like gslgen just has problems with the UTF-8 byte-order mark at the beginning of the XML file. Woohoo!

David Pickett (2003-07-10)

BTW, in case anyone else runs across the problem, it’s not difficult to hack the GSLgen source to ignore UTF-8 BOMs in XML files. Will post a link to the fixed-up source if anyone ever happens to need it.