Triple Quoted
triple_quoted.nr70 lines
triple_quoted.nr· neuro
// Triple-quoted (block) strings: multi-line text with the closing delimiter's indentation
// stripped from each content line. Escapes and `{...}` holes work as in a `"..."` literal.
// Every block below is checked against its expected text and printed between markers, so
// the dedent shows column by column.
func check(actual: string, expected: string) -> i32 {
if actual == expected {
print("[")
print(actual)
println("]")
return 1
}
println("MISMATCH: rendered {actual}, expected {expected}")
return 0
}
func main() -> i32 {
mut passed: i32 = 0
// The newlines that touch the delimiters are punctuation, not content: the one
// right after the opening `"""` and the one before the closing line both go, so
// the value is exactly its two lines. The four spaces the closing `"""` sits at
// are stripped from both.
val basic = """
first
second
"""
passed += check(basic, "first\nsecond")
// Indentation deeper than the closing delimiter survives: only the common
// prefix goes away, so nested structure inside the block is preserved.
val nested = """
root
child
"""
passed += check(nested, "root\n child")
// A blank line needs no indentation of its own and normalizes to empty. It is
// also how a trailing newline is written: put one before the closing line and
// its terminator is the one that survives.
val paragraphs = """
one
two
"""
passed += check(paragraphs, "one\n\ntwo")
// Interpolation and the format mini-language work inside a block.
val name = "Neuro"
val ratio: f64 = 0.5
val report = """
engine: {name}
ratio: {ratio:.3}
"""
passed += check(report, "engine: Neuro\nratio: 0.500")
// A `"` needs no escaping inside a block: only `"""` ends it.
val quoted = """
he said "yes"
"""
passed += check(quoted, "he said \"yes\"")
// Escape sequences decode as usual.
val escaped = """
tab\there
"""
passed += check(escaped, "tab\there")
return passed
}
neuroc run triple_quoted.nr
[first
second]
[root
child]
[one
two]
[engine: Neuro
ratio: 0.500]
[he said "yes"]
[tab here]