concept Order in category dsl

appears as: Order
DSLs in Action

This is an excerpt from Manning's book DSLs in Action.

Java is an object-oriented (OO) language. As the first step in designing the DSL, you need an object representation of the Order abstraction that encapsulates the various attributes of a client order.

Listing 7.4. Order.java: The Order abstraction
public class Order { private String buySell; private String security; private int price; private String account; public Order(String bs, String sec, int p, String acc) { buySell = bs; security = sec; price = p; account = acc; } public String toString() { return new StringBuilder() .append("Order is ") .append(buySell) .append("/") .append(security) .append("/") .append(price) .append("/") .append(account) .toString(); } }

As you look at this output, you must be wondering what use it could have in a real application. You’re right; we can’t do anything meaningful with such an unstructured representation of our DSL script that contains an aggregate of tuples and lists. Let’s build up a semantic model of our Order abstraction and see how we can use more combinators to populate it as we go along with the parsing process.

Building a semantic model for the Order abstraction isn’t a problem. But how do we integrate the model with the parsing process?

The ^^ combinator applies the pattern-matching anonymous function to the result of the parse defined by items ~ account_spec. Instead of a default return value, the parser returns a Parser[Order]. Note that the Order abstraction that the function returns is lifted into its Parser through an implicit definition in Parsers trait of the library. Look at figure 8.9 and its subsequent explanation for more details.

Figure 8.9. A detailed run-down of how a sample grammar rule returns a Parser[Order]. items and account_spec each flow in as a Parser , to the rule. The sequence combinator runs Parser[Items] and Parser[AccountSpec] and passes the results as an instance of ~ to the function application combinator . Pattern matching is done and an Order instance is created . Through an implicit conversion, Order is lifted into a Parser and is returned .