Indentation-sensitive Erlang 2

So, I considered the comments (thank you, all), and thought I’d have another go at making the ending ‘dot’ optional.

I decided to introduce another token, ‘GAP’, to denote an empty line. Most likely, the scanner, in its current state, will not be able to handle empty lines with white space in them, etc, and the code is starting to look a bit confused. Oh well…

The toplevel rule for a function now becomes:

form -> function dot : '$1'.
form -> function 'GAP' : '$1'.

and the rule for alternative function clauses is as before:

function_clauses -> function_clause : ['$1'].
function_clauses ->
   function_clause ';' function_clauses : ['$1'|'$3'].
function_clauses -> 
  function_clause 'OUT' : ['$1'].
function_clauses ->
  function_clause 'END' function_clauses : ['$1'|'$3'].

The first two rules are the original rules for indentation-insensitive code. The last two are for the indentation tokens. The ‘OUT’ token is for symmetry, to match the ‘IN’ token after the arrow in function_body. Remember that indentation tokens are normalized in the scanner.

The test program now looks like this:



f(X) ->

g(X) ->

h(X) ->
    Y = case X of
          a ->
          b ->

i(a) -> a
i(b) -> b

test() ->
    2 = f(0),
    4 = f(2),
    4 = g(0),
    8 = g(4),
    {a} = h(a),
    {b} = h(b),
    a = i(a),
    b = i(b),

A little bit better. The ‘end’ tokens are still needed, though. One thing at a time…

5 thoughts on “Indentation-sensitive Erlang 2

  1. Oh, thanks… I guess. (:

    The purpose of the test module is of course not to illustrate how beautiful it can be, but rather to verify that the parser does its job correctly, including parsing “old-style” functions, like test(). But I agree: I think that there is no loss of understanding – only less noise.

  2. Oh, no! Not another language with indentation requirements, please!

    Having paid my dues with columns in FORTRAN and my hatred for indentation in Python, an otherwise superb language, please resist the temptation to make Erlang column sensitive.

    With so many editors and IDEs disagreeing over tab settings, it’s only a matter of time before my code gets mixed up.

  3. Peter – how about simply disallowing TABs in the indentation-sensitive scanner?

    The way this hack works, you have to explicitly tell the compiler that the code is indentation-sensitive, at which point it switches to another token scanner.

    The only indentation-sensitive language I’ve done any half-serious work in was FORTRAN, so I’m not crusading to get this introduced into Erlang – I’m mainly playing around because I was curious to see how it would work out.

  4. Hi! I’ve been working through Pragmatic Erlang and it’s a very interesting language. One of the things I don’t like too much is the dots and semicolons and commas, thought. I’ve also programmed a little in Python because I’m learning Django, and I always heard that significant indentation was a bad idea, but I’ve found that it works surprisingly well. It reduces a lot of noise and typing, and it’s not at all confusing because the indentation is relative and it has no problem recognizing a TAB or a single space even in a single file.

    So I was just wondering if Erlang could be made indentation sensitive. I’m happy to know that someone’s working on it!

Leave a Reply

Your email address will not be published. Required fields are marked *