In XCHEME all expressions are composed of operands and operators, in this section we will learn how to work with them.
The following table lists all the available operands. Click on the description to know about the respective operand.
Description | Operand |
---|---|
Any | any |
Range | from '…' to '…' |
String | '…' |
Reference | … |
Map | map { … } |
End of Source | eos |
The any
operand is used to accept any character in a token directive or any token in a node directive.
Syntax:
any
Note:
any
and*
are interchangeable.
Token example:
token <0> T_FOO as *;
A new
T_FOO
token will be generated for any input character.
Node example:
node <0> N_FOO as *;
A new
N_FOO
node will be generated for any input token.
The range operand is used to accept a range of characters in a token directive. When the range operand is used in a node directive a new token directive is generated automatically and referenced into the respective node directive, this behavior is called loose token generation.
Syntax:
from FOO to BAR
Token example:
token <0> T_FOO as from '0' to '9';
For each digit between
'0'
and'9'
a newT_FOO
token will be generated.
Node example:
node <0> N_FOO as from '0' to '9';
For each loose token that corresponds to a digit between
'0'
and'9'
, a newN_FOO
node will be generated.
The string operand is used to accept a sequence of characters in a token directive. When strings are used within node directives a loose token directive will be generated.
Syntax:
'foo'
Token example:
token< 0> T_FOO as 'foo';
For each
'foo'
string a newT_FOO
token will be generated.
Node example:
node <0> N_FOO as 'foo';
For each loose token that corresponds to
'foo'
, a newN_FOO
node will be generated.
The reference operand is used to reuse an expression from a token or node directive without the need of duplicating it. There are some rules for using references, you can learn more about here.
Example:
alias token T_FOO as 'foo';
alias token T_BAR as 'bar';
token <0> T_BAZ as (T_FOO & T_BAR) | (T_BAR & T_FOO);
For each
T_FOO
andT_BAR
orT_BAT
andT_FOO
, a newT_BAZ
token will be generated.
The map operand is used to group characters or strings in a token directive, or a set of tokens in a node directive.
Syntax:
map {
FOO,
BAR
}
Example:
token <0> T_FOO_OR_BAR as map {
'foo',
'bar'
};
For each
'foo'
or'bar'
occurrence, a newT_FOO_OR_BAR
token will be generated with the same identity.
Maps can perform better than sequences of token directives and can be useful to combine multiple directives into a single one, but in some cases, we would like to define an individual identity for each map entry as we can do with multiple directives, let's see down below how to achieve that.
token <auto> T_FOO_OR_BAR as map {
<0> FOO as 'foo',
<1> BAR as 'bar'
};
For each
'foo'
or'bar'
occurrence, a newT_FOO_OR_BAR
token will be generated, and theauto
identity in the token directive ensures that every token generated will assume the identity provided in the respective map entry.
Now that we've added identities and identifier to each map entry, it's also possible to use an individual reference for each one, as we will see below.
token <auto> T_FOO_OR_BAR as map {
<0> FOO as 'foo',
<1> BAR as 'bar'
};
node <0> N_FOOBAR as T_FOO_OR_BAR.FOO & T_FOO_OR_BAR.BAR;
A new
N_FOOBAR
node will be generated for each occurrence ofT_FOO_OR_BAR.FOO
andT_FOO_OR_BAR.BAR
.
For node directives we may also not use identifiers as we cannot reference node map entries in other directives, in this case, we can omit the identifier for the map entry as shown below.
token <auto> T_FOO_OR_BAR as map {
<0> FOO as 'foo',
<1> BAR as 'bar'
};
node <auto> N_FOO_OR_BAR as map {
<0> T_FOO_OR_BAR.FOO,
<1> T_FOO_OR_BAR.BAR
};
A new node will be generated for each occurrence of its respective token.
The eos
(End of Source) operand is used to accept only the end of source in a directive.
Syntax:
eos
Token example:
token <0> T_FOO as 'foo' & eos;
It generates a new
T_FOO
token for a'foo'
string followed by the end of the source.
Node example:
node <0> N_FOO as T_FOO & eos;
It generates a new
N_FOO
node for aT_FOO
token followed by the end of the source.