moonscript
moonscript copied to clipboard
Proposal: Indented Strings
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.
I think this is a pretty cool idea actually. Do any other languages do this?
Thank you! No other languages do this that I'm aware of.
Yeah this is good
Awesome!
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.
+1 awesome idea!
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.
+1 Strings that are ignoring tabs? I really like this, reminds me of HTML
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.
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.