dslWhat is a Domain Specific Language (DSL) ? According to Wikipedia:

A domain-specific language (DSL) is a programming language dedicated to a particular problem domain, a particular problem representation technique, and/or a particular solution technique. The concept isn’t newbut the term has become more popular due to the rise of domain-specific modeling.

Rebol VID (View Dialect) is Rebol’s DSL for creating GUI, written with Rebol itself ! DSL or Dialecting is a major feature of Rebol according to its inventor:

Although it can be used for programming, writing functions, and performing processes, its greatest strength is the ability to easily create domain-specific languages or dialects.

Carl Sassenrath - Dr Dobbs 2000

Today, you’re going to learn how to use this amazing power of Rebol to easily create your own DSL for generating PHP, Java, C# or VB.Net Code.

This is an imaginary scenario: you’re a coder who often needs to switch between these 4 languages, you’d like to just say “Create a C# class with Id, First Name, Last Name, Birth Date” and then “Save class spec”. 2 days later, you would just say “Load class spec”, “Generate PHP class”, “Add Address Line 1, Address Line 2, Zip Code, City”, “Save class spec” and at a push of a button, all the code will be generated for you.

Rebol’s Dialecting is based on the Parse function. We encountered its simplest uses in 2 previous articles on Rebol parsing without revealing its full capability explained in Rebol’s User Guide. You may read the theory first or just follow our step by step tutorial below:

Launch Rebol’s Console:


phrase: "Create a C# class with Id, First Name, Last Name, Birth Date"
rule: []
parse phrase rule


The purpose was only to show you the general syntax for parsing a phrase with a rule’s block (rebol’s array:


parse phrase rule

Let’s now try to implement a first rule:


rule:  ["Create a " copy Language to " class" to end]
parse phrase rule
Language


Language is a variable that now contains “C#” that has been copied after “Create a ” to ” class”.

Let’s refine the rule to get the attributes list:


rule: ["Create a " copy Language to " class" thru " with " copy attributes-list to end]
parse phrase rule
Language
attributes-list

thru is not really necessary in this case, we just use it to show you how to skip some words while parsing.


Next, we will see how to refine the rule to call the right method to generate the targeted language (C#, Java, PHP, …).

References:
An introduction to Rebol Parse
Codeconscious Parse Tutorial.

Bookmark and Share