concept sequence combinator in category dsl

appears as: sequence combinator, The sequence combinator, sequencing combinator
DSLs in Action

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

The biggest advantage of parsing with combinators is improved composability; you take primitive parsers and compose them functionally to generate larger ones (I explain the virtues of composability in appendix A). Composing with combinators is like building with LEGOs—we start with smaller pieces and build higher-order structures out of them. Figure 8.2 demonstrates a sequence combinator in action.

In the world of DSL design, you can use parser combinators to compose smaller language fragments that model parts of your DSL syntax and build the whole DSL structure out of them. The sequence combinator that you see in figure 8.2 is only one such combinator, which is shown in the figure composing two DSL syntax parsers sequentially. Any standard implementation includes a variety of such combinators, much like LEGO sets. Let’s start with an overview of how some of the commonly used combinators consume an input stream and recognize the grammar of your language, shown in table 8.1.

Implementing the sequencing combinator—the hard way

Consider the sequencing combinator that Scala offers and that we’ve already discussed. One way to implement it is shown in the following listing.

Listing 8.3. Implementing the sequencing combinator in Scala