ebook img

DSLs in Action PDF

377 Pages·2010·6.19 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview DSLs in Action

IN ACTION Debasish Ghosh FOREWORD BY JONAS BONÉR M A N N I N G DSLs in Action DSLs in ACTION DEBASISH GHOSH MANNING Greenwich (74° w. long.) 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: [email protected] ©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 1 2 3 4 5 6 7 8 9 10 – MAL – 16 15 14 13 12 11 10 To my grandfather, who taught me my first alphabet 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 vvii 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 vii viii CONTENTS 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 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

Description:
Your success—and sanity—are closer at hand when you work at a higher level of abstraction, allowing your attention to be on the business problem rather than the details of the programming platform. Domain Specific Languages -- “little languages” implemented on top of conventional programming
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.