Ajaxian.com published an article on Ojay, an interesting JavaScript library that rests on top of Yahoo!’s YUI. The library has a DSLish way of expressing form validation rules, like in these snippets from their website:
form('signup')
.requires('username').toHaveLength({minimum: 6})
.requires('email').toMatch(EMAIL_FORMAT, 'must be a valid email address')
.expects('email_conf').toConfirm('email')
.expects('title').toBeOneOf(['Mr', 'Mrs', 'Miss'])
.requires('dob', 'Birth date').toMatch(/^\d{4}\D*\d{2}\D*\d{2}$/)
.requires('tickets').toHaveValue({maximum: 12})
.requires('phone')
.requires('accept', 'Terms and conditions').toBeChecked('must be accepted');
//...
form('signup').submitsUsingAjax();
when('signup').responseArrives(displayResponseIn(result));
}});
What makes this a Domain-Specific Language? The code written above is clearly something different from the standard high-order-programming-like JavaScript style. The author of the library changed the language so it could represent the domain in a more expressive way. An Internal DSL is a language adaptation focusing on writing expressive or more efficient code for a given domain.
Browsing the documentation I found how keyboard events are defined in Ojay:
var rulesA = new Ojay.Keyboard.RuleSet({
'SHIFT S': function() { alert('S') },
'CONTROL Y': function() { alert('Y') }
});
rulesA.enable();
Recently I’ve been playing around keyboard event definition in a toy Domain-Specific Language so this caught my attention. I really don’t like Strings used as commands, the DSL I’m writing uses a fancier syntax for keyboard event handlers the focusing in removing some syntactic noise.
Here is the equivalent code using my Ruby Hotkeys DSL:
rules_a = @dsl.map {{
[shift] + [s] => lambda{ puts 'S'},
[crtl] + [y] => lambda{ puts 'Y'}
}}
The Hotkey DSL won’t be production-ready, it’s just a proof-of-concept of some ideas I have. Anyway I can imagine that getting something like this is JavaScript won’t be that hard. Actually, I reckon it is very easy to have something like this:
var rulesA = new Ojay.Keyboard.RuleSet([
hotkey([shift] + [S], function() { alert('S') }),
hotkey([ctrl] + [y], function() { alert('Y') })
]);
rulesA.enable();
It is still verbose but at least we have something less noisy and more expressive than strings.

Very interesting Philip…
God Job!
Why is [shift] + [S] more expressive then ‘SHIFT S’?
Hi Tetsuo,
It’s the same issue on what’s more expressive:
String myNumber = “1″; // or my_number = “1″
Integer myNumber = 1; // or my_number = 1
Using Strins to represent data types has a lot of semantic noise. Strings are used to store text that is meaningless for the program. Say you want to add two numbers:
String n1 = “2″;
String n2 = “3″;
String result = n1 + n3;
You would have to interpret the string and keep converting from how it is stored and what it really means. The same issue would be found in the hotkeys:
String ctrl = “CTRL”;
String alt = “ALT”;
String ctrlAlt = ctrl + ” ” + alt;
Above we had to add the ” ” string only because we are using strings. This concatenation takes you away from the keyboard domains as you don’t have to say “press the control key SPACE then the alt key”.
As for syntactic noise, SHIFT F is not something we use to represent key combinations. We often use the plus sign, as in CTRL + ALT + DEL
http://www.keyxl.com/aaa144e/63/Firefox-web-browser-keyboard-shortcuts.htm
The plus sign has meaning, it means that you should press both together.
cheers