aspose file tools*
The moose likes IDEs, Version Control and other tools and the fly likes Building an Internal DSL? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "Building an Internal DSL?" Watch "Building an Internal DSL?" New topic
Author

Building an Internal DSL?

Burk Hufnagel
Ranch Hand

Joined: Oct 01, 2001
Posts: 814
    
    3
I'm wondering the book describes how to create a DSL that can be used within a Java application. If so, how does one actually invoke a program written in the DSL from the Java application?


SCJP, SCJD, SCEA 5 "Any sufficiently analyzed magic is indistinguishable from science!" Agatha Heterodyne (Girl Genius)
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
ANTLR, for example, can generate Java code from your DSL, which makes it simple to use the resulting code in your Java application. (And all of the book's example code is Java.) The way you invoke the generated code depends on the kind of code you've generated.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

An "internal" DSL is written in the language itself--i.e. an internal Java DSL is just plain old Java code, and you'd call it the same way you'd call any other Java code.

As examples, consider Mockito or Hibernate queries (or that abomination of the regex DSL).
Burk Hufnagel
Ranch Hand

Joined: Oct 01, 2001
Posts: 814
    
    3
Lasse,
I did see that the book is using Java (makes sense since we're looking at it here) and ANTLR. not having used ANTLR, I was looking for a little more detail on how complicated a process it is to launch an program written in the DSL.
Burk
Burk Hufnagel
Ranch Hand

Joined: Oct 01, 2001
Posts: 814
    
    3
David Newton wrote:... o that abomination of the regex DSL


What makes regex an abomination? I don't use it often, but when I do I've been able to turn many lines of code into one or two.

Burk
Terence Parr
author
Ranch Hand

Joined: Jan 13, 2010
Posts: 35
Burk Hufnagel wrote:...I was looking for a little more detail on how complicated a process it is to launch an program written in the DSL.


Hi Burk, it's really up to you as to how you create the interface to your DSL. For example, my StringTemplate (ST) template engine is very easy to use from within Java (there our ports to Python or C#). It's a DSL for emitting structured text, whether that's e-mail or html or source code etc. Here's an example that generates an SQL statement:

Burk Hufnagel
Ranch Hand

Joined: Oct 01, 2001
Posts: 814
    
    3
Terence Parr wrote:Here's an example that generates an SQL statement:


Terrence,
Thanks. If I understand this properly then the "execution" of the template (DSL program?) would result in the String "SELECT salary from employees" being returned. Is that right?

This seems like a much simpler idea of what a DSL is than what I've been thinking. More useful too. I thought something would have to be much more complex to be considered a "real" DSL .

Thanks for providing a different point of view,
Burk
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Burk Hufnagel wrote:What makes regex an abomination?

You've misunderstood:

regex DSL

Burk Hufnagel
Ranch Hand

Joined: Oct 01, 2001
Posts: 814
    
    3
David Newton wrote:You've misunderstood:

regex DSL


Hmm... Reading the article, I see what you mean.

I mist admit that in some ways I like the humane interface they created, and I can see how it might make regex easier for newbies and people that seldom have a need for regex.

On the other hand, I kind of like the conciseness of the existing form. I don't know that I'd call it an abomination, but I don't think I'd want to use it either.

Burk
Terence Parr
author
Ranch Hand

Joined: Jan 13, 2010
Posts: 35
Burk Hufnagel wrote:If I understand this properly then the "execution" of the template (DSL program?) would result in the String "SELECT salary from employees" being returned. Is that right?


yep

This seems like a much simpler idea of what a DSL is than what I've been thinking. More useful too. I thought something would have to be much more complex to be considered a "real" DSL .


Personally, I think of the term DSL as having a connotation of small.
Burk Hufnagel
Ranch Hand

Joined: Oct 01, 2001
Posts: 814
    
    3
Terence Parr wrote:Personally, I think of the term DSL as having a connotation of small.


I think I like your style. Just big enough to do the job. Feels unix-ish - in a good way.
 
jQuery in Action, 2nd edition
 
subject: Building an Internal DSL?