# Macro to Make Identifiers from String Diagram

**URL:** <https://racket.discourse.group/t/macro-to-make-identifiers-from-string-diagram/2296>\
**Category:** Questions & Answers\
**Tags:** macro\
**Created:** [September 7, 2023, 4:56pm UTC](https://racket.discourse.group/t/macro-to-make-identifiers-from-string-diagram/2296 "2023-09-07T16:56:23Z")\
**Posts on this page:** 1\
**Showing post:** 9

<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:** [September 8, 2023, 4:48pm UTC](https://racket.discourse.group/t/macro-to-make-identifiers-from-string-diagram/2296/9 "2023-09-08T16:48:41Z")

</div>

Hi, @joeld. I understood it in terms of the following, although I am obviously unclear about the matter at large:

The context being passed to `prefix-id` in my original attempt is "removed" from where I want it to be, because I am not referencing the original scope, i.e. the context inside of `(name-pt ...)` is "fresh", but in the answers from @soegaard and @sorawee, the context is the same as the original call to the macro, i.e., the `stx` in the code below.

```scheme
(define-syntax (parse-diagram stx)
  (syntax-parse stx
    [(parse-diagram WHOLE)     
     (with-syntax ([((id x y) ...) (find-ids-and-positions (syntax->datum #'WHOLE))])       
       (with-syntax ([(id ...) (change-contexts stx #'(id ...))])
         #'(begin
             (define id (pt x y))
             ...)))]))

```

For what it's worth, I have gotten a similar thing to work before, to name desired directory paths so that I can reference the paths more easily when creating files and folders, by way of this code:

```scheme
#lang br

(define (normalize elements)
  (map (lambda (x)
         (if (path? x) x (symbol->string x)))
       (reverse elements)))

(define-macro-cases make-named-paths
  [(make-named-paths in ROOT using SIGIL [DIR ...] ...)
   #'(begin
       (walk-paths SIGIL `[,ROOT] [DIR ...])
       ...)]
  
  [(make-named-paths in ROOT [DIR ...] ...)
   #'(begin
       (make-named-paths in ROOT using @ [DIR ...])
       ...)])

(define-macro-cases walk-paths
  [(walk-paths SIGIL PATH [.. CONTENT ...])
   #'(begin
       (walk-paths SIGIL PATH CONTENT)
       ...)]
  
  [(walk-paths SIGIL PATH [NAME CONTENT0 CONTENT ...])
   (with-pattern ([$NAME (prefix-id #'SIGIL #'NAME)])
     #'(begin
         (define $NAME
           (path->directory-path
            (apply build-path (normalize (cons 'NAME PATH)))))
         
         (walk-paths SIGIL (cons 'NAME PATH) CONTENT0)
         (walk-paths SIGIL (cons 'NAME PATH) CONTENT)
         ...))]
  
  [(walk-paths SIGIL PATH [NAME])
   (with-pattern ([$NAME (prefix-id #'SIGIL #'NAME)])
     #'(define $NAME
         (path->directory-path
          (apply build-path (normalize (cons 'NAME PATH))))))]
  
  [(walk-paths SIGIL PATH NAME)
   (with-pattern ([$NAME (prefix-id #'SIGIL #'NAME)])
     #'(define $NAME
         (apply build-path (normalize (cons 'NAME PATH)))))])

;; here I use these definitions
(make-named-paths
 in (find-system-path 'temp-dir)
 using $
 [.. archive.zip hide-wallpaper.jpg]
 [HOME self.exe spaghetti.exe data.txt data.zip live.ps1 yeet.ps1 wallpaper.jpg])

(make-named-paths
 in (find-system-path 'doc-dir)
 using $
 [.. logs.txt hide-self.exe])

$self.exe

```

> #path:C:\Users\CHRIST~1\AppData\Local\Temp\HOME\self.exe

Perhaps it is as you say, because of the derived syntax object when using the `with-syntax*` in my original attempt. I can see the outline but not necessarily all the details.

---

_[View the full topic](https://racket.discourse.group/t/macro-to-make-identifiers-from-string-diagram/2296)._
