read-syntax sets line/column numbers to #f

Viewed 28

When parsing syntax from a string input port, all line/column numbers get set to #f. Why is that?

(let* ([port (open-input-string "123")]
       [stx (read-syntax "my-input" port)])
  (printf "line ~a column ~a" (syntax-line stx) (syntax-column stx))
)

Expected output:

line 1 column 0

Actual output:

line #f column #f

1 Answers

You have to explicitly turn on line based location tracking for ports, for example by setting the port-count-lines-enabled parameter:

(parameterize ([port-count-lines-enabled #t])
  (let* ([port (open-input-string "123")]
         [stx (read-syntax "my-input" port)])
    (printf "line ~a column ~a" (syntax-line stx) (syntax-column stx))))

which prints

line 1 column 0

There's also port-count-lines! for enabling it on existing ports.

Related