Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Lexer] Allow multiline delimiter to be escaped in raw Strings #23115

Closed
wants to merge 12 commits into from
Closed

[Lexer] Allow multiline delimiter to be escaped in raw Strings #23115

wants to merge 12 commits into from

Conversation

maustinstar
Copy link
Contributor

The Lexer currently eats triple-quotes as a multiline delimiter for Strings. This Pull request allows the delimiter to be escaped in raw Strings when the correct number of terminating '#' marks are found at the end of the line.
E.g.
#"""# is now equivalent to "\""
#"""text"# equates to "\"\"text"
#""" is still the beginning of a multiline raw string.

Resolves SR-10011.

…strings.

The multiline delimiter for Strings (aka. """ or triple quote) can now be escaped when the correct number of terminating '#' marks are found within the same line. Before, #"""# was invalid because the multiline delimiter only allowed a newline character following the triple quote. Now, #"""# is a valid String equivalent to "\"". [See SR-10011](https://bugs.swift.org/browse/SR-10011)
@theblixguy
Copy link
Collaborator

cc @rintaro

@rintaro rintaro self-requested a review March 6, 2019 20:40
Copy link
Member

@rintaro rintaro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Michael, welcome to the project!
Thank you for the PR. This is a good start!

For testing, the content of the literals are tested in test/Parse/raw_string.swift. Please add test cases there. As for error cases like #"""meow#\n, please add them to test/Parse/raw_string_errors.swift.

lib/Parse/Lexer.cpp Outdated Show resolved Hide resolved
lib/Parse/Lexer.cpp Outdated Show resolved Hide resolved
@maustinstar
Copy link
Contributor Author

Tests have been moved; logic has been rewritten. Thank you for the warm welcome:)

Copy link
Collaborator

@xwu xwu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome! Minor nits :)

lib/Parse/Lexer.cpp Outdated Show resolved Hide resolved
lib/Parse/Lexer.cpp Outdated Show resolved Hide resolved
Co-Authored-By: maustinstar <maustinstar@gmail.com>
lib/Parse/Lexer.cpp Outdated Show resolved Hide resolved
lib/Parse/Lexer.cpp Outdated Show resolved Hide resolved
Copy link
Member

@rintaro rintaro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementation looks good! Could you add several test cases?

test/Parse/raw_string.swift Show resolved Hide resolved
test/Parse/raw_string_errors.swift Show resolved Hide resolved
@rintaro
Copy link
Member

rintaro commented Mar 15, 2019

@swift-ci Please smoke test

lib/Parse/Lexer.cpp Outdated Show resolved Hide resolved
@rintaro
Copy link
Member

rintaro commented Mar 25, 2019

@swift-ci Please smoke test

@rintaro
Copy link
Member

rintaro commented Mar 25, 2019

******************** TEST 'Swift(linux-x86_64) :: Parse/raw_string_errors.swift' FAILED ********************
Script:
--
: 'RUN: at line 1';   /home/buildnode/jenkins/workspace/swift-PR-Linux-smoke-test/branch-master/buildbot_linux/swift-linux-x86_64/bin/swift -frontend -target x86_64-unknown-linux-gnu  -module-cache-path '/home/buildnode/jenkins/workspace/swift-PR-Linux-smoke-test/branch-master/buildbot_linux/swift-linux-x86_64/swift-test-results/x86_64-unknown-linux-gnu/clang-module-cache' -swift-version 4   -typecheck -verify -disable-objc-attr-requires-foundation-module /home/buildnode/jenkins/workspace/swift-PR-Linux-smoke-test/branch-master/swift/test/Parse/raw_string_errors.swift
--
Exit Code: 1

Command Output (stderr):
--
/home/buildnode/jenkins/workspace/swift-PR-Linux-smoke-test/branch-master/swift/test/Parse/raw_string_errors.swift:15:15: error: unexpected error produced: multi-line string literal content must begin on a new line
let _ = ###"""invalid"##
              ^
/home/buildnode/jenkins/workspace/swift-PR-Linux-smoke-test/branch-master/swift/test/Parse/raw_string_errors.swift:19:4: error: expected error not produced
// expected-error@-1{{too many '#' characters in closing delimiter}}{{24-27=}}
~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/buildnode/jenkins/workspace/swift-PR-Linux-smoke-test/branch-master/swift/test/Parse/raw_string_errors.swift:20:4: error: expected error not produced
// expected-error@-2{{consecutive statements on a line must be separated by ';'}}
~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/buildnode/jenkins/workspace/swift-PR-Linux-smoke-test/branch-master/swift/test/Parse/raw_string_errors.swift:21:4: error: expected error not produced
// expected-error@-3{{expected expression}}
~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/buildnode/jenkins/workspace/swift-PR-Linux-smoke-test/branch-master/swift/test/Parse/raw_string_errors.swift:26:4: error: expected error not produced
// expected-error@-1{{multi-line string literal content must begin on a new line}}{{14-14=\n}}
~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/buildnode/jenkins/workspace/swift-PR-Linux-smoke-test/branch-master/swift/test/Parse/raw_string_errors.swift:27:4: error: expected error not produced
// expected-error@-2{{multi-line string literal closing delimiter must begin on a new line}}{{5-5=\n}}
~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/buildnode/jenkins/workspace/swift-PR-Linux-smoke-test/branch-master/swift/test/Parse/raw_string_errors.swift:31:4: error: expected error not produced
// expected-error@-1{{multi-line string literal content must begin on a new line}}{{14-14=\n}}
~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--

********************

This is because let _ = ###"""invalid"## eats the rest of the file. Feel free to split this test case to another file.

@maustinstar
Copy link
Contributor Author

Changed the test case to something that won't eat the whole file but still ensures that the raw string would act like a single-liner.

Sorry for being out of touch for so long.

@rintaro
Copy link
Member

rintaro commented Apr 18, 2019

@swift-ci Please smoke test

@maustinstar
Copy link
Contributor Author

Does delimiterMatches return false when the lhs is shorter than the rhs? (e.g. ##"string"###). If so, then tests like the ones I wrote won't work because the logic I wrote will only treat the raw string as a one-liner if the delimiterMatches is true before a line break. My guess is that delimiterMatches is not returning true when rhs is longer than lhs.

Should I change the logic to look for any delimiter (not necessarily of matching length) and treat the raw string as a one-liner? And inherently the parse tests would behave like the one-line counterparts?

@shahmishal
Copy link
Member

Please update the base branch to main by Oct 5th otherwise the pull request will be closed automatically.

  • How to change the base branch: (Link)
  • More detail about the branch update: (Link)

@shahmishal shahmishal closed this Oct 5, 2020
@xwu
Copy link
Collaborator

xwu commented Oct 5, 2020

@maustinstar Are you still interested in working on this issue?

@maustinstar
Copy link
Contributor Author

@xwu If someone else wants to take it, that's okay. But if I attempt this again, I'll start from scratch and open a different PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants