Skip to content

Latest commit

 

History

History
212 lines (139 loc) · 5.17 KB

operands.md

File metadata and controls

212 lines (139 loc) · 5.17 KB

XCHEME - Operands

In XCHEME all expressions are composed of operands and operators, in this section we will learn how to work with them.

Operand types

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

Any operand

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.

Range operand

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 new T_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 new N_FOO node will be generated.

String operand

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 new T_FOO token will be generated.

Node example:

node <0> N_FOO as 'foo';

For each loose token that corresponds to 'foo', a new N_FOO node will be generated.

Reference operand

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 and T_BAR or T_BAT and T_FOO, a new T_BAZ token will be generated.

Map operand

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 new T_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 new T_FOO_OR_BAR token will be generated, and the auto 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 of T_FOO_OR_BAR.FOO and T_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.

End of Source operand

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 a T_FOO token followed by the end of the source.

Next steps

License

MIT