Reputation: 83
I am on working on SQL grammars using Rascal MPL as a Language workbench I have noticed there are a lot of similarities between the various dialects. How can I implement an OOP-style inheritance to facilitate code reuse I will want to have a base SQL grammar that other dialects can inherit and override parts of. I know that Rascal supports composition but not inheritance, is there a workaround?
Upvotes: 2
Views: 104
Reputation: 6696
Rascal grammars work like "mixins" in OO:
In one module you'd write:
module A
syntax Stat = "if" Exp "then" Stat "else" Stat;
Then in another you would write
module B
syntax Stat = "while" Exp "do" Stat;
And finally in a third module you combine the two:
module C
extend A;
extend B;
Now the syntax definitions are fused such that also at the recursive positions of Stat
and Exp
the additional alternatives are active.
So this is not like composition or inheritance, but like the kind of open recursion you get from mixins.
To be able to handle processing these trees, the functions in Rascal are also openly extensible, also for their recursive calls:
module A
syntax Stat = "if" Exp "then" Stat "else" Stat;
int count(`if <Exp e> then <Stat s1> else <Stat s2>`)) = 1 + count(e) + count(s1) + count(s2)
module B
syntax Stat = "while" Exp "do" Stat;
int count(`while <Exp e> do <Stat s1>`)) = 1 + count(e) + count(s1)
module C
extend A;
extend B;
Here after the extend
the recursive calls for count
will deal with the new alternatives of count
(i.e. the case for the while loop from the other module).
If you don't use dynamic dispatch (overloading) like this, but hide the processing inside of function bodies (visits and switches, or nested if-then-elses) then your language processor is not composable or extensible.
Upvotes: 0
Reputation: 173
Yes, rascal by design allows for modular grammars, that can be composed. The extend
keyword will be the one you need, if allows you to extend an existing grammar in a different module.
Also, might be interesting to read this comparison to OO.
Upvotes: 1