Files
swift-mirror/test/Parse/raw_string.swift
Becca Royal-Gordon 8770c7f826 Rework ASTDumper (#68438)
This PR refactors the ASTDumper to make it more structured, less mistake-prone, and more amenable to future changes. For example:

```cpp
  // Before:
  void visitUnresolvedDotExpr(UnresolvedDotExpr *E) {
    printCommon(E, "unresolved_dot_expr")
      << " field '" << E->getName() << "'";
    PrintWithColorRAII(OS, ExprModifierColor)
      << " function_ref=" << getFunctionRefKindStr(E->getFunctionRefKind());
    if (E->getBase()) {
      OS << '\n';
      printRec(E->getBase());
    }
    PrintWithColorRAII(OS, ParenthesisColor) << ')';
  }

  // After:
  void visitUnresolvedDotExpr(UnresolvedDotExpr *E, StringRef label) {
    printCommon(E, "unresolved_dot_expr", label);

    printFieldQuoted(E->getName(), "field");
    printField(E->getFunctionRefKind(), "function_ref", ExprModifierColor);

    if (E->getBase()) {
      printRec(E->getBase());
    }

    printFoot();
  }
```

* Values are printed through calls to base class methods, rather than direct access to the underlying `raw_ostream`.
    * These methods tend to reduce the chances of bugs like missing/extra spaces or newlines, too much/too little indentation, etc.
    * More values are quoted, and unprintable/non-ASCII characters in quoted values are escaped before printing.
* Infrastructure to label child nodes now exists.
    * Some weird breaks from the normal "style", like `PatternBindingDecl`'s original and processed initializers, have been brought into line.
* Some types that previously used ad-hoc dumping functions, like conformances and substitution maps, are now structured similarly to the dumper classes.
* I've fixed the odd dumping bug along the way. For example, distributed actors were only marked `actor`, not `distributed actor`.

This PR doesn't change the overall style of AST dumps; they're still pseudo-S-expressions. But the logic that implements this style is now isolated into a relatively small base class, making it feasible to introduce e.g. JSON dumping in the future.
2023-09-11 23:56:38 -07:00

180 lines
3.8 KiB
Swift
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// RUN: %target-swift-frontend -dump-ast %s | %FileCheck --strict-whitespace %s
import Swift
_ = #"""
###################################################################
## This source file is part of the Swift.org open source project ##
###################################################################
"""#
// CHECK: "###################################################################\n## This source file is part of the Swift.org open source project ##\n###################################################################"
_ = #"""
# H1 #
## H2 ##
### H3 ###
"""#
// CHECK: "# H1 #\n## H2 ##\n### H3 ###"
// ===---------- Multiline RawString --------===
_ = ##"""
One
""Alpha""
"""##
// CHECK: "One\n\"\"Alpha\"\""
_ = ##"""
Two
Beta
"""##
// CHECK: " Two\nBeta"
_ = #"""
Three\r
Gamma\
"""#
// CHECK: " Three\\r\n Gamma\\"
_ = ###"""
Four \(foo)
Delta
"""###
// CHECK: " Four \\(foo)\n Delta"
_ = ##"""
print("""
Five\##n\##n\##nEpsilon
""")
"""##
// CHECK: "print(\"\"\"\n Five\n\n\nEpsilon\n \"\"\")"
// ===---------- Single line --------===
_ = #""Zeta""#
// CHECK: "\"Zeta\""
_ = #""Eta"\#n\#n\#n\#""#
// CHECK: "\"Eta\"\n\n\n\""
_ = #""Iota"\n\n\n\""#
// CHECK: "\"Iota\"\\n\\n\\n\\\""
_ = #"a raw string with \" in it"#
// CHECK: "a raw string with \\\" in it"
_ = ##"""
a raw string with """ in it
"""##
// CHECK: "a raw string with \"\"\" in it"
// ===---------- False Multiline Delimiters --------===
/// Source code contains zero-width character in this format: `#"[U+200B]"[U+200B]"#`
/// If this check fails after you implement `diagnoseZeroWidthMatchAndAdvance`,
/// then you may need to tweak how to test for single-line string literals that
/// resemble a multiline delimiter in `advanceIfMultilineDelimiter` so that it
/// passes again.
/// See https://github.com/apple/swift/issues/51192.
_ = #"""#
// CHECK: "\xE2\x80\x8B\"\xE2\x80\x8B"
_ = #""""#
// CHECK: "\"\""
_ = #"""""#
// CHECK: "\"\"\""
_ = #""""""#
// CHECK: "\"\"\"\""
_ = #"""#
// CHECK: "\""
_ = ##""" foo # "# "##
// CHECK: "\"\" foo # \"# "
_ = ###""" "# "## "###
// CHECK: "\"\" \"# \"## "
_ = ###"""##"###
// CHECK: "\"\"##"
_ = "interpolating \(#"""false delimiter"#)"
// CHECK: "interpolating "
// CHECK: "\"\"false delimiter"
_ = """
interpolating \(#"""false delimiters"""#)
"""
// CHECK: "interpolating "
// CHECK: "\"\"false delimiters\"\""
let foo = "Interpolation"
_ = #"\b\b \#(foo)\#(foo) Kappa"#
// CHECK: "\\b\\b "
// CHECK: " Kappa"
_ = """
interpolating \(##"""
delimited \##("string")\#n\##n
"""##)
"""
// CHECK: "interpolating "
// CHECK: "delimited "
// CHECK: "string"
// CHECK: "\\#n\n"
#"unused literal"#
// CHECK: "unused literal"
// ===---------- From proposal --------===
_ = #"This is a string"#
// CHECK: "This is a string"
_ = #####"This is a string"#####
// CHECK: "This is a string"
_ = #"enum\s+.+\{.*case\s+[:upper:]"#
// CHECK: "enum\\s+.+\\{.*case\\s+[:upper:]"
_ = #"Alice: "How long is forever?" White Rabbit: "Sometimes, just one second.""#
// CHECK: "Alice: \"How long is forever?\" White Rabbit: \"Sometimes, just one second.\""
_ = #"\#\#1"#
// CHECK: "\\#1"
_ = ##"\#1"##
// CHECK: "\\#1"
_ = #"c:\windows\system32"#
// CHECK: "c:\\windows\\system32"
_ = #"\d{3) \d{3} \d{4}"#
// CHECK: "\\d{3) \\d{3} \\d{4}"
_ = #"""
a string with
"""
in it
"""#
// CHECK: "a string with\n\"\"\"\nin it"
_ = #"a raw string containing \r\n"#
// CHECK: "a raw string containing \\r\\n"
_ = #"""
[
{
"id": "12345",
"title": "A title that \"contains\" \\\""
}
]
"""#
// CHECK: "[\n {\n \"id\": \"12345\",\n \"title\": \"A title that \\\"contains\\\" \\\\\\\"\"\n }\n]"
_ = #"# #"#
// CHECK: "# #"