Chapter 9. Writing Little Languages in PowerShell

The term little language was coined by Jon Bentley in a 1986 article he wrote for Communications of the ACM (http://bit.ly/akBpXr).[2] Little languages are also referred to as domain-specific languages, or DSLs.

Some little languages are intended to save development time and effort by allowing a developer to express his or her intentions at a much higher level of abstraction. This allows the developer’s programs to be much shorter than equivalent programs in other languages. The “little” part primarily refers to the scope of what the language tackles.

In this chapter, we will focus on using PowerShell as a better XML, building our own little language that can do more than just plain XML. Then, we’ll build another DSL to blend Graphviz, an open source graph visualization toolset, into the PowerShell ecosystem. This offers us a better way to manipulate abstractions when working with structural information like diagrams of abstract graphs and networks.

There are many examples of DSLs in the software industry—for example, MSBuild is a little language, to my way of thinking. While others may view MSBuild as a build system that reads a specific schema of XML as input, I see it as a domain-specific language in an XML format, specific to dealing with Visual Studio builds.

One of the challenges of using MSBuild with XML happens when we want to extend it. Adding a custom task to MSBuild requires dropping into C#, inheriting from some ...

Get Windows PowerShell for Developers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.