summaryrefslogtreecommitdiff
path: root/module/ice-9/source-properties.scm
blob: b8abc276eedd0a140fbf8d31bdc617d0a43f2d20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
;;; Copyright (C) 2025 Free Software Foundation, Inc.
;;;
;;; This library is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU Lesser General Public License as
;;; published by the Free Software Foundation, either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; This library is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; Lesser General Public License for more details.
;;;
;;; You should have received a copy of the GNU Lesser General Public
;;; License along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

;;; Commentary:
;;;
;;; Code:


(define-module (ice-9 source-properties)
  #:use-module (ice-9 weak-tables)
  #:use-module (system syntax internal)
  #:use-module (ice-9 match)
  ;; FIXME: Change to #:export when deprecated bindings removed.
  #:replace (supports-source-properties?
             source-property
             set-source-property!
             source-properties
             set-source-properties!
             read))

(define global-source-properties (make-weak-key-hash-table))

(define (immediate? x)
  (cond
   ((exact-integer? x) (<= most-negative-fixnum x most-positive-fixnum))
   ((char? x)          #t)
   ((eq? x #f)         #t)
   ((eq? x #nil)       #t)
   ((eq? x '())        #t)
   ((eq? x #t)         #t)
   ((unspecified? x)   #t)
   ((eof-object? x)    #t)
   (else               #f)))

(define (supports-source-properties? x)
  (cond
   ((immediate? x) #f)
   ((symbol? x) #f)
   ((keyword? x) #f)
   (else #t)))

(define (source-properties obj)
  (if (supports-source-properties? obj)
      (weak-key-hash-table-ref global-source-properties obj
                               #:default (lambda (k) '()))
      '()))

(define (set-source-properties! obj props)
  (unless (supports-source-properties? obj)
    (scm-error 'wrong-type-arg "set-source-properties!"
               "Unexpected immediate value: ~S"
               (list obj) #f))
  (weak-key-hash-table-set! global-source-properties obj props))

(define (source-property obj key)
  (and (supports-source-properties? obj)
       (assq-ref (source-properties obj) key)))

(define (set-source-property! obj key value)
  (unless (supports-source-properties? obj)
    (scm-error 'wrong-type-arg "set-source-properties!"
               "Unexpected immediate value: ~S"
               (list obj) #f))
  (set-source-properties! obj (assq-set! (source-properties obj) key value)))

(define (cons-source orig x y)
  (let ((pair (cons x y))
        (src (source-properties orig)))
    (when (pair? src)
      (set-source-properties! pair src))
    pair))

(define* (read #:optional (port (current-input-port)))
  (define (annotate x src)
    (when (supports-source-properties? x)
      (match src
        (#(filename line column)
         (set-source-properties! x `((filename . ,filename)
                                     (line . ,line)
                                     (column . ,column))))
        (#f (values))))
    x)
  (define (strip-and-annotate x)
    (cond
     ((syntax? x)
      (annotate (strip-and-annotate (syntax-expression x))
                (syntax-source x)))
     ((pair? x)
      (cons (strip-and-annotate (car x))
            (strip-and-annotate (cdr x))))
     ((vector? x)
      (list->vector (map strip-and-annotate (vector->list x))))
     (else
      x)))
  (strip-and-annotate (read-syntax port)))