<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Hi all,</p>
<p>I’m wondering if there are any resources that discuss the design
of GHC’s implementation of layout. (I haven’t been able to find
any.) From looking at the code, here’s what I’ve gathered so far:</p>
<ul>
<li>Layout is implemented in the lexer
(compiler/GHC/Parser/Lexer.x).<br>
<br>
</li>
<li>The implementation is similar in some respects to the approach
described in the Haskell Report, but still fairly different.
Virtual braces and semicolons are inserted during the lexing
process itself with the assistance of Alex lexer states (aka
“start codes”).<br>
<br>
</li>
<li>In order to handle particularly tricky cases like<br>
<br>
<pre> if e then do x; y else z</pre>
<br>
where the virtual close brace must be inserted in the middle of
a line, tokens such as <tt>in</tt> and <tt>else</tt> are given
special context-sensitive treatment. This appears to be quite
subtle.</li>
</ul>
<p>Overall, I can mostly follow the code, but I still have a few
unanswered questions:</p>
<ul>
<li>The layout-related code consistently uses the phrase
“alternative layout rule”—what does “alternative” mean here?
Does it refer to GHC’s implementation of layout? Or maybe it
refers to <tt>NondecreasingIndentation</tt>? It isn’t clear.<br>
<br>
</li>
<li>The implementation of layout seems quite complex, in large
part because it has to worry about parsing concerns in the lexer
in order to handle tricky cases like the one I provided above.
Is there are reason all this is done in the lexer, rather than
deferring some more of the work to the parser?</li>
</ul>
<p>I’ve found remarkably little information about implementing
layout in general, so perhaps I’m missing some resources or
keywords to search for, but any information or perspectives would
be appreciated!</p>
<p>Thanks,<br>
Alexis<br>
</p>
</body>
</html>