DSL概念、类别、为什么要写DSL 1

简介: DSL概念 Martin Fowler defines a domain-specific language (DSL) as “a computer language that’s targeted to a particular kind of problem, rather than a g...

DSL概念

Martin Fowler defines a domain-specific language (DSL) as “a computer language that’s targeted to a particular kind of problem, rather than a general purpose language that’s aimed at any kind of software problem”

Domain-specific languages aren’t a new idea by any means. DSLs have been around since long before the start of computing. People have always developed specialized

vocabularies for specialized tasks.

Regular expressions and SQL are similarly specialized languages:Both are languages designed for a narrow domain—text processing and database querying, respectively.

Both are focused on letting you express what you mean, not how the implementation should work—that’s left to some magic engine in the background.

The reason these languages are so successful is that the focus they offer is incredibly useful. They reduce the complexity that you need to handle, and they’re flexible in

terms of what you can make them do.

DSL类别

External DSLs

DSLs that exist outside the confines of an existing language. SQL and regular expressions are two examples of external DSLs.

Common tools for building external DSLs include Lex, Yacc, ANTLR, GOLD Parser,

and Coco/R, among others. Those tools handle the first stage, translating text in a

known syntax to a format that a computer program can consume to produce execut-

able output. The part about “producing executable output” is usually left as an exer-

cise for the reader.

Graphical DSLs

uses shapes and lines to express intent rather than using text.

UML is a good example of a graphical DSL

Microsoft has the Visual Studio DSL Tools, which is a framework that allows you

to build tools similar to the class designer and generate code with them.

Fluent interfaces

Fluent interfaces are ways to structure your API so that operations flow naturally and

provide more readable code. They tend to be valid only when used by developersdur-

ing actual development, which limits their scope compared to other DSLs.

new Pipeline("rhino.png")

.Rotate(90)

.Watermark("Mocks")

.RoundCorners(100, Color.Bisque)

.Save("fluent-rhino.png");

User.FindAll(

Where.User.City == "London" &&

Where.User.RegisteredAt>= DateTime.Now.AddMonths(-3)

);

registry.AddInstanceOf<IWidget>()

.WithName("DarkGreen")

.UsingConcreteType<ColorWidget>()

.WithProperty("Color").EqualTo("DarkGreen");

 Internal or embedded DSLs

Internal DSLs are built on top of an existing language, but they don’t try to remain

true to the original programming language syntax. They try to express things in a way

that makes sense to both the author and the reader, not to the compiler.

Obviously, the expressiveness of an internal DSL is limited by whatever constraints

are imposed by the underlying language syntax. You can’t build a good DSL on top of

C# or Java; they have too much rigidity in their syntax to allow it. You probably could

build a good DSL on C++, but it would probably include preprocessor macros galore,

and I wouldn’t place any bets on how maintainable it would be.

The popular choices for building internal DSLs are dynamic languages; Lisp and

Smalltalk were probably the first common choices. Today, people mostly use Ruby,

Python, and Boo.

Rake

task :default => [:test]

task :test do

ruby "test/unittest.rb"

end

 

 

Other features that usually appear in dynamic languages are also useful when building DSLs: closures, macros, and duck typing.


The major advantage of an internal DSL is that it takes on all the power of the lan-

uage it’s written for. You don’t have to write the semantics of an if statement, or

edefine operator precedence, for instance. Sometimes that’s useful, and in one of my

DSL implementations I did redefine the if statement, but that’s probably not a good

hing to do in general, and it’s rarely necessary.

 

A DSL built on top of an existing language can also be problematic, because you

want to limit the options of the language to clarify what is going on. The DSL

houldn’t be a full-fledged programming language; you already have that in the base

anguage, after all.

The main purpose of an internal DSL is to reduce the amount of work required to

make the compiler happy and increase the clarity of the code in question.

The other purpose is to expose the domain. A DSLshould be readable by someone who is familiar with the domain, not the programming language.

为什么要写DSL

l 技术DSL:Making a technical issue or task simpler

This works well if your target audience is developers.

l 业务DSL:Expressing rules and actions in a way that’s close to the domain and understandable to businesspeople

A business DSL needs to be (at the very least) readable to a businessperson with no

background in programming. This type of DSL is mainly expressive in terms of the

domain, and it has a lot less emphasis on the programming features that may still exist.

l 扩展、自动化类的DSL : Automating tasks and actions, usually as part of adding scriptability and extensibility features to your applications.Automatic or extensible DSLs may also be called IT DSLs. This type of DSL is often

used to expose the internals of an application to the outside world.

如:Modern games are usually engines configured with some sort of scripting language. Another use for this style of DSL would be to get into the internals of an application and manage it.

 

参考图书: DSLs in Boo Domain-Specific Languages in .NET 

相关文章
|
监控 Java
如何使用VisualVM分析内存泄漏?具体流程看这里
如何使用VisualVM分析内存泄漏?具体流程看这里
1533 1
|
数据采集 存储 JSON
【专栏】网络爬虫与数据抓取的基础知识,包括爬虫的工作原理、关键技术和不同类型
【4月更文挑战第27天】本文介绍了网络爬虫与数据抓取的基础知识,包括爬虫的工作原理、关键技术和不同类型。通过实例展示了如何构建简单爬虫,强调实战中的环境搭建、目标分析及异常处理。同时,文章探讨了法律、伦理考量,如尊重版权、隐私保护和合法用途,并分享了应对反爬策略。最后,倡导遵守数据抓取道德规范,以负责任的态度使用这项技术,促进数据科学的健康发展。
1413 2
|
12月前
|
缓存 搜索推荐 数据挖掘
TPS和QPS是什么?都是什么区别?
TPS和QPS是什么?都是什么区别?
8715 4
|
存储 数据管理 数据库
理解数据库中的参照完整性
【6月更文挑战第13天】数据库设计旨在创建和维护企业的数据管理系统,确保数据完整性和消除冲突。好的数据库设计应减少冗余,保证信息准确完整,并满足处理和报告需求。设计工具包括E-R图和UML等。
1106 2
理解数据库中的参照完整性
|
Java 应用服务中间件 数据库连接
【软件版本】软件版本GA、RC、Beta、Alpha等的详细解释和含义
【软件版本】软件版本GA、RC、Beta、Alpha等的详细解释和含义
635 0
|
Kotlin
kotlin协程withContext的使用
kotlin协程withContext的使用
410 0
|
数据库 Python
什么问题会导致404?如何解决404?
什么问题会导致404?如何解决404?
|
安全 程序员 数据库
进程间同步的方式有哪些
进程间同步的方式有哪些
947 0
|
算法 调度 决策智能
Python高级算法——模拟退火算法(Simulated Annealing)
Python高级算法——模拟退火算法(Simulated Annealing)
1559 1
|
XML 自然语言处理 IDE
一杆到底:DSL 领域特定语言
一、DSL了解1、DSL介绍DSL(Domain Specific Language)是针对某一领域,具有受限表达性的一种计算机程序设计语言。 常用于聚焦指定的领域或问题,这就要求 DSL 具备强大的表现力,同时在使用起来要简单。说到DSL,大家也会自然的想到通用语言(如Java、C等)。为什么没有一种语言同时 兼具『简洁』和『业务表达』能力呢?从信息论本质上来讨论这个问题,每个语言的程序都可以抽
17147 0
一杆到底:DSL 领域特定语言