Parsing a language using scala parser combinators <p>I have the following template:</p> <pre><code>#foo(args)# // START CONTAINER1 #foo(foo &lt;- foos)(args)# // BLOCK STARTS HERE (`args` can be on either side of `block`) #bar(args)# // START CONTAINER2 #.bar# // END CONTAINER2 #.foo# // END BLOCK #.foo# // END CONTAINER1 </code></pre> <p>*notice how <code>#.foo#</code> closes each container/block</p> <p>The trouble I see here is that there's no unique <code>id</code> of some sort to represent each block so I have to keep track of how many container openers/closers there are (<code>#foo#</code>/<code>#.foo#</code>) so that a block with an inside container's <code>END CONTAINER</code> hash won't confuse the parser as ending the block.</p> <p><strong>How would I use Scala's parsers to parse blocks in a language like this?</strong></p> <hr> <p>I started off with this:</p> <pre><code>def maybeBlockMaybeJustContainer:Content = { (openingHash ~ identifier ~ opt(args) ~&gt; opt(blockName) &lt;~ opt(args) ~ closingHash) ~ opt(content) ~ openHash ~ dot ~ identifier ~ closingHash ^^ ... } </code></pre> <p>I'm also thinking about preprocessing it but not sure where to start.</p>