How to add new escape sequence?

If we read a string like "\()", we will get

read: unknown escape sequence `\(` in string

can we change this behavior?

2 Likes

I think you can use a custom readtable to make this possible, but so far I had no reason to do so, so I don't have practical details in that area, have a look at:
17.2.2 Readtables (Guide)
13.7.1 Readtables (Reference)
1.3.1 Delimiters and Dispatch (Default Read Table)

3 Likes

After some works, here is an example for escaping "\$" as "$" since "$expr" produces (format "~a" expr)

(define (action c in src line col pos)
    (define (conv src in)
      (define args '())
      (define s
        (let loop ([l '()])
          (define c (peek-char in))
          (cond
            [(eof-object? c) l]
            [(char=? c #\") (read-char in) l]
            [(char=? c #\$) (read-char in)
                            (set! args (append args (list (read-syntax src in))))
                            (loop (append l (list #\~ #\a)))]
            [(char=? c #\\) (read-char in)
                            (cond
                              [(char=? (peek-char in) #\$)
                               (loop (append l (list (read-char in))))]
                              [else (loop (append l (list #\\ c)))])]
            [else (read-char in)
                  (loop (append l (list c)))])))
      (if (= (length args) 0)
          (list->string s)
          (with-syntax ([fmt (list->string s)]
                        [(arg ...) args])
            (syntax/loc (srcloc src line col pos #f)
              (format fmt arg ...)))))
    (conv src in))
3 Likes