Multi-line Strings - Triple Quote Syntax Ideas

I have created a Discussion in the Odin repository for this:

Multi-line Strings - Triple Quote Syntax Ideas · odin-lang/Odin · Discussion #6595 · GitHub

Earlier @gingerBill mentioned the idea of Python like triple quote strings in [request] multi-line string where indentation get's eaten · Issue #1971 · odin-lang/Odin · GitHub

IFF I am going to do this as a feature in the language, it will be Python-like triple quotes as I suggested.

Please don’t suggest any other lexical/syntax bits please.

@gingerBill

So, I was wondering what your thoughts are on the following triple quote syntax inspired from Markdown fenced code blocks. Many people are already familiar with Markdown syntax.

fmt.printf(
    """
    ERROR: \tSocket address length exceeds maximum size.
           \tSocket address is %q
    """,
    address,
)

sql :=
    """
    SELECT
        a,
        b,
        c
    FROM bar
    """

\t is escape character. Other escape characters are also allowed.

Every line ends in newline character which can be controlled using attributes.

Just like in Markdown, the indentation before the opening """ is subtracted from every line. Odin compiler may be able to output a syntax error if there is not enough indentation in every line.

The following example will have a syntax error because lines b, and c do not have sufficient indention.

sql :=
    """
    SELECT
    a,
  b,
c
    FROM bar
    """

You could also use triple backticks for raw multi-line strings. Escape characters are treated as raw string.

help_message :=
    ```
    Odin Escape Characters:
    \n - newline
    \r - carriage return
    \t - tab
    \v - vertical tab (VT)
    \\ - backslash
    \" - double quote (if needed)
    \' - single quote (if needed)
    ```

Also, if the string includes triple quotes or triple backticks, then you can use more than 3 quotes or backticks for multi-line string, just like in Markdown. (For example, in this discussion, I used 6 backticks in Markdown to show the above code block that contains three backticks.)

markdown_source :=
    ``````
    Note: `:=` is two tokens, `:` and `=`. The following are all equivalent:

    ```odin
    x: int = 123
    x:     = 123 // default type for an integer literal is `int`
    x := 123
    ```
    ``````

multiline_string_example :=
    """"""
    // This is a multiline string:
    odin_multiline_string :=
    """
    ERROR: Socket address length exceeds maximum size.
           Socket address is %q
    """
    """"""

Attributes

Attributes can be used to pre-process the string.

Attributes are basically functions that pre-process the string at compile time or runtime. Custom attributes can also be created.

message :=
    """remove_last_newline(),no_subtract_indent()
    The newline character of the last line will be removed.
    But the indentation will not be subtracted.
    This line has no newline character.
    """

Attributes can be also used for syntax highlighting. Just like in Markdown Fenced Code Blocks, the source programming language can be set, so that text editors like Neovim can syntax highlight the code using Tree-sitter.

build_file_content :=
    ```lang("bash")
    #!/usr/bin/env bash

    cd build
    make build
    make install
    ```

This allows Neovim to syntax highlight bash source code within Odin source code.

This will be very useful for other languages such as SQL.

Summary

  • Every line ends with newline character.

  • Indentation is subtracted similar to Markdown Fenced Code Blocks.

  • Triple quotes for strings with escape characters.

  • Triple backticks for raw strings.

  • More than 3 quotes or backticks for escaping multiple quotes/backticks, just like in Markdown.

  • Attributes can be used to pre-process the string.

  • Custom attributes can also be used.

I like the triple quotes/backticks semantic but I am strongly against the suggested pre-processing attribute proposal.

1 Like

Having just multi-line strings is very useful. Pre-processing attribute feature is not that much important.

But the ability to write comments on the line of the beginning quotes is very useful.

We can write a comment that tells the language inside the multi-line strings. This allows text-editors such as Neovim to use syntax highlighting for the language inside multi-line strings.

build_file_content :=
    ``` // lang:bash
    #!/usr/bin/env bash

    cd build
    make build
    make install
    ```

// lang:bash comment will make it easier for Neovim to recognize that the language is bash.

1 Like

Also, even if there is no pre-processing attribute feature, we can still use normal Odin functions to do manual pre-processing of multi-line strings. I think this might be more acceptable for Odin design philosophy.

Example:

message :=
    """
    The newline character of the last line will be removed.
    The function remove_last_newline() will remove the last newline character.
    This line will not have newline character.
    """

message_2 := remove_last_newline(message)
  • message will contain the last newline character.
  • message_2 will NOT contain the last newline character.
1 Like