# URL macro

**URL:** <https://forums.swift.org/t/url-macro/63772>\
**Category:** Macros\
**Tags:** string, literals, swiftsyntax\
**Created:** [March 17, 2023, 2:19pm UTC](https://forums.swift.org/t/url-macro/63772 "2023-03-17T14:19:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nikolai.ruhe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nikolai.ruhe/32/23230_2.png) [@nikolai.ruhe](https://forums.swift.org/u/nikolai.ruhe)\
**Post date:** [March 17, 2023, 2:19pm UTC](https://forums.swift.org/t/url-macro/63772/1 "2023-03-17T14:19:17Z")

</div>

I've played around with the sample repo and built [a simple macro to create compile time checked URLs](https://github.com/DougGregor/swift-macro-examples/pull/21).

> **Motivation**
>
> This is similar in spirit to compile time checked regular expressions. For a literal `Regex` the compiler makes sure that the regex has no syntactic errors by parsing it at compile time. In case of a syntax error the compiler emits an error (and does not produce any code).
> 
> When the regex parses without error a non-optional, non-throwing initializer is inserted. During runtime the Regex has to be parsed again to create the actual instance but it is known to not throw an error.
> 
> This behavior, where the compiler checks the validity of an initializer's arguments would be helpful for many other types, for example a [SemVer](https://semver.org) type that checks for a valid semantic version.
> 
> In these cases macros seem like a good workaround (until we get statically checked arguments).

So the macro would take the provided string literal and, during compile time, check if it's valid.

```swift
let url = #URL("https://swift.org/") // fine: url is a non-optional URL
let bad = #URL("not a url") // emits error

```

This went very well and I'm happy with the result. Declaring and implementing the macro was straight forward and the emitted diagnostics are as expected. There's even unit test support which is nice and helpful in an environment with limited debug tooling.

With one detail I could need some help, though:

The macro implementation needs to access the string value of the passed literal. It is of type `StringSegmentSyntax` which can be converted to a `String` using `segment.content.text`. The result is the verbatim source text between the double quotes from the original string literal. In most cases this is fine.

But when there are escaped characters in the literal, like in `"https://swift\u{2e}org/"`, the macro fails. (`\u{2e}` is the escaped ASCII `'.'` character.) With my limited experience in SwiftSyntax I have not managed to get an unescaped string from the `StringSegmentSyntax`.

---

<div class="post-metadata">

**Author:** ![ahoppen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ahoppen/32/36046_2.png) [@ahoppen](https://forums.swift.org/u/ahoppen)\
**Post date:** [March 29, 2023, 9:47pm UTC](https://forums.swift.org/t/url-macro/63772/2 "2023-03-29T21:47:29Z")

</div>

Hey @nikolai.ruhe,

Are you still seeing this problem? I only just saw the post.

So if I understand you correctly, you would like to have a utility to which you can pass a string literal that might contain unicode escape sequences and have those replaced by the characters they represent? If this is the case, then the answer is that SwiftSyntax does not provide such a utility yet. If you would be interested in writing one and opening a pull request to [GitHub - apple/swift-syntax: A set of Swift libraries for parsing, inspecting, generating, and transforming Swift source code.](http://github.com/apple/swift-syntax) that would be a very welcome addition.

---

<div class="post-metadata">

**Author:** ![nikolai.ruhe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nikolai.ruhe/32/23230_2.png) [@nikolai.ruhe](https://forums.swift.org/u/nikolai.ruhe)\
**Post date:** [April 7, 2023, 3:12pm UTC](https://forums.swift.org/t/url-macro/63772/3 "2023-04-07T15:12:39Z")

</div>

> [...] that would be a very welcome addition.

Hi @ahoppen,

Thanks for the suggestion 🙂 I have looked into SwiftSyntax a bit and came up with this: [`StringLiteralExprSyntax/contentValue`](https://github.com/NikolaiRuhe/swift-syntax/pull/2).

Since I'm not familiar with the architecture of SwiftSyntax there might be issues with placing and usage of code. It would be great if you could have a look before I make the PR on the SwiftSyntax repo.

---

<div class="post-metadata">

**Author:** ![ahoppen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ahoppen/32/36046_2.png) [@ahoppen](https://forums.swift.org/u/ahoppen)\
**Post date:** [April 7, 2023, 8:56pm UTC](https://forums.swift.org/t/url-macro/63772/4 "2023-04-07T20:56:11Z")

</div>

I just skimmed over your PR and it looks very good to me. Thank you 🙏 Clever to re-use the lexer to get the escaped values. I’ll probably have a few comments but it’s easier to post them after you open a PR to the apple/swift-syntax repository. That way we’ll have all the conversation in a single place.
