List of Listings – DSLs in Action

List of Listings

Chapter 1. Learning to speak the language of the domain

Listing 1.1. Order-processing DSL

Chapter 2. The DSL in the wild

Listing 2.1. Order abstraction for Java DSL

Listing 2.2. Order processing DSL in Groovy

Chapter 3. DSL-driven application development

Listing 3.1. ClientOrder.groovy: order-processing DSL in Groovy

Listing 3.2. order.dsl: Groovy script for placing orders

Listing 3.3. Java application code that invokes the Groovy DSL

Listing 3.4. Account domain object in Java

Listing 3.5. AccountDSL in Scala

Listing 3.6. DSL integration using GroovyClassLoader

Listing 3.7. order.dsl: the DSL script now returns a Closure

Chapter 4. Internal DSL implementation patterns

Listing 4.1. DSL that creates a client account

Listing 4.2. Expressive domain vocabulary in implementation of Account

Listing 4.3. Implicit context set up for method dispatch in Groovy

Listing 4.4. Mailer class with fluent interfaces

Listing 4.5. Trade and its decorators in Java

Listing 4.6. Trade abstraction in Ruby

Listing 4.7. Trade abstraction in Groovy

Listing 4.8. Dynamic builders for Trade objects in Groovy

Listing 4.9. Activity report with specialized implementations of groupBy

Listing 4.10. Generic implementation of groupBy

Listing 4.11. Trade object with typed constraints in Scala

Listing 4.12. EquityTrade and FixedIncomeTrade model

Listing 4.13. Trade with domain validation

Listing 4.14. Allocation function for execution

Chapter 5. Internal DSL design in Ruby, Groovy, and Clojure

Listing 5.1. Polymorphism with duck typing

Listing 5.2. XML builder in Groovy: the power of dynamic metaprogramming

Listing 5.3. SecurityTrade in Ruby (Iteration 1)

Listing 5.4. Instrument traded in Ruby

Listing 5.5. Instrument DSL using monkey patching

Listing 5.6. Trade DSL in Ruby, as an interpreter (Iteration 2)

Listing 5.7. Using the Trade DSL

Listing 5.8. Trade DSL in Ruby: blocks for side effects (Iteration 3)

Listing 5.9. Calculating the cash value of the trade

Listing 5.10. Order class in Groovy

Listing 5.11. Order-processing DSL script

Listing 5.12. Adding methods to Integer using categories

Listing 5.13. Trade generation in Clojure

Listing 5.14. Wrap trade with additional behavior

Listing 5.15. Decorator in Clojure

Chapter 6. Internal DSL design in Scala

Listing 6.1. Expressive and concise syntax in Scala

Listing 6.2. Instrument model in Scala

Listing 6.3. Account model in Scala

Listing 6.4. Trade model in Scala

Listing 6.5. FixedIncomeTrade implementation and instantiation

Listing 6.6. TradeImplicits defines the conversion functions

Listing 6.7. The implicit definitions in TradeImplicits

Listing 6.8. Tax and fee calculation components in Scala

Listing 6.9. DSL for tax and fee calculation business rules

Listing 6.10. Base class for TradingService in Scala

Listing 6.11. A concrete trading service for equity trades

Listing 6.12. Trading service for fixed income trades in Scala

Listing 6.13. Trade DSL for FixedIncomeTrade

Listing 6.14. Trade DSL for EquityTrade

Listing 6.15. A new semantics for TradeDsl—another business rule

Listing 6.16. DSL composition

Listing 6.17. Modeling the trade lifecycle in a DSL

Listing 6.18. The trade lifecycle DSL

Listing 6.19. A DSL with implementation coupling

Listing 6.20. DSL for modeling account balance

Listing 6.21. The Portfolio DSL contract

Listing 6.22. A DSL implementation of Portfolio

Listing 6.23. Another implementation of the Portfolio DSL

Listing 6.24. Handling alternate routes in computation using Java

Listing 6.25. Scala monadic for comprehensions

Listing 6.26. Monadic TradeDsl

Chapter 7. External DSL implementation artifacts

Listing 7.1. OrderLexer.g: The lexer for your DSL in ANTLR

Listing 7.2. OrderParser.g: Grammar rules for your DSL in ANTLR

Listing 7.3. OrderParser.g: Action code embedded in the grammar rules

Listing 7.4. The Order abstraction

Listing 7.5. The driver code for our parser module

Listing 7.6. Xtext grammar rules

Listing 7.7. Template for generating the semantic model for orders

Listing 7.8. Class generated by the template in listing 7.7

Chapter 8. Designing external DSLs using Scala parser combinators

Listing 8.1. How Scala models the parse result

Listing 8.2. A sample external DSL using Scala parser combinators

Listing 8.3. Implementing the sequencing combinator in Scala

Listing 8.4. Running the DSL processor

Listing 8.5. Semantic model for the order-processing DSL

Listing 8.6. AST for the order-processing DSL

Listing 8.7. Grammar rules for SSI_Dsl

Listing 8.8. The semantic model (or AST) for the SSI DSL

Listing 8.9. The complete DSL that generates our semantic model

Listing 8.10. A validating parser that can add domain semantics to a parser

Listing 8.11. ValidatingParser as a decorator

Appendix A. Role of abstractions in domain modeling

Listing A.1. TradeProcessor processes trade details

Listing A.2. TradeProcessor annotated with nonessential details

Listing A.3. TradeProcessor distilled