-
Notifications
You must be signed in to change notification settings - Fork 441
[SR-10241] A way to get raw string without quotes from StringLiteralExprSyntax #433
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
Comments
Seems good idea to me. The benefit is you will be able to structurally distinguish what kind of quotes (multiline or single) were used, instead of searching the string literal text string in order to distinguish. If we do this we should probably get rid of 'string_literal' token kind and use 'string_segment' token kind for the non-interpolation case as well. |
Comment by Yusuke Kita (JIRA) Exactly. Can I implement this fix? Seems quite interesting to dive into more details. |
Sure! Note that after changing the syntax definition file it would need a change on the C++ parser side. |
Comment by Yusuke Kita (JIRA) I have a question about literal expression for this. I'm looking into Parser and wondering whether I should extend 'StringLiteralExpr' or create new one like 'NonInterpolatedStringLiteralExpr'. Which one do you think is better? @akyrtzi I figured out how 'string_segment' is parsed in 'parseExprStringLiteral' func, but I haven't had conclusion for the interface yet. |
I think we should generalize
like: Node('StringLiteralExpr', kind='Expr',
children=[
Child('OpenQuote', kind='Token',
token_choices=[
'StringQuoteToken',
'MultilineStringQuoteToken',
]),
Child('Segments', kind='StringLiteralSegments'),
Child('CloseQuote', kind='Token',
token_choices=[
'StringQuoteToken',
'MultilineStringQuoteToken',
]),
]), We can easily check whether it's interpolated or not by |
I'm not a big fan of adding `NonInterpolatedStringLiteralExpr` either. The reason is users are likely to have two overrides for either visit or rewrite functions to handle all string literal cases. We should strive to help users reduce code duplication. However, feel free to add a convenient function to `StringLIteralExpr` indicating whether there are interpolations in the node. |
Comment by Yusuke Kita (JIRA) What Rintaro suggested makes sense to me. I'll go with `StringLiteralExpr` for the case. Thanks for your feedbacks! |
…ricted-rewriter Update for the more restrictive return types in SyntaxRewriter
Additional Detail from JIRA
md5: 0345f8455f9bcaa284679f0dc943b32a
relates to:
Issue Description:
Background
I would like to have a better way to extract raw string without quotes from `StringLiteralExprSyntax`. `StringInterpolationExprSyntax` looks well-organized because its cursor has openQuote, segments and closeQuote to express strings and quotes separately.
My proposal
Can we have quotes in children of StringLiteralExpr like StringInterpolationExpr?
https://github.com/apple/swift/blob/c04a7dec9f739fbe53f3c305a32e8d426972bfd8/utils/gyb_syntax_support/ExprNodes.py#L266:L269
https://github.com/apple/swift/blob/c04a7dec9f739fbe53f3c305a32e8d426972bfd8/utils/gyb_syntax_support/ExprNodes.py#L468:L481
References
Data structures and examples are below.
The text was updated successfully, but these errors were encountered: