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

Add support for raw strings #12

Merged
merged 2 commits into from
Oct 14, 2021
Merged

Add support for raw strings #12

merged 2 commits into from
Oct 14, 2021

Conversation

alex-pinkus
Copy link
Owner

Uses a custom scanner and ongoing state to parse code like:

extension URL {
    func html(withTitle title: String) -> String {
        return #"<a href="\#(absoluteString)">\#(title)</a>"#
    }
}

Fixes #7, #11

grammar.js Outdated Show resolved Hide resolved
@alex-pinkus
Copy link
Owner Author

Hmm, looks like this causes tests to hang in CI. Weird since the tests pass for me locally...

@alex-pinkus
Copy link
Owner Author

OK, I can repro the infinite loop on mac with some other code, so there's definitely something wrong in this PR. This also makes me think there should be some additional checks to show we can parse some top N Swift repositories - I got lucky that the tests caught this on CI. In the meantime, I think this is probably not good enough for testing.

Next step: find a minimal repro on mac and debug it.

@jandamm
Copy link

jandamm commented Oct 11, 2021

Had the issue once. Ran it again on the same file (without modification) and it did work.

@jandamm
Copy link

jandamm commented Oct 11, 2021

Maybe it's related but another issue I found was that the raw string ends at the end of line even if unterminated.

@alex-pinkus
Copy link
Owner Author

😶‍🌫️ figured out the issue and it is... embarrassingly silly. Update incoming

Uses a custom scanner and ongoing state to parse code like:
```
extension URL {
    func html(withTitle title: String) -> String {
        return #"<a href="\#(absoluteString)">\#(title)</a>"#
    }
}
```

Fixes #7, #11
The syntax highlighting Xcode does is #"abc as String, \#( as Plain Text,
value depending on the variables scope, ) as Plain Text and ."# as String.

In order for us to do the same, we need to separate out the `\#(` from
the prior `raw_str_part` (and the same with the closing parenthesis).
This requires a slightly complicated dance between the grammar and the
custom scanner, where the scanner validates the `\#(` but does not parse
it, and the grammar parses it but does not validate it. In turn, the
closing parenthesis gets consumed as a separate token by the grammar but
must leave some remnant behind in the form of a synthetic external rule
that just signals its position.
@alex-pinkus alex-pinkus merged commit 79cad78 into main Oct 14, 2021
@alex-pinkus alex-pinkus deleted the raw-strings branch October 14, 2021 02:28
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.

Extended string literal delimiters
2 participants