I’ve noticed that each time when I need to create yet another language definition or some rules grammar, I write the same (or a very similar) “prelude”. I guess that you do the same thing. To simplify the process I’ve crystallized this basis into a simple module. It contains the definition of a Whitespaces, Single- and Multi-line comments and a commands list. You can reuse this module by importing it into your language definition.
Here is the example of a very simple language:
// This is a comment
This_is_some_command;
This_is_another_command;
/* This is a multi-
line comment */
This_is_the_last_command;
The grammar is pretty trivial (if you reuse the foundation grammar – note the import in the beginning):
module SomeLanguageModule
{
import LanguageBaseModule { Common as C };
@{CaseInsensitive}
language RulesDefinition
{
// Delimiters
@{Classification["Delimiter"]}
token commandDelimiter = ";";
token Command = c:(C.AlphaNum | "_")+;
// The Program
syntax Main
= C.CommandList(Command, commandDelimiter);
// Ignore whitespace
interleave Whitespace = C.Whitespace | C.Comment | C.MultiLineComment;
}
}
The middle section of the grammar (Command) is where you concentrate your development efforts. Make sure to replace the Command “token” with Command “syntax”.
The Tree result from this grammar and the sample, shown at the top of this post, looks like this:
Main[
[
"This_is_some_command",
"This_is_another_command",
"This_is_the_last_command"
]
]
Download the sample language and the grammar from my SkyDrive.
See the LanguageBaseModule grammar after the break …
Read the rest of this entry »