Multi-line Strings - Triple Quote Syntax Ideas #6595
stoicon
started this conversation in
Ideas/Requests
Replies: 1 comment
|
Odin Forum Discussion: |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Earlier @gingerBill mentioned the idea of Python like triple quote strings in #1971 (comment)
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.
\tis 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,andcdo 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 GitHub 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
bashsource code withinOdinsource 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.
All reactions