Layout Processing Rules

Layout processing is handled by a cooperation between the lexer and the parser. This page describes how that works.

Objective and Overview

The goal of the layout system is to provide programmer convenience by automatically inserting curly braces and semicolons wherever they have not been inserted by the programmer. Left curly braces are conditionally inserted after certain preceding tokens. Semicolons are conditionally inserted based on the indentation level of each line. Right curly braces are conditionally inserted before certain tokens and also based on the indentation level of the curren tline.

The ``trick'' to the layout scheme is in two parts:

  1. There are several sequencing constructs in the language whose general form is a semicolon-separated sequence of things surrounded by semicolons. This lets us use the same layout rules for multiple purposes.

  2. At each point where a left curly brace might be automatically inserted, a preceding token (one of let, do, or ``='') signals unambiguously that a left curly brace must follow:

        def f x y = { ...
        def x = { 5 }
        struct S 'a 'b = { a : int; ...
        let { x = { 5 } } in { ...
        while (expr) do { ...
        do { ... } while (expr)

We rely on the fact that (a) knowing the preceding and current token is enough to know whether to insert a curly brace (b) because blocks are values, all binding expressions can safely be wrapped in blocks, (c) because such wrapping is always safe, it can be done in a way that is invisible to the programmer In fact, the parser insists that this be true. While it is nearly universal practice in Bitc to write code without semicolons in many places, most notably:

    let x = 5 in body

What the compiler actually sees is:

    let { x = { 5 } } in body

Specification


Generated on Thu May 17 23:59:16 2012 for BitC Compiler by  doxygen 1.4.7