Note that there are some explanatory texts on larger screens.

plurals
  1. POInelegant structural repetition in parser combinators
    primarykey
    data
    text
    <p>While parsing some complex text, where I need to split out regular expression definitions for reuse and readability reasons, I seem to be often ending up with Scala code of this general structure (<code>pn</code> - a regex pattern, <code>vn</code> a variable):</p> <pre><code>val cp1 = p1 ~ p2 ~ p3 ~ p4 ~ p5 ~ p6 ^^ case { dummy1 ~ v2 ~ dummy3 ~ v4 ~ dummy5 ~ v6 =&gt; ACaseClass(v2, v4, v6) } </code></pre> <p>The obvious issue is the readability and maintainability of the code as new patterns need to be inserted because of the separation of the useful matches (<code>vn</code>) from the placeholder ones (<code>dummyn</code>).</p> <p>So, is there a neater way to express the intent? Could I use <code>_</code> instead for every <code>dummyn</code>?</p> <p>In the SNOBOL language, one could write <code>(pat . var)</code> or <code>(pat $ var)</code> which would assign the result of the match to the variable; similarly, in the latest regex syntax we have named capture groups <code>(?P&lt;name&gt;pat)</code>. The intent is clearly to keep the match capture variable close to the pattern.</p> <p>So, what I would like to write is something along the general lines of:</p> <pre><code>val cp1 = p1 ~ ( p2 $$ v2 ) ~ p3 ~ ( p4 $$ v4 ) ~ p5 ~ ( p6 $$ v6 ) $=&gt; ACaseClass(v2, v4, v6) </code></pre> <p>Obviously I am assuming some sort of new operators <code>$$</code> and <code>$=&gt;</code> which enable this simpler syntax.</p> <p>Conceivably macros could help, but they are rather beyond my abilities at present. Any input welcome!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload