Software Secret Weapons™

 
GS Lgen Example 1
by Pavel Simakov on 2005-08-01 20:05:03 under Code Generation, view comments
Bookmark and Share
 
This is an excerpt from the original GSLgen documentation.
A More Complex Example

This is a script which generates a simple DOS batch file to install programs by copying them into various directories. It's something you might want to generate for specific projects:


    .-
    .- install.gsl   Generates install script from install.xml
    .-
    .output "install.bat"
    @echo off
    rem generated by gslgen from install.xml, $(script)
    .for file
    .  if    type = "binary"
    echo $(name) -- \\usr\\bin
    copy $(name) \\usr\\bin >nul
    .  elsif type = "script"
    echo $(name) -- \\usr\\lib
    copy $(name) \\usr\\lib >nul
    .  elsif type = "doc"
    echo $(name) -- \\usr\\doc
    copy $(name) \\usr\\doc >nul
    .  endif
    copy $(name) install >nul
    .endfor

The input is an XML file like this:

    <?xml version="1.0"?>
    <INSTALL script="install.gsl">
    <FILE name="gslgen.exe"  type="binary" />
    <FILE name="gslgen.htm"  type="doc"    />
    <FILE name="install.gsl" type="script" />
    <FILE name="hello.gsl"   type="script" />
    </INSTALL>

We run GSLgen to process the XML file:

    gslgen install 

This is the result:


    @echo off
    rem generated by gslgen from install.xml, install.gsl
    echo gslgen.exe -- \usr\bin
    copy gslgen.exe \usr\bin >nul
    copy gslgen.exe install >nul
    echo gslgen.htm -- \usr\doc
    copy gslgen.htm \usr\doc >nul
    copy gslgen.htm install >nul
    echo install.gsl -- \usr\lib
    copy install.gsl \usr\lib >nul
    copy install.gsl install >nul
    echo hello.gsl -- \usr\lib
    copy hello.gsl \usr\lib >nul
    copy hello.gsl install >nul

What's Going On?

The scripts hello.gsl and install.gsl contain lines with a point (`.') in the first column. These are called script lines and contain instructions to be interpreted. The script install.gsl also contains lines which do not begin with a point. These are called template lines and contain text to be output when the line is interpreted. They may also contain substitution symbols which look like the text `$(name)'. Notice that each occurrence of the backslash character is repeated in the script, but not in the output file. This is because GSLgen, like many programs, uses the backslash to introduce a special character, and two backslashes are required to output one backslash.

The `for' instruction requires some explanation; it is the way to iterate through XML data. This is somewhat redundant in the case of `hello.gsl' since there is only one instance of the XML items `WORLD' and `HELLO'. The `for' instruction makes available the attributes of the XML item of the same name. Thus the attributes `name' and `type' of the items named `FILE' in `install.xml' can be used in the lines between the `for' and corresponding `endfor'. Notice that in the first example, the items `WORLD' and `HELLO' both contain an attribute `NAME' which the script accesses independently by specifying `$(world.name)' or `$(name)'. It could also use `$(hello.name)' in the second case; if the attribute name appears alone then GSLgen searches for an attribute with that name in all open XML items beginning with the most recently opened.

No comments yet


Leave a comment


  Copyright © 2004-2012 by Pavel Simakov
any conclusions, recommendations, ideas, thoughts or the source code presented on this site are my own and do not reflect a official opinion of my current or past employers, partners or clients