# How do I associate mangled identifiers with the original syntax?

**URL:** https://racket.discourse.group/t/how-do-i-associate-mangled-identifiers-with-the-original-syntax/3267
**Category:** Questions & Answers
**Created:** [October 26, 2024, 9:18am UTC](https://racket.discourse.group/t/how-do-i-associate-mangled-identifiers-with-the-original-syntax/3267 "2024-10-26T09:18:51Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![bakgatviooldoos](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/bakgatviooldoos/32/1381_2.png) [@bakgatviooldoos](https://racket.discourse.group/u/bakgatviooldoos)
#### Post date: [October 26, 2024, 9:18am UTC](https://racket.discourse.group/t/how-do-i-associate-mangled-identifiers-with-the-original-syntax/3267/1 "2024-10-26T09:18:51Z")

</div>

Hi, Racket Discourse.

I am busy working on a macro that, among other things, expands into a struct with certain fields being made up of concatenations via `.` of the original syntax.

As an example, we have this little definition below, and some of the field accessors generated by the resulting struct.

 ![image](https://global.discourse-cdn.com/free1/uploads/racket/original/2X/8/892117455f64c14570539281fe0472d4821c8dbb.png)

Note how the original syntax is associated with the resulting field accessor.

Now, because the path parameter `ip.geo` is a compound, it is not the original syntax so is not associated in the same manner to the original `geo`.

I have taken a shallow look at `define-struct.rkt` but it is a hefty one. What in particular am I supposed to be looking at, to achieve this? Rename-transformers, maybe syntax source location data, are the vague directions I think I am headed to?

It's not super critical, I am just curious how that would be accomplished.

---

<div class="post-metadata">

### Author: ![benknoble](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/benknoble/32/16_2.png) [@benknoble](https://racket.discourse.group/u/benknoble)
#### Post date: [October 26, 2024, 2:14pm UTC](https://racket.discourse.group/t/how-do-i-associate-mangled-identifiers-with-the-original-syntax/3267/2 "2024-10-26T14:14:42Z")

</div>

(From memory) you might be looking for something called sub-range-binders? I think format-id has some support for that.

---

<div class="post-metadata">

### Author: ![bakgatviooldoos](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/bakgatviooldoos/32/1381_2.png) [@bakgatviooldoos](https://racket.discourse.group/u/bakgatviooldoos)
#### Post date: [October 27, 2024, 7:58am UTC](https://racket.discourse.group/t/how-do-i-associate-mangled-identifiers-with-the-original-syntax/3267/3 "2024-10-27T07:58:59Z")

</div>

That's really cool, thank you, @benknoble 😁

I will have to mess around a bit to see what's what, but now I can look for examples of what others have done!

* * *

Edit: dit werk!

 ![image](https://global.discourse-cdn.com/free1/uploads/racket/original/2X/4/43f85f8658ddb0868b2c5f84a7908f76c065ab63.png)

All that was needed (in this case), as @benknoble mentioned, was this:

```scheme
(format-id stx "~a:~a" api-id name #:subs? #true)

```

---

<div class="post-metadata">

### Author: ![benknoble](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/benknoble/32/16_2.png) [@benknoble](https://racket.discourse.group/u/benknoble)
#### Post date: [October 31, 2024, 9:19pm UTC](https://racket.discourse.group/t/how-do-i-associate-mangled-identifiers-with-the-original-syntax/3267/4 "2024-10-31T21:19:43Z")

</div>

As I've just reminded myself, sometimes what you want is _actually_ to use `#:source` and the `'original-for-check-syntax` property. Example:

```scheme
#lang racket

(require syntax/parse/define
         (for-syntax racket/syntax))

(define-syntax-parser define-f*
  [(_ f:id)
   #:with f* (format-id #'f "~a*" #'f)
   #'(define f* 1)])

(define-f* g)
g*

```

DrRacket doesn't draw any arrows here. With `#:subs? #t`, we get an arrow from `g` in `g*` to the input `g` to the macro.

But what if we want an arrow from `g*` (the entire identifier) to the `g` in the macro? For that, we should use `#:source #'f` and the syntax property I mentioned (which is one of 2 ways DrRacket draws arrows; the other is for `syntax-original?` syntax), and we should _omit_ `#:subs?` (or things get confused):

```scheme
#lang racket

(require syntax/parse/define
         (for-syntax racket/syntax))

(define-syntax-parser define-f*
  [(_ f:id)
   #:with f* (syntax-property (format-id #'f #:source #'f "~a*" #'f)
                              'original-for-check-syntax #t)
   #'(define f* 1)])

(define-f* g)
g*

```

Now we have an arrow from `g*` to `g`, rather than just `g` to `g`.

This matters a bit more when building prefixed IDs, I think:

- If `a-b-c` is derived from `b` with `#:subs?` arrows, then the only place my editor can jump-to-definition is when on `b` in `a-b-c`.
- However, if `a-b-c` is derived from `b` with `#:source` + `'original-for-check-syntax` arrows, then my editor can jump-to-definition from anywhere in `a-b-c` because the whole identifier gets the arrow.
