Line Endings

graeme-defty edited this page Feb 19, 2011 · 1 revision
Clone this wiki locally

NOTE: The line-ending rules described here apply only to the new 'peg' parser which has not yet been adopted. Line end handling under the current leex/yecc parser is very different (in general, much less permissive).

Also, the parser is under fairly active development, and some of these rules are likely to change (as noted below)

General Philosophy

The general philosophy for handling line endings is that they are required (and forbidden) in as few places as possible. To achieve this, the parser assumes that if a line ending occurs in a place where a statement could end, then the statement does indeed terminate there.

For example, in writing 1 + 2 + 3, newlines may occur after the + signs, since the expression may not legally terminate there:

    1 +
    2 +

will have the desired effect. Conversely,

    + 2
    + 3

will not, as this will be seen as a single expression (1) followed by '+ 2', which on its own is not legal syntax.


Newlines Required

Newlines are required in the following places:

  • Expression lists :

    Reia has two forms of expression list. The first is comma-separated (e.g. parameters to a function). The second is newline-separated, (e.g. the body of a function or code block is considered to be a list of expressions). In the second, newline-separated form the items must obviously be separated by newlines.

  • Class Declaration / methods :

    In a class declaration, there must be a newline after the classname (or the parent name if the '< parent' form is used) and before the first expression in the body of the class.

  • if, elseif, when, after and catch Clauses :

    The list of expressions which follow an 'if', 'elseif', 'when', 'after' or 'catch' clause take the form of the expression list described above. Newlines must also separate the end of one clause form the next.

##Newlines Forbidden NOTE that none of these are actually exceptions. Each of them is imposed by the general principle stated above.

Newlines are forbidden in the following places:

  • inline if/unless :

    In the inline form of the if and unless expressions, there must not be a newline before of after the if (or the unless) (It is likely that this will be relaxed to apply to only before the if/unless)

  • Indexing :

    in an indexing operation the '[' must not have a newline before it.

  • Function call parameters/block :

    when calling a function, if parameters and/or a block are provided, the opening parenthesis or block '{' or 'do' must not be preceded by a newline.

  • List Comprehension expressions :

    In a list comprehension, the commas between the expressions, if more than one is provided, must not be preceded by a newline. (It is likely that this requirement will be dropped)

  • if/unless :

    In an if/unless expression, the whitespace following the 'if'/'unless' must not include a newline (It is likely that this requirement will be dropped)

  • throw :

    In a throw expression, if a module name is provided, the whitespace following it must not include a newline (It is likely that this requirement will be dropped)