1. Trang chủ
  2. » Công Nghệ Thông Tin

dsls in action

377 467 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 377
Dung lượng 8,35 MB

Nội dung

MANNING Debasish Ghosh FOREWORD BY JONAS BONÉR IN ACTION www.it-ebooks.info DSLs in Action www.it-ebooks.info www.it-ebooks.info DSLs in ACTION DEBASISH GHOSH MANNING Greenwich (74° w. long.) www.it-ebooks.info For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 180 Broad St. Suite 1323 Stamford, CT 06901 Email: orders@manning.com ©2011 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Icons in the book are from www.picol.org, used under license as described at http://creativecommons.org/licenses/by-sa/3.0/ Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. Development Editor: Cynthia Kane 180 Broad St. Copyeditor: Joan Celmer Suite 1323 Typesetters: Dennis Dalinnik Stamford, CT 06901 Cover designer: Marija Tudor ISBN 9781935182450 Printed in the United States of America 12345678910 –MAL–16151413121110 www.it-ebooks.info To my grandfather, who taught me my first alphabet www.it-ebooks.info vi vi brief contents PART 1 INTRODUCING DOMAIN-SPECIFIC LANGUAGES . 1 1 ■ Learning to speak the language of the domain 3 2 ■ The DSL in the wild 25 3 ■ DSL-driven application development 54 PART 2 IMPLEMENTING DSLS 85 4 ■ Internal DSL implementation patterns 87 5 ■ Internal DSL design in Ruby, Groovy, and Clojure 128 6 ■ Internal DSL design in Scala 166 7 ■ External DSL implementation artifacts 211 8 ■ Designing external DSLs using Scala parser combinators 241 PART 3 FUTURE TRENDS IN DSL DEVELOPMENT 275 9 ■ DSL design: looking forward 277 www.it-ebooks.info vii contents foreword xiii preface xv acknowledgments xvii about the book xix PART I INTRODUCING DOMAIN-SPECIFIC LANGUAGES 1 1 Learning to speak the language of the domain 3 1.1 The problem domain and the solution domain 4 The problem domain 4 ■ The solution domain 4 1.2 Domain modeling: establishing a common vocabulary 6 Benefits of a common vocabulary 7 1.3 Introducing DSLs 8 What’s a DSL? 10 ■ Popular DSLs in use 12 Structure of a DSL 14 1.4 Execution model of a DSL 15 1.5 Classifying DSLs 17 Internal DSLs 18 ■ External DSLs 18 Nontextual DSLs 19 www.it-ebooks.info CONTENTS viii 1.6 When do you need a DSL? 20 The advantages 20 ■ The disadvantages 21 1.7 DSLs and abstraction design 22 1.8 Summary 23 1.9 References 24 2 The DSL in the wild 25 2.1 Building your first Java DSL 26 Setting up the common vocabulary 27 ■ Your first Java implementation 28 2.2 Making friendlier DSLs 32 Externalizing the domain with XML 32 ■ Groovy: a more expressive implementation language 33 Executing the Groovy DSL 35 2.3 DSL implementation patterns 36 Internal DSL patterns: commonality and variability 37 External DSL patterns: commonality and variability 45 2.4 Choosing DSL implementations 50 2.5 Summary 52 2.6 References 53 3 DSL-driven application development 54 3.1 Exploring DSL integration 55 Why you should care about DSL integration 56 3.2 Internal DSL integration patterns 58 Using the Java 6 scripting engine 60 ■ Using a DSL wrapper 64 ■ Language-specific integration features 73 ■ Spring-based integration 75 3.3 External DSL integration patterns 76 3.4 Handling errors and exceptions 78 Naming an exception 79 ■ Handling incorrect typing errors 80 Handling exceptional business conditions 81 3.5 Managing performance 82 3.6 Summary 83 3.7 References 84 www.it-ebooks.info CONTENTS ix PART II IMPLEMENTING DSLS 85 4 Internal DSL implementation patterns 87 4.1 Filling your DSL toolbox 88 4.2 Embedded DSLs: patterns in metaprogramming 90 Implicit context and Smart APIs 91 ■ Reflective metaprogramming with dynamic decorators 96 Reflective metaprogramming with builders 102 Lessons learned: metaprogramming patterns 105 4.3 Embedded DSLs: patterns with typed abstractions 106 Higher-order functions as generic abstractions 106 Using explicit type constraints to model domain logic 113 Lessons learned: thinking in types 117 4.4 Generative DSLs: boilerplates for runtime generation 118 How generative DSLs work 118 ■ Ruby metaprogramming for concise DSL design 119 4.5 Generative DSLs: macros for compile-time code generation 122 Metaprogramming with Clojure 122 ■ Implementing the domain model 123 ■ The beauty of Clojure macros 125 4.6 Summary 126 4.7 References 127 5 Internal DSL design in Ruby, Groovy, and Clojure 128 5.1 Making DSLs concise with dynamic typing 129 Readability 130 ■ Duck typing 131 ■ Metaprogramming— again! 133 ■ Why Ruby, Groovy, and Clojure? 134 5.2 A trade-processing DSL in Ruby 135 Getting started with an API 136 ■ A little bit of monkey-patching 139 ■ Rolling out a DSL interpreter 140 Adding domain rules as decorators 143 5.3 The order-processing DSL: the final frontier in Groovy 148 The order-processing DSL so far 148 ■ Controlling the scope of metaprogramming 149 ■ Rounding it off 152 www.it-ebooks.info [...]... patterns 181 ■ 6.5 Modeling business rules with a DSL 182 Pattern matching as an extensible Visitor 183 Enriching the domain model 184 Calculating tax and fee business rules in a DSL 187 ■ ■ 6.6 Stitching ’em all together 190 More abstraction with traits and types 190 components concrete 192 6.7 Composing DSLs ■ Making domain 193 Composing using extensions 194 Composing different DSLs using hierarchical composition... attributes in text are also presented in fixed-width font Source code can be verbose and detailed in order to provide a meaningful explanation of the semantics and the context In many cases, the original source code has been reformatted, adding line breaks and reworking indentation, to accommodate the available page space in the book In rare cases, this was not enough, and some listings include line continuation... source code for all examples in this book is available from Manning’s website, http:/ /www.manning.com/DSLsinAction, which also contains relevant instructions on how to build and run them in your environment Be sure to keep a copy of these listings when you go through the text Author Online The purchase of DSLs in Action includes free access to a private web forum run by Manning Publications, where you... familiar with the required terminology 1.1.1 The problem domain In a domain modeling activity, the problem domain is the processes, entities, and constraints that are part of the business that you’re analyzing Domain modeling, also known as domain analysis (see [1] in section 1.9), involves the identification of all the major components of the domain and how they collaborate In the example you began with,... and the outline view 232 The metamodel for your grammar 233 Generating code for the semantic model 236 ■ ■ 7.5 7.6 8 Summary 238 References 239 Designing external DSLs using Scala parser combinators 241 8.1 Parser combinators 242 What are parser combinators? parser combinator way 244 8.2 243 Designing DSLs the ■ The Scala parser combinator library 246 The base abstractions in the parser combinator library... same terminology and reflect the same understanding in the domain model that they’re designing Sometime back I started working on a project that involved modeling the backoffice operations of a large financial brokerage organization I wasn’t a domain expert, and I didn’t know much about the details and complexities involved in the practices of the securities industry practices Now, after working in that... to appropriate artifacts in a solution domain The entities shown on the left (security, trade, and so on) need corresponding representations on the right www.it-ebooks.info 6 1.2 CHAPTER 1 Learning to speak the language of the domain Domain modeling: establishing a common vocabulary When you start an exercise in domain modeling, you start with the problem domain that you’re going to model You need to... always enjoyed working with compilers, the engineering part of it as much as the creative process, on crafting my own language Programming languages in general and Domain Specific Languages (DSLs) in particular are something I’m very passionate about The concept of DSLs is nothing new For example, Lisp developers have developed and used “little languages” for a long time But in recent years DSLs have become... working in that domain for quite a while, I think it’s similar enough to other domains that you might deal with to model most of my examples and annotations in this book on that domain The sidebar in this section gives a brief introduction to the domain of securities trading and financial brokerage, which you’ll use as running examples for implementing DSLs As you progress, I’ll define new concepts wherever... you expect to find in a program that you write in any other computer language A DSL needs to give you www.it-ebooks.info Introducing DSLs 11 the ability to design abstractions that form part of the domain In the same way that you can build a larger entity out of many smaller ones in the problem domain, a welldesigned DSL gives you that flexibility of composition in the solution domain You should be . MANNING Debasish Ghosh FOREWORD BY JONAS BONÉR IN ACTION www.it-ebooks.info DSLs in Action www.it-ebooks.info www.it-ebooks.info DSLs in ACTION DEBASISH GHOSH MANNING Greenwich. 190 ■ Making domain components concrete 192 6.7 Composing DSLs 193 Composing using extensions 194 ■ Composing different DSLs using hierarchical composition 199 6.8 Monadic structures in DSL. Internal DSL integration patterns 58 Using the Java 6 scripting engine 60 ■ Using a DSL wrapper 64 ■ Language-specific integration features 73 ■ Spring-based integration 75 3.3 External DSL integration

Ngày đăng: 28/04/2014, 15:57

Xem thêm

TỪ KHÓA LIÊN QUAN