background preloader

Dsl

Facebook Twitter

Groovy

Jetbrains. Fun DSL Query Language for Scala. (Quick Note: If you just want to skip to the code, it's interspersed throughout the post. But, for your convenience, I've also put it all in one place at the bottom of the post.) I've done it! Sick as a dog yesterday, I refactored the crap out of my Scrabble stuff, to create a really powerful query DSL. The kicker? It's only 24 lines of code! It allows you to do all sorts of crazily powerful queries. I'd love to talk about the code smells of my last post, and how I ended up where I did, but to be honest, the code seems so much different now that it would be difficult. Once again I'll start with the test code, which models the original requirements. Val dictionary = new Dictionary(getWords()) val tray = "defilnr" // the 4 original requirements val all = dictionary find includes_all(tray) val any = dictionary find includes_any(tray) val only = dictionary find includes_only(tray) val all_and_only = dictionary find (includes_all(tray) and includes_only(tray)) print(all, only, all_and_only)

Parsing domain-specific languages with JavaCC. Www.qi4j.org - www.qi4j.org. When Scala DSLs Fail. The hot new contender in the space of interal DSLs (domain-specific languages as libraries) is Scala. Scala is, as is implied by its name, a language that is designed to scale from small one-off scripts to large enterprise applications. It is a statically typed language, and in many ways can be seen as a successor to Java. It also compiles to JVM bytecode. Similar to Ruby, Scala has a flexible syntax, which makes it an interesting choice to develop internal DSLs for (like Ruby on Rails). The most popular web application framework for Scala is Lift. Lift is an expressive and elegant framework for writing web applications.

In Lift, more configuration is done in Scala code rather than in XML files like in a framework like Seam. Mistake #1: mistyping a class name What happens if my satanistic fingers force me to type hellWorld, instead of helloWorld: <lift:surround with="default" at="content"> <h2>Welcome to your project! We get no error at compile time. In the console we see the following; Domain Specific Languages and Haskell. DSLs in JavaScript. Writing a domain specific language DSL with python. A domain-specific language is a piece of software designed to be useful for a specific task in a fixed problem domain, they’re gaining popularity because they enhance productivity and reusability of artifacts. DSLs also enable expression and validation of concepts at the level of abstraction of the problem domain, this approach is very useful when you need to describe a user interface, a business process, a database, or the flow of information.

The DSL concept isn’t new after all, special-purpose programming languages and all kinds of modeling, specification languages have always existed, but this term rise due the popularity of domain-specific model. You can easily implement dsls using the ruby language, Java or even C# if you prefer, but this isn’t the main propose of this article. The sine qua non become visible when I was implementing a simple test case with python. Import tokenize import codecs , cStringIO , encodings from encodings import utf_8 class StreamReader ( utf_8 . Spawning a Thread, the D way. Spawning a thread in non-functional languages is considered a very low-level primitive. Often or takes a function pointer and an untyped (void) pointer to “data”. The newly created thread will execute the function, passing it the untyped pointer, and it’s up to the function to cast the data into something more palatable.

This is indeed the lowest of the lowest. It’s the stinky gutters of programming. Isn’t it much nicer to create a or a object and let the ugly casting be done under the covers? As they say, Cooks cover their mistakes with sauces; doctors, with six feet of dirt; language designers, with objects. Requirements But enough ranting! Should take an arbitrary function as the main argument. I wish I could use the more precise race-free type system that I’ve been describing in my previous posts, but since I can’t get it into D2, there’s still a little bit of “programmer beware” in this implementation. These requirement seem like a tall order for any language other than D. Unit Tests. MetaEdit+ and DSM resources. What is all the fuss about how you can write DSLs in Lisp? Domain Specific Languages in Erlang. Language Workbenches: The Killer-App for Domain Specific Languag. Most new ideas in software developments are really new variations on old ideas.

This article describes one of these, the growing idea of a class of tools that I call Language Workbenches - examples of which include Intentional Software, JetBrains's Meta Programming System, and Microsoft's Software Factories. These tools take an old style of development - which I call language oriented programming and use IDE tooling in a bid to make language oriented programming a viable approach. Although I'm not enough of a prognosticator to say whether they will succeed in their ambition, I do think that these tools are some of the most interesting things on the horizon of software development. Interesting enough to write this essay to try to explain, at least in outline, how they work and the main issues around their future usefulness.

For a long time there's been a style of software development that seeks to describe software systems using a collection of domain specific languages. Lisp GUI Builders. Language Oriented Programming: The Next Programming Paradigm. I think the problem with claiming this is the "next big paradigm" is that the average programmer, and frankly, even the merely above-average programmer, is not going to be competent to design a non-trivial special-purpose language of any value.

