Programming Language Theory
Domain-Specific Languages
SQL was written in the 1970s and remains the main language for working with data. CSS specifies design without programming. GraphQL changed how frontends fetch data. DSLs are not exotic; they are the languages developers use every day.
- **Terraform HCL**: a declarative DSL for infrastructure. HashiCorp created the language that manages AWS, GCP, and Azure resources. 10M+ users.
- **Kubernetes YAML/Jsonnet**: declarative DSLs for container orchestration. Jsonnet is a DSL for generating JSON/YAML configurations.
- **dbt (data build tool)**: a SQL DSL for transformations in a data warehouse. Adds modularity, testing, and documentation to SQL.
Internal DSL
An internal DSL is a DSL built with host-language features. It looks like specialized code but is really plain functions and methods. Benefits: IDE support, integration with existing infrastructure, no separate compiler. Examples: Gradle, RSpec, Ktor routing.
What is the main advantage of an internal DSL over an external one?
External DSL
An external DSL is a standalone language with its own parser. Examples: SQL, CSS, regular expressions, YAML, Protocol Buffers. Benefits: any syntax you want, usable by non-programmers, easier to version.
Why does SQL remain the most widely used external DSL after 60+ years?
Embedded DSL
An embedded DSL is an external DSL embedded into a host language via special syntax or a library. Template Haskell quasiquotes, inline SQL in Rust via sqlx!, regex literals. The best of both worlds: external convenience plus internal integration.
What makes the sqlx! macro fundamentally different from ordinary string SQL queries?
Language Workbenches
A language workbench is a tool for creating DSLs with IDE support out of the box. JetBrains MPS lets you create language extensions with a projectional editor. Xtext generates an Eclipse plugin from an EBNF grammar.
A DSL is needless complexity. Use a general-purpose language instead
A DSL significantly reduces cognitive load for a specific task. SQL reads better than equivalent Python for relational queries.
Domain experts can read and write a DSL without knowing the general-purpose language. SQL developers do not know Python. Business analysts write SQL, not Java. DSLs lower the barrier of entry to a domain.
What is projectional editing in JetBrains MPS?
Summary
- **Internal DSL**: host language plus API design equals a DSL-like syntax. Gradle KTS, Ktor, RSpec. IDE support comes for free.
- **External DSL**: separate parser, custom syntax. SQL, GraphQL, CSS. For domain experts, not only developers.
- **Embedded DSL**: external DSL embedded in the host via macros or quasi-quoting. sqlx! SQL in Rust with compile-time checks.
- **Language workbenches**: JetBrains MPS (projectional), Xtext (grammar to IDE). Building DSLs with IDE support.
Related topics
DSLs apply language design in practice:
- Syntax design — DSL design applies syntax design principles to a specific domain.
- DSL compilers — Compiling a DSL into another language or machine code.
Вопросы для размышления
- Terraform is a declarative DSL for infrastructure. Why is declarativeness (what, not how) especially important for infrastructure-as-code?
- SQL has lived 50+ years without inherent changes. What is so right about its design that it has outlasted several generations of technology?
- JetBrains MPS is a projectional editor without text files. How does that reshape git workflows, code review, and merge conflicts?