Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Page Contents

Table of Contents
maxLevel3

Grammar Node attributes

The Grammar Node is the outer containing node for the grammar. It allows you to set default attribute values that are inherited by child Attribute Nodes, unless overridden by those child nodes. The Grammar Node may have any of the following attributes:

Attribute name

Description

Possible Values

Mandatory

Default

byteOrder

The order of bytes.

L (for little endian) or B (for big endian).

No

B

nibbleOrder

The order of individual nibbles (4 bits) within each byte.

L (for little endian) or B (for big endian).

No

B

stringType

The character set used to encode strings in the file.

A string representing the name of the character set.

No

None

complete

Whether this is a complete grammar. If the flag is set to T for true, if there are any bytes left over in the file once the grammar has completed (i.e. there are no further grammar nodes to process) then an error will be raised.

T for true or F for false.

No

T

Repeat Node attributes

The child nodes of a Repeat Node may repeat 0, 1, or more times. In addition to common attributes Repeat Nodes may have the following attributes:

Attribute name

Description

Possible Values

Mandatory

Default

times

The number of times that the child nodes will repeat

Any of:

  • A number
  • A variable name, the variable holding the number of repeats – this variable must have been declared in the variables attribute of this or an ancestor node
  • An Expression inside curly braces which will calculate a whole number. The Expression may refer to Attribute Nodes which have been declared in the scriptVariables attribute of this or an ancestor node
    If this property is not set then the first attribute following the repeat node must contain a discriminator attribute. The repeat loop will stop once that discriminator value is encountered.

No

No limit

Snippet 1

For example, the following grammar snippet:

...


The grammar would read exactly three two-character strings, separated by commas, before moving to the next attribute. However, if the exact number of times to carry out the repeat was not specified then the grammar would have no way of knowing when to stop. It would then read the bytes Jo as the next two-character string, and declare an error when it did not find the expected comma in the next byte.

Snippet 2

This next snippet is the same as the first, except that the number of times to repeat is read from a variable defined in an ancestor node.

...


To use the attribute repeatCount as a variable in the times parameter of the Repeat block, it must first be declared on an ancestor node in a variables attribute. The scope of the variable is the hierarchy of nodes inside the node that it is declared on. The value in the variable will be set to null when the scope finishes. The variable may only be used within the defined scope.

Snippet 3

The next snippet is the same as the previous, except that the number of times to repeat is calculated by an Expression using a script variable declared on an ancestor node.

...


To use the attribute repeatCount as a script variable in the times parameter of the Repeat block, it must first be declared on an ancestor node in scriptVariables attribute. Note that in Snippet 2 the variable can be used simply as repeatCount; but in the snippet above the script variable must be used with the format $scope_variableName, i.e. $commaSeparatedBlock_repeatCount.

Snippet 4

If there is no way to determine the number of repeats expected, the grammar must include a way of identifying the end of a repeat loop. Typically this is with some sequence of terminating characters. These are specified in the grammar by using the discriminator attribute, as shown in the snippet below.

...


In the above example the repeat loop will continue until the string "||" is found, when it will stop.

Snippet 5

The bytes that indicate the end of the repeat loop can also be specified as a hex string, using the hexDiscriminator attribute. The snippet below shows an example of this.

Code Block
languagexml
<Repeat>
  <Attr name="text" bytes="2" type="String"/>
  <Attr name="separator" discriminator="," type="String"/>
</Repeat>
<Attr name="terminator" discriminator="A0FF" type="Integer" hexDiscriminator="T"/>

Sequence Node

Sequence Nodes simply define a collection of child nodes. These can be used to define a set of child nodes as a template; or to define the start and end of a sequence of Attribute Nodes that have an overall length specified by another Attribute Node (for example, a Length Node somewhere within the same node hierarchy.
In addition to common attributes  Sequence Nodes may have the following attributes:

Attribute name

Description

Possible Values

Mandatory

Default

length

The overall expected number of bytes for all Attribute Nodes that are descendants of this node. This is helpful if a group of fields in the file includes optional fields – setting the length helps the grammar to determine where this group of fields has ended, and the next field or group of fields begins.
See the example below.

Any of:

  • A number.
  • A variable name, the variable holding the overall number of bytes of Attribute Nodes – this variable must have been declared in the variables attribute of this or an ancestor node.
  • An Expression inside curly braces which will calculate a whole number. The Expression may refer to Attribute Nodes which have been declared in the scriptVariables attribute of this or an ancestor node.

No

None

Consider a file which includes the following sequence of bytes:

...

Code Block
languagexml
<Sequence name="body" variables="bodyLength" length="bodyLength">
  <Attr name="bodyLength" bytes="1" type="Integer"/>
  <Attr name="attribute1" bytes="2" type="String"/>
  <Attr name="attribute2" bytes="2" type="String"/>
  <Attr name="attribute3" bytes="2" type="String"/>
  <Attr name="attribute4" bytes="1" type="Integer" optional="T"/>
</Sequence>
<Attr name="fileLength" bytes="1" type="Integer"/>

Choice Node

A Choice Node specifies that only one or zero of its child nodes (with all its descendant nodes) may be present in the file. Each child node is therefore optional, and setting the optional attribute to F will have no effect. 

...

If the first byte encountered is a 3 then the reader will fail with an error since the snippet only handles 1 or 2 in the first byte. If a 3 is a valid possibility there are three possible solutions:

Solution 1

Add an additional Attribute Node to the Choice Node with a discriminator of 3, as below:

Code Block
languagexml
<Choice>
  <Sequence name="option1">
    <Attr name="type1" discriminator="1" type="String"/>
    <Attr name="numberValue" bytes="2" type="Integer"/>
  </Sequence>
  <Sequence name="option2">
    <Attr name="type2" discriminator="2" type="String"/>
    <Attr name="stringValue" bytes="8" type="String"/>
  </Sequence>
  <Attr name="type3" discriminator="3" type="String/>
</Choice>
<Attr name="final" bytes="2" type="Integer"/>

Solution 2

Add an Attribute Node to the Choice Node to pick up any value that is not 1 or 2, as below:

Code Block
languagexml
<Choice>
  <Sequence name="option1">
    <Attr name="type1" discriminator="1" type="String"/>
    <Attr name="numberValue" bytes="2" type="Integer"/>
  </Sequence>
  <Sequence name="option2">
    <Attr name="type2" discriminator="2" type="String"/>
    <Attr name="stringValue" bytes="8" type="String"/>
  </Sequence>
  <Attr name="type3" bytes="1" type="String/>
</Choice>
<Attr name="final" bytes="2" type="Integer"/>

Solution 3

Note that in the snippet above, if no discriminator is set on the Attribute Node type3 then the number of bytes or bits must be set. You cannot have more than one generic option like this in a Choice Node, since this will make the grammar ambiguous.

...