Skip to content

Triple-quoted multiline strings with trim margin: '''...''' #98

@tmteam

Description

@tmteam

Summary

Introduce triple-quoted strings '''...''' (and """...""") for multiline text with automatic indentation trimming (trim margin).

Dialect setting

enum MultilineString
{
    Deny,     // '''...''' not recognized
    Allow,    // '''...''' with trim margin + $-prefix escape levels
}

Default: Deny (current behavior). Allow enables the full feature set including $'''...''' escape levels (#99).

Syntax

sql = '''
    SELECT *
    FROM users
    WHERE age > 18
    '''
# → "SELECT *\nFROM users\nWHERE age > 18"

Trim margin algorithm (Swift/Java/C# style)

  1. First newline after opening ''' is stripped
  2. Last newline before closing ''' is stripped
  3. Find the minimum common whitespace prefix across all non-empty content lines and the closing ''' line
  4. Strip that prefix from every line
  5. Tabs and spaces are compared character-by-character (mixed = error or no trim)

Rules

  • Opening ''' must be followed by a newline (nothing else on that line)
  • Closing ''' determines the baseline indentation
  • A content line with less indentation than closing '''compile error
  • Both ''' and """ are supported (equivalent)
  • Interpolation uses {expr} — same as single-quoted strings
  • Escape sequences (\n, \t, \{, etc.) work as usual
  • To include literal ''' inside: use \''' or switch to """

Escape level prefix ($-prefix)

When MultilineString.Allow is set, $-prefix is also available (for both '...' and '''...'''):

Syntax Interpolation { literal $ literal
'''...''' {expr} \{ literal
$'''...''' ${expr} literal \$
$$'''...''' $${expr} literal literal
# JSON — braces literal, interpolation via ${expr}
json = $'''
    {"name": "${name}", "age": ${age}}
    '''

# Template with $ signs — interpolation via $${expr}
report = $$'''
    Price: $100
    Customer: $${customer}
    '''

Edge cases

  • Empty triple-quote: '''\n'''""
  • Single line: '''hello''' → error? Or treat as regular string? (to decide)
  • Tabs vs spaces mixed indentation → compile error

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    SyntaxSyntactic and semantic improvements and sugars

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions