Wednesday, November 12, 2008

DSL for Banking - interactive DSLs

A few weeks ago I posted an M model for a banking transactions. I forgot to post the DSL.
 
Here's a simple DSL that would be quite easy to use. And, it only took me 10 minutes to write it.

module Banking
{
    language BankingLanguage
    {
        interleave Ignorable = ' ' | '\n' | '\t' | '\r';
        
        syntax Main = t:Transaction+
            => Transactions { t };
            
        syntax Transaction =    
            t:TransactionKind 
                a:Amount 
                Transitions 
                Account 
                AccountNumber => { Kind { t }, Amount {a }};

        token Account = "Account";
        token Transitions = "from" | "to";
        token AccountNumber = Digit+;
        token TransactionKind = "Deposit" | "Withdrawal";
        token Amount = CurrencyQualifier? DecimalNumber;
        token CurrencyQualifier = "$";
        token Digit = '0'..'9';
        token DigitWithoutZero = Digit - '0';
        token DecimalNumber = DigitWithoutZero Digit* '.'? Digit+;
    }
}

Here's some sample input:

Withdrawal 200.00 from Account 5
Deposit $3039483.00 to Account 4932

Now, SpankyJ and I were talking (IM'ing actually).

Wouldn't it be cool if you could write a grammar that defining an iteractive textual experience that you could then deploy to a Intellipad session?

Something like this, where you indicate that the syntax is interactive causing the parser to be re-executed with each parse episode termined by the named token, in this case "loop".

         @{Interactive[loop]}
        syntax Main = t:Transaction+
            => Transactions { t };
            
        syntax Transaction =    
            t:TransactionKind 
                a:Amount 
                Transitions 
                Account 
                AccountNumber loop => { Kind { t }, Amount {a }};

        token loop = "\n" | "\r" | "\r\n";

Then, you could have an iterative, transaction-like experience with this particular DSL where people just type in transactions, and get immediate feedback. 

I suppose you'd want the runtime to be hooked up as well... Hmmm - just thinking out loud :)

1 comment:

Colin Jack said...

"Withdrawal 200.00 from Account 5
Deposit $3039483.00 to Account 4932"

I'm not entirely convinced thats what most business centric DSL's will look like, from what I've seen the examples most people are working on have more logic in them that that. More like:

order.IsPreferredCustomer and order.Amount > n:
...