The most exciting thing about REBOL is its dialects. This does not mean that the programming language changes according the part of the world in which it is used.
Instead it means that the programmer can produce a project or problem specific set of grammar rules that can handle blocks of data. REBOL (see A Brief Introduction to REBOL Programming) can then parse the data through the grammar rules. This makes REBOL the perfect language for creating business rules.
Creating a Very Simple REBOL Dialect
A REBOL dialect consists of:
- a block of statements
- a block of grammar rules
- the parse method which applies the grammar rules to the block of statements
Fortunately the programmer can carry out each of these steps very quickly. That is, of course, assuming that they have a good understanding of the rules to be modelled.
Creating a REBOL Block of Data
A REBOL block of data is simple a statement defining the data to be used. For example, consider the block:
Here the business rule is simple: whoever gets home on time is allowed out again; whoever gets home late is grounded. The next step is, therefore, to create the grammar rules.
Creating REBOL Grammar Rules
First any required variables need to be set:
There are a few points to take note of in this code:
- the "some" keyword implies that one or more matches are to be made. As well as "some", the following can also be used:
- none - no matches to be made
- any - none or more matches to be made
- specific words are matched by using the single quotation mark at the start of the word. For example, 'name will match the word "name", 'got-home will match the words "got-home". And this raises an important distinction:
- "got - home" means variable "home" subtracted from variable "got"
- "got-home" is a single variable name
- data types are matched by using the exclamation mark at the end of the word. For example "string!" will match to any string types, "time!" with match to a time", etc
- alternative matches are added by using |
- optional matches are defined by using the opt key word
Next the business rule for the grammar block can be created:
REBOL does not have an if..then..else statement. Instead it uses either in the form:
Now the dialect is ready for use.
REBOL Parsing
The REBOL dialect is accessed by using the parse key word. The technique consists of stepping through the data block and applying the grammar rules to each sub-block:
And the end result is: