Removing the default racket reader from the reading table for symbol |

I am trying to write a racket reader extension procedure that disables special pipe symbol processing |.

I have two files: mylang/lang/reader.rktfor implementing lang reader and mylang/testing.rktto try. I ran raco pkg install --linkto install lang.

Here reader.rkt:

#lang s-exp syntax/module-reader
racket
#:read my-read
#:read-syntax my-read-syntax

(define (parse-pipe char in srcloc src linum colnum)
  #'\|)

(define my-readtable
  (make-readtable #f #\| 'terminating-macro parse-pipe))

(define (my-read-syntax src in)
  (parameterize ((current-readtable my-readtable))
    (read-syntax src in)))

(define (my-read in)
  (syntax->datum
   (my-read-syntax #f in)))

With testing.rktas follows:

#lang mylang
(define | 3)
(+ 3 2)

starts and produces 5 as expected. But this does not follow:

#lang mylang
(define |+ 3)
(+ |+ 2)

complains that define: bad syntax (multiple expressions after identifier) in: (define \| + 3), which is reasonable, since it parse-pipecreates a syntax object, not a string, so it prematurely stops reading the character.

, , , , , , char , | ..

, | , , .

+4
1

, . make-readtable :

char like- char readtable - char -char readtable, readtable #f .

| a :

(define my-readtable
  (make-readtable #f #\| #\a #f))

(define hawdy|+ "hello")    
(string-append hawdy|+ "|world")
; => "hello|world"
+6

Source: https://habr.com/ru/post/1612138/


All Articles