<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">I've just had an enlightening conversation with Michael Adams about indentation-sensitive parsing. The executive summary is<div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>-- It's much more complicated than I thought</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>-- depending on th details of the rules that we adopt for Grace, we can probably avoid a lot of the complexity</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>-- There are a number of decisions that we need to make about what we WANT indentation to do in Grace</div><div><br></div><div>Unfortunately, the details of what sort of indentation we want will affect (to some extent) the combinators that we need to implement indentation.</div><div><br></div><div>The decisions that we need to make include:</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>- What productions in Grace's grammar start new indentation scopes:</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>method</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>object </div><div><span class="Apple-tab-span" style="white-space:pre"> </span>class</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>block</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>argument-list</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> Are there any others?</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>- When the indentation increases, does it increase relative to the <i>line</i> on which the indentation scope starts, or relative to the <i>enclosing</i> indentation scope?</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> Michael's mail message dated 12 July 2013 08:34 discusses these alternatives. For example, consider</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span><b>object</b> { <b>method</b> doit {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> foobar(x)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> }</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span> Is this legal? foobar(x) is syntactically inside the method doit, so it should be indented with respect to that method. It <i>is</i> indented with respect to</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> the indentation of the <i>line</i> on which the method starts, but <i>not </i>with respect to the indentation of the enclosing indentation scope, which starts </div><div><span class="Apple-tab-span" style="white-space:pre"> </span> with the keyword <b>method</b>. </div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span> One problem with line-based indentation is that adding newlines matters, so while the above would be accepted, the following would be rejected:</div><div><br></div><div><div><span class="Apple-tab-span" style="white-space: pre; "> </span><b>object</b> { </div><div><b><span class="Apple-tab-span" style="white-space:pre"> </span> method</b> doit {</div><div><span class="Apple-tab-span" style="white-space: pre; "> </span> foobar(x)</div><div><span class="Apple-tab-span" style="white-space: pre; "> </span> }</div><div><span class="Apple-tab-span" style="white-space: pre; "> </span>}</div></div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>- Another choice is whether to <i>require</i> a newline after the token that starts a new indentation scope, unless the end of the scope is on the same line.</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> this would outlaw </div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space: pre; "> </span><b>object</b> { <b>method</b> doit {</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span> altogether, and force us to write</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space: pre; "> </span><b>object</b> { </div><div><b><span class="Apple-tab-span" style="white-space:pre"> </span> method</b> doit {</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span> although it would still allow single-line objects and single-line methods if everything fits on one line. The main objection to this is that it may make it </div><div><span class="Apple-tab-span" style="white-space:pre"> </span> harder to fit code snippets into papers with a hard page limit! It's a good idea if you like to use proportionally-spaced fonts for your programs </div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (because all indentations are now determined by the number of leading <i>spaces</i>, and all spaces have equal width). It also means that the </div><div> <span class="Apple-tab-span" style="white-space:pre"> </span> distinction between "current indentation scope" and "current line" goes away.</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>- Do we allow semicolons at all, in multi-line layout, or do we require one statement on each line? So can I write:</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span><b>method</b> doIt {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>stmt1 </div><div><span class="Apple-tab-span" style="white-space:pre"> </span>stmt2; stmt3</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>stmt4</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span> If we allow semicolons, is a trailing semicolon allowed? </div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>- Do we want to get rid of the braces around class, object and method? As Michael pointed out, we can't make them optional, because that</div><div> introduces an ambiguity.</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>- Are there <i>any</i> productions in Grace's grammar that "forget" about indentation. An example is Landin's "except possibly for [parenthesized] </div><div><span class="Apple-tab-span" style="white-space:pre"> </span> subsegments" rule, enclosed by Adam's circle-star combinator, which allows</div><div><br></div><div><span class="Apple-tab-span" style="white-space: pre; "> </span> x + v where</div><div><span class="Apple-tab-span" style="white-space: pre; "> </span> x = -(</div><div><span class="Apple-tab-span" style="white-space: pre; "> </span> y + z) + w</div><div><br></div><div><br></div><div>That may be all for now</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>Andrew</div><div><br></div><div><br></div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span> </div></body></html>