Language reference · section 4

4. Lexical rules

Feature: Source text

  Scenario: A tab in indentation is an error
    Given a line that starts with a tab
    When the compiler reads the line
    Then the compiler reports an error "tabs are not allowed; indent with spaces"

  Scenario: Indentation must match an open block level
    Given a line whose indentation is not equal to any open block level
    When the compiler reads the line
    Then the compiler reports an error "inconsistent indentation"

  Scenario: A carriage return is whitespace
    Given a source file with CRLF line ends
    When the compiler reads the file
    Then the carriage return is ignored

  Scenario: Blank lines and comment-only lines do not change indentation
    Given a comment-only line between two statements at the same level
    Then the statements stay in the same block

4.1 Comments

4.2 Line continuation

A statement continues on the next line when the line before it ends with one of these tokens:

|>  +  -  *  /  %  **  and  or  is  isnt  <  <=  >  >=  ??  ,

A line that starts with |> continues the statement before it. A continued line does not start or end a block.

Feature: Line continuation

  Scenario: A line ends with an operator
    Given the code "total = 1 +" followed by a line "  2"
    Then total is 3

  Scenario: A line starts with a pipe
    Given a statement followed by a line that starts with "  |> clean"
    Then the line continues the statement

in does not continue a line.

4.3 Names

Feature: Names

  Scenario: A name with a non-ASCII letter is an error
    Given the code "é = 1"
    Then the compiler reports an error

4.4 Numbers

4.5 Strings

Feature: Strings

  Scenario: A double-quoted string interpolates values
    Given the code "hi #{name}"
    Then the string includes the value of name

  Scenario: A single-quoted string does not interpolate
    Given the code 'hi #{name}'
    Then the text is "hi #{name}" with no value inserted

  Scenario: Interpolation can contain another string
    Given the code "#{ \"in\" }"
    Then the result is "in"

  Scenario: A string must end on the same line
    Given a double-quoted string with a line break before its closing quote
    When the compiler reads the string
    Then the compiler reports an error "unterminated string"

  Scenario: An unknown escape is an error
    Given the code "a\q"
    When the compiler reads the code
    Then the compiler reports an error "unknown escape sequence"

Escape sequences in both kinds of string:

Sequence Meaning
\n line feed
\t tab
\r carriage return
\" double quote
\' single quote
\\ backslash

Any other sequence is an error.

4.6 Block strings

A block string starts with """ on its own line. It ends at the next """. Escapes and #{...} work as in a double-quoted string.

Feature: Block strings

  Scenario: A block string keeps its lines
    Given a block string whose lines are "  Dear #{name},", "    thanks for joining.", and "  "
    Then the least indented line loses its indentation
    And the line breaks before the first line and after the last line are dropped
    And the value is "Dear Ada,\n  thanks for joining." when name is "Ada"
name = "Ada"
letter = """
  Dear #{name},
    thanks for joining.
  """

# "Dear Ada,\n  thanks for joining."