A framework to create embedded Domain-Specific Languages in Scala
Latest commit f7ebf33 Oct 28, 2018
Type Name Latest commit message Commit time
Failed to load latest commit information.
Dsl Remove derivedContinuationDsl Sep 30, 2018
compilerplugins-BangNotation Update scalatest to 3.0.6-SNAP2 Oct 5, 2018
compilerplugins-ResetEverywhere Type check the return type for explicit typed methods Aug 7, 2018
domains-cats Merge pull request #201 from scala-steward/update/cats-core-1.4.0 Oct 6, 2018
domains-scalaz Merge pull request #195 from scala-steward/update/invariant-2.1.0 Oct 6, 2018
domains-task Update scalatest to 3.0.6-SNAP2 Oct 5, 2018
keywords-AsynchronousIo/src/main/scala/com/thoughtworks/dsl/keywords Suppress a compiler warning Jul 23, 2018
keywords-Await Update scalatest to 3.0.6-SNAP2 Oct 5, 2018
keywords-Catch Update scalatest to 3.0.6-SNAP2 Oct 5, 2018
keywords-Continue Update scalatest to 3.0.6-SNAP2 Oct 5, 2018
keywords-Each Update scalatest to 3.0.6-SNAP2 Oct 5, 2018
keywords-Fork Revert "Rename Continue to Abort" Sep 20, 2018
keywords-Get/src/main/scala/com/thoughtworks/dsl/keywords Improve naming Aug 28, 2018
keywords-Monadic Improve Scaladoc Sep 30, 2018
keywords-NoneSafe/src/main/scala/com/thoughtworks/dsl/keywords Add NoneSafe keyword Sep 30, 2018
keywords-NullSafe Update scalatest to 3.0.6-SNAP2 Oct 5, 2018
keywords-Put Update scalatest to 3.0.6-SNAP2 Oct 5, 2018
keywords-Return Update scalatest to 3.0.6-SNAP2 Oct 5, 2018
keywords-Shift Remove workaround for scala-js/scala-js#3415 Sep 9, 2018
keywords-Using Update scalatest to 3.0.6-SNAP2 Oct 5, 2018
keywords-Yield Update scalatest to 3.0.6-SNAP2 Oct 5, 2018
package Merge branch '1.0.x' into update/scala-parser-combinators-1.1.1 Oct 6, 2018
project Update sbt-scalajs-crossproject to 0.6.0 Oct 5, 2018
.gitignore Merge remote-tracking branch 'remotes/origin/features/sbt-best-practi… Dec 8, 2017
.sbtopts Revert "Revert "Decrease the memory allocated for JVM"" Sep 3, 2018
.scalafmt.conf Set up maxColumn to 120 Nov 3, 2016
.travis.yml Use sudo-enabled Travis CI environment Aug 15, 2018
LICENSE Initial commit Oct 14, 2017
README.md Update README.md Oct 28, 2018
build.sbt Add NoneSafe keyword Sep 30, 2018
deploy.sbt.disabled Setting version to 1.0.0-RC21 Oct 10, 2018
version.sbt Setting version to 1.0.0-SNAPSHOT Oct 10, 2018



Build Status Latest version Scaladoc Join the chat at https://gitter.im/ThoughtWorksInc/Dsl.scala

Dsl.scala is a framework to create embedded Domain-Specific Languages in Scala.

A DSL author is able to create language keywords by implementing the Dsl trait, which contains only one abstract method to be implemented. No knowledge about Scala compiler or AST macros is required.

DSLs written in Dsl.scala are collaborative with others DSLs and Scala control flows. A DSL user can create functions that contains interleaved DSLs implemented by different vendors, along with ordinary Scala control flows.

We also provide some built-in keywords, including:

  • The Await keyword for creating memoized asynchronous values as Scala Futures, similar to the await / async keywords in C#, Python and JavaScript.
  • The Shift keyword for creating asynchronous tasks as delimited continuations, similar to the shift operator in Scala Continuations.
  • The AsynchronousIo.Connect, AsynchronousIo.Accept, AsynchronousIo.Read and AsynchronousIo.Write keyword for perform I/O on an asynchronous channel.
  • The Yield keyword for generating lazy streams, similar to yield in C#, Python and JavaScript.
  • The Each keyword for iterating on a collection, similar to the list comprehension feature in Scala, Haskell, OCaml, Python and Lisp.
  • The Continue keyword LDK for skipping an element in a Each collection comprehension, similar to the native continue keyword in C/C++ or the mzero in Haskell.
  • The Fork keyword for duplicating current context, similar to the fork system call in POSIX.
  • The Return keyword for early returning, similar to the native return keyword in Scala.
  • The Using keyword to automatically close resources when exiting a scope, similar to the native using keyword in C#.
  • The Monadic keyword for creating Scalaz or Cats monadic control flow, similar to the !-notation in Idris.
  • The NullSafe keyword for the null safe operator, similar to the ? operator in Kotlin and Groovy.
  • The NoneSafe keyword for the None safe operator, similar to the Maybe monad in Haskell.

All the above keywords can be used together with each others. For example you can perform list comprehension to manipulate native resources in an asynchronous task by using Each, AutoClose and Shift together.

Getting Started

Suppose you want to create a random number generator. The generated numbers should be stored in a lazily evaluated infinite stream, which can be built with the help of our built-in domain-specific keyword Yield.

So, you need to add the library that contains the implementation of the keyword Yield:

// Add the following setting in your build.sbt 
libraryDependencies += "com.thoughtworks.dsl" %% "keywords-yield" % "latest.release"

And the Dsl.scala compiler plug-ins that are shared by all DSLs:

// Add the following settings in your build.sbt 
addCompilerPlugin("com.thoughtworks.dsl" %% "compilerplugins-bangnotation" % "latest.release")
addCompilerPlugin("com.thoughtworks.dsl" %% "compilerplugins-reseteverywhere" % "latest.release")

The random number generator can be implemented as a recursive function that produces the next random number in each iteration.

import com.thoughtworks.dsl.keys.Yield
def xorshiftRandomGenerator(seed: Int): Stream[Int] = {
  val tmp1 = seed ^ (seed << 13)
  val tmp2 = tmp1 ^ (tmp1 >>> 17)
  val tmp3 = tmp2 ^ (tmp2 << 5)

Note that a keyword is a plain case class. You need a ! prefix to the keyword to activate the DSL.

It's done. We can test it in ScalaTest:

val myGenerator = xorshiftRandomGenerator(seed = 123)
myGenerator(0) should be(31682556)
myGenerator(1) should be(-276305998)
myGenerator(2) should be(2101636938)

The call to xorshiftRandomGenerator does not throw a StackOverflowError because the execution of xorshiftRandomGenerator will be paused at the keyword Yield, and it will be resumed when the caller is looking for the next number.


(Feel free to add your project here)