Once you step out of the domain of totally-trivial languages, language design immediately becomes tricky, subtle, and prone to exotic interactions and quirky tradeoffs that even our absolute best teams of language designers can only mitigate, not eliminate... and it's not all going to be "absolute best" teams, after all. And... then you want multiple languages to be sitting there interacting, too? That's not even possible without somehow limiting those interactions ("thy languages shalt have lexical binding that works thusly"), and now you're just making another meta-language like Lisp, which, presumably, doesn't fit the bill.

DSL Composition techniques in Scala. One of the benefits of being on Twitter is the real time access to the collective thought streams of many great minds of our industry. Some time back, Paul Snively pointed to this paper on Polymorphic Embedding of DSLs in Scala. It discusses many advanced Scala idioms that you can implement while designing embedded DSLs.

I picked up a couple of cool techniques on DSL composition using the power of Scala type system, which I could use in one of my implementations. A big challenge with DSLs is composability. Consider this simple language interface for salary processing of employees .. Here's a sample implementation of the above interface .. Irrespective of the number of implementations that we may have, the accounting process needs to record all of them in their books, where they would like to have all separate components of the salary separately from one single API. Here's the definition of the Accounting trait that embeds the semantics of the other language that it composes with .. Patterns in Internal DSL implementations. I have been thinking recently that classifying DSLs as Internal and External is too broadbased considering the multitude of architectural patterns that we come across various implementations.

