We have an alternative implementation has now been merged
main ← dotty-staging:new-multiline-strings
opened 05:55PM - 12 Jun 26 UTC
Alternative to #24185, written by hand.
- Widen TokenData interface
Let … it also contain for string literals the enclosing delimiter and the
number of delimiters. Avoids breaking the Scanner abstraction by direct access
to in.buf from Parser.
- Integrate tests from #24185
- Adopt some error message wording from #24185
- Drop a contentious run test from #24185 (triple quotes containing quadruple quotes)
- Change touched code in Scanner to Scala 3 syntax
Comparison with #24185:
Both PRs are split between Scanner and Parser, with the string trimming logic in Parser. This is necessary since we know the indentation width of the last quote only when we see the last part of an interpolated string. An important aspect of this PR is that it widens the `TokenData` interface to also include the delimiter character and the number of delimiters of a string literal. This has two benefits
- It allows to share most of logic of parsing multi-string literals in Scanner between single and double quotes.
- It allows to plug an abstraction leak in Scanner so that Parser does not need to directly access the source buffer anymore.
The trimming logic is functionally equivalent but implemented quite differently. Instead of converting everything to an array of lines representation, we work directly on the character arrays and compute a list of "cut" intervals that are all applied to the original array at the end.
- This commit: 21 lines added to Scanner, 109 lines added to Parser
- #24185: 81 lines added to Scanner, 127 lines added to Parser
This PR should be more efficient since it does not convert multi-line-string literals to a new representation, and
has an optimized implementation for removing a list of segments from a string literal.
It’s available as an experimental feature in the nightlies today and will ship first in Scala 3.10.0.
If you are interested in the feature, give it a try and report back here.
2 Likes
What seems to have been implemented is this, am I correct ?
def example =
def notIndented = '''
hello
'''
def indented = '''
hello
'''
def oldIndented = """
hello
"""
Where
notIndented is "hello"
indented is " hello"
oldIndented is the same as indented maybe with some extra \n (unchanged, but I don’t remember how it goes)
oldIndented = "\n hello\n "
I have implemented SIP-72 into scala-object-notation 0.4.1, and as an example use case as a config file .
1 Like