Chapter 9 Generation Reference Guide (GTL)
The head and tail string are useful because they are only generated when necessary, it is especially useful when using new lines '\n'. They are added respectively at the beginning, and at the end of the generated code from a macro. If no code is generated, the head and tail string do not appear in the generated code.
You want to generate the name of a class and its attributes under the following format (one empty line between attributes and class):
Attribute 1 attr1 Attribute 2 attr2 Class
You can insert the separator "\n" after the .foreach statement to make sure each attribute is displayed in a separate line. You can also add "\n\n " after the .endfor statement to insert an empty line after the attribute list and before the word "Class".
.foreach (Attribute) ("\n") Attribute %Code% .endfor ("\n\n") Class
Consider a class named Nurse, with a class code Nurse, and two attributes:
Attribute name | Data type | Initial value |
---|---|---|
NurseName | String | __ |
NurseGender | Char | 'F' |
The following templates are given as examples, together with the text generated for each of them, and a description of each output:
class "%Code%" { // Attributes .foreach_item(Attributes) %DataType% %Code% .if (%InitialValue%) = %InitialValue% .endif .next // Operations .foreach_item(Operations) %ReturnType% %Code%(...) .next }
class "Nurse" {
// Attributes String nurseName char nurseGender = 'F' // Operations}
Below the class code, the code is generated on one line. It is an example of a block macro (.if, .endif macro).
class "%Code%" { // Attributes .foreach_item(Attributes) %DataType% %Code% .if (%InitialValue%) = %InitialValue% .endif .next(\n) // Operations .foreach_item(Operations) %ReturnType% %Code%(...) .next(\n) }
class "Nurse" {
// Attributes String nurseName
char nurseGender = 'F' // Operations}
String nurseName and char nurseGender are on two lines
In Template 1, String nurseName and char nurseGender were on the same line, whereas in Template 2, the addition of the \n at .next(\n) puts String nurseName and char nurseGender on two different lines.
In addition, // Operations is displayed in the output even if there is no operation (see Description 3).
class "%Code%" { .foreach_item(Attributes, // Attributes\n,\n) %DataType% %Code% .if (%InitialValue%) = %InitialValue% .endif .next(\n) .foreach_item(Operations, // Operations\n,\n) %ReturnType% %Code%(...) .next(\n) }
class "Nurse" {// Attributes
String nurseName
char nurseGender = 'F'
}
The blank space between .foreach_item(Attributes, and // Attributes\n,\n) is not generated, as shown in the output: class "Nurse" {// Attributes instead of .... { // Attributes
// Operations is not displayed in the output because it is positioned in the .foreach_item macro. It is positioned in the head of the macro for this purpose.
class "%Code%" {\n .foreach_item(Attributes," // Attributes\n",\n) %DataType% %Code%[ = %InitialValue%] .next(\n) .foreach_item(Operations," // Operations\n",\n) %ReturnType% %Code%(...) .next(\n) }
class "Nurse" {
// Attributes
String nurseName
char nurseGender = 'F'
}
The double quote characters ("") in " // Attributes\n" allows you to insert a blank space as shown in the output: // Attributes
Newline preceding the macro
The newline immediately preceding a macro is ignored as well as the one immediately following it, as in the following example:Jack
.set_value(v, John)
Paul
yields: JackPaulinstead of:
Jack
Paul
Copyright (C) 2005. Sybase Inc. All rights reserved. |