I guess the more interesting implementations are within the internal DSL genre, starting from plain old fluent interfaces mostly popularized by Martin Fowler down to the very sophisticated polymorphic embedding that has recently been demonstrated in Scala. I like to use the term embedded more than internal, since it makes explicit the fact that the DSL piggybacks the infrastructure of an existing language (aka the host language of the DSL). This is the commonality part of all embedded DSLs. But DSLs are nothing more than well-designed abstractions expressive enough for the specific domain of use. On top of this commonality, internal DSL implementations also exhibit systematic variations in form, feature and architecture. Plain Old Smart APIs, Fluent Interfaces Purely Embedded typed DSLs. LESS - Leaner CSS. Boolean Algebra Internal DSL in Scala (aka fun with Unicode name. In Scala, operator names can be characters from the unicode set of mathematical symbols (Sm) or other symbols (So).

That can lead to interesting possibilities: val Π = Math.Pi def √(x:Double)=Math.sqrt(x) val x= √(9*Π) Here, "Π" is just another constant and "√" is a function like "q". (To use unicode in eclipse, you have to go to Preferences->General->Workspace, and change the text encoding to UTF-16) Another example: with the ∑ and ∏ you can have define summation and the product of a sequence as follows: def ∑(r:Range)(f:Int =>Int)=r.reduceLeft(_+ f(_)) def ∏(r:Range)(f:Int =>Int)=r.reduceLeft(_* f(_)) And now we can write: val s= ∑(1 to 100)(x=>x^2) val p= ∑(1 to 100 by 2)(x=>x^2) val y= ∏(1 to 30 by 3)(_) Of course, I'm not advocating using Scala as a substitute of a specific mathematical tool, besides, there's already a "language with symbols" (btw, I wonder if one can make a Scala "skin" that looks like APL).

The only prefix operators that can be defined in Scala are ‘+’, ‘’, ‘!’ Introduction to Domain Specific Languages. Combinators for Pretty Printers, Part 1. Scala has a flexible and powerful syntax that is very convenient when building libraries. Among the best examples of Scala's syntactic power put to excellent use are the parser combinator libraries in the standard distribution. Code that uses Scala's parser combinators looks remarkably like BNF (the standard notation for describing formal language grammars) but is actually an executable program which parses the language it describes. Consider the BNF for a simple expression grammar: And the equivalent Scala code using parser combinators: def expr = sumdef sum = product ~ rep(("+" | "-") ~ product)def product = power ~ rep(("*" | "/") ~ power)def power = factor ~ opt("^" ~ factor)def factor = "(" ~ expr ~ ")" | variable | constantdef variable = identdef constant = floatLit If you want to learn more about Scala's parser combinators, there are several good articles on them by Debasish Ghosh, Daniel Spiewak, Jim McBeath, and Ted Neward.

A Pretty Printer Algebra Much better! Purpose-Built Languages. Purpose-Built Languages While often breaking the rules of traditional language design, the growing ecosystem of purpose-built “little” languages is an essential part of systems development. Mike Shapiro, Sun Microsystems In my college computer science lab, two eternal debates flourished during breaks from long nights of coding and debugging: “emacs versus vi?” ; and “what is the best programming language?” Later, as I began my career in industry, I noticed that the debate over programming languages was also going on in the hallways of Silicon Valley campuses.

I have always found the notion of best language to be too subjective and too dependent on the nature of the programming task at hand. In examining these questions I have found it particularly interesting to look not at the battle of the heavyweights, but rather at their less well-studied offshoots, the purpose-built languages. Evolution Trumps Intelligent Design Thus, a little language was born. Address $<proc . Symbiosis. The Need for More Lack of Understanding. People are always claiming that if only there was more understanding in the world, it would be a better place. This post will argue that less is more: we need less understanding - specifically more not understanding. A couple of weeks ago I gave a talk at DSL Dev Con. One of the encouraging things that was evident there was the increased understanding that not understanding is important. Tangent: While I'm advertising this talk, I might as well advertise my interview on Microsoft's channel 9 which explains the motivation for Newspeak and its relation to cloud computing.

Several programming languages support a mechanism by which a class or object can declare a general-purpose handler for method invocations it does not explicitly support. Smalltalk was, AFIK, the first language to introduce this idea. Aficionados of these languages know that this is an extremely useful mechanism. We’ll use an example from my talk at DSL Dev Con. Class Shell { public Collection... an infinity of other stuff} Program like you mean it. In my mind one of the best features of Scala is the ability to write a program in a manner that makes sense for the problem space. I am not discussing external DSLs in this discussion. Although external DSLs have their use I am reluctant to use them too often, Martin Fowler has a number of informative blog entries regarding the uses and dangers of external DSLs.

Instead I want to discuss the ability to write nice internal DSLs in Scala. Scala provides a number of strategies in which a library author can create a library that can be used in a way that makes sense given the problem. GUI programming may make sense with a declarative syntax. Parser coding might be improved with a DSL that closely mirrors formal grammar. Why is this such an important issue? First one there is the up-front productivity gain. Some of the issues that make XSLT a burden to use: XML is horribly verbose making it slow to program with and to be able to tell what are the important parts of the program. But I digress. Little Language. DSLs: The Good, the Bad, and the Ugly. DSLs - A powerful Scala feature. Domain Specific Languages (DSL) written in Scala have become powerful tool in the hands of Scala programmers. In the original language design great care was taken to ensure that the syntax would allow programmers to create natural looking DSLs.

Spend a moment to enjoy Michael Fogus's humour and the clever use he makes of implicits to create "Baysick". It looks like BASIC feels like BASIC but it's a Scala DSL. Then you may like to follow some other links to see DSL examples for Financial Asset Management, Apache Camel Services and OSGi. Michael decided to extend the simple BASIC interpreter posted by Szymon Jachim to the Scala Nabble forums. Here is a simple Lunar Lander game he has written using his BASIC-like DSL. More information can be found on his site and the source code is located at Github. If you are new to Scala or to DSLs you may like to watch Bill Venners video "The Feel Of Scala" where he both talks about Scala the language and DSLs in the context of program testing.

-= Baysick: A Scala DSL Implementing BASIC =- Scala DSL - EIP. DSL supported Icon The Scala DSL supports every DSL from the Java DSL. On this page we have examples for a number of the EIPs. You can check the unit test source code for the Scala Component to find more examples. Messaging systems Pipeline There is a simple syntax available for specifying pipeline, by simple putting to or → between the different steps in the pipeline. For more advanced use cases, you can also use a block-based syntax, where every step in the pipeline starts with either to or →. Filter For a message filter, use the when() method with a parameter of type The Exchange ⇒ Boolean.

Once again, if you need to specify a more advanced route, you can use the more elaborate syntax. Messaging channels Dead letter channel The dead letter channel can be created with the syntax similar to the one used in Java DSL. You can also use different error handler available for the Java DSL. Message routing Aggregator The aggregator EIP aggregates messages based on some message correlation criteria. Delayer. GUI Building with Scala. Comparing DSLs in Ruby and Scala.

BusinessReadableDSL. What is all the fuss about how you can write DSLs in Lisp? More On My Scala Query DSL. Two Lightweight DSLs for Rich UI Programming. Using Scala for Domain-Specific-Languages. Domain Specific Languages. LIL, The Little Implementation Language. Rascal-haskell. If It&#039;s Not Nailed Down, Steal It. Fear of parsers? Deriving a Virtual Machine.

Dow.ngra.de &quot; Blog Archive &quot; Typesafe DSLs in Java: Pa.