moonscript icon indicating copy to clipboard operation
moonscript copied to clipboard

Proposal: Indented Strings

Open exists-forall opened this issue 12 years ago • 10 comments

When writing long strings, lua provides the [[...]] syntax to reduce the amount of escaping that must be done. However, even with [[...]] you must be careful to add equal signs in certain situations. What's more, all indentation is taken into account when lua parses a long string, even indentation already present from the code's formatting. For example

do
    mystr = [=[
        some contents of a string
        that spans multiple lines
          this section is indented a little bit,
          but the previous section is not
        you can close most long lua strings with a ]],
        but not this one!
    ]=]
    print(mystr)
end

will introduce unwanted tabs into the string. In this case, the only way to achieve the desired string is by removing indentation in the block

do
    mystr = [=[
some contents of a string
that spans multiple lines
  this section is indented a little bit,
  but the previous section is not
you can close most long lua strings with a ]],
but not this one!
]=]
    print(mystr)
end

which looks bad, especially in moonscript, where indentation is so significant.

To solve both the indentation and escaping problems with the standard implementation of long strings, I propose moonscript include a new kind of string literal, the indented string, which determines where the string begins and ends entirely with whitespace, and an opening """ token. With this new syntax, our example simply becomes:

do
    mystr = """
        some contents of a string
        that spans multiple lines
          this section is indented a little bit
          but the previous section is not
        you can close most long lua strings with a ]],
        but not this one!

you can include literally any text you want, because there is no closing delimiter, and all indentation that does not deviate from the "baseline" indentation is left out of the result, allowing you to smoothly integrate it into the rest of your moonscript code.

exists-forall avatar Jan 02 '13 02:01 exists-forall

I think this is a pretty cool idea actually. Do any other languages do this?

leafo avatar Jan 02 '13 03:01 leafo

Thank you! No other languages do this that I'm aware of.

exists-forall avatar Jan 02 '13 03:01 exists-forall

Yeah this is good

fillest avatar Jan 02 '13 10:01 fillest

Awesome!

etandel avatar Jan 03 '13 15:01 etandel

Yes, something like this would be great - it's an eyesore having to left shift multi-line strings. With regards to the proposed syntax I'll just point out that while it works fine for humans, it's adds some complexity for lexing and parsing . Particularly, it makes it harder to provide good tool support for Moonscript with indentation acting as the end-of-string marker. Having the string being closed with ''' would make that easier, as well as align better with other languages such as Python. It would still look good as well if line containing the ''' would be stripped of any leading white space by the parser.

nilnor avatar Jan 03 '13 20:01 nilnor

+1 awesome idea!

qaisjp avatar Aug 17 '13 16:08 qaisjp

Do any other languages do this?

Not exactly. Look at Bash manual:

3.6.6 Here Documents ... If the redirection operator is ‘<<-’, then all leading tab characters are stripped from input lines and the line containing delimiter. This allows here-documents within shell scripts to be indented in a natural fashion.

van-de-bugger avatar Jun 02 '15 09:06 van-de-bugger

+1 Strings that are ignoring tabs? I really like this, reminds me of HTML

deathbeam avatar Jun 02 '15 10:06 deathbeam

Do any other languages do this?

Not exactly. Look at Bash manual:

3.6.6 Here Documents ... If the redirection operator is ‘<<-’, then all leading tab characters are stripped from input lines and the line containing delimiter. This allows here-documents within shell scripts to be indented in a natural fashion.

Python provides the same via the textwrap module, without altering the language.

jmc-88 avatar May 06 '20 00:05 jmc-88

Do any other languages do this?

Also, indented here-documents are in Perl since 5.26.0:

Indented Here-documents This adds a new modifier "~" to here-docs that tells the parser that it should look for /^\s*$DELIM\n/ as the closing delimiter. ... The "~" modifier will strip, from each line in the here-doc, the same whitespace that appears before the delimiter.

van-de-bugger avatar Jun 28 '20 17:06 van-de-bugger