Uploaded image for project: 'Swift'
  1. Swift
  2. SR-10241

A way to get raw string without quotes from StringLiteralExprSyntax

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Resolved
    • Priority: Medium
    • Resolution: Done
    • Component/s: SwiftSyntax
    • Labels:
      None

      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.

       

      public struct StringLiteralExprSyntax: ExprSyntax, _SyntaxBase, Hashable {
        enum Cursor: Int {
          case stringLiteral
        }
      }
      
      // let foo = "foo"
      print(node.stringLiteral.text) // "\"foo\""

       

      public struct StringInterpolationExprSyntax: ExprSyntax, _SyntaxBase, Hashable {
        enum Cursor: Int {
          case openQuote
          case segments
          case closeQuote
        }
      }
      
      // let foo = "foo \(bar)"
      print((node.segments[0] as! StringSegmentSyntax).content.text) // "foo"

       

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              kitasuke Yusuke Kita
              Reporter:
              kitasuke Yusuke Kita
